-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathptime.go
1200 lines (1054 loc) · 26.1 KB
/
ptime.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
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
// In the name of Allah
// Persian Calendar
// Please visit https://github.com/yaa110/go-persian-calendar for more information.
//
// Copyright (c) 2016 Navid Fathollahzade
// This source code is licensed under MIT license that can be found in the LICENSE file.
// Package ptime provides functionality for implementation of Persian (Solar Hijri) Calendar.
package ptime
import (
"fmt"
"math"
"strconv"
"strings"
"time"
)
// A Month specifies a month of the year starting from Farvardin = 1.
type Month int
// A Weekday specifies a day of the week starting from Shanbe = 0.
type Weekday int
// A AmPm specifies the 12-Hour marker.
type AmPm int
// A DayTime represents a part of the day based on hour.
type DayTime int
// A Time represents a moment in time in Persian (Jalali) Calendar.
type Time struct {
year int
month Month
day int
hour int
min int
sec int
nsec int
loc *time.Location
wday Weekday
}
// List of months in Persian calendar.
const (
Farvardin Month = 1 + iota
Ordibehesht
Khordad
Tir
Mordad
Shahrivar
Mehr
Aban
Azar
Dey
Bahman
Esfand
)
// List of Dari months in Persian calendar.
const (
Hamal Month = 1 + iota
Sur
Jauza
Saratan
Asad
Sonboleh
Mizan
Aqrab
Qos
Jady
Dolv
Hut
)
// List of days in a week.
const (
Shanbeh Weekday = iota
Yekshanbeh
Doshanbeh
Seshanbeh
Charshanbeh
Panjshanbeh
Jomeh
)
// List of 12-Hour markers.
const (
Am AmPm = 0 + iota
Pm
)
// List of day times.
const (
Midnight DayTime = iota
Dawn
Morning
BeforeNoon
Noon
AfterNoon
Evening
Night
)
var amPm = [2]string{
"قبل از ظهر",
"بعد از ظهر",
}
var sAmPm = [2]string{
"ق.ظ",
"ب.ظ",
}
var months = [12]string{
"فروردین",
"اردیبهشت",
"خرداد",
"تیر",
"مرداد",
"شهریور",
"مهر",
"آبان",
"آذر",
"دی",
"بهمن",
"اسفند",
}
var dmonths = [12]string{
"حمل",
"ثور",
"جوزا",
"سرطان",
"اسد",
"سنبله",
"میزان",
"عقرب",
"قوس",
"جدی",
"دلو",
"حوت",
}
var days = [7]string{
"شنبه",
"یک\u200cشنبه",
"دوشنبه",
"سه\u200cشنبه",
"چهارشنبه",
"پنج\u200cشنبه",
"جمعه",
}
var sdays = [7]string{
"ش",
"ی",
"د",
"س",
"چ",
"پ",
"ج",
}
var daytimes = []string{
"نیمه\u200cشب",
"سحر",
"صبح",
"قبل از ظهر",
"ظهر",
"بعد از ظهر",
"عصر",
"شب",
}
// {days, leap_days, days_before_start}
var pMonthCount = [12][3]int{
{31, 31, 0}, // Farvardin
{31, 31, 31}, // Ordibehesht
{31, 31, 62}, // Khordad
{31, 31, 93}, // Tir
{31, 31, 124}, // Mordad
{31, 31, 155}, // Shahrivar
{30, 30, 186}, // Mehr
{30, 30, 216}, // Aban
{30, 30, 246}, // Azar
{30, 30, 276}, // Dey
{30, 30, 306}, // Bahman
{29, 30, 336}, // Esfand
}
// Iran returns a pointer to time.Location of Asia/Tehran
func Iran() *time.Location {
loc, err := time.LoadLocation("Asia/Tehran")
if err != nil {
loc = time.FixedZone("Asia/Tehran", 12600) // UTC + 03:30
}
return loc
}
// Afghanistan returns a pointer to time.Location of Asia/Kabul
func Afghanistan() *time.Location {
loc, err := time.LoadLocation("Asia/Kabul")
if err != nil {
loc = time.FixedZone("Asia/Kabul", 16200) // UTC + 04:30
}
return loc
}
// String returns t in RFC3339Nano format.
func (t Time) String() string {
return t.Format("yyyy-MM-ddTHH:mm:ss.nsZ")
}
// Dari returns the Dari name of the month.
func (m Month) Dari() string {
switch {
case m < 1:
return dmonths[0]
case m > 11:
return dmonths[11]
default:
return dmonths[m-1]
}
}
// String returns the Persian name of the month.
func (m Month) String() string {
switch {
case m < 1:
return months[0]
case m > 11:
return months[11]
default:
return months[m-1]
}
}
// String returns the Persian name of the day in week.
func (d Weekday) String() string {
switch {
case d < 0:
return days[0]
case d > 6:
return days[6]
default:
return days[d]
}
}
// Short returns the Persian short name of the day in week.
func (d Weekday) Short() string {
switch {
case d < 0:
return sdays[0]
case d > 6:
return sdays[6]
default:
return sdays[d]
}
}
// String returns the Persian name of 12-Hour marker.
func (a AmPm) String() string {
switch {
case a < 0:
return amPm[0]
case a > 1:
return amPm[1]
default:
return amPm[a]
}
}
// Short returns the Persian short name of 12-Hour marker.
func (a AmPm) Short() string {
switch {
case a < 0:
return sAmPm[0]
case a > 1:
return sAmPm[1]
default:
return sAmPm[a]
}
}
// String returns the Persian name of day time.
func (d DayTime) String() string {
switch {
case d < 0:
return daytimes[0]
case d > 7:
return daytimes[7]
default:
return daytimes[d]
}
}
// New converts Gregorian calendar to Persian calendar and
//
// returns a new instance of Time corresponding to the time of t or a zero instance of time if Gregorian year is less than 1097.
//
// t is an instance of time.Time in Gregorian calendar.
func New(t time.Time) Time {
if t.Year() < 1097 {
return Time{}
}
pt := new(Time)
pt.SetTime(t)
return *pt
}
// Time converts the Shamsi (Solar Hijri) testDate stored in the Time struct to the corresponding
// Gregorian testDate and returns it as a Go time.Time object.
func (t Time) Time() time.Time {
var year, month, day int
// Convert the Shamsi testDate to the corresponding Julian Day Number (JDN)
jdn := convertShamsiToJDN(t.year, int(t.month), t.day)
// Convert the JDN to a Gregorian testDate
if jdn > gregorianReformJulianDay {
year, month, day = convertJDNToGregorianPostReform(jdn)
} else {
year, month, day = convertJDNToGregorianPreReform(jdn)
}
// Use the location stored in the Time struct, or default to the local time zone
loc := t.loc
if loc == nil {
loc = time.Local
}
// Return the corresponding time.Time object
return time.Date(year, time.Month(month), day, t.hour, t.min, t.sec, t.nsec, loc)
}
// Date returns a new instance of Time.
//
// year, month and day represent a day in Persian calendar.
//
// hour, min minute, sec seconds, nsec nanoseconds offsets represent a moment in time.
//
// loc is a pointer to time.Location, if loc is nil then the local time is used.
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
if loc == nil {
loc = time.Local
}
t := new(Time)
t.Set(year, month, day, hour, min, sec, nsec, loc)
return *t
}
// Unix returns a new instance of PersianDate from unix timestamp.
//
// sec seconds and nsec nanoseconds since January 1, 1970 UTC.
func Unix(sec, nsec int64) Time {
return New(time.Unix(sec, nsec))
}
// Now returns a new instance of Time corresponding to the current time.
func Now() Time {
return New(time.Now())
}
// SetTime sets the time and testDate for the `Time` struct based on the input `time.Time` object.
// This function converts a Gregorian testDate (as provided by `ti`) to the Shamsi (Persian) calendar.
// It first calculates the Julian Day Number (JDN), a continuous count of days since the beginning
// of the Julian Period, and then converts this JDN to a Shamsi testDate.
func (t *Time) SetTime(ti time.Time) {
var year, month, day int
t.nsec = ti.Nanosecond()
t.sec = ti.Second()
t.min = ti.Minute()
t.hour = ti.Hour()
t.loc = ti.Location()
t.wday = getWeekday(ti.Weekday())
var jdn int
gy, gmm, gd := ti.Date()
gm := int(gmm)
if isAfterGregorianReform(gy, gm, gd) {
jdn = convertGregorianPostReformToJDN(gy, gm, gd)
} else {
jdn = convertGregorianPreReformToJDN(gy, gm, gd)
}
year, month, day = convertJDNToShamsi(jdn)
t.year = year
t.month = Month(month)
t.day = day
}
// SetUnix sets t to represent the corresponding unix timestamp of
//
// sec seconds and nsec nanoseconds since January 1, 1970 UTC.
func (t *Time) SetUnix(sec, nsec int64) {
t.SetTime(time.Unix(sec, nsec))
}
// norm returns nhi, nlo such that
//
// hi * base + lo == nhi * base + nlo
// 0 <= nlo < base
func norm(hi, lo, base int) (int, int) {
if lo < 0 {
n := (-lo-1)/base + 1
hi -= n
lo += n * base
}
if lo >= base {
n := lo / base
hi += n
lo -= n * base
}
return hi, lo
}
// norm returns nhi, nlo such that
//
// hi * base + lo == nhi * base + nlo
// 0 <= nlo < base
func normDay(hi, lo, base int) (int, int) {
if lo < 1 {
n := (-lo-1)/base + 1
hi -= n
lo += n * base
}
if lo > base {
n := lo / base
hi += n
lo -= n * base
}
return hi, lo
}
// Set sets t.
//
// year, month and day represent a day in Persian calendar.
//
// hour, min minute, sec seconds, nsec nanoseconds offsets represent a moment in time.
//
// loc is a pointer to time.Location and must not be nil.
func (t *Time) Set(year int, month Month, day, hour, min, sec, nsec int, loc *time.Location) {
if loc == nil {
panic("ptime: the Location must not be nil in call to Set")
}
// Normalize nsec, sec, min, hour, overflowing into day.
sec, nsec = norm(sec, nsec, 1e9)
min, sec = norm(min, sec, 60)
hour, min = norm(hour, min, 60)
day, hour = norm(day, hour, 24)
// Normalize month, overflowing into year.
m := int(month) - 1
year, m = norm(year, m, 12)
if m < 0 {
m = 0
} else if m > 11 {
m = 11
}
if isLeap(year) {
m, day = normDay(m, day, pMonthCount[m][1])
} else {
m, day = normDay(m, day, pMonthCount[m][0])
}
year, m = norm(year, m, 12)
month = Month(m) + 1
t.year = year
t.month = month
t.day = day
t.hour = hour
t.min = min
t.sec = sec
t.nsec = nsec
t.loc = loc
t.resetWeekday()
t.norm()
}
// SetYear sets the year of t.
func (t *Time) SetYear(year int) {
t.year = year
t.normDay()
t.resetWeekday()
}
// SetMonth sets the month of t.
func (t *Time) SetMonth(month Month) {
t.month = month
t.normMonth()
t.normDay()
t.resetWeekday()
}
// SetDay sets the day of t.
func (t *Time) SetDay(day int) {
t.day = day
t.normDay()
t.resetWeekday()
}
// SetHour sets the hour of t.
func (t *Time) SetHour(hour int) {
t.hour = hour
t.normHour()
}
// SetMinute sets the minute offset of t.
func (t *Time) SetMinute(min int) {
t.min = min
t.normMinute()
}
// SetSecond sets the second offset of t.
func (t *Time) SetSecond(sec int) {
t.sec = sec
t.normSecond()
}
// SetNanosecond sets the nanosecond offset of t.
func (t *Time) SetNanosecond(nsec int) {
t.nsec = nsec
t.normNanosecond()
}
// In sets the location of t.
//
// loc is a pointer to time.Location and must not be nil.
func (t Time) In(loc *time.Location) Time {
if loc == nil {
panic("ptime: the Location must not be nil in call to In")
}
t.loc = loc
t.resetWeekday()
return t
}
// At sets the hour, min minute, sec second and nsec nanoseconds offsets of t.
func (t *Time) At(hour, min, sec, nsec int) {
t.SetHour(hour)
t.SetMinute(min)
t.SetSecond(sec)
t.SetNanosecond(nsec)
}
// IsZero returns true if t is zero time instance
func (t Time) IsZero() bool {
return t == Time{}
}
// Unix returns the number of seconds since January 1, 1970 UTC.
func (t Time) Unix() int64 {
return t.Time().Unix()
}
// UnixNano seturns the number of nanoseconds since January 1, 1970 UTC.
func (t Time) UnixNano() int64 {
return t.Time().UnixNano()
}
// Date returns the year, month, day of t.
func (t Time) Date() (int, Month, int) {
return t.year, t.month, t.day
}
// Clock returns the hour, minute, seconds offsets of t.
func (t Time) Clock() (int, int, int) {
return t.hour, t.min, t.sec
}
// Year returns the year of t.
func (t Time) Year() int {
return t.year
}
// Month returns the month of t in the range [1, 12].
func (t Time) Month() Month {
return t.month
}
// Day returns the day of month of t.
func (t Time) Day() int {
return t.day
}
// Hour returns the hour of t in the range [0, 23].
func (t Time) Hour() int {
return t.hour
}
// Hour12 returns the hour of t in the range [0, 11].
func (t Time) Hour12() int {
h := t.hour
if h >= 12 {
h -= 12
}
return h
}
// Minute returns the minute offset of t in the range [0, 59].
func (t Time) Minute() int {
return t.min
}
// Second returns the seconds offset of t in the range [0, 59].
func (t Time) Second() int {
return t.sec
}
// Nanosecond returns the nanoseconds offset of t in the range [0, 999999999].
func (t Time) Nanosecond() int {
return t.nsec
}
// DayTime returns the dayTime of that part of the day.
// [0,3) -> midnight
// [3,6) -> dawn
// [6,9) -> morning
// [9,12) -> before noon
// [12,15) -> noon
// [15,18) -> afternoon
// [18,21) -> evening
// [21,24) -> night
func (t Time) DayTime() DayTime {
return DayTime(t.hour / 3)
}
// Location returns a pointer to time.Location of t.
func (t Time) Location() *time.Location {
return t.loc
}
// YearDay returns the day of year of t.
func (t Time) YearDay() int {
m := t.month - 1
if m < 0 {
m = 0
} else if m > 11 {
m = 11
}
return pMonthCount[m][2] + t.day
}
// RYearDay returns the number of remaining days of the year of t.
func (t Time) RYearDay() int {
y := 365
if t.IsLeap() {
y++
}
return y - t.YearDay()
}
// Weekday returns the weekday of t.
func (t Time) Weekday() Weekday {
return t.wday
}
// RMonthDay returns the number of remaining days of the month of t.
func (t Time) RMonthDay() int {
i := 0
if t.IsLeap() {
i = 1
}
m := t.month - 1
if m < 0 {
m = 0
} else if m > 11 {
m = 11
}
return pMonthCount[m][i] - t.day
}
// BeginningOfWeek returns a new instance of Time representing the first day of the week of t.
// The time is reset to 00:00:00
func (t Time) BeginningOfWeek() Time {
nt := t.AddDate(0, 0, int(Shanbeh-t.wday))
nt.SetHour(0)
nt.SetMinute(0)
nt.SetSecond(0)
nt.SetNanosecond(0)
return nt
}
// FirstWeekDay returns a new instance of Time representing the first day of the week of t.
func (t Time) FirstWeekDay() Time {
if t.wday == Shanbeh {
return t
}
return t.AddDate(0, 0, int(Shanbeh-t.wday))
}
// LastWeekday returns a new instance of Time representing the last day of the week of t.
func (t Time) LastWeekday() Time {
if t.wday == Jomeh {
return t
}
return t.AddDate(0, 0, int(Jomeh-t.wday))
}
// BeginningOfMonth returns a new instance of Time representing the first day of the month of t.
// The time is reset to 00:00:00
func (t Time) BeginningOfMonth() Time {
return Date(t.year, t.month, 1, 0, 0, 0, 0, t.loc)
}
// FirstMonthDay returns a new instance of Time representing the first day of the month of t.
func (t Time) FirstMonthDay() Time {
if t.day == 1 {
return t
}
return Date(t.year, t.month, 1, t.hour, t.min, t.sec, t.nsec, t.loc)
}
// LastMonthDay returns a new instance of Time representing the last day of the month of t.
func (t Time) LastMonthDay() Time {
i := 0
if t.IsLeap() {
i = 1
}
m := t.month - 1
if m < 0 {
m = 0
} else if m > 11 {
m = 11
}
ld := pMonthCount[m][i]
if ld == t.day {
return t
}
return Date(t.year, t.month, ld, t.hour, t.min, t.sec, t.nsec, t.loc)
}
// BeginningOfYear returns a new instance of Time representing the first day of the year of t.
// The time is reset to 00:00:00
func (t Time) BeginningOfYear() Time {
return Date(t.year, Farvardin, 1, 0, 0, 0, 0, t.loc)
}
// FirstYearDay returns a new instance of Time representing the first day of the year of t.
func (t Time) FirstYearDay() Time {
if t.month == Farvardin && t.day == 1 {
return t
}
return Date(t.year, Farvardin, 1, t.hour, t.min, t.sec, t.nsec, t.loc)
}
// LastYearDay returns a new instance of Time representing the last day of the year of t.
func (t Time) LastYearDay() Time {
i := 0
if t.IsLeap() {
i = 1
}
ld := pMonthCount[Esfand-1][i]
if t.month == Esfand && t.day == ld {
return t
}
return Date(t.year, Esfand, ld, t.hour, t.min, t.sec, t.nsec, t.loc)
}
// MonthWeek returns the week of month of t.
func (t Time) MonthWeek() int {
return int(math.Ceil(float64(t.day+int(t.FirstMonthDay().Weekday())) / 7.0))
}
// YearWeek returns the week of year of t.
func (t Time) YearWeek() int {
return int(math.Ceil(float64(t.YearDay()+int(t.FirstYearDay().Weekday())) / 7.0))
}
// RYearWeek returns the number of remaining weeks of the year of t.
func (t Time) RYearWeek() int {
return 52 - t.YearWeek()
}
// Yesterday returns a new instance of Time representing a day before the day of t.
func (t Time) Yesterday() Time {
return t.AddDate(0, 0, -1)
}
// Tomorrow returns a new instance of Time representing a day after the day of t.
func (t Time) Tomorrow() Time {
return t.AddDate(0, 0, 1)
}
// Add returns a new instance of Time for t+d.
func (t Time) Add(d time.Duration) Time {
return New(t.Time().Add(d))
}
// AddDate returns a new instance of Time for t.year+years, t.month+months and t.day+days.
func (t Time) AddDate(years, months, days int) Time {
t.Set(t.year+years, Month(int(t.month)+months), t.day+days, t.hour, t.min, t.sec, t.nsec, t.loc)
return t
}
// Since returns the number of seconds between t and t2.
func (t Time) Since(t2 Time) int64 {
return int64(math.Abs(float64(t2.Unix() - t.Unix())))
}
// IsLeap returns true if the year of t is a leap year.
func (t Time) IsLeap() bool {
return isLeap(t.year)
}
func isLeap(year int) bool {
return divider(25*year+11, 33) < 8
}
// AmPm returns the 12-Hour marker of t.
func (t Time) AmPm() AmPm {
m := Am
if t.hour > 12 || (t.hour == 12 && (t.min > 0 || t.sec > 0)) {
m = Pm
}
return m
}
// Zone returns the zone name and its offset in seconds east of UTC of t.
func (t Time) Zone() (string, int) {
return t.Time().Zone()
}
// ZoneOffset returns the zone offset of t in the format of [+|-]HH:mm.
// If `f` is set, then return format is based on `f`.
func (t Time) ZoneOffset(f ...string) string {
format := "-07:00"
if len(f) > 0 {
format = f[0]
if format != "-0700" && format != "-07" && format != "-07:00" && format != "Z0700" && format != "Z07:00" {
format = "-07:00"
}
}
_, offset := t.Zone()
if offset == 0 {
switch format {
case "-0700":
return "+0000"
case "-07":
return "+00"
case "-07:00":
return "+00:00"
case "Z0700", "Z07:00":
return "Z"
}
}
h := offset / 3600
m := (offset - h*3600) / 60
switch format {
case "-0700", "Z0700":
return fmt.Sprintf("%+03d%02d", h, m)
case "-07":
return fmt.Sprintf("%+03d", h)
default:
return fmt.Sprintf("%+03d:%02d", h, m)
}
}
// Format returns the formatted representation of t.
//
// yyyy, yyy, y year (e.g. 1394)
// yy 2-digits representation of year (e.g. 94)
// MMM the Persian name of month (e.g. فروردین)
// MMI the Dari name of month (e.g. حمل)
// MM 2-digits representation of month (e.g. 01)
// M month (e.g. 1)
// rw remaining weeks of year
// w week of year
// RW remaining weeks of month
// W week of month
// RD remaining days of year
// D day of year
// rd remaining days of month
// dd 2-digits representation of day (e.g. 01)
// d day (e.g. 1)
// E the Persian name of weekday (e.g. شنبه)
// e the Persian short name of weekday (e.g. ش)
// A the Persian name of 12-Hour marker (e.g. قبل از ظهر)
// a the Persian short name of 12-Hour marker (e.g. ق.ظ)
// HH 2-digits representation of hour [00-23]
// H hour [0-23]
// kk 2-digits representation of hour [01-24]
// k hour [1-24]
// hh 2-digits representation of hour [01-12]
// h hour [1-12]
// KK 2-digits representation of hour [00-11]
// K hour [0-11]
// mm 2-digits representation of minute [00-59]
// m minute [0-59]
// ss 2-digits representation of seconds [00-59]
// s seconds [0-59]
// n hour name (e.g. صبح)
// ns nanoseconds
// S 3-digits representation of milliseconds (e.g. 001)
// z the name of location
// Z zone offset (e.g. +03:30)
func (t Time) Format(format string) string {
year := strconv.Itoa(t.year)
if len(year) < 4 {
year = fmt.Sprintf("%04d", t.year)
}
r := strings.NewReplacer(
"yyyy", year,
"yyy", year,
"MMM", t.month.String(),
"MMI", t.month.Dari(),
"yy", year[2:],
"MM", fmt.Sprintf("%02d", t.month),
"rw", strconv.Itoa(t.RYearWeek()),
"RD", strconv.Itoa(t.RYearDay()),
"rd", strconv.Itoa(t.RMonthDay()),
"dd", fmt.Sprintf("%02d", t.day),
"HH", fmt.Sprintf("%02d", t.hour),
"KK", fmt.Sprintf("%02d", t.Hour12()),
"kk", fmt.Sprintf("%02d", modifyHour(t.hour, 24)),
"hh", fmt.Sprintf("%02d", modifyHour(t.Hour12(), 12)),
"mm", fmt.Sprintf("%02d", t.min),
"ns", strconv.Itoa(t.nsec),
"ss", fmt.Sprintf("%02d", t.sec),
"y", year,
"M", strconv.Itoa(int(t.month)),
"w", strconv.Itoa(t.YearWeek()),
"W", strconv.Itoa(t.MonthWeek()),
"D", strconv.Itoa(t.YearDay()),
"d", strconv.Itoa(t.day),
"E", t.wday.String(),
"e", t.wday.Short(),
"A", t.AmPm().String(),
"a", t.AmPm().Short(),
"H", strconv.Itoa(t.hour),
"K", strconv.Itoa(t.Hour12()),
"k", strconv.Itoa(modifyHour(t.hour, 24)),
"h", strconv.Itoa(modifyHour(t.Hour12(), 12)),
"m", strconv.Itoa(t.min),
"n", t.DayTime().String(),
"s", strconv.Itoa(t.sec),
"S", fmt.Sprintf("%03d", t.nsec/1e6),
"z", t.loc.String(),
"Z", t.ZoneOffset(),
)
return r.Replace(format)
}
// TimeFormat formats in standard time format.
//
// 2006 four digit year (e.g. 1399)
// 06 two digit year (e.g. 99)
// 01 two digit month (e.g. 01)
// 1 one digit month (e.g. 1)
// Jan month name (e.g. آذر)
// January month name (e.g. آذر)
// 02 two digit day (e.g. 07)
// 2 one digit day (e.g. 7)
// _2 right justified two character day (e.g. 7)
// Mon weekday (e.g. شنبه)
// Monday weekday (e.g. شنبه)
// Morning hour name (e.g. صبح)
// 03 two digit 12 hour format (e.g. 03)
// 3 one digit 12 hour format (e.g. 3)
// 15 two digit 24 hour format (e.g. 15)
// 04 two digit minute (e.g. 03)
// 4 one digit minute (e.g. 03)
// 05 two digit minute (e.g. 09)
// 5 one digit minute (e.g. 9)
// .000 millisecond (e.g. .120)
// .000000 microsecond (e.g. .123400)
// .000000000 nanosecond (e.g. .123456000)
// .999 trailing zeros removed millisecond (e.g. .12)
// .999999 trailing zeros removed microsecond (e.g. .1234)
// .999999999 trailing zeros removed nanosecond (e.g. .123456)
// PM full 12-Hour marker (e.g. قبل از ظهر)
// pm short 12-Hour marker (e.g. ق.ظ)
// MST the name of location