Skip to content

Commit

Permalink
🧹 chore: support for abbreviations
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyuexing committed Jan 30, 2024
1 parent 635e86c commit 0a5cae7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ const (
FormatMonth FormatTemplate = "MM"
FormatShortMonth FormatTemplate = "M"
FormatDay FormatTemplate = "dd"
FormatShortDay FormatTemplate = "d"
FormatHour FormatTemplate = "HH"
FormatShortHour FormatTemplate = "H"
FormatSecond FormatTemplate = "ss"
FormatShortSecond FormatTemplate = "s"
FormatMinute FormatTemplate = "mm"
FormatShortMinute FormatTemplate = "m"
FormatMillisecond FormatTemplate = "ms"
FormatWeek FormatTemplate = "W"
FormatShortWeek FormatTemplate = "WW"
Expand All @@ -30,16 +33,19 @@ func DateTimeFormat(date time.Time, format string) string {
FormatMonth: int(date.Month()),
FormatShortMonth: int(date.Month()),
FormatDay: date.Day(),
FormatShortDay: date.Day(),
FormatHour: date.Hour(),
FormatShortHour: date.Hour() % 12,
FormatSecond: date.Second(),
FormatShortSecond: date.Second(),
FormatMinute: date.Minute(),
FormatShortMinute: date.Minute(),
FormatMillisecond: date.Nanosecond() / int(time.Millisecond),
FormatWeek: int(date.Weekday()),
FormatShortWeek: int(date.Weekday()),
}

for _, key := range []FormatTemplate{FormatYear, FormatMonth, FormatShortYear, FormatShortMonth, FormatDay, FormatHour, FormatShortHour, FormatSecond, FormatMinute, FormatMillisecond, FormatWeek, FormatShortWeek} {
for _, key := range []FormatTemplate{FormatYear, FormatMonth, FormatShortYear, FormatShortMonth, FormatDay, FormatShortDay, FormatHour, FormatShortHour, FormatSecond, FormatShortSecond, FormatMinute, FormatShortMinute, FormatMillisecond, FormatWeek, FormatShortWeek} {
formatted = strings.Replace(
formatted,
string(key),
Expand Down
9 changes: 7 additions & 2 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,14 @@ func TestDateTimeFormat(t *testing.T){
t.Error("format date wrong")
}

result2 := utils.DateTimeFormat(datetime,"MM/dd/YYYY HH:mm:ss.ms")
result2 := utils.DateTimeFormat(datetime,"MM/dd/YYYY HH:mm:ss")

if result2 != "01/30/2024 07:51:32.00"{
if result2 != "01/30/2024 07:51:32"{
t.Error("format datetime wrong")
}

result3 := utils.DateTimeFormat(datetime,"YYYY年M月d日 H时m分s秒")
if result3 != "2024年1月30日 7时51分32秒"{
t.Error("format has wrong")
}
}

0 comments on commit 0a5cae7

Please sign in to comment.