Skip to content

Commit

Permalink
✨ feat: format time
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyuexing committed Jan 30, 2024
1 parent 2b13cae commit d6d1edd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
59 changes: 59 additions & 0 deletions datetime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package utils

import (
"strings"
"time"
)

type FormatTemplate string

const (
FormatYear FormatTemplate = "YYYY"
FormatShortYear FormatTemplate = "YY"
FormatMonth FormatTemplate = "MM"
FormatShortMonth FormatTemplate = "M"
FormatDay FormatTemplate = "dd"
FormatHour FormatTemplate = "HH"
FormatShortHour FormatTemplate = "H"
FormatSecond FormatTemplate = "ss"
FormatMinute FormatTemplate = "mm"
FormatMillisecond FormatTemplate = "ms"
FormatWeek FormatTemplate = "W"
FormatShortWeek FormatTemplate = "WW"
)

func DateTimeFormat(date time.Time, format string) string {
formatted := format
formatMap := map[FormatTemplate]int{
FormatYear: date.Year(),
FormatShortYear: date.Year() % 100,
FormatMonth: int(date.Month()),
FormatShortMonth: int(date.Month()),
FormatDay: date.Day(),
FormatHour: date.Hour(),
FormatShortHour: date.Hour() % 12,
FormatSecond: date.Second(),
FormatMinute: 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} {
formatted = strings.Replace(
formatted,
string(key),
strings.Join(
PadStart(
strings.Split(ToString(formatMap[key]), ""),
len(string(key)),
"0",
),
"",
),
1,
)
}

return formatted
}
26 changes: 26 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"testing"
"time"

utils "github.com/jingyuexing/go-utils"
)
Expand Down Expand Up @@ -89,6 +90,11 @@ func TestCookieParse(t *testing.T) {
fmt.Printf("%s\n", cookie.ToString())
}

func TestDebounce(t *testing.T){

}


func TestgetPathValue(t *testing.T) {
result := utils.GetPathValue("/user/:id", "/user/12")
if result["id"] != "12" {
Expand Down Expand Up @@ -263,3 +269,23 @@ func TestNestedAttr(t *testing.T) {
t.Error(fmt.Sprintf("Expect: %s, but got %s", "Haaaa", finalValue2.(string)))
}
}


func TestDateTimeFormat(t *testing.T){
datetime := time.Unix(1706572292,0)
year := utils.DateTimeFormat(datetime,"YYYY")
if year != "2024" {
t.Error("format year is wrong")
}

result1 := utils.DateTimeFormat(datetime,"YY/MM/dd")
if result1 != "24/01/30" {
t.Error("format date wrong")
}

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

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

0 comments on commit d6d1edd

Please sign in to comment.