-
Notifications
You must be signed in to change notification settings - Fork 7
/
epoch.go
180 lines (168 loc) · 4.52 KB
/
epoch.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package tiledb
import "time"
const secondsInCommonYear = 31536000
const secondsInLeapYear = 31622400
const secondsInDay = 24 * 60 * 60
const secondsInHour = 60 * 60
const secondsInMin = 60
const epochYear = 1970
func isLeapYear(year int) bool {
leapFlag := false
if year%4 == 0 {
if year%100 == 0 {
if year%400 == 0 {
leapFlag = true
} else {
leapFlag = false
}
} else {
leapFlag = true
}
} else {
leapFlag = false
}
return leapFlag
}
func secondsFromEpochYears(numOfYears int64) int64 {
var numOfSeconds int64 = 0
if numOfYears > 0 {
for y := int64(epochYear); y < epochYear+numOfYears; y++ {
if isLeapYear(int(y)) {
numOfSeconds += secondsInLeapYear
} else {
numOfSeconds += secondsInCommonYear
}
}
} else {
for y := int64(epochYear) - 1; y >= epochYear+numOfYears; y-- {
if isLeapYear(int(y)) {
numOfSeconds -= secondsInLeapYear
} else {
numOfSeconds -= secondsInCommonYear
}
}
}
return numOfSeconds
}
// abs returns the absolute value of x
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}
func secondsFromEpochMonths(numOfMonths int64) int64 {
numOfYears := numOfMonths / 12
numOfSeconds := secondsFromEpochYears(numOfYears)
numOfMonthsInLastYear := abs(numOfMonths) % 12
if numOfMonths > 0 {
for m := int64(1); m <= numOfMonthsInLastYear; m++ {
switch m {
case 1:
numOfSeconds += 31 * secondsInDay
case 2:
currentYear := epochYear + numOfYears + 1
if isLeapYear(int(currentYear)) {
numOfSeconds += 29 * secondsInDay
} else {
numOfSeconds += 28 * secondsInDay
}
case 3:
numOfSeconds += 31 * secondsInDay
case 4:
numOfSeconds += 30 * secondsInDay
case 5:
numOfSeconds += 31 * secondsInDay
case 6:
numOfSeconds += 30 * secondsInDay
case 7:
numOfSeconds += 31 * secondsInDay
case 8:
numOfSeconds += 31 * secondsInDay
case 9:
numOfSeconds += 30 * secondsInDay
case 10:
numOfSeconds += 31 * secondsInDay
case 11:
numOfSeconds += 30 * secondsInDay
case 12:
numOfSeconds += 31 * secondsInDay
}
}
} else {
for m := int64(12); m > 12-numOfMonthsInLastYear; m-- {
switch m {
case 1:
numOfSeconds -= 31 * secondsInDay
case 2:
currentYear := epochYear - numOfYears - 1
if isLeapYear(int(currentYear)) {
numOfSeconds -= 29 * secondsInDay
} else {
numOfSeconds -= 28 * secondsInDay
}
case 3:
numOfSeconds -= 31 * secondsInDay
case 4:
numOfSeconds -= 30 * secondsInDay
case 5:
numOfSeconds -= 31 * secondsInDay
case 6:
numOfSeconds -= 30 * secondsInDay
case 7:
numOfSeconds -= 31 * secondsInDay
case 8:
numOfSeconds -= 31 * secondsInDay
case 9:
numOfSeconds -= 30 * secondsInDay
case 10:
numOfSeconds -= 31 * secondsInDay
case 11:
numOfSeconds -= 30 * secondsInDay
case 12:
numOfSeconds -= 31 * secondsInDay
}
}
}
return numOfSeconds
}
// GetTimeFromTimestamp returns a time.Time object for a time related TileDB datatype
// Datetimes in TileDB are deltas from unix epoch with a resolution of the specified time.
func GetTimeFromTimestamp(datatype Datatype, timestamp int64) time.Time {
var then time.Time
switch datatype {
case TILEDB_DATETIME_YEAR:
numOfSeconds := secondsFromEpochYears(timestamp)
then = time.Unix(int64(numOfSeconds), 0)
case TILEDB_DATETIME_MONTH:
numOfSeconds := secondsFromEpochMonths(timestamp)
then = time.Unix(int64(numOfSeconds), 0)
case TILEDB_DATETIME_WEEK:
numOfSeconds := 7 * timestamp * secondsInDay
then = time.Unix(int64(numOfSeconds), 0)
case TILEDB_DATETIME_DAY:
numOfSeconds := timestamp * secondsInDay
then = time.Unix(int64(numOfSeconds), 0)
case TILEDB_DATETIME_HR, TILEDB_TIME_HR:
numOfSeconds := timestamp * secondsInHour
then = time.Unix(int64(numOfSeconds), 0)
case TILEDB_DATETIME_MIN, TILEDB_TIME_MIN:
numOfSeconds := timestamp * secondsInMin
then = time.Unix(int64(numOfSeconds), 0)
case TILEDB_DATETIME_SEC, TILEDB_TIME_SEC:
then = time.Unix(timestamp, 0)
case TILEDB_DATETIME_MS, TILEDB_TIME_MS:
then = time.Unix(0, int64(timestamp*1000*1000))
case TILEDB_DATETIME_US, TILEDB_TIME_US:
then = time.Unix(0, int64(timestamp*1000))
case TILEDB_DATETIME_NS, TILEDB_TIME_NS:
then = time.Unix(0, timestamp)
case TILEDB_DATETIME_PS, TILEDB_TIME_PS:
then = time.Unix(0, int64(timestamp/1000))
case TILEDB_DATETIME_FS, TILEDB_TIME_FS:
then = time.Unix(0, int64(timestamp/(1000*1000)))
case TILEDB_DATETIME_AS, TILEDB_TIME_AS:
then = time.Unix(0, int64(timestamp/(1000*1000)))
}
return then.UTC()
}