Skip to content

Commit

Permalink
feat:add new time util function GetBeijingTime
Browse files Browse the repository at this point in the history
  • Loading branch information
R22627 authored and R22627 committed Nov 13, 2023
1 parent 05f8281 commit f17cb52
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dablelv/cyan

go 1.19
go 1.21

require (
golang.org/x/exp v0.0.0-20221229233502-02c3fc3b3eb4
Expand Down
11 changes: 11 additions & 0 deletions time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,14 @@ func IsSameWeek(ts1, ts2 int64) bool {
t2 := time.Unix(ts2, 0)
return GetMonDate(t1) == GetMonDate(t2)
}

// GetBeijingTime gets Beijing Time from time string and layout.
// The location name Asia/Shanghai from IANA Time Zone Database standards for Beijing Time.
func GetBeijingTime(layout, value string) (t time.Time, err error) {
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
return
}
t, err = time.ParseInLocation(layout, value, location)
return
}
12 changes: 12 additions & 0 deletions time/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package time

import (
"testing"
"time"

"github.com/dablelv/cyan/internal"
)
Expand Down Expand Up @@ -197,3 +198,14 @@ func TestIsSameWeek(t *testing.T) {
assert.Equal(tt.want, got)
}
}

func TestGetBeijingTime(t *testing.T) {
assert := internal.NewAssert(t, "TestGetBeijingTime")

got, err := GetBeijingTime(time.DateTime, "2022-11-04 08:30:00")

Check failure on line 205 in time/time_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: time.DateTime
assert.IsNil(err)
assert.Equal("2022-11-04 08:30:00", got.Format(time.DateTime))

Check failure on line 207 in time/time_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: time.DateTime

_, err = GetBeijingTime(time.RFC3339, "2022-11-04 08:30:00")
assert.IsNotNil(err)
}

0 comments on commit f17cb52

Please sign in to comment.