Skip to content

Commit

Permalink
🚀 chore: Update LocalTime methods for better string formatting and sc…
Browse files Browse the repository at this point in the history
…anning
  • Loading branch information
sohaha committed Jun 27, 2024
1 parent 0bd6082 commit 3b598a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
20 changes: 16 additions & 4 deletions ztime/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ func (t LocalTime) Value() (driver.Value, error) {
return t.Time, nil
}

func (t LocalTime) String() string {
return inlay.FormatTime(t.Time, "2006-01-02 15:04:05.999999999 -0700 MST")
}

func (t LocalTime) Format(layout string) string {
return inlay.FormatTime(t.Time, layout)
}

func (t *LocalTime) Scan(v interface{}) error {
if value, ok := v.(time.Time); ok {
*t = LocalTime{Time: value}
switch vv := v.(type) {
case time.Time:
*t = LocalTime{Time: vv}
return nil
case LocalTime:
*t = vv
return nil
default:
return fmt.Errorf("expected time.Time, got %T", v)
}

return fmt.Errorf("expected time.Time, got %T", v)
}
9 changes: 8 additions & 1 deletion ztime/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestLocalTime(t *testing.T) {

now := time.Now()
lt := ztime.LocalTime{now}
tt.Equal(now, lt.Time)
tt.Equal(now.Unix(), lt.Unix())

j, err := json.Marshal(lt)
tt.NoError(err)
Expand All @@ -49,4 +49,11 @@ func TestLocalTime(t *testing.T) {
tt.NoError(err)
tt.Log(string(j2))
tt.EqualTrue(string(j2) != string(nj))

lt3 := ztime.LocalTime{}
lt3.Scan(data.Birthday)
tt.Log(lt3.String())

lt3.Scan(data.BirthdayLocal)
tt.Log(lt3.String())
}

0 comments on commit 3b598a2

Please sign in to comment.