forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetter_example_test.go
436 lines (341 loc) · 12.5 KB
/
setter_example_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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package carbon_test
import (
"fmt"
"time"
"github.com/dromara/carbon/v2"
)
func ExampleSetLayout() {
defer carbon.SetLayout(carbon.DateTimeLayout)
fmt.Println("current layout:", carbon.SetLayout(carbon.DateTimeLayout).CurrentLayout())
fmt.Println("current layout:", carbon.SetLayout(carbon.TimeLayout).CurrentLayout())
// Output:
// current layout: 2006-01-02 15:04:05
// current layout: 15:04:05
}
func ExampleCarbon_SetLayout() {
c := carbon.Parse("2020-08-05 13:14:15.999999 +0000 UTC")
fmt.Println("date layout:", c.SetLayout(carbon.DateLayout))
fmt.Println("time layout:", c.SetLayout(carbon.TimeLayout))
fmt.Println("datetime layout:", c.SetLayout(carbon.DateTimeLayout))
fmt.Println("timestamp layout:", c.SetLayout(carbon.TimestampLayout))
// Output:
// date layout: 2020-08-05
// time layout: 13:14:15
// datetime layout: 2020-08-05 13:14:15
// timestamp layout: 1596633255
}
func ExampleSetFormat() {
defer carbon.SetFormat(carbon.DateTimeFormat)
fmt.Println("current layout:", carbon.SetFormat(carbon.DateTimeFormat).CurrentLayout())
fmt.Println("current layout:", carbon.SetFormat(carbon.TimeFormat).CurrentLayout())
// Output:
// current layout: 2006-01-02 15:04:05
// current layout: 15:04:05
}
func ExampleCarbon_SetFormat() {
c := carbon.Parse("2020-08-05 13:14:15.999999 +0000 UTC")
fmt.Println("date format:", c.SetFormat(carbon.DateFormat))
fmt.Println("time format:", c.SetFormat(carbon.TimeFormat))
fmt.Println("datetime format:", c.SetFormat(carbon.DateTimeFormat))
fmt.Println("timestamp format:", c.SetFormat(carbon.TimestampFormat))
// Output:
// date format: 2020-08-05
// time format: 13:14:15
// datetime format: 2020-08-05 13:14:15
// timestamp format: 1596633255
}
func ExampleSetWeekStartsAt() {
defer carbon.SetWeekStartsAt(carbon.Sunday)
fmt.Println(carbon.SetWeekStartsAt(carbon.Sunday).WeekStartsAt())
fmt.Println(carbon.SetWeekStartsAt(carbon.Monday).WeekStartsAt())
// Output:
// Sunday
// Monday
}
func ExampleCarbon_SetWeekStartsAt() {
fmt.Println("week starts at:", carbon.Now().SetWeekStartsAt(carbon.Sunday).WeekStartsAt())
fmt.Println("week starts at:", carbon.Now().SetWeekStartsAt(carbon.Monday).WeekStartsAt())
// Output:
// week starts at: Sunday
// week starts at: Monday
}
func ExampleSetTimezone() {
defer carbon.SetTimezone(carbon.UTC)
fmt.Println("UTC zone location:", carbon.SetTimezone(carbon.UTC).Timezone())
fmt.Println("UTC zone name:", carbon.SetTimezone(carbon.UTC).ZoneName())
fmt.Println("UTC zone offset:", carbon.SetTimezone(carbon.UTC).ZoneOffset())
fmt.Println("PRC zone location:", carbon.SetTimezone(carbon.PRC).Timezone())
fmt.Println("PRC zone name:", carbon.SetTimezone(carbon.PRC).ZoneName())
fmt.Println("PRC zone offset:", carbon.SetTimezone(carbon.PRC).ZoneOffset())
fmt.Println("Japan zone location:", carbon.SetTimezone(carbon.Japan).Timezone())
fmt.Println("Japan zone name:", carbon.SetTimezone(carbon.Japan).ZoneName())
fmt.Println("Japan zone offset:", carbon.SetTimezone(carbon.Japan).ZoneOffset())
// Output:
// UTC zone location: UTC
// UTC zone name: UTC
// UTC zone offset: 0
// PRC zone location: PRC
// PRC zone name: LMT
// PRC zone offset: 29143
// Japan zone location: Japan
// Japan zone name: LMT
// Japan zone offset: 33539
}
func ExampleCarbon_SetTimezone() {
fmt.Println("UTC zone location:", carbon.Now().SetTimezone(carbon.UTC).Timezone())
fmt.Println("UTC zone name:", carbon.Now().SetTimezone(carbon.UTC).ZoneName())
fmt.Println("UTC zone offset:", carbon.Now().SetTimezone(carbon.UTC).ZoneOffset())
fmt.Println("PRC zone location:", carbon.Now().SetTimezone(carbon.PRC).Timezone())
fmt.Println("PRC zone name:", carbon.Now().SetTimezone(carbon.PRC).ZoneName())
fmt.Println("PRC zone offset:", carbon.Now().SetTimezone(carbon.PRC).ZoneOffset())
fmt.Println("Japan zone location:", carbon.Now().SetTimezone(carbon.Japan).Timezone())
fmt.Println("Japan zone name:", carbon.Now().SetTimezone(carbon.Japan).ZoneName())
fmt.Println("Japan zone offset:", carbon.Now().SetTimezone(carbon.Japan).ZoneOffset())
// Output:
// UTC zone location: UTC
// UTC zone name: UTC
// UTC zone offset: 0
// PRC zone location: PRC
// PRC zone name: CST
// PRC zone offset: 28800
// Japan zone location: Japan
// Japan zone name: JST
// Japan zone offset: 32400
}
func ExampleSetLocation() {
defer carbon.SetLocation(time.UTC)
utcTZ, _ := time.LoadLocation(carbon.UTC)
fmt.Println("UTC zone location:", carbon.SetLocation(utcTZ).Timezone())
fmt.Println("UTC zone name:", carbon.SetLocation(utcTZ).ZoneName())
fmt.Println("UTC zone offset:", carbon.SetLocation(utcTZ).ZoneOffset())
prcTZ, _ := time.LoadLocation(carbon.PRC)
fmt.Println("PRC zone location:", carbon.SetLocation(prcTZ).Timezone())
fmt.Println("PRC zone name:", carbon.SetLocation(prcTZ).ZoneName())
fmt.Println("PRC zone offset:", carbon.SetLocation(prcTZ).ZoneOffset())
japanTZ, _ := time.LoadLocation(carbon.Japan)
fmt.Println("Japan zone location:", carbon.SetLocation(japanTZ).Timezone())
fmt.Println("Japan zone name:", carbon.SetLocation(japanTZ).ZoneName())
fmt.Println("Japan zone offset:", carbon.SetLocation(japanTZ).ZoneOffset())
// Output:
// UTC zone location: UTC
// UTC zone name: UTC
// UTC zone offset: 0
// PRC zone location: PRC
// PRC zone name: LMT
// PRC zone offset: 29143
// Japan zone location: Japan
// Japan zone name: LMT
// Japan zone offset: 33539
}
func ExampleCarbon_SetLocation() {
defer carbon.SetLocation(time.UTC)
c := carbon.Parse("2020-08-05")
loc, _ := time.LoadLocation(carbon.PRC)
c.SetLocation(loc)
fmt.Println("default location:", carbon.DefaultTimezone)
fmt.Println("zone location:", c.Timezone())
fmt.Println("zone name:", c.ZoneName())
fmt.Println("zone offset:", c.ZoneOffset())
// Output:
// default location: UTC
// zone location: PRC
// zone name: CST
// zone offset: 28800
}
func ExampleSetLocale() {
defer carbon.SetLocale("en")
carbon.SetLocale("zh-CN")
c := carbon.Parse("2020-08-05")
fmt.Println(c.Constellation())
fmt.Println(c.Season())
fmt.Println(c.ToMonthString())
fmt.Println(c.ToShortMonthString())
fmt.Println(c.ToWeekString())
fmt.Println(c.ToShortWeekString())
// Output:
// 狮子座
// 夏季
// 八月
// 8月
// 星期三
// 周三
}
func ExampleCarbon_SetLocale() {
c := carbon.Parse("2020-08-05").SetLocale("zh-CN")
fmt.Println(c.Constellation())
fmt.Println(c.Season())
fmt.Println(c.ToMonthString())
fmt.Println(c.ToShortMonthString())
fmt.Println(c.ToWeekString())
fmt.Println(c.ToShortWeekString())
// Output:
// 狮子座
// 夏季
// 八月
// 8月
// 星期三
// 周三
}
func ExampleCarbon_SetLanguage() {
lang := carbon.NewLanguage()
lang.SetLocale("en")
fmt.Println(carbon.Parse("2020-08-05").SetLanguage(lang).ToMonthString())
lang.SetLocale("zh-CN")
fmt.Println(carbon.Parse("2020-08-05").SetLanguage(lang).ToMonthString())
// Output:
// August
// 八月
}
func ExampleCarbon_SetDateTime() {
fmt.Println(carbon.Parse("2020-08-05").SetDateTime(2020, 8, 5, 13, 14, 15).ToString())
// Output:
// 2020-08-05 13:14:15 +0000 UTC
}
func ExampleCarbon_SetDateTimeMilli() {
fmt.Println(carbon.Parse("2020-08-05").SetDateTimeMilli(2020, 8, 5, 13, 14, 15, 999).ToString())
// Output:
// 2020-08-05 13:14:15.999 +0000 UTC
}
func ExampleCarbon_SetDateTimeMicro() {
fmt.Println(carbon.Parse("2020-08-05").SetDateTimeMicro(2020, 8, 5, 13, 14, 15, 999999).ToString())
// Output:
// 2020-08-05 13:14:15.999999 +0000 UTC
}
func ExampleCarbon_SetDateTimeNano() {
fmt.Println(carbon.Parse("2020-08-05").SetDateTimeNano(2020, 8, 5, 13, 14, 15, 999999999).ToString())
// Output:
// 2020-08-05 13:14:15.999999999 +0000 UTC
}
func ExampleCarbon_SetDate() {
fmt.Println(carbon.Parse("2020-08-05").SetDate(2020, 8, 5).ToString())
// Output:
// 2020-08-05 00:00:00 +0000 UTC
}
func ExampleCarbon_SetDateMilli() {
fmt.Println(carbon.Parse("2020-08-05").SetDateMilli(2020, 8, 5, 999).ToString())
// Output:
// 2020-08-05 00:00:00.999 +0000 UTC
}
func ExampleCarbon_SetDateMicro() {
fmt.Println(carbon.Parse("2020-08-05").SetDateMicro(2020, 8, 5, 999999).ToString())
// Output:
// 2020-08-05 00:00:00.999999 +0000 UTC
}
func ExampleCarbon_SetDateNano() {
fmt.Println(carbon.Parse("2020-08-05").SetDateNano(2020, 8, 5, 999999999).ToString())
// Output:
// 2020-08-05 00:00:00.999999999 +0000 UTC
}
func ExampleCarbon_SetTime() {
fmt.Println(carbon.Parse("2020-08-05").SetTime(13, 14, 15).ToString())
// Output:
// 2020-08-05 13:14:15 +0000 UTC
}
func ExampleCarbon_SetTimeMilli() {
fmt.Println(carbon.Parse("2020-08-05").SetTimeMilli(13, 14, 15, 999).ToString())
// Output:
// 2020-08-05 13:14:15.999 +0000 UTC
}
func ExampleCarbon_SetTimeMicro() {
fmt.Println(carbon.Parse("2020-08-05").SetTimeMicro(13, 14, 15, 999999).ToString())
// Output:
// 2020-08-05 13:14:15.999999 +0000 UTC
}
func ExampleCarbon_SetTimeNano() {
fmt.Println(carbon.Parse("2020-08-05").SetTimeNano(13, 14, 15, 999999999).ToString())
// Output:
// 2020-08-05 13:14:15.999999999 +0000 UTC
}
func ExampleCarbon_SetYear() {
fmt.Println(carbon.Parse("2020-01-01").SetYear(2019).ToString())
fmt.Println(carbon.Parse("2020-01-31").SetYear(2019).ToString())
fmt.Println(carbon.Parse("2020-02-29").SetYear(2019).ToString())
// Output:
// 2019-01-01 00:00:00 +0000 UTC
// 2019-01-31 00:00:00 +0000 UTC
// 2019-03-01 00:00:00 +0000 UTC
}
func ExampleCarbon_SetYearNoOverflow() {
fmt.Println(carbon.Parse("2020-01-01").SetYearNoOverflow(2019).ToString())
fmt.Println(carbon.Parse("2020-01-31").SetYearNoOverflow(2019).ToString())
fmt.Println(carbon.Parse("2020-02-29").SetYearNoOverflow(2019).ToString())
// Output:
// 2019-01-01 00:00:00 +0000 UTC
// 2019-01-31 00:00:00 +0000 UTC
// 2019-02-28 00:00:00 +0000 UTC
}
func ExampleCarbon_SetMonth() {
fmt.Println(carbon.Parse("2020-01-01").SetMonth(2).ToString())
fmt.Println(carbon.Parse("2020-01-30").SetMonth(2).ToString())
fmt.Println(carbon.Parse("2020-01-31").SetMonth(2).ToString())
// Output:
// 2020-02-01 00:00:00 +0000 UTC
// 2020-03-01 00:00:00 +0000 UTC
// 2020-03-02 00:00:00 +0000 UTC
}
func ExampleCarbon_SetMonthNoOverflow() {
fmt.Println(carbon.Parse("2020-01-01").SetMonthNoOverflow(2).ToString())
fmt.Println(carbon.Parse("2020-01-30").SetMonthNoOverflow(2).ToString())
fmt.Println(carbon.Parse("2020-01-31").SetMonthNoOverflow(2).ToString())
// Output:
// 2020-02-01 00:00:00 +0000 UTC
// 2020-02-29 00:00:00 +0000 UTC
// 2020-02-29 00:00:00 +0000 UTC
}
func ExampleCarbon_SetDay() {
fmt.Println(carbon.Parse("2020-01-01").SetDay(31).ToString())
fmt.Println(carbon.Parse("2020-02-01").SetDay(31).ToString())
fmt.Println(carbon.Parse("2020-02-28").SetDay(31).ToString())
// Output:
// 2020-01-31 00:00:00 +0000 UTC
// 2020-03-02 00:00:00 +0000 UTC
// 2020-03-02 00:00:00 +0000 UTC
}
func ExampleCarbon_SetHour() {
fmt.Println(carbon.Parse("2020-01-01").SetHour(10).ToString())
fmt.Println(carbon.Parse("2020-02-01").SetHour(24).ToString())
fmt.Println(carbon.Parse("2020-02-28").SetHour(31).ToString())
// Output:
// 2020-01-01 10:00:00 +0000 UTC
// 2020-02-02 00:00:00 +0000 UTC
// 2020-02-29 07:00:00 +0000 UTC
}
func ExampleCarbon_SetMinute() {
fmt.Println(carbon.Parse("2020-01-01").SetMinute(10).ToString())
fmt.Println(carbon.Parse("2020-02-01").SetMinute(24).ToString())
fmt.Println(carbon.Parse("2020-02-28").SetMinute(60).ToString())
// Output:
// 2020-01-01 00:10:00 +0000 UTC
// 2020-02-01 00:24:00 +0000 UTC
// 2020-02-28 01:00:00 +0000 UTC
}
func ExampleCarbon_SetSecond() {
fmt.Println(carbon.Parse("2020-01-01").SetSecond(10).ToString())
fmt.Println(carbon.Parse("2020-02-01").SetSecond(24).ToString())
fmt.Println(carbon.Parse("2020-02-28").SetSecond(60).ToString())
// Output:
// 2020-01-01 00:00:10 +0000 UTC
// 2020-02-01 00:00:24 +0000 UTC
// 2020-02-28 00:01:00 +0000 UTC
}
func ExampleCarbon_SetMillisecond() {
fmt.Println(carbon.Parse("2020-01-01").SetMillisecond(100).ToString())
fmt.Println(carbon.Parse("2020-01-01").SetMillisecond(999).ToString())
// Output:
// 2020-01-01 00:00:00.1 +0000 UTC
// 2020-01-01 00:00:00.999 +0000 UTC
}
func ExampleCarbon_SetMicrosecond() {
fmt.Println(carbon.Parse("2020-01-01").SetMicrosecond(100000).ToString())
fmt.Println(carbon.Parse("2020-01-01").SetMicrosecond(999999).ToString())
// Output:
// 2020-01-01 00:00:00.1 +0000 UTC
// 2020-01-01 00:00:00.999999 +0000 UTC
}
func ExampleCarbon_SetNanosecond() {
fmt.Println(carbon.Parse("2020-01-01").SetNanosecond(100000000).ToString())
fmt.Println(carbon.Parse("2020-01-01").SetNanosecond(999999999).ToString())
// Output:
// 2020-01-01 00:00:00.1 +0000 UTC
// 2020-01-01 00:00:00.999999999 +0000 UTC
}