forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.go
84 lines (69 loc) · 1.81 KB
/
eval.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
package dateparse
import (
"fmt"
"time"
)
func evaluateDate(dt datetime) string {
var year, month, day int
if dt.year != nil {
year = int(*dt.year)
}
if dt.month != nil {
month = int(*dt.month)
}
if dt.day != nil {
day = int(*dt.day)
}
if dt.dayOfYear != nil {
// offset from Jan 1st by the specified number of days
dayOffsetted := time.Date(year, time.January, 0, 0, 0, 0, 0, time.Local).AddDate(0, 0, int(*dt.dayOfYear))
month = int(dayOffsetted.Month())
day = dayOffsetted.Day()
}
return fillWithZero(year, 4) + "-" + fillWithZero(month, 2) + "-" + fillWithZero(day, 2)
}
func evaluateTime(dt datetime) string {
var hours, minutes, seconds, milliseconds, microseconds, nanoseconds int
if dt.hours != nil {
if *dt.hours < 13 && dt.am != nil && !*dt.am {
*dt.hours += 12
}
hours = int(*dt.hours)
}
if dt.minutes != nil {
minutes = int(*dt.minutes)
}
if dt.seconds != nil {
seconds = int(*dt.seconds)
}
t := fillWithZero(hours, 2) + ":" + fillWithZero(minutes, 2) + ":" + fillWithZero(seconds, 2)
includeMicrosecond := false
if dt.milliseconds != nil {
milliseconds = int(*dt.milliseconds)
includeMicrosecond = true
}
if dt.microseconds != nil {
microseconds = int(*dt.microseconds)
includeMicrosecond = true
}
if dt.nanoseconds != nil {
nanoseconds = int(*dt.nanoseconds)
includeMicrosecond = true
}
// convert partial seconds to nanoseconds
nanosecondDuration := time.Microsecond*time.Duration(microseconds) + time.Millisecond*time.Duration(milliseconds) + time.Nanosecond*time.Duration(nanoseconds)
if includeMicrosecond {
t = t + "." + fillWithZero(int(nanosecondDuration), 6)
}
return t
}
func fillWithZero(n int, length int) string {
r := fmt.Sprintf("%d", n)
if len(r) > length {
r = ""
}
for len(r) < length {
r = fmt.Sprintf("0%s", r)
}
return r
}