Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kit): datetime's ToLocalTime now support value already in time #37

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions kit/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ func ToTime(value any) time.Time {

// ToLocalTime returns time in local time zone by specifying the time zone offset hours (+7 for GMT+7).
func ToLocalTime(value any, tzOffsetHours int) time.Time {
t := ToTime(value)
if t == (time.Time{}) {
t, ok := value.(time.Time)
if !ok {
t = ToTime(value)
}

if t.IsZero() {
return t
}

Expand Down
8 changes: 7 additions & 1 deletion kit/datetime/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestToTime(t *testing.T) {
}
}

func Test(t *testing.T) {
func TestToLocalTime(t *testing.T) {
locJakarta, err := time.LoadLocation("Asia/Jakarta")
if err != nil {
t.Fatal(err)
Expand All @@ -66,6 +66,11 @@ func Test(t *testing.T) {
value: 1029622579, // int int
result: time.Time{},
},
{
name: "value is already in time.Time",
value: time.Date(2022, 8, 17, 05, 16, 19, 0, time.UTC),
result: time.Date(2022, 8, 17, 12, 16, 19, 0, locJakarta),
},
}

for _, tc := range tt {
Expand All @@ -78,6 +83,7 @@ func Test(t *testing.T) {
}
}

// 2022-08-17T05:16:19+07:00, got: 2022-08-17T12:16:19+07:00
func TestTzOffsetHours(t *testing.T) {
tt := []struct {
name string
Expand Down