-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
132 lines (112 loc) · 2.84 KB
/
utils.go
File metadata and controls
132 lines (112 loc) · 2.84 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package sqlite
import (
"strconv"
"strings"
"time"
)
const (
julianDay1970 = 2440587.5
julianDayMin = 1721425.5
julianDayMax = 5373484.5
secondsPerDay = 86400
nanosecondsPerSecond = 1e9
nanosecondsPerDay = secondsPerDay * nanosecondsPerSecond
millisecondsPerSecond = 1000
microsecondsPerSecond = 1000000
nanosecondsPerMillisecond = 1000000
nanosecondsPerMicrosecond = 1000
unixEpochMax = 253402300799
unixMillisMin = 1000000000000
unixMicrosMin = 1000000000000000
unixNanosMin = 1000000000000000000
)
var timeFormats = []string{
time.RFC3339Nano,
time.RFC3339,
"2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04:05.999",
"2006-01-02T15:04:05.999",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02",
"15:04:05.999999999",
"15:04:05.999",
"15:04:05",
"15:04",
}
func parseTimeString(s string) (time.Time, bool) {
s = strings.TrimSpace(s)
if s == "" {
return time.Time{}, false
}
for _, format := range timeFormats {
t, err := time.Parse(format, s)
if err == nil {
return t, true
}
}
i, err := strconv.ParseInt(s, 10, 64)
if err == nil {
t, ok := parseTimeInteger(i)
if ok {
return t, ok
}
}
f, err := strconv.ParseFloat(s, 64)
if err == nil {
t, ok := parseTimeFloat(f)
if ok {
return t, ok
}
}
return time.Time{}, false
}
func parseTimeInteger(i int64) (time.Time, bool) {
if i < 0 {
return time.Time{}, false
}
// Unix timestamp in nanoseconds (19+ digits)
if i >= unixNanosMin {
sec := i / nanosecondsPerSecond
nsec := i % nanosecondsPerSecond
return time.Unix(sec, nsec).UTC(), true
}
// Unix timestamp in microseconds (16-18 digits)
if i >= unixMicrosMin {
sec := i / microsecondsPerSecond
nsec := (i % microsecondsPerSecond) * nanosecondsPerMicrosecond
return time.Unix(sec, nsec).UTC(), true
}
// Unix timestamp in milliseconds (13-15 digits)
if i >= unixMillisMin {
sec := i / millisecondsPerSecond
nsec := (i % millisecondsPerSecond) * nanosecondsPerMillisecond
return time.Unix(sec, nsec).UTC(), true
}
return time.Unix(i, 0).UTC(), true
}
func parseTimeFloat(f float64) (time.Time, bool) {
if f >= julianDayMin && f <= julianDayMax {
return julianToTime(f), true
}
if f < 0 || f > float64(unixEpochMax) {
return time.Time{}, false
}
sec := int64(f)
nsec := int64((f - float64(sec)) * nanosecondsPerSecond)
return time.Unix(sec, nsec).UTC(), true
}
func julianToTime(jd float64) time.Time {
unix := (jd - julianDay1970) * secondsPerDay
sec := int64(unix)
nsec := int64((unix - float64(sec)) * nanosecondsPerSecond)
return time.Unix(sec, nsec).UTC()
}
func timeToJulian(t time.Time) float64 {
unixSeconds := float64(t.Unix())
unixNanos := float64(t.Nanosecond())
return unixSeconds/secondsPerDay + julianDay1970 + unixNanos/nanosecondsPerDay
}