Skip to content

Commit

Permalink
Add HMS() method to WideTime and unmarshal from json.Number (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Jul 19, 2024
1 parent 268c6b1 commit 9ac96cd
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tl/tt/widetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tt

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -28,6 +29,17 @@ func NewWideTimeFromSeconds(value int) WideTime {
return wt
}

func (wt WideTime) HMS() (int, int, int) {
secs := wt.Seconds
if secs < 0 {
secs = 0
}
hours := secs / 3600
minutes := (secs % 3600) / 60
seconds := (secs % 3600) % 60
return hours, minutes, seconds
}

func (wt WideTime) String() string {
if !wt.Valid {
return ""
Expand Down Expand Up @@ -79,7 +91,11 @@ func (wt *WideTime) Scan(src interface{}) error {
return nil
}
wt.Seconds = int(v)
case json.Number:
a, _ := v.Int64()
wt.Seconds = int(a)
default:
fmt.Printf("%T\n", v)
p = errors.New("could not parse time")
}
wt.Valid = (p == nil)
Expand Down

0 comments on commit 9ac96cd

Please sign in to comment.