-
Notifications
You must be signed in to change notification settings - Fork 16
/
date.go
152 lines (130 loc) · 3.15 KB
/
date.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
package pgo
import (
"regexp"
"strconv"
"time"
)
const (
DivideMicroseconds = 1e3
DivideMilliseconds = 1e6
)
var specCases map[string]interface{}
type goDate struct {
parsedSymbols []string
inputDateFormat string
t time.Time
unix int64
phpToGoFormat map[string]string
}
type Time time.Time
const phpDateFormatSymbols = "[\\D]"
// Date returns formatted output of system data/time
// 1st argument formatted string e.g.: Y-m-d H:i:s
// 2nd argument int64 unix timestamp
func Date(args ...interface{}) string {
var date goDate
if len(args) == 0 { // return default "2006-01-02 15:04:05.999999999 -0700 MST" formatted string
return date.t.String()
}
for i, p := range args {
switch i {
case 0:
param, ok := p.(string)
isOk(ok, "You must provide format string parameter as string")
date.inputDateFormat = param
case 1:
param, ok := p.(int64)
isOk(ok, "You must provide timestamp as int64 type")
date.unix = param
}
}
date.t = time.Now()
if date.unix > 0 {
// parse unix representation with int type to Time object
date.t = time.Unix(date.unix, 0)
}
date.initMapping()
return date.parse()
}
func (date *goDate) parse() string {
date.convert()
var convertedString string
for _, v := range date.parsedSymbols {
if val, ok := date.phpToGoFormat[v]; ok {
convertedString += date.t.Format(val)
} else if sVal, ok := specCases[v]; ok {
v, ok := sVal.(int)
if ok {
convertedString += strconv.Itoa(v)
} else {
convertedString += sVal.(string)
}
} else {
convertedString += date.t.Format(v)
}
}
return convertedString
}
func (date *goDate) convert() {
r, _ := regexp.Compile(phpDateFormatSymbols)
for _, chSlice := range r.FindAllStringSubmatch(date.inputDateFormat, -1) {
date.parsedSymbols = append(date.parsedSymbols, chSlice...)
}
}
// initializes date formats mapping between go and php
func (date *goDate) initMapping() {
date.phpToGoFormat = map[string]string{
"Y": "2006",
"m": "01",
"d": "02",
"H": "15",
"i": "04",
"s": "05",
"D": "Mon",
"l": "Monday",
"M": "Jan",
"F": "January",
"r": time.RubyDate,
"c": time.RFC3339,
"A": "PM",
}
// todo: move all the spec cases to parser, because of waste of the resources
_, isoWeek := date.t.ISOWeek()
zone, offset := date.t.Zone()
// determine quarter of a year
q := 1
m := int(date.t.Month())
if m > 3 && m <= 6 {
q = 2
} else if m > 6 && m <= 9 {
q = 3
} else if m > 9 && m <= 12 {
q = 4
}
specCases = map[string]interface{}{
"l": date.t.Weekday().String(),
"N": int(date.t.Weekday()),
"z": date.t.YearDay(),
"j": date.t.Day(),
"W": isoWeek,
"Z": offset,
"T": zone,
"Q": q,
}
}
// Milliseconds from time.Time Go type
func (t Time) Milliseconds() int64 {
return time.Time(t).UnixNano() / DivideMilliseconds
}
// Microseconds from time.Time Go type
func (t Time) Microseconds() int64 {
return time.Time(t).UnixNano() / DivideMicroseconds
}
// UnixMilli milliseconds since unix epoch
func UnixMilli() int64 {
return time.Now().UnixNano() / DivideMilliseconds
}
// UnixMicro microseconds since unix epoch
func UnixMicro() int64 {
return time.Now().UnixNano() / DivideMicroseconds
}