-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanotime.go
84 lines (66 loc) · 2.5 KB
/
nanotime.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
83
84
package smalltime
import "time"
type Nanotime uint64
const zeroYearNanotime = 1970
const bitshiftYearNanotime = 56
const bitshiftMonthNanotime = 52
const bitshiftDayNanotime = 47
const bitshiftHourNanotime = 42
const bitshiftMinuteNanotime = 36
const bitshiftSecondNanotime = 30
const maskYearNanotime = Nanotime(0xff) << (bitshiftYearNanotime)
const maskMonthNanotime = Nanotime(0xf) << (bitshiftMonthNanotime)
const maskDayNanotime = Nanotime(0x1f) << (bitshiftDayNanotime)
const maskHourNanotime = Nanotime(0x1f) << (bitshiftHourNanotime)
const maskMinuteNanotime = Nanotime(0x3f) << (bitshiftMinuteNanotime)
const maskSecondNanotime = Nanotime(0x3f) << (bitshiftSecondNanotime)
const maskNanoNanotime = Nanotime(0x3fffffff)
func NanotimeFromTime(t time.Time) Nanotime {
t = t.UTC()
return NewNanotime(t.Year(), int(t.Month()), t.Day(), t.Hour(),
t.Minute(), t.Second(), t.Nanosecond())
}
func NewNanotime(year, month, day, hour, minute, second, nanosecond int) Nanotime {
return Nanotime(year-zeroYearNanotime)<<(bitshiftYearNanotime) |
Nanotime(month)<<(bitshiftMonthNanotime) |
Nanotime(day)<<(bitshiftDayNanotime) |
Nanotime(hour)<<(bitshiftHourNanotime) |
Nanotime(minute)<<(bitshiftMinuteNanotime) |
Nanotime(second)<<(bitshiftSecondNanotime) |
Nanotime(nanosecond)
}
func NewNanotimeWithDoy(year, dayOfYear, hour, minute, second, nanosecond int) Nanotime {
month, day := doyToYmd(year, dayOfYear)
return NewNanotime(year, month, day, hour, minute, second, nanosecond)
}
func (t Nanotime) AsTime() time.Time {
return t.AsTimeInLocation(time.UTC)
}
func (t Nanotime) AsTimeInLocation(loc *time.Location) time.Time {
return time.Date(t.Year(), time.Month(t.Month()), t.Day(), t.Hour(),
t.Minute(), t.Second(), t.Nanosecond(), loc)
}
func (time Nanotime) Year() int {
return int(time>>bitshiftYearNanotime) + zeroYearNanotime
}
func (time Nanotime) Doy() int {
return ymdToDoy(time.Year(), time.Month(), time.Day())
}
func (time Nanotime) Month() int {
return int((time & maskMonthNanotime) >> (bitshiftMonthNanotime))
}
func (time Nanotime) Day() int {
return int((time & maskDayNanotime) >> (bitshiftDayNanotime))
}
func (time Nanotime) Hour() int {
return int((time & maskHourNanotime) >> (bitshiftHourNanotime))
}
func (time Nanotime) Minute() int {
return int((time & maskMinuteNanotime) >> (bitshiftMinuteNanotime))
}
func (time Nanotime) Second() int {
return int((time & maskSecondNanotime) >> (bitshiftSecondNanotime))
}
func (time Nanotime) Nanosecond() int {
return int(time & maskNanoNanotime)
}