This repository was archived by the owner on Jul 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
1484 lines (1207 loc) · 39.7 KB
/
Copy pathexample_test.go
File metadata and controls
1484 lines (1207 loc) · 39.7 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package goda_test
import (
"encoding/json"
"fmt"
"time"
"github.com/iseki0/goda"
)
// Example demonstrates basic usage of the goda package.
func Example() {
// Create a specific date
date := goda.MustLocalDateOf(2024, goda.March, 15)
fmt.Println("LocalDate:", date)
// Create a specific time
timeOfDay := goda.MustLocalTimeOf(14, 30, 45, 0)
fmt.Println("LocalTime:", timeOfDay)
// Combine date and time
datetime := date.AtTime(timeOfDay)
fmt.Println("LocalDateTime:", datetime)
// With timezone offset
offset := goda.MustZoneOffsetOfHours(8) // +08:00
offsetDateTime := datetime.AtOffset(offset)
fmt.Println("OffsetDateTime:", offsetDateTime)
// Parse from string
parsedDate := goda.MustLocalDateParse("2024-03-15")
parsedTime := goda.MustLocalTimeParse("14:30:45.123456789")
parsedDateTime := goda.MustLocalDateTimeParse("2024-03-15T14:30:45.123456789")
parsedOffsetDateTime := goda.MustOffsetDateTimeParse("2024-03-15T14:30:45+08:00")
fmt.Println("Parsed LocalDate:", parsedDate)
fmt.Println("Parsed LocalTime:", parsedTime)
fmt.Println("Parsed LocalDateTime:", parsedDateTime)
fmt.Println("Parsed OffsetDateTime:", parsedOffsetDateTime)
// LocalDate arithmetic
tomorrow := date.Chain().PlusDays(1).MustGet()
nextMonth := date.Chain().PlusMonths(1).MustGet()
nextYear := date.Chain().PlusYears(1).MustGet()
fmt.Println("Tomorrow:", tomorrow)
fmt.Println("Next month:", nextMonth)
fmt.Println("Next year:", nextYear)
// Comparisons
date1 := goda.MustLocalDateOf(2024, goda.March, 15)
date2 := goda.MustLocalDateOf(2024, goda.March, 20)
if date1.IsBefore(date2) {
fmt.Println("date1 is before date2")
}
// Serialization
jsonBytes, _ := json.Marshal(date)
fmt.Println("JSON:", string(jsonBytes))
str := date.String()
fmt.Println("String:", str)
// Output:
// LocalDate: 2024-03-15
// LocalTime: 14:30:45
// LocalDateTime: 2024-03-15T14:30:45
// OffsetDateTime: 2024-03-15T14:30:45+08:00
// Parsed LocalDate: 2024-03-15
// Parsed LocalTime: 14:30:45.123456789
// Parsed LocalDateTime: 2024-03-15T14:30:45.123456789
// Parsed OffsetDateTime: 2024-03-15T14:30:45+08:00
// Tomorrow: 2024-03-16
// Next month: 2024-04-15
// Next year: 2025-03-15
// date1 is before date2
// JSON: "2024-03-15"
// String: 2024-03-15
}
// ExampleLocalDateNow demonstrates how to get the current date.
func ExampleLocalDateNow() {
// Get current date in local timezone
today := goda.LocalDateNow()
// check that we got a valid date
fmt.Printf("Got valid date: %v\n", !today.IsZero())
fmt.Printf("Has year component: %v\n", today.Year() != 0)
// Output:
// Got valid date: true
// Has year component: true
}
// ExampleLocalDateNowUTC demonstrates how to get the current date in UTC.
func ExampleLocalDateNowUTC() {
// Get current date in UTC
todayUTC := goda.LocalDateNowUTC()
// Verify it's a valid date
fmt.Printf("Valid: %v\n", !todayUTC.IsZero())
// Output:
// Valid: true
}
// ExampleLocalDateNowIn demonstrates how to get the current date in a specific timezone.
func ExampleLocalDateNowIn() {
// Get current date in Tokyo timezone
tokyo, _ := time.LoadLocation("Asia/Tokyo")
todayTokyo := goda.LocalDateNowIn(tokyo)
// Verify it's valid
fmt.Printf("Valid: %v\n", !todayTokyo.IsZero())
// Output:
// Valid: true
}
// ExampleLocalDateParse demonstrates parsing a date from a string.
func ExampleLocalDateParse() {
// Parse a date string
date, err := goda.LocalDateParse("2024-03-15")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(date)
// Output:
// 2024-03-15
}
// ExampleMustLocalDateParse demonstrates parsing a date that panics on error.
func ExampleMustLocalDateParse() {
// Parse a date string (panics if invalid)
date := goda.MustLocalDateParse("2024-03-15")
fmt.Println(date)
// Output:
// 2024-03-15
}
// ExampleNewLocalDate demonstrates how to create a date.
func ExampleLocalDateOf() {
// Create a valid date
date, err := goda.LocalDateOf(2024, goda.January, 15)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(date)
// Try to create an invalid date
_, err = goda.LocalDateOf(2024, goda.February, 30)
fmt.Println("Error:", err)
// Output:
// 2024-01-15
// Error: goda: invalid date February 30
}
// ExampleMustNewLocalDate demonstrates how to create a date that panics on error.
func ExampleMustLocalDateOf() {
// Create a date (panics if invalid)
date := goda.MustLocalDateOf(2024, goda.March, 15)
fmt.Println(date)
// Output:
// 2024-03-15
}
// ExampleLocalDateOfGoTime demonstrates converting from time.Time to LocalDate.
func ExampleLocalDateOfGoTime() {
// Convert from time.LocalTime
goTime := time.Date(2024, time.March, 15, 14, 30, 0, 0, time.UTC)
date := goda.LocalDateOfGoTime(goTime)
fmt.Println(date)
// Output:
// 2024-03-15
}
// ExampleLocalDateChain_PlusDays demonstrates adding days to a date.
func ExampleLocalDateChain_PlusDays() {
date := goda.MustLocalDateOf(2024, goda.January, 15)
fmt.Println("Original:", date)
fmt.Println("Plus 10 days:", date.Chain().PlusDays(10).MustGet())
fmt.Println("Minus 10 days:", date.Chain().PlusDays(-10).MustGet())
// Output:
// Original: 2024-01-15
// Plus 10 days: 2024-01-25
// Minus 10 days: 2024-01-05
}
// ExampleLocalDateChain_PlusMonths demonstrates adding months to a date.
func ExampleLocalDateChain_PlusMonths() {
date := goda.MustLocalDateOf(2024, goda.January, 31)
fmt.Println("Original:", date)
fmt.Println("Plus 1 month:", date.Chain().PlusMonths(1).MustGet())
fmt.Println("Plus 2 months:", date.Chain().PlusMonths(2).MustGet())
// Output:
// Original: 2024-01-31
// Plus 1 month: 2024-02-29
// Plus 2 months: 2024-03-31
}
// ExampleLocalDate_Compare demonstrates comparing dates.
func ExampleLocalDate_Compare() {
date1 := goda.MustLocalDateOf(2024, goda.March, 15)
date2 := goda.MustLocalDateOf(2024, goda.March, 20)
date3 := goda.MustLocalDateOf(2024, goda.March, 15)
fmt.Println("date1 < date2:", date1.IsBefore(date2))
fmt.Println("date1 > date2:", date1.IsAfter(date2))
fmt.Println("date1 == date3:", date1.Compare(date3) == 0)
// Output:
// date1 < date2: true
// date1 > date2: false
// date1 == date3: true
}
// ExampleLocalDate_DayOfWeek demonstrates getting the day of week.
func ExampleLocalDate_DayOfWeek() {
date := goda.MustLocalDateOf(2024, goda.March, 15)
fmt.Println("Day of week:", date.DayOfWeek())
fmt.Println("Is Friday?", date.DayOfWeek() == goda.Friday)
// Output:
// Day of week: Friday
// Is Friday? true
}
// ExampleLocalTimeNow demonstrates how to get the current time.
func ExampleLocalTimeNow() {
// Get current time in local timezone
now := goda.LocalTimeNow()
// Verify it's valid and within range
fmt.Printf("Valid: %v\n", !now.IsZero())
fmt.Printf("Hour in range: %v\n", now.Hour() >= 0 && now.Hour() < 24)
// Output:
// Valid: true
// Hour in range: true
}
// ExampleLocalTimeNowUTC demonstrates how to get the current time in UTC.
func ExampleLocalTimeNowUTC() {
// Get current time in UTC
nowUTC := goda.LocalTimeNowUTC()
// Verify it's valid
fmt.Printf("Valid: %v\n", !nowUTC.IsZero())
// Output:
// Valid: true
}
// ExampleLocalTimeNowIn demonstrates how to get the current time in a specific timezone.
func ExampleLocalTimeNowIn() {
// Get current time in Tokyo timezone
tokyo, _ := time.LoadLocation("Asia/Tokyo")
nowTokyo := goda.LocalTimeNowIn(tokyo)
// Verify it's valid
fmt.Printf("Valid: %v\n", !nowTokyo.IsZero())
// Output:
// Valid: true
}
// ExampleLocalTimeParse demonstrates parsing a time from a string.
func ExampleLocalTimeParse() {
// Parse a time string
t, err := goda.LocalTimeParse("14:30:45.123456789")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(t)
// Output:
// 14:30:45.123456789
}
// ExampleMustLocalTimeParse demonstrates parsing a time that panics on error.
func ExampleMustLocalTimeParse() {
// Parse a time string (panics if invalid)
t := goda.MustLocalTimeParse("14:30:45")
fmt.Println(t)
// Output:
// 14:30:45
}
// ExampleLocalTimeOf demonstrates how to create a time.
func ExampleLocalTimeOf() {
// Create a valid time
t, err := goda.LocalTimeOf(14, 30, 45, 123456789)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(t)
// Try to create an invalid time
_, err = goda.LocalTimeOf(25, 0, 0, 0)
fmt.Println("Error:", err)
// Output:
// 14:30:45.123456789
// Error: goda: invalid value of HourOfDay (valid range 0 - 23): 25
}
// ExampleMustLocalTimeOf demonstrates how to create a time that panics on error.
func ExampleMustLocalTimeOf() {
// Create a time (panics if invalid)
t := goda.MustLocalTimeOf(14, 30, 45, 0)
fmt.Println(t)
// Midnight
midnight := goda.MustLocalTimeOf(0, 0, 0, 0)
fmt.Println("Midnight:", midnight)
// Output:
// 14:30:45
// Midnight: 00:00:00
}
// ExampleLocalTimeOfGoTime demonstrates converting from time.Time to LocalTime.
func ExampleLocalTimeOfGoTime() {
// Convert from time.LocalTime
goTime := time.Date(2024, time.March, 15, 14, 30, 45, 123456789, time.UTC)
t := goda.LocalTimeOfGoTime(goTime)
fmt.Println(t)
// Output:
// 14:30:45.123456789
}
// ExampleLocalTime_hour demonstrates accessing time components.
func ExampleLocalTime_hour() {
t := goda.MustLocalTimeOf(14, 30, 45, 123456789)
fmt.Println("Hour:", t.Hour())
fmt.Println("Minute:", t.Minute())
fmt.Println("Second:", t.Second())
fmt.Println("Millisecond:", t.Millisecond())
fmt.Println("Nano:", t.Nano())
// Output:
// Hour: 14
// Minute: 30
// Second: 45
// Millisecond: 123
// Nano: 123456789
}
// ExampleLocalTime_Compare demonstrates comparing times.
func ExampleLocalTime_Compare() {
t1 := goda.MustLocalTimeOf(14, 30, 0, 0)
t2 := goda.MustLocalTimeOf(15, 0, 0, 0)
t3 := goda.MustLocalTimeOf(14, 30, 0, 0)
fmt.Println("t1 < t2:", t1.IsBefore(t2))
fmt.Println("t1 > t2:", t1.IsAfter(t2))
fmt.Println("t1 == t3:", t1.Compare(t3) == 0)
// Output:
// t1 < t2: true
// t1 > t2: false
// t1 == t3: true
}
// ExampleLocalTime_String demonstrates the string format with fractional seconds.
func ExampleLocalTime_String() {
// LocalTime without fractional seconds
t1 := goda.MustLocalTimeOf(14, 30, 45, 0)
fmt.Println(t1)
// LocalTime with milliseconds
t2 := goda.MustLocalTimeOf(14, 30, 45, 123000000)
fmt.Println(t2)
// LocalTime with microseconds
t3 := goda.MustLocalTimeOf(14, 30, 45, 123456000)
fmt.Println(t3)
// LocalTime with nanoseconds
t4 := goda.MustLocalTimeOf(14, 30, 45, 123456789)
fmt.Println(t4)
// LocalTime with trailing zeros aligned to 3-digit boundaries
t5 := goda.MustLocalTimeOf(14, 30, 45, 100000000)
fmt.Println(t5)
// Output:
// 14:30:45
// 14:30:45.123
// 14:30:45.123456
// 14:30:45.123456789
// 14:30:45.100
}
// ExampleMonth demonstrates working with months.
func ExampleMonth() {
// Months are constants from January (1) to December (12)
fmt.Println("January:", goda.January)
fmt.Println("December:", goda.December)
// Get month length
fmt.Println("Days in February (non-leap):", goda.February.Length(false))
fmt.Println("Days in February (leap):", goda.February.Length(true))
// Output:
// January: January
// December: December
// Days in February (non-leap): 28
// Days in February (leap): 29
}
// ExampleYear_IsLeapYear demonstrates checking for leap years.
func ExampleYear_IsLeapYear() {
fmt.Println("2024 is leap:", goda.Year(2024).IsLeapYear())
fmt.Println("2023 is leap:", goda.Year(2023).IsLeapYear())
fmt.Println("2000 is leap:", goda.Year(2000).IsLeapYear())
fmt.Println("1900 is leap:", goda.Year(1900).IsLeapYear())
// Output:
// 2024 is leap: true
// 2023 is leap: false
// 2000 is leap: true
// 1900 is leap: false
}
// ExampleLocalDate_MarshalJSON demonstrates JSON serialization.
func ExampleLocalDate_MarshalJSON() {
date := goda.MustLocalDateOf(2024, goda.March, 15)
jsonBytes, _ := json.Marshal(date)
fmt.Println(string(jsonBytes))
// Output:
// "2024-03-15"
}
// ExampleLocalDate_UnmarshalJSON demonstrates JSON deserialization.
func ExampleLocalDate_UnmarshalJSON() {
var date goda.LocalDate
jsonData := []byte(`"2024-03-15"`)
err := json.Unmarshal(jsonData, &date)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(date)
// Output:
// 2024-03-15
}
// ExampleLocalTime_MarshalJSON demonstrates JSON serialization.
func ExampleLocalTime_MarshalJSON() {
t := goda.MustLocalTimeOf(14, 30, 45, 123456789)
jsonBytes, _ := json.Marshal(t)
fmt.Println(string(jsonBytes))
// Output:
// "14:30:45.123456789"
}
// ExampleLocalTime_UnmarshalJSON demonstrates JSON deserialization.
func ExampleLocalTime_UnmarshalJSON() {
var t goda.LocalTime
jsonData := []byte(`"14:30:45.123456789"`)
err := json.Unmarshal(jsonData, &t)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(t)
// Output:
// 14:30:45.123456789
}
// ExampleLocalDateTime demonstrates basic LocalDateTime usage.
func ExampleLocalDateTime() {
// Create from components
dt := goda.MustLocalDateTimeOf(2024, goda.March, 15, 14, 30, 45, 123456789)
fmt.Println(dt)
// Access date and time parts
fmt.Printf("LocalDate: %s\n", dt.LocalDate())
fmt.Printf("LocalTime: %s\n", dt.LocalTime())
// Access individual components
fmt.Printf("Year: %d, Hour: %d\n", dt.Year(), dt.Hour())
// Output:
// 2024-03-15T14:30:45.123456789
// LocalDate: 2024-03-15
// LocalTime: 14:30:45.123456789
// Year: 2024, Hour: 14
}
// ExampleLocalDateTimeParse demonstrates parsing a datetime from a string.
func ExampleLocalDateTimeParse() {
dt, err := goda.LocalDateTimeParse("2024-03-15T14:30:45.123456789")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(dt)
// Output:
// 2024-03-15T14:30:45.123456789
}
// ExampleMustLocalDateTimeParse demonstrates parsing that panics on error.
func ExampleMustLocalDateTimeParse() {
dt := goda.MustLocalDateTimeParse("2024-03-15T14:30:45")
fmt.Println(dt)
// Output:
// 2024-03-15T14:30:45
}
// ExampleLocalDate_AtTime demonstrates creating a datetime from date and time.
func ExampleLocalDate_AtTime() {
date := goda.MustLocalDateOf(2024, goda.March, 15)
time := goda.MustLocalTimeOf(14, 30, 45, 123456789)
dt := date.AtTime(time)
fmt.Println(dt)
// Output:
// 2024-03-15T14:30:45.123456789
}
// ExampleLocalTime_AtDate demonstrates creating a datetime from time and date.
func ExampleLocalTime_AtDate() {
time := goda.MustLocalTimeOf(14, 30, 45, 123456789)
date := goda.MustLocalDateOf(2024, goda.March, 15)
dt := time.AtDate(date)
fmt.Println(dt)
// Output:
// 2024-03-15T14:30:45.123456789
}
// ExampleLocalDateTimeNow demonstrates getting the current datetime.
func ExampleLocalDateTimeNow() {
now := goda.LocalDateTimeNow()
// Verify it's valid
fmt.Printf("Valid: %v\n", !now.IsZero())
fmt.Printf("Has components: %v\n", now.Year() != 0 && now.Hour() >= 0)
// Output:
// Valid: true
// Has components: true
}
// ExampleLocalDateTime_Compare demonstrates comparing datetimes.
func ExampleLocalDateTime_Compare() {
dt1 := goda.MustLocalDateTimeParse("2024-03-15T14:30:45")
dt2 := goda.MustLocalDateTimeParse("2024-03-15T14:30:45")
dt3 := goda.MustLocalDateTimeParse("2024-03-15T15:30:45")
fmt.Printf("dt1 == dt2: %v\n", dt1.Compare(dt2) == 0)
fmt.Printf("dt1 < dt3: %v\n", dt1.IsBefore(dt3))
fmt.Printf("dt3 > dt1: %v\n", dt3.IsAfter(dt1))
// Output:
// dt1 == dt2: true
// dt1 < dt3: true
// dt3 > dt1: true
}
// ExampleLocalDateTimeChain_PlusDays demonstrates adding days.
func ExampleLocalDateTimeChain_PlusDays() {
dt := goda.MustLocalDateTimeParse("2024-03-15T14:30:45")
future := dt.Chain().PlusDays(10).MustGet()
fmt.Println(future)
// Output:
// 2024-03-25T14:30:45
}
// ExampleLocalDateTime_MarshalJSON demonstrates JSON serialization.
func ExampleLocalDateTime_MarshalJSON() {
dt := goda.MustLocalDateTimeParse("2024-03-15T14:30:45.123456789")
jsonBytes, _ := json.Marshal(dt)
fmt.Println(string(jsonBytes))
// Output:
// "2024-03-15T14:30:45.123456789"
}
// ExampleLocalDateTime_UnmarshalJSON demonstrates JSON deserialization.
func ExampleLocalDateTime_UnmarshalJSON() {
var dt goda.LocalDateTime
jsonData := []byte(`"2024-03-15T14:30:45.123456789"`)
err := json.Unmarshal(jsonData, &dt)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(dt)
// Output:
// 2024-03-15T14:30:45.123456789
}
// ExampleLocalDateTime_IsSupportedField demonstrates checking field support.
func ExampleLocalDateTime_IsSupportedField() {
dt := goda.MustLocalDateTimeOf(2024, goda.March, 15, 14, 30, 45, 0)
fmt.Printf("Supports FieldHourOfDay: %v\n", dt.IsSupportedField(goda.FieldHourOfDay))
fmt.Printf("Supports FieldDayOfMonth: %v\n", dt.IsSupportedField(goda.FieldDayOfMonth))
fmt.Printf("Supports FieldOffsetSeconds: %v\n", dt.IsSupportedField(goda.FieldOffsetSeconds))
// Output:
// Supports FieldHourOfDay: true
// Supports FieldDayOfMonth: true
// Supports FieldOffsetSeconds: false
}
// ExampleLocalDate_GetField demonstrates querying date fields with TemporalValue.
func ExampleLocalDate_GetField() {
date := goda.MustLocalDateOf(2024, goda.March, 15) // Friday
// Query various date fields
dayOfWeek := date.GetField(goda.FieldDayOfWeek)
if dayOfWeek.Valid() {
fmt.Printf("Day of week: %d (1=Monday, 7=Sunday)\n", dayOfWeek.Int())
}
month := date.GetField(goda.FieldMonthOfYear)
if month.Valid() {
fmt.Printf("Month: %d\n", month.Int())
}
year := date.GetField(goda.FieldYear)
if year.Valid() {
fmt.Printf("Year: %d\n", year.Int())
}
// Query unsupported field (time field on date)
hour := date.GetField(goda.FieldHourOfDay)
if hour.Unsupported() {
fmt.Println("Hour field is not supported for LocalDate")
}
// Output:
// Day of week: 5 (1=Monday, 7=Sunday)
// Month: 3
// Year: 2024
// Hour field is not supported for LocalDate
}
// ExampleLocalDate_GetField_advancedFields demonstrates advanced date field queries.
func ExampleLocalDate_GetField_advancedFields() {
date := goda.MustLocalDateOf(2024, goda.March, 15)
// Day of year (1-366)
dayOfYear := date.GetField(goda.FieldDayOfYear)
fmt.Printf("Day of year: %d\n", dayOfYear.Int())
// Epoch days (days since 1970-01-01)
epochDay := date.GetField(goda.FieldEpochDay)
fmt.Printf("Days since Unix epoch: %d\n", epochDay.Int64())
// Proleptic month (months since year 0)
prolepticMonth := date.GetField(goda.FieldProlepticMonth)
fmt.Printf("Proleptic month: %d\n", prolepticMonth.Int64())
// FieldEra (0=BCE, 1=CE)
era := date.GetField(goda.FieldEra)
if era.Int() == 1 {
fmt.Println("FieldEra: CE (Common FieldEra)")
}
// Output:
// Day of year: 75
// Days since Unix epoch: 19797
// Proleptic month: 24290
// FieldEra: CE (Common FieldEra)
}
// ExampleLocalTime_GetField demonstrates querying time fields with TemporalValue.
func ExampleLocalTime_GetField() {
t := goda.MustLocalTimeOf(14, 30, 45, 123456789)
// Query various time fields
hour := t.GetField(goda.FieldHourOfDay)
if hour.Valid() {
fmt.Printf("Hour of day (0-23): %d\n", hour.Int())
}
minute := t.GetField(goda.FieldMinuteOfHour)
if minute.Valid() {
fmt.Printf("Minute of hour: %d\n", minute.Int())
}
second := t.GetField(goda.FieldSecondOfMinute)
if second.Valid() {
fmt.Printf("Second of minute: %d\n", second.Int())
}
nanos := t.GetField(goda.FieldNanoOfSecond)
if nanos.Valid() {
fmt.Printf("Nanoseconds: %d\n", nanos.Int())
}
// Query unsupported field (date field on time)
dayOfMonth := t.GetField(goda.FieldDayOfMonth)
if dayOfMonth.Unsupported() {
fmt.Println("FieldDayOfMonth field is not supported for LocalTime")
}
// Output:
// Hour of day (0-23): 14
// Minute of hour: 30
// Second of minute: 45
// Nanoseconds: 123456789
// FieldDayOfMonth field is not supported for LocalTime
}
// ExampleLocalTime_GetField_clockHours demonstrates 12-hour clock field queries.
func ExampleLocalTime_GetField_clockHours() {
// Afternoon time (2:30 PM)
afternoon := goda.MustLocalTimeOf(14, 30, 0, 0)
// 24-hour format
hourOfDay := afternoon.GetField(goda.FieldHourOfDay)
fmt.Printf("24-hour format: %d:30\n", hourOfDay.Int())
// 12-hour format components
hourOfAmPm := afternoon.GetField(goda.FieldHourOfAmPm)
amPm := afternoon.GetField(goda.FieldAmPmOfDay)
amPmStr := "AM"
if amPm.Int() == 1 {
amPmStr = "PM"
}
fmt.Printf("12-hour format: %d:30 %s\n", hourOfAmPm.Int(), amPmStr)
// Clock hour (1-12 instead of 0-11)
clockHour := afternoon.GetField(goda.FieldClockHourOfAmPm)
fmt.Printf("Clock hour (1-12): %d:30 %s\n", clockHour.Int(), amPmStr)
// Midnight special case
midnight := goda.MustLocalTimeOf(0, 0, 0, 0)
midnightClock := midnight.GetField(goda.FieldClockHourOfDay)
fmt.Printf("Midnight clock hour: %d:00\n", midnightClock.Int())
// Output:
// 24-hour format: 14:30
// 12-hour format: 2:30 PM
// Clock hour (1-12): 2:30 PM
// Midnight clock hour: 24:00
}
// ExampleLocalTime_GetField_ofDayFields demonstrates querying cumulative daily values.
func ExampleLocalTime_GetField_ofDayFields() {
t := goda.MustLocalTimeOf(14, 30, 45, 500000000) // 2:30:45.5 PM
// Total seconds elapsed since midnight
secondOfDay := t.GetField(goda.FieldSecondOfDay)
fmt.Printf("Seconds since midnight: %d\n", secondOfDay.Int())
// Total minutes elapsed since midnight
minuteOfDay := t.GetField(goda.FieldMinuteOfDay)
fmt.Printf("Minutes since midnight: %d\n", minuteOfDay.Int())
// Total milliseconds elapsed since midnight
milliOfDay := t.GetField(goda.FieldMilliOfDay)
fmt.Printf("Milliseconds since midnight: %d\n", milliOfDay.Int64())
// Total nanoseconds elapsed since midnight
nanoOfDay := t.GetField(goda.FieldNanoOfDay)
fmt.Printf("Nanoseconds since midnight: %d\n", nanoOfDay.Int64())
// Output:
// Seconds since midnight: 52245
// Minutes since midnight: 870
// Milliseconds since midnight: 52245500
// Nanoseconds since midnight: 52245500000000
}
// ExampleLocalDateTime_GetField demonstrates querying fields from a date-time.
func ExampleLocalDateTime_GetField() {
dt := goda.MustLocalDateTimeOf(2024, goda.March, 15, 14, 30, 45, 123456789)
// Query date fields
year := dt.GetField(goda.FieldYear)
month := dt.GetField(goda.FieldMonthOfYear)
day := dt.GetField(goda.FieldDayOfMonth)
if year.Valid() && month.Valid() && day.Valid() {
fmt.Printf("Date: %04d-%02d-%02d\n", year.Int(), month.Int(), day.Int())
}
// Query time fields
hour := dt.GetField(goda.FieldHourOfDay)
minute := dt.GetField(goda.FieldMinuteOfHour)
second := dt.GetField(goda.FieldSecondOfMinute)
if hour.Valid() && minute.Valid() && second.Valid() {
fmt.Printf("Time: %02d:%02d:%02d\n", hour.Int(), minute.Int(), second.Int())
}
// Query day of week
dayOfWeek := dt.GetField(goda.FieldDayOfWeek)
if dayOfWeek.Valid() {
fmt.Printf("Day of week: %d (Friday)\n", dayOfWeek.Int())
}
// Output:
// Date: 2024-03-15
// Time: 14:30:45
// Day of week: 5 (Friday)
}
// ExampleLocalDateTime_GetField_delegation demonstrates field delegation.
func ExampleLocalDateTime_GetField_delegation() {
dt := goda.MustLocalDateTimeOf(2024, goda.March, 15, 14, 30, 45, 0)
// LocalDateTime delegates date fields to LocalDate
dayOfYear := dt.GetField(goda.FieldDayOfYear)
fmt.Printf("Day of year: %d\n", dayOfYear.Int())
// LocalDateTime delegates time fields to LocalTime
nanoOfDay := dt.GetField(goda.FieldNanoOfDay)
fmt.Printf("Nanoseconds of day: %d\n", nanoOfDay.Int64())
// Unsupported fields return unsupported TemporalValue
offsetSeconds := dt.GetField(goda.FieldOffsetSeconds)
if offsetSeconds.Unsupported() {
fmt.Println("FieldOffsetSeconds is not supported for LocalDateTime")
}
// Output:
// Day of year: 75
// Nanoseconds of day: 52245000000000
// FieldOffsetSeconds is not supported for LocalDateTime
}
// ExampleZoneOffset demonstrates basic ZoneOffset usage.
func ExampleZoneOffset() {
// Create zone offsets
utc := goda.ZoneOffsetUTC()
fmt.Println("UTC:", utc)
// From hours
est := goda.MustZoneOffsetOfHours(-5)
fmt.Println("EST:", est)
// From hours and minutes
ist := goda.MustZoneOffsetOfHoursMinutes(5, 30)
fmt.Println("IST:", ist)
// From total seconds
jst := goda.MustZoneOffsetOfSeconds(9 * 3600)
fmt.Println("JST:", jst)
// Output:
// UTC: Z
// EST: -05:00
// IST: +05:30
// JST: +09:00
}
// ExampleZoneOffsetParse demonstrates parsing zone offsets.
func ExampleZoneOffsetParse() {
// Parse various formats
z1 := goda.MustZoneOffsetParse("Z")
fmt.Println("Z:", z1.TotalSeconds())
z2 := goda.MustZoneOffsetParse("+02:00")
fmt.Println("+02:00:", z2.TotalSeconds())
z3 := goda.MustZoneOffsetParse("-05:30")
fmt.Println("-05:30:", z3.TotalSeconds())
z4 := goda.MustZoneOffsetParse("+0930")
fmt.Println("+0930:", z4.TotalSeconds())
// Output:
// Z: 0
// +02:00: 7200
// -05:30: -19800
// +0930: 34200
}
// ExampleZoneOffset_TotalSeconds demonstrates accessing offset components.
func ExampleZoneOffset_TotalSeconds() {
offset := goda.MustZoneOffsetParse("+05:30")
fmt.Println("Total seconds:", offset.TotalSeconds())
fmt.Println("Hours:", offset.Hours())
fmt.Println("Minutes:", offset.Minutes())
fmt.Println("Seconds:", offset.Seconds())
// Output:
// Total seconds: 19800
// Hours: 5
// Minutes: 30
// Seconds: 0
}
// ExampleZoneOffset_Compare demonstrates comparing zone offsets.
func ExampleZoneOffset_Compare() {
utc := goda.ZoneOffsetUTC()
est := goda.MustZoneOffsetOfHours(-5)
cet := goda.MustZoneOffsetOfHours(1)
fmt.Printf("EST < UTC: %v\n", est.Compare(utc) < 0)
fmt.Printf("CET > UTC: %v\n", cet.Compare(utc) > 0)
fmt.Printf("EST < CET: %v\n", est.Compare(cet) < 0)
// Output:
// EST < UTC: true
// CET > UTC: true
// EST < CET: true
}
// ExampleZoneOffset_MarshalJSON demonstrates JSON serialization.
func ExampleZoneOffset_MarshalJSON() {
offset := goda.MustZoneOffsetParse("+05:30")
jsonBytes, _ := json.Marshal(offset)
fmt.Println(string(jsonBytes))
// Output:
// "+05:30"
}
// ExampleOffsetDateTime demonstrates basic OffsetDateTime usage.
func ExampleOffsetDateTime() {
// Create from components
offset := goda.MustZoneOffsetOfHours(8) // +08:00 (China Standard Time)
odt := goda.MustOffsetDateTimeOf(2024, goda.March, 15, 14, 30, 45, 0, offset)
fmt.Println(odt)
// Create from LocalDateTime
dt := goda.MustLocalDateTimeOf(2024, goda.March, 15, 14, 30, 45, 0)
odt2 := dt.AtOffset(offset)
fmt.Println(odt2)
// Parse from string
odt3 := goda.MustOffsetDateTimeParse("2024-03-15T14:30:45+08:00")
fmt.Println(odt3)
// UTC
utcOdt := goda.MustOffsetDateTimeParse("2024-03-15T14:30:45Z")
fmt.Println(utcOdt)
// Output:
// 2024-03-15T14:30:45+08:00
// 2024-03-15T14:30:45+08:00
// 2024-03-15T14:30:45+08:00
// 2024-03-15T14:30:45Z
}
// ExampleOffsetDateTimeParse demonstrates parsing offset date-times.
func ExampleOffsetDateTimeParse() {
// With positive offset
odt1, _ := goda.OffsetDateTimeParse("2024-03-15T14:30:45+08:00")
fmt.Println(odt1)
// With negative offset
odt2, _ := goda.OffsetDateTimeParse("2024-03-15T14:30:45-05:00")
fmt.Println(odt2)
// UTC (Z)
odt3, _ := goda.OffsetDateTimeParse("2024-03-15T14:30:45Z")
fmt.Println(odt3)
// With minutes offset
odt4, _ := goda.OffsetDateTimeParse("2024-03-15T14:30:45+05:30")
fmt.Println(odt4)
// Output:
// 2024-03-15T14:30:45+08:00
// 2024-03-15T14:30:45-05:00
// 2024-03-15T14:30:45Z
// 2024-03-15T14:30:45+05:30
}
// ExampleOffsetDateTimeNow demonstrates getting the current time with offset.
func ExampleOffsetDateTimeNow() {
// Get current time with system's local offset
now := goda.OffsetDateTimeNow()
fmt.Printf("Has current time: %v\n", !now.IsZero())
// Get current UTC time
utcNow := goda.OffsetDateTimeNowUTC()
fmt.Printf("UTC offset: %d seconds\n", utcNow.Offset().TotalSeconds())