-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTimestamps.go
36 lines (34 loc) · 1.09 KB
/
parseTimestamps.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
package dates
import (
"fmt"
"math"
"time"
)
func int64DateParser(unixTime int64, tz *time.Location) (time.Time, string, error) {
numDigits := int(math.Log10(float64(unixTime))) + 1
precision := ""
var parsedTime time.Time
switch {
case numDigits <= 5: // Excel timestamps
parsedTime, precision, err := float64DateParser(float64(unixTime), tz)
if err != nil {
return time.Time{}, "", err
}
return parsedTime, precision, nil
case numDigits > 5 && numDigits <= 10: // Unix timestamps with 5 to 10 digits
parsedTime = time.Unix(unixTime, 0)
precision = SecondPrecision
case numDigits == 13: // milliseconds
parsedTime = time.Unix(0, unixTime*int64(time.Millisecond))
precision = MillisecondPrecision
case numDigits == 16: // microseconds
parsedTime = time.Unix(0, unixTime*int64(time.Microsecond))
precision = MicrosecondPrecision
case numDigits == 19: // nanoseconds
parsedTime = time.Unix(0, unixTime)
precision = NanosecondPrecision
default:
return time.Time{}, "", fmt.Errorf("unknown Unix timestamp length: %d digits", numDigits)
}
return parsedTime, precision, nil
}