diff --git a/README.md b/README.md index aa476bd..814d36d 100644 --- a/README.md +++ b/README.md @@ -50,28 +50,28 @@ Import all necessary packages.("fmt" - Print, "time" - Getting time from machine | | Token | Output | |----------------|-------|----------------------------------------| -| Month | M | 1 2 ... 11 12 | -| | MM | 01 01 ... 11 12 | -| | MMM | Jan Feb ... Nov Dec | -| | MMMM | January February ... November December | -| Day of Month | D | 1 2 ... 30 31 | -| | DD | 01 02 ... 30 31 | -| | DDDD | Mon, Tue ... Sat Sun | -| | DDDDD | Monday, Tuesday ... Saturday Sunday | -| Day of Year | DDD | 1 2 ... 364 365 | -| Year | YY | 70 71 ... 29 30 | -| | YYYY | 1970 1971 ... 2029 2030 | -| Quarter | Q | 1 2 3 4 | -| AM/PM | A | AM PM | -| | a | am pm | -| Hour | H | 0 1 ... 22 23 | -| | HH | 00 01 ... 22 23 | -| | h | 1 2 ... 11 12 | -| | hh | 01 02 ... 11 12 | -| Minute | m | 0 1 ... 58 59 | -| | mm | 00 01 ... 58 59 | -| Second | s | 0 1 ... 58 59 | -| | ss | 00 01 ... 58 59 | -| Time Zone | Z | -07:00 -06:00 ... +06:00 +07:00 | -| | ZZ | -0700 -0600 ... +0600 +0700 | -| Unix Timestamp | X | 1360013296 | +| Month | TIME_M | 1 2 ... 11 12 | +| | TIME_MM | 01 01 ... 11 12 | +| | TIME_MMM | Jan Feb ... Nov Dec | +| | TIME_MMMM | January February ... November December | +| Day of Month | TIME_D | 1 2 ... 30 31 | +| | TIME_DD | 01 02 ... 30 31 | +| | TIME_DDDD | Mon, Tue ... Sat Sun | +| | TIME_DDDDD | Monday, Tuesday ... Saturday Sunday | +| Day of Year | TIME_DDD | 1 2 ... 364 365 | +| Year | TIME_YY | 70 71 ... 29 30 | +| | TIME_YYYY | 1970 1971 ... 2029 2030 | +| Quarter | TIME_Q | 1 2 3 4 | +| AM/PM | TIME_A | AM PM | +| | TIME_a | am pm | +| Hour | TIME_H | 0 1 ... 22 23 | +| | TIME_HH | 00 01 ... 22 23 | +| | TIME_h | 1 2 ... 11 12 | +| | TIME_hh | 01 02 ... 11 12 | +| Minute | TIME_m | 0 1 ... 58 59 | +| | TIME_mm | 00 01 ... 58 59 | +| Second | TIME_s | 0 1 ... 58 59 | +| | TIME_ss | 00 01 ... 58 59 | +| Time Zone | TIME_Z | -07:00 -06:00 ... +06:00 +07:00 | +| | TIME_ZZ | -0700 -0600 ... +0600 +0700 | +| Unix Timestamp | TIME_X | 1360013296 | diff --git a/time.go b/time.go index 2bb5172..fc2c528 100644 --- a/time.go +++ b/time.go @@ -31,97 +31,97 @@ var languageMonthsMap = map[LocaleType][]string{ } var formatFuncsMap = map[FormatType]func(to ToOpts) string{ - D: func(to ToOpts) string { + TIME_D: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Day()) }, - DD: func(to ToOpts) string { + TIME_DD: func(to ToOpts) string { return fmt.Sprintf("%02d", to.time.Day()) }, - DDD: func(to ToOpts) string { + TIME_DDD: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.YearDay()) }, - DDDD: func(to ToOpts) string { + TIME_DDDD: func(to ToOpts) string { return languageDaysMap[to.locale][to.time.Weekday()][:3] }, - DDDDD: func(to ToOpts) string { + TIME_DDDDD: func(to ToOpts) string { return languageDaysMap[to.locale][to.time.Weekday()] }, - M: func(to ToOpts) string { + TIME_M: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Month()) }, - MM: func(to ToOpts) string { + TIME_MM: func(to ToOpts) string { return fmt.Sprintf("%02d", to.time.Month()) }, - MMM: func(to ToOpts) string { + TIME_MMM: func(to ToOpts) string { return languageMonthsMap[to.locale][to.time.Month()-1][:3] }, - MMMM: func(to ToOpts) string { + TIME_MMMM: func(to ToOpts) string { return languageMonthsMap[to.locale][to.time.Month()-1] }, - YY: func(to ToOpts) string { + TIME_YY: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Year())[2:] }, - YYYY: func(to ToOpts) string { + TIME_YYYY: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Year()) }, - Q: func(to ToOpts) string { + TIME_Q: func(to ToOpts) string { return fmt.Sprintf("%d", (to.time.Month()/4)+1) }, - A: func(to ToOpts) string { + TIME_A: func(to ToOpts) string { if to.time.Hour() >= 12 { return "PM" } else { return "AM" } }, - a: func(to ToOpts) string { + TIME_a: func(to ToOpts) string { if to.time.Hour() >= 12 { return "pm" } else { return "am" } }, - H: func(to ToOpts) string { + TIME_H: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Hour()) }, - HH: func(to ToOpts) string { + TIME_HH: func(to ToOpts) string { return fmt.Sprintf("%02d", to.time.Hour()) }, - h: func(to ToOpts) string { + TIME_h: func(to ToOpts) string { if to.time.Hour() > 12 { return fmt.Sprintf("%d", to.time.Hour()-12) } else { return fmt.Sprintf("%d", to.time.Hour()) } }, - hh: func(to ToOpts) string { + TIME_hh: func(to ToOpts) string { if to.time.Hour() > 12 { return fmt.Sprintf("%02d", to.time.Hour()-12) } else { return fmt.Sprintf("%02d", to.time.Hour()) } }, - m: func(to ToOpts) string { + TIME_m: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Minute()) }, - mm: func(to ToOpts) string { + TIME_mm: func(to ToOpts) string { return fmt.Sprintf("%02d", to.time.Minute()) }, - s: func(to ToOpts) string { + TIME_s: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Second()) }, - ss: func(to ToOpts) string { + TIME_ss: func(to ToOpts) string { return fmt.Sprintf("%02d", to.time.Second()) }, - Z: func(to ToOpts) string { + TIME_Z: func(to ToOpts) string { name, _ := to.time.Zone() return fmt.Sprintf("%s:00", name) }, - ZZ: func(to ToOpts) string { + TIME_ZZ: func(to ToOpts) string { name, _ := to.time.Zone() return fmt.Sprintf("%s00", name) }, - X: func(to ToOpts) string { + TIME_X: func(to ToOpts) string { return fmt.Sprintf("%d", to.time.Unix()) }, } diff --git a/time_test.go b/time_test.go index e973944..8dda091 100644 --- a/time_test.go +++ b/time_test.go @@ -60,8 +60,8 @@ func TestFormatter_To(t *testing.T) { timeNow := time.Now() currentLocale := formatter.CurrentLocaleType() - assert.Equal(t, languageDaysMap[currentLocale][timeNow.Weekday()], formatter.To(timeNow, fmt.Sprintf("%s", DDDDD))) - assert.Equal(t, languageMonthsMap[currentLocale][timeNow.Month()-1], formatter.To(timeNow, fmt.Sprintf("%s", MMMM))) + assert.Equal(t, languageDaysMap[currentLocale][timeNow.Weekday()], formatter.To(timeNow, fmt.Sprintf("%s", TIME_DDDDD))) + assert.Equal(t, languageMonthsMap[currentLocale][timeNow.Month()-1], formatter.To(timeNow, fmt.Sprintf("%s", TIME_MMMM))) }) t.Run("CreateFormatterWithNewLocale", func(t *testing.T) { @@ -78,8 +78,8 @@ func TestFormatter_To(t *testing.T) { assert.Nil(t, err) assert.NotNil(t, formatter) - assert.Equal(t, languageDaysMap[currentLocale][timeNow.Weekday()], formatter.To(timeNow, fmt.Sprintf("%s", DDDDD))) - assert.Equal(t, languageMonthsMap[currentLocale][timeNow.Month()-1], formatter.To(timeNow, fmt.Sprintf("%s", MMMM))) + assert.Equal(t, languageDaysMap[currentLocale][timeNow.Weekday()], formatter.To(timeNow, fmt.Sprintf("%s", TIME_DDDDD))) + assert.Equal(t, languageMonthsMap[currentLocale][timeNow.Month()-1], formatter.To(timeNow, fmt.Sprintf("%s", TIME_MMMM))) }) t.Run("DateFormatWithDefaultLocale", func(t *testing.T) { @@ -105,7 +105,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", D), + layout: fmt.Sprintf("%s", TIME_D), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Day()) @@ -117,7 +117,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", DD), + layout: fmt.Sprintf("%s", TIME_DD), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%02d", timeNow.Day()) @@ -129,7 +129,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", DDD), + layout: fmt.Sprintf("%s", TIME_DDD), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.YearDay()) @@ -141,7 +141,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", DDDD), + layout: fmt.Sprintf("%s", TIME_DDDD), }, want: func(timeNow time.Time) string { return languageDaysMap[currentLocale][timeNow.Weekday()][:3] @@ -153,7 +153,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", DDDDD), + layout: fmt.Sprintf("%s", TIME_DDDDD), }, want: func(timeNow time.Time) string { return languageDaysMap[currentLocale][timeNow.Weekday()] @@ -165,7 +165,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", M), + layout: fmt.Sprintf("%s", TIME_M), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Month()) @@ -177,7 +177,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", MM), + layout: fmt.Sprintf("%s", TIME_MM), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%02d", timeNow.Month()) @@ -189,7 +189,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", MMM), + layout: fmt.Sprintf("%s", TIME_MMM), }, want: func(timeNow time.Time) string { return languageMonthsMap[currentLocale][timeNow.Month()-1][:3] @@ -201,7 +201,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", MMMM), + layout: fmt.Sprintf("%s", TIME_MMMM), }, want: func(timeNow time.Time) string { return languageMonthsMap[currentLocale][timeNow.Month()-1] @@ -213,7 +213,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", YY), + layout: fmt.Sprintf("%s", TIME_YY), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Year())[2:] @@ -225,7 +225,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", YYYY), + layout: fmt.Sprintf("%s", TIME_YYYY), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Year()) @@ -237,7 +237,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", Q), + layout: fmt.Sprintf("%s", TIME_Q), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", (timeNow.Month()/4)+1) @@ -249,7 +249,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", A), + layout: fmt.Sprintf("%s", TIME_A), }, want: func(timeNow time.Time) string { if timeNow.Hour() >= 12 { @@ -265,7 +265,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now().Add(12 * time.Hour), - layout: fmt.Sprintf("%s", A), + layout: fmt.Sprintf("%s", TIME_A), }, want: func(timeNow time.Time) string { if timeNow.Hour() >= 12 { @@ -281,7 +281,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", a), + layout: fmt.Sprintf("%s", TIME_a), }, want: func(timeNow time.Time) string { if timeNow.Hour() >= 12 { @@ -297,7 +297,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now().Add(12 * time.Hour), - layout: fmt.Sprintf("%s", a), + layout: fmt.Sprintf("%s", TIME_a), }, want: func(timeNow time.Time) string { if timeNow.Hour() >= 12 { @@ -313,7 +313,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", H), + layout: fmt.Sprintf("%s", TIME_H), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Hour()) @@ -325,7 +325,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", HH), + layout: fmt.Sprintf("%s", TIME_HH), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%02d", timeNow.Hour()) @@ -337,7 +337,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", h), + layout: fmt.Sprintf("%s", TIME_h), }, want: func(timeNow time.Time) string { if timeNow.Hour() > 12 { @@ -353,7 +353,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now().Add(12 * time.Hour), - layout: fmt.Sprintf("%s", h), + layout: fmt.Sprintf("%s", TIME_h), }, want: func(timeNow time.Time) string { if timeNow.Hour() > 12 { @@ -369,7 +369,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", hh), + layout: fmt.Sprintf("%s", TIME_hh), }, want: func(timeNow time.Time) string { if timeNow.Hour() > 12 { @@ -385,7 +385,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now().Add(12 * time.Hour), - layout: fmt.Sprintf("%s", hh), + layout: fmt.Sprintf("%s", TIME_hh), }, want: func(timeNow time.Time) string { if timeNow.Hour() > 12 { @@ -401,7 +401,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", m), + layout: fmt.Sprintf("%s", TIME_m), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Minute()) @@ -413,7 +413,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", mm), + layout: fmt.Sprintf("%s", TIME_mm), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%02d", timeNow.Minute()) @@ -425,7 +425,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", s), + layout: fmt.Sprintf("%s", TIME_s), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Second()) @@ -437,7 +437,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", ss), + layout: fmt.Sprintf("%s", TIME_ss), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%02d", timeNow.Second()) @@ -449,7 +449,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", Z), + layout: fmt.Sprintf("%s", TIME_Z), }, want: func(timeNow time.Time) string { name, _ := timeNow.Zone() @@ -462,7 +462,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", ZZ), + layout: fmt.Sprintf("%s", TIME_ZZ), }, want: func(timeNow time.Time) string { name, _ := timeNow.Zone() @@ -475,7 +475,7 @@ func TestFormatter_To(t *testing.T) { fields: fields{}, args: args{ t: time.Now(), - layout: fmt.Sprintf("%s", X), + layout: fmt.Sprintf("%s", TIME_X), }, want: func(timeNow time.Time) string { return fmt.Sprintf("%d", timeNow.Unix()) @@ -499,7 +499,7 @@ func TestFormatter_To(t *testing.T) { timeNow := time.Now() - value := formatter.To(timeNow, fmt.Sprintf("%s:%s:%s, %s", D, MMMM, YYYY, D)) + value := formatter.To(timeNow, fmt.Sprintf("%s:%s:%s, %s", TIME_D, TIME_MMMM, TIME_YYYY, TIME_D)) assert.Equal(t, fmt.Sprintf("%s:%s:%s, %s", fmt.Sprintf("%d", timeNow.Day()), diff --git a/types.go b/types.go index 05d417c..733162e 100644 --- a/types.go +++ b/types.go @@ -10,40 +10,40 @@ const ( type FormatType string const ( - M FormatType = "$M$" // 1 2 ... 11 12 - MM FormatType = "$MM$" // 01 01 ... 11 12 - MMM FormatType = "$MMM$" // Jan Feb ... Nov Dec - MMMM FormatType = "$MMMM$" // January February ... November December + TIME_M FormatType = "$M$" // 1 2 ... 11 12 + TIME_MM FormatType = "$MM$" // 01 01 ... 11 12 + TIME_MMM FormatType = "$MMM$" // Jan Feb ... Nov Dec + TIME_MMMM FormatType = "$MMMM$" // January February ... November December - D FormatType = "$D$" // 1 2 ... 30 31 - DD FormatType = "$DD$" // 01 02 ... 30 31 - DDD FormatType = "$DDD$" // 1 2 ... 364 365 - DDDD FormatType = "$DDDD$" // Mon, Tue ... Sat Sun - DDDDD FormatType = "$DDDDD$" // Monday, Tuesday ... Saturday Sunday + TIME_D FormatType = "$D$" // 1 2 ... 30 31 + TIME_DD FormatType = "$DD$" // 01 02 ... 30 31 + TIME_DDD FormatType = "$DDD$" // 1 2 ... 364 365 + TIME_DDDD FormatType = "$DDDD$" // Mon, Tue ... Sat Sun + TIME_DDDDD FormatType = "$DDDDD$" // Monday, Tuesday ... Saturday Sunday - YY FormatType = "$YY$" // 70 71 ... 29 30 - YYYY FormatType = "$YYYY$" // 1970 1971 ... 2029 2030 + TIME_YY FormatType = "$YY$" // 70 71 ... 29 30 + TIME_YYYY FormatType = "$YYYY$" // 1970 1971 ... 2029 2030 - Q FormatType = "$Q$" // 1 2 3 4 + TIME_Q FormatType = "$Q$" // 1 2 3 4 - A FormatType = "$A$" // AM PM - a FormatType = "$a$" // am pm + TIME_A FormatType = "$A$" // AM PM + TIME_a FormatType = "$a$" // am pm - H FormatType = "$H$" // 0 1 ... 22 23 - HH FormatType = "$HH$" // 00 01 ... 22 23 - h FormatType = "$h$" // 1 2 ... 11 12 - hh FormatType = "$hh$" // 01 02 ... 11 12 + TIME_H FormatType = "$H$" // 0 1 ... 22 23 + TIME_HH FormatType = "$HH$" // 00 01 ... 22 23 + TIME_h FormatType = "$h$" // 1 2 ... 11 12 + TIME_hh FormatType = "$hh$" // 01 02 ... 11 12 - m FormatType = "$m$" // 0 1 ... 58 59 - mm FormatType = "$mm$" // 00 01 ... 58 59 + TIME_m FormatType = "$m$" // 0 1 ... 58 59 + TIME_mm FormatType = "$mm$" // 00 01 ... 58 59 - s FormatType = "$s$" // 0 1 ... 58 59 - ss FormatType = "$ss$" // 00 01 ... 58 59 + TIME_s FormatType = "$s$" // 0 1 ... 58 59 + TIME_ss FormatType = "$ss$" // 00 01 ... 58 59 - Z FormatType = "$Z$" // -07:00 -06:00 ... +06:00 +07:00 - ZZ FormatType = "$ZZ$" // -0700 -0600 ... +0600 +0700 + TIME_Z FormatType = "$Z$" // -07:00 -06:00 ... +06:00 +07:00 + TIME_ZZ FormatType = "$ZZ$" // -0700 -0600 ... +0600 +0700 - X FormatType = "$X$" // 1360013296 + TIME_X FormatType = "$X$" // 1360013296 ) type Formatter struct {