-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_timestamp.go
45 lines (40 loc) · 1.56 KB
/
parser_timestamp.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
package gotimeparser
import (
"math"
"time"
)
// List of times in int64 formats.
const (
// Positive values, bigger than 0 nanoseconds.
MaxNanoseconds = int64(math.MaxInt64)
MaxMicroseconds = MaxNanoseconds / 1000
MaxMilliseconds = MaxMicroseconds / 1000
MaxSeconds = MaxMilliseconds / 1000
// Negative values, smaller than 0 nanoseconds.
MinNanoseconds = int64(math.MinInt64)
MinMicroseconds = MinNanoseconds / 1000
MinMilliseconds = MinMicroseconds / 1000
MinSeconds = MinMilliseconds / 1000
)
// ParseTimestamp parses time from int64.
// Support all time formats from nanoseconds to seconds.
// There is a small overlap around 0 between all formats, but this overlap is insignificant.
func ParseTimestamp(timestamp int64) time.Time {
switch {
case timestamp < MinMicroseconds:
return time.Unix(0, timestamp) // Before 1970 in nanoseconds.
case timestamp < MinMilliseconds:
return time.Unix(0, timestamp*int64(time.Microsecond)) // Before 1970 in microseconds.
case timestamp < MinSeconds:
return time.Unix(0, timestamp*int64(time.Millisecond)) // Before 1970 in milliseconds.
case timestamp < 0:
return time.Unix(timestamp, 0) // Before 1970 in seconds.
case timestamp < MaxSeconds:
return time.Unix(timestamp, 0) // After 1970 in seconds.
case timestamp < MaxMilliseconds:
return time.Unix(0, timestamp*int64(time.Millisecond)) // After 1970 in milliseconds.
case timestamp < MaxMicroseconds:
return time.Unix(0, timestamp*int64(time.Microsecond)) // After 1970 in microseconds.
}
return time.Unix(0, timestamp) // After 1970 in nanoseconds.
}