-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.go
69 lines (55 loc) · 1.67 KB
/
clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package time
import (
"context"
"database/sql/driver"
"time"
)
// UnixTimeStamp represents number of seconds
// elapsed since January 1, 1970 UTC.
type UnixTimeStamp int64
// Value implements the sql driver Valuer interface.
func (uts UnixTimeStamp) Value() (driver.Value, error) {
return int64(uts), nil
}
// Scan implements the Scanner interface.
func (uts *UnixTimeStamp) Scan(value interface{}) error {
*uts = UnixTimeStamp(value.(int64))
return nil
}
func (uts UnixTimeStamp) SecondsTo(t time.Time) int64 {
return int64(uts) - t.Unix()
}
// UserTypeCtxKey is type to keep time in context
type TimeCtxKey string
// TimeKey is key to keep current time in context
var CurrentTimeKey = TimeCtxKey("current_time")
// Clock provides information about current time
type Clock interface {
Today(ctx context.Context) LocalDate
Now(ctx context.Context) LocalDateTime
GoTime(ctx context.Context) time.Time
ToUnixTimestamp(ctx context.Context, localDateTime LocalDateTime) UnixTimeStamp
}
// NewClock creates new Clock
func NewClock() Clock {
timeZoneWarsaw, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
panic(err)
}
return clock{timeZoneWarsaw}
}
type clock struct {
timeZone *time.Location
}
func (c clock) Today(ctx context.Context) LocalDate {
return ToLocalDate(c.GoTime(ctx))
}
func (c clock) Now(ctx context.Context) LocalDateTime {
return NewLocalDateTime(c.GoTime(ctx))
}
func (c clock) ToUnixTimestamp(ctx context.Context, localDateTime LocalDateTime) UnixTimeStamp {
return UnixTimeStamp(localDateTime.GoTime(c.timeZone).Unix())
}
func (c clock) GoTime(ctx context.Context) time.Time {
return (*(ctx.Value(CurrentTimeKey).(*time.Time))).In(c.timeZone)
}