forked from hyperjiang/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime_test.go
370 lines (322 loc) · 9.42 KB
/
datetime_test.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package php_test
import (
"fmt"
"time"
"github.com/hyperjiang/php"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Date/Time Functions", func() {
It("Strtotime", func() {
ts170101 := int64(1483228800) // the UTC timestamp of 2017-01-01
ts170111 := int64(1484092800) // the UTC timestamp of 2017-01-11
tests := []struct {
input string
want int64
}{
{"2017-01-01", ts170101},
{"2017-1-01", ts170101},
{"2017-1-1", ts170101},
{"20170101", ts170101},
{"2017-1-11", ts170111},
{"2017-01-01 00:00:00", ts170101},
{"17-01-01", ts170101},
{"20170101000000", ts170101},
{"2017", ts170101},
{"2017-07-14T10:40:00+08:00", 1500000000},
{"Fri, 14 Jul 2017 02:40:00 +0000", 1500000000},
{"xxx", -1},
{"+1 day", php.Time() + 86400},
{"+2 days", php.Time() + 86400*2},
{"-1 day", php.Time() - 86400},
{"+1 month", php.Time() + 86400*30},
{"+1 year", php.Time() + 86400*365},
{"+1 week", php.Time() + 86400*7},
{"+1 hour", php.Time() + 3600},
{"+1 minute", php.Time() + 60},
{"+1 second", php.Time() + 1},
{"2017-01-1", ts170101},
{"17-01-1", ts170101},
{"2017-01-01T00:00:00Z", ts170101},
{"2017-1-01 00:00:00", ts170101},
{"2017-1-1 00:00:00", ts170101},
{"2017-01-1 00:00:00", ts170101},
{"17-01-01 00:00:00", ts170101},
{"17-1-01 00:00:00", ts170101},
{"17-1-1 00:00:00", ts170101},
{"17-01-1 00:00:00", ts170101},
{"01 Jan 2017", ts170101},
{"01 Jan 17", ts170101},
{"1 Jan 2017", ts170101},
{"1 Jan 17", ts170101},
{"Jan 01 2017", ts170101},
{"Jan 01 17", ts170101},
{"Jan 1 2017", ts170101},
{"Jan 1 17", ts170101},
}
for _, t := range tests {
Expect(php.Strtotime(t.input)).To(Equal(t.want))
}
Expect(php.Strtotime("now")).To(BeNumerically(">", 0))
})
It("Date", func() {
ts := int64(1500000000)
tests := []struct {
format string
timestamp int64
want string
}{
{"Y-m-d H:i:s", ts, "2017-07-14 02:40:00"},
{"Y-m d", ts, "2017-07 14"},
{"c", ts, "2017-07-14T02:40:00+00:00"},
{"Y n j", ts, "2017 7 14"},
{"D", ts, "Fri"},
{"z", ts, "194"},
{"M", ts, "Jul"},
{"l", ts, "Friday"},
{"L", ts, "0"},
{"L", php.Strtotime("2016-01-01"), "1"},
{"w", ts, "5"},
{"w", php.Strtotime("2017-07-09"), "0"},
{"W", ts, "28"},
{"F", ts, "July"},
{"o", ts, "2017"},
{"y", ts, "17"},
{"a A", ts, "am AM"},
{"t", ts, "31"},
{"t", php.Strtotime("2017-02-01"), "28"},
{"t", php.Strtotime("2017-12-11"), "31"},
{"g G h H", ts, "2 2 02 02"},
{"g G h H", php.Strtotime("2017-07-01 18:00:00"), "6 18 06 18"},
{"i s", ts, "40 00"},
{"e T", ts, "UTC UTC"},
{"O P", ts, "+0000 +00:00"},
{"U", ts, fmt.Sprintf("%v", ts)},
{"r", ts, "Fri, 14 Jul 2017 02:40:00 +0000"},
{"Y年m月d日", ts, "2017年07月14日"},
}
for _, t := range tests {
Expect(php.Date(t.format, t.timestamp)).To(Equal(t.want))
}
})
It("Today", func() {
Expect(php.Today("Y-m-d")).To(Equal(time.Now().Format("2006-01-02")))
Expect(php.Today("xxx")).To(Equal("xxx"))
})
It("LocalDate", func() {
ts := int64(1500000000)
zone, offset := time.Unix(0, 0).Zone()
// offset is the seconds east of UTC.
if zone != "GMT" {
// note that BST is one hour ahead of UTC, so the below assertion will fail in GMT timezone
Expect(php.LocalDate("Y-m-d H:i:s", ts)).To(Equal(php.Date("Y-m-d H:i:s", ts+int64(offset))))
}
})
It("FirstDateOfMonth", func() {
ts := int64(1500000000)
d := time.Unix(ts, 0) // 2017-07-14
fd := php.FirstDateOfMonth(d)
Expect(fd.Format("2006-01-02")).To(Equal("2017-07-01"))
})
It("FirstDateOfLastMonth", func() {
tests := []struct {
input time.Time
want string
}{
{time.Date(2017, time.July, 14, 0, 0, 0, 0, time.UTC), "2017-06-01"},
{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), "2017-12-01"},
}
for _, t := range tests {
Expect(php.FirstDateOfLastMonth(t.input).Format("2006-01-02")).To(Equal(t.want))
}
})
It("Checkdate", func() {
type args struct {
month int
day int
year int
}
tests := []struct {
args args
want bool
}{
{args{12, 31, 2000}, true},
{args{12, 31, 1}, true},
{args{12, 31, 0}, false},
{args{2, 29, 2001}, false},
}
for _, t := range tests {
Expect(php.Checkdate(t.args.month, t.args.day, t.args.year)).To(Equal(t.want))
}
})
It("Microtime", func() {
start := php.Microtime()
time.Sleep(100 * time.Millisecond)
end := php.Microtime()
duration := end - start
Expect(duration).To(BeNumerically(">=", 0.09))
Expect(duration).To(BeNumerically("<=", 1))
})
It("DateCreateFromFormat", func() {
type args struct {
f string
t string
}
tests := []struct {
args args
want string
}{
{
args{"j-M-Y", "15-Feb-2009"},
"2009-02-15 00:00:00",
},
{
args{"j-F-Y", "15-January-2009"},
"2009-01-15 00:00:00",
},
{
args{"D M d H:i:s O Y", "Mon Jan 15 10:10:10 +0800 2009"},
"2009-01-15 10:10:10",
},
{
args{"l M d H:i:s T Y", "Monday Jan 15 10:10:10 EST 2009"},
"2009-01-15 10:10:10",
},
}
for _, t := range tests {
d, err := php.DateCreateFromFormat(t.args.f, t.args.t)
Expect(err).NotTo(HaveOccurred())
Expect(d.Format("2006-01-02 15:04:05")).To(Equal(t.want))
}
})
It("DateDateSet", func() {
date, err := php.DateDateSet(2001, 2, 3)
Expect(err).NotTo(HaveOccurred())
Expect(date.Format("2006-01-02")).To(Equal("2001-02-03"))
})
It("DateDefaultTimezoneSet and DateDefaultTimezoneGet", func() {
tz := "Asia/Shanghai"
err := php.DateDefaultTimezoneSet(tz)
Expect(err).NotTo(HaveOccurred())
Expect(php.DateDefaultTimezoneGet()).To(Equal("CST")) // China Standard Time
err = php.DateDefaultTimezoneSet("unknown/timezone")
Expect(err).To(HaveOccurred())
})
It("DateTimezoneSet and DateTimezoneGet", func() {
tz := "Asia/Shanghai"
t1 := time.Now()
t2, err := php.DateTimezoneSet(t1, tz)
Expect(err).NotTo(HaveOccurred())
Expect(php.DateTimezoneGet(t2)).To(Equal("CST")) // China Standard Time
_, err = php.DateTimezoneSet(t1, "unknown/timezone")
Expect(err).To(HaveOccurred())
})
It("DateDiff", func() {
t1, err := php.DateCreate("2019-09-30")
Expect(err).NotTo(HaveOccurred())
t2, err := php.DateCreate("2019-10-01")
Expect(err).NotTo(HaveOccurred())
d := php.DateDiff(t1, t2)
Expect(d.Hours()).To(Equal(24.0))
})
It("DateFormat", func() {
date, err := php.DateCreate("2019-09-30")
Expect(err).NotTo(HaveOccurred())
Expect(php.DateFormat(date, "Y-m-d H:i:s")).To(Equal("2019-09-30 00:00:00"))
})
It("DateIntervalCreateFromDateString", func() {
tests := []struct {
input string
want time.Duration
}{
{"1 minute +1 second", 61 * time.Second},
{"1 day", 86400 * time.Second},
{"2 weeks", 86400 * 14 * time.Second},
{"1 year + 1 day", 86400 * 366 * time.Second},
{"day + 12 hours", (86400 + 3600*12) * time.Second},
{"3600 seconds", 3600 * time.Second},
}
for _, t := range tests {
d, err := php.DateIntervalCreateFromDateString(t.input)
Expect(err).NotTo(HaveOccurred())
Expect(d).To(Equal(t.want))
}
_, err := php.DateIntervalCreateFromDateString("invalid string")
Expect(err).To(HaveOccurred())
})
It("DateISODateSet", func() {
type args struct {
year int
week int
day int
}
tests := []struct {
args args
want string
}{
{args{2008, 2, 1}, "2008-01-07"},
{args{2008, 2, 7}, "2008-01-13"},
{args{2008, 53, 7}, "2009-01-04"},
{args{2009, 1, 1}, "2008-12-29"},
}
for _, t := range tests {
d, err := php.DateISODateSet(t.args.year, t.args.week, t.args.day)
Expect(err).NotTo(HaveOccurred())
Expect(d.Format("2006-01-02")).To(Equal(t.want))
}
_, err := php.DateISODateSet(-1, 1, 1)
Expect(err).To(HaveOccurred())
})
Describe("DateModify", func() {
It("+1 day", func() {
date1, _ := php.DateCreate("2006-12-12")
date2, _ := php.DateCreate("2006-12-13")
got, err := php.DateModify(date1, "+1 day")
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(date2))
})
It("+1 month", func() {
date1, _ := php.DateCreate("2000-12-31")
date2, _ := php.DateCreate("2001-01-30")
got, err := php.DateModify(date1, "+1 month")
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(date2))
})
It("invalid input", func() {
date, _ := php.DateCreate("2006-12-12")
_, err := php.DateModify(date, "invalid string")
Expect(err).To(HaveOccurred())
})
})
It("DateOffsetGet", func() {
loc, _ := time.LoadLocation("America/New_York")
winter, _ := php.DateCreate("2010-12-21")
winter = winter.In(loc)
Expect(php.DateOffsetGet(winter)).To(Equal(-18000))
summer, _ := php.DateCreate("2008-06-21")
summer = summer.In(loc)
Expect(php.DateOffsetGet(summer)).To(Equal(-14400))
})
It("DateAdd", func() {
date1, _ := php.DateCreate("2000-01-01")
date2, _ := php.DateCreate("2000-01-11")
interval, _ := php.DateIntervalCreateFromDateString("10 days")
Expect(php.DateAdd(date1, interval)).To(Equal(date2))
})
It("DateSub", func() {
date1, _ := php.DateCreate("2000-01-20")
date2, _ := php.DateCreate("2000-01-10")
interval, _ := php.DateIntervalCreateFromDateString("10 days")
Expect(php.DateSub(date1, interval)).To(Equal(date2))
})
It("DateTimestampGet", func() {
var now = time.Now()
Expect(php.DateTimestampGet(now)).To(Equal(now.Unix()))
})
It("DateTimestampSet", func() {
var now = time.Now()
var ts = now.Unix()
var d = now.Sub(php.DateTimestampSet(ts))
Expect(d).To(BeNumerically("<", time.Second))
})
})