add parsing multiple time formats
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
This commit is contained in:
@@ -2,6 +2,7 @@ package helpers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -36,13 +37,16 @@ type KontorTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
const layout = "2006-01-02 15:04:05.000000"
|
||||
var layouts = []string{
|
||||
"2006-01-02 15:04:05.000000",
|
||||
"2006-01-02T15:04:05.000000Z",
|
||||
}
|
||||
|
||||
func (kt KontorTime) MarshalJSON() ([]byte, error) {
|
||||
if kt.IsZero() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return json.Marshal(kt.Format(layout))
|
||||
return json.Marshal(kt.Format(layouts[0]))
|
||||
}
|
||||
|
||||
func (kt *KontorTime) UnmarshalJSON(b []byte) error {
|
||||
@@ -53,10 +57,12 @@ func (kt *KontorTime) UnmarshalJSON(b []byte) error {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
t, err := time.Parse(layout, s)
|
||||
if err != nil {
|
||||
return err
|
||||
for _, layout := range layouts {
|
||||
t, err := time.Parse(layout, s)
|
||||
if err == nil {
|
||||
kt.Time = t
|
||||
return nil
|
||||
}
|
||||
}
|
||||
kt.Time = t
|
||||
return nil
|
||||
return fmt.Errorf("failed to parse time from %s", s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user