Gisle Aune
3 years ago
9 changed files with 236 additions and 18 deletions
-
3app/services/events.go
-
41cmd/lucy/command.go
-
11cmd/lucy/handlercmd.go
-
1cmd/lucy/help.go
-
2cmd/lucy/main.go
-
19internal/mysql/eventhandlerrepo.go
-
26models/eventhandler.go
-
138models/timeofday.go
-
13scripts/20211002131800_eventhandler_from_to.sql
@ -0,0 +1,138 @@ |
|||
package models |
|||
|
|||
import ( |
|||
"bytes" |
|||
"encoding/json" |
|||
"fmt" |
|||
"strconv" |
|||
"strings" |
|||
"time" |
|||
) |
|||
|
|||
type TimeOfDay int |
|||
|
|||
func (t *TimeOfDay) UnmarshalJSON(v []byte) error { |
|||
if bytes.Equal(v, []byte("null")) { |
|||
*t = -1 |
|||
} |
|||
|
|||
var str string |
|||
err := json.Unmarshal(v, &str) |
|||
if err != nil { |
|||
var n int |
|||
err = json.Unmarshal(v, &n) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if n < -1 && n >= 86400 { |
|||
return fmt.Errorf("value outside range 0..86400: %d", n) |
|||
} |
|||
} |
|||
|
|||
|
|||
t2, err := ParseTimeOfDay(str) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
*t = t2 |
|||
|
|||
return nil |
|||
} |
|||
|
|||
func (t TimeOfDay) MarshalJSON() ([]byte, error) { |
|||
if t.IsNever() { |
|||
return []byte("null"), nil |
|||
} |
|||
|
|||
return json.Marshal(t.String()) |
|||
} |
|||
|
|||
const Never = TimeOfDay(-1) |
|||
|
|||
func (t *TimeOfDay) String() string { |
|||
if *t < 0 { |
|||
return "n/a" |
|||
} |
|||
|
|||
return fmt.Sprintf("%02d:%02d:%02d", *t/3600, (*t/60)%60, *t%60) |
|||
} |
|||
|
|||
func (t *TimeOfDay) StringPtr() *string { |
|||
if *t < 0 { |
|||
return nil |
|||
} |
|||
|
|||
s := t.String() |
|||
return &s |
|||
} |
|||
|
|||
func (t TimeOfDay) IsNever() bool { |
|||
return t == Never |
|||
} |
|||
|
|||
func (t TimeOfDay) IsBetween(from TimeOfDay, to TimeOfDay) bool { |
|||
if from == to { |
|||
return t == from |
|||
} else if from > to { |
|||
return t >= to || t <= from |
|||
} else { |
|||
return t >= from && t <= to |
|||
} |
|||
} |
|||
|
|||
func CurrentTimeOfDay() TimeOfDay { |
|||
return TimeOfDayFromDate(time.Now()) |
|||
} |
|||
|
|||
func TimeOfDayFromDate(date time.Time) TimeOfDay { |
|||
return NewTimeOfDay(date.Hour(), date.Minute(), date.Second()) |
|||
} |
|||
|
|||
func NewTimeOfDay(hours, minutes, seconds int) TimeOfDay { |
|||
return TimeOfDay(((hours % 24) * 3600) + ((minutes % 60) * 60) + seconds) |
|||
} |
|||
|
|||
func ParseTimeOfDayPtr(str *string) (TimeOfDay, error) { |
|||
if str == nil { |
|||
return Never, nil |
|||
} |
|||
|
|||
return ParseTimeOfDay(*str) |
|||
} |
|||
|
|||
func ParseTimeOfDay(str string) (TimeOfDay, error) { |
|||
if str == "" || str == "n/a" || str == "N/A" { |
|||
return -1, nil |
|||
} |
|||
|
|||
split := strings.SplitN(str, ":", 4) |
|||
res := 0 |
|||
|
|||
n, err := strconv.Atoi(split[0]) |
|||
if err != nil { |
|||
return Never, err |
|||
} |
|||
res += n * 3600 |
|||
|
|||
if len(split) >= 2 { |
|||
n, err := strconv.Atoi(split[1]) |
|||
if err != nil { |
|||
return Never, err |
|||
} |
|||
res += n * 60 |
|||
} |
|||
if len(split) >= 3 { |
|||
n, err := strconv.Atoi(split[2]) |
|||
if err != nil { |
|||
return Never, err |
|||
} |
|||
res += n |
|||
} |
|||
|
|||
if res >= 86400 || res < 0 { |
|||
return Never, fmt.Errorf("invalid time of day string %s (=%d)", str, res) |
|||
} |
|||
|
|||
return TimeOfDay(res), nil |
|||
} |
@ -0,0 +1,13 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
ALTER TABLE event_handler |
|||
ADD COLUMN from_tod VARCHAR(255), |
|||
ADD COLUMN to_tod VARCHAR(255); |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
ALTER TABLE event_handler |
|||
DROP COLUMN IF EXISTS from_tod, |
|||
DROP COLUMN IF EXISTS to_tod; |
|||
-- +goose StatementEnd |
Write
Preview
Loading…
Cancel
Save
Reference in new issue