-
Notifications
You must be signed in to change notification settings - Fork 1
/
smalltime.go
82 lines (65 loc) · 2.17 KB
/
smalltime.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
70
71
72
73
74
75
76
77
78
79
80
81
82
package smalltime
import "time"
type Smalltime int64
const bitshiftYear = 46
const bitshiftMonth = 42
const bitshiftDay = 37
const bitshiftHour = 32
const bitshiftMinute = 26
const bitshiftSecond = 20
const maskYear = uint64(0x3ffff) << bitshiftYear
const maskMonth = Smalltime(0xf) << bitshiftMonth
const maskDay = Smalltime(0x1f) << bitshiftDay
const maskHour = Smalltime(0x1f) << bitshiftHour
const maskMinute = Smalltime(0x3f) << bitshiftMinute
const maskSecond = Smalltime(0x3f) << bitshiftSecond
const maskMicrosecond = Smalltime(0xfffff)
func SmalltimeFromTime(t time.Time) Smalltime {
t = t.UTC()
return NewSmalltime(t.Year(), int(t.Month()), t.Day(), t.Hour(),
t.Minute(), t.Second(), t.Nanosecond()/1000)
}
func NewSmalltime(year, month, day, hour, minute, second, microsecond int) Smalltime {
return Smalltime(year)<<bitshiftYear |
Smalltime(month)<<bitshiftMonth |
Smalltime(day)<<bitshiftDay |
Smalltime(hour)<<bitshiftHour |
Smalltime(minute)<<bitshiftMinute |
Smalltime(second)<<bitshiftSecond |
Smalltime(microsecond)
}
func NewSmalltimeWithDoy(year, dayOfYear, hour, minute, second, microsecond int) Smalltime {
month, day := doyToYmd(year, dayOfYear)
return NewSmalltime(year, month, day, hour, minute, second, microsecond)
}
func (t Smalltime) AsTime() time.Time {
return t.AsTimeInLocation(time.UTC)
}
func (t Smalltime) AsTimeInLocation(loc *time.Location) time.Time {
return time.Date(t.Year(), time.Month(t.Month()), t.Day(), t.Hour(),
t.Minute(), t.Second(), t.Microsecond()*1000, loc)
}
func (time Smalltime) Year() int {
return int(time >> bitshiftYear)
}
func (time Smalltime) Doy() int {
return ymdToDoy(time.Year(), time.Month(), time.Day())
}
func (time Smalltime) Month() int {
return int((time & maskMonth) >> bitshiftMonth)
}
func (time Smalltime) Day() int {
return int((time & maskDay) >> bitshiftDay)
}
func (time Smalltime) Hour() int {
return int((time & maskHour) >> bitshiftHour)
}
func (time Smalltime) Minute() int {
return int((time & maskMinute) >> bitshiftMinute)
}
func (time Smalltime) Second() int {
return int((time & maskSecond) >> bitshiftSecond)
}
func (time Smalltime) Microsecond() int {
return int(time & maskMicrosecond)
}