Skip to content

Commit

Permalink
feat: add parse timestamp methods
Browse files Browse the repository at this point in the history
  • Loading branch information
guobinhit committed Apr 28, 2024
1 parent 3c4a323 commit 292651b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
28 changes: 19 additions & 9 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,57 @@ import (
"time"
)

// ParseMmDd returns time.Time with '01-02' format value,others location is default value.
// ParseMmDd return time.Time with '01-02' format value,others location is default value.
func ParseMmDd(dStr string) (time.Time, error) {
return time.ParseInLocation(MmDd, dStr, time.Local)
}

// ParseYyyyMm returns time.Time with '2006-01' format value,others location is default value.
// ParseYyyyMm return time.Time with '2006-01' format value,others location is default value.
func ParseYyyyMm(dStr string) (time.Time, error) {
return time.ParseInLocation(YyyyMm, dStr, time.Local)
}

// ParseYyyyMmDd returns time.Time with '2006-01-02' format value,others location is default value.
// ParseYyyyMmDd return time.Time with '2006-01-02' format value,others location is default value.
func ParseYyyyMmDd(dStr string) (time.Time, error) {
return time.ParseInLocation(YyyyMmDd, dStr, time.Local)
}

// ParseMmDdHhMm returns time.Time with '01-02 15:04' format value,others location is default value.
// ParseMmDdHhMm return time.Time with '01-02 15:04' format value,others location is default value.
func ParseMmDdHhMm(dStr string) (time.Time, error) {
return time.ParseInLocation(MmDdHhMm, dStr, time.Local)
}

// ParseMmDdHhMmSs returns time.Time with '01-02 15:04:05' format value,others location is default value.
// ParseMmDdHhMmSs return time.Time with '01-02 15:04:05' format value,others location is default value.
func ParseMmDdHhMmSs(dStr string) (time.Time, error) {
return time.ParseInLocation(MmDdHhMmSs, dStr, time.Local)
}

// ParseMmDdHhMmSsSss returns time.Time with '01-02 15:04:05.000' format value,others location is default value.
// ParseMmDdHhMmSsSss return time.Time with '01-02 15:04:05.000' format value,others location is default value.
func ParseMmDdHhMmSsSss(dStr string) (time.Time, error) {
return time.ParseInLocation(MmDdHhMmSsSss, dStr, time.Local)
}

// ParseYyyyMmDdHhMm returns time.Time with '2006-01-02 15:04' format value,others location is default value.
// ParseYyyyMmDdHhMm return time.Time with '2006-01-02 15:04' format value,others location is default value.
func ParseYyyyMmDdHhMm(dStr string) (time.Time, error) {
return time.ParseInLocation(YyyyMmDdHhMm, dStr, time.Local)
}

// ParseYyyyMmDdHhMmSs returns time.Time with '2006-01-02 15:04:05' format value,others location is default value.
// ParseYyyyMmDdHhMmSs return time.Time with '2006-01-02 15:04:05' format value,others location is default value.
func ParseYyyyMmDdHhMmSs(dStr string) (time.Time, error) {
return time.ParseInLocation(YyyyMmDdHhMmSs, dStr, time.Local)
}

// ParseYyyyMmDdHhMmSsSss returns time.Time with '2006-01-02 15:04:05.000' format value,others location is default value.
// ParseYyyyMmDdHhMmSsSss return time.Time with '2006-01-02 15:04:05.000' format value,others location is default value.
func ParseYyyyMmDdHhMmSsSss(dStr string) (time.Time, error) {
return time.ParseInLocation(YyyyMmDdHhMmSsSss, dStr, time.Local)
}

// ParseTimestampUnix return time.Time by parse timeUnix seconds.
func ParseTimestampUnix(timeUnix int64) time.Time {
return time.Unix(timeUnix, 0)
}

// ParseTimestampUnixMilli return time.Time by parse timeUnixMilli milliseconds.
func ParseTimestampUnixMilli(timeUnixMilli int64) time.Time {
return time.UnixMilli(timeUnixMilli)
}
50 changes: 50 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,53 @@ func TestParseYyyyMmDdHhMmSsSss(t *testing.T) {
})
}
}

func TestParseTimestampUnix(t *testing.T) {
d, _ := time.ParseInLocation(YyyyMmDdHhMmSs, "2024-04-28 17:45:00", time.Local)
type args struct {
timeUnix int64
}
tests := []struct {
name string
args args
want time.Time
}{
{
name: "ParseTimestampUnix success",
args: args{timeUnix: 1714297500},
want: d,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseTimestampUnix(tt.args.timeUnix); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseTimestampUnix() = %v, want %v", got, tt.want)
}
})
}
}

func TestParseTimestampUnixMilli(t *testing.T) {
d, _ := time.ParseInLocation(YyyyMmDdHhMmSs, "2024-04-28 17:45:00", time.Local)
type args struct {
timeUnixMilli int64
}
tests := []struct {
name string
args args
want time.Time
}{
{
name: "ParseTimestampUnix success",
args: args{timeUnixMilli: 1714297500000},
want: d,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseTimestampUnixMilli(tt.args.timeUnixMilli); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseTimestampUnixMilli() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 292651b

Please sign in to comment.