-
Notifications
You must be signed in to change notification settings - Fork 6
/
format.go
132 lines (113 loc) · 3.58 KB
/
format.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
package datetime
import (
strings2 "github.com/goradd/goradd/pkg/strings"
"strings"
"time"
)
const (
// taken from time.Format
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
DateOnlyFormat = "2006-01-02"
TimeOnlyFormat = "15:04:05"
)
// Parse parses the given layout string to turn a string int a DateTime
// Since the time.Time doc is not that great about really describing the format, here it is:
//
// Day of the week Mon Monday
// Day 02 2 _2 (width two, right justified)
// Month 01 1 Jan January
// Year 06 2006
// Hour 03 3 15
// Minute 04 4
// Second 05 5
// ms μs ns .000 .000000 .000000000
// ms μs ns .999 .999999 .999999999 (trailing zeros removed)
// am/pm PM pm
// Timezone MST
// Offset -0700 -07 -07:00 Z0700 Z07:00
func Parse(layout, value string) (DateTime, error) {
var ts bool
if layout == Stamp || layout == StampMilli || layout == StampMicro || layout == StampNano {
ts = true
}
layout, value = strings.ToUpper(layout), strings.ToUpper(value) // standardize Pm, PM or pm
t, err := time.Parse(layout, value)
return DateTime{t, ts}, err
}
// ParseInOffset is like time.ParseInLocation but uses the given timezone offset in minutes from UTC to
// be the location of the parsed time.
func ParseInOffset(layout, value string, tzOffset int) (DateTime, error) {
dt, err := Parse(layout, value)
if err == nil {
dt = dt.As(time.FixedZone("", tzOffset * 60))
}
return dt, err
}
// FromSqlDateTime will receive a Date, Time, DateTime or Timestamp type of string that is typically output by SQL and
// convert it to our own DateTime object. If the SQL date time string does not have timezone information,
// the resulting value will be in UTC time, will not be a timestamp, and we are assuming that the SQL itself is in UTC.
// If it DOES have timezone information, we will assume its a timestamp.
// If an error occurs, the returned value will be the zero date.
func FromSqlDateTime(s string) (t DateTime, err error) {
var hasDate, hasTime, hasNano, hasTZ bool
var form string
if strings.Contains(s, ".") {
hasNano = true
}
if strings.Contains(s, "-") {
hasDate = true
}
if strings.Contains(s, ":") {
hasTime = true
if strings.LastIndexAny(s, "+-") > strings.LastIndex(s, ":") {
hasTZ = true
}
}
if hasNano {
form = "2006-01-02 15:04:05.999999"
if hasTZ {
form += "-07"
}
} else if hasDate && hasTime {
form = "2006-01-02 15:04:05"
if hasTZ {
form += "-07"
}
} else if hasDate {
form = "2006-01-02"
} else {
form = "15:04:05"
}
t, err = Parse(form, s)
if hasTZ {
t.isTimestamp = true
}
return
}
func LayoutHasDate(layout string) bool {
return strings2.ContainsAnyStrings(layout, "6", "2", "Jan")
}
func LayoutHasTime(layout string) bool {
return strings2.ContainsAnyStrings(layout, "15", "4", "5", "3")
}
func (d DateTime) Format(layout string) string {
if d.IsZero() {
return ""
}
return d.Time.Format(layout)
}