forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (61 loc) · 1.89 KB
/
main.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
package time
import (
"strconv"
goTime "time"
)
//Millis represents time as milliseconds since epoch without any timezone adjustments
type Millis int64
//MillisFromString generates a Millis struct from a string representing an int64
func MillisFromString(s string) (Millis, error) {
millis, err := strconv.ParseInt(s, 10, 64)
return Millis(int64(millis)), err
}
//MillisFromInt64 generates a Millis struct from given millis int64
func MillisFromInt64(millis int64) Millis {
return Millis(millis)
}
//MillisFromSeconds generates a Millis struct from given seconds int64
func MillisFromSeconds(seconds int64) Millis {
return Millis(seconds * 1000)
}
func (t Millis) increment(millisToAdd int64) Millis {
return Millis(int64(t) + millisToAdd)
}
//IsNil returns true if the timeMillis has not been initialized to a date other then 0 from epoch
func (t Millis) IsNil() bool {
return t == 0
}
//RoundUp returns a new Millis instance with a rounded up to d millis
func (t Millis) RoundUp(d int64) Millis {
if d == 0 {
return t
}
if int64(t)%d != 0 {
return t.RoundDown(d).increment(d)
}
return t
}
//RoundUp returns a new ToInt64 instance with a down to d millis
func (t Millis) RoundDown(d int64) Millis {
//round down to the nearest d
return Millis(int64(int64(t)/d) * d)
}
//ToInt64 returns the actual int64 millis since epoch
func (t Millis) ToInt64() int64 {
return int64(t)
}
//ToTime returns a go time.Time timestamp, UTC adjusted
func (t Millis) ToTime() goTime.Time {
// Milliseconds 1510831636149
// Nanoseconds 1510831636149000000
// Unix sec arg 1510831636
// Unix nsec arg 149000000
return goTime.Unix(int64(t)/1000, int64(t)%1000*int64(goTime.Millisecond)).UTC()
}
//Now returns current time in millis
func Now() Millis {
return Millis(goTime.Now().UTC().UnixNano() / int64(goTime.Millisecond))
}
func (t Millis) String() string {
return strconv.FormatInt(t.ToInt64(), 10)
}