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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,13 +37,16 @@ type KontorTime struct {
|
|||||||
time.Time
|
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) {
|
func (kt KontorTime) MarshalJSON() ([]byte, error) {
|
||||||
if kt.IsZero() {
|
if kt.IsZero() {
|
||||||
return []byte("null"), nil
|
return []byte("null"), nil
|
||||||
}
|
}
|
||||||
return json.Marshal(kt.Format(layout))
|
return json.Marshal(kt.Format(layouts[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kt *KontorTime) UnmarshalJSON(b []byte) error {
|
func (kt *KontorTime) UnmarshalJSON(b []byte) error {
|
||||||
@@ -53,10 +57,12 @@ func (kt *KontorTime) UnmarshalJSON(b []byte) error {
|
|||||||
if s == "" {
|
if s == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
for _, layout := range layouts {
|
||||||
t, err := time.Parse(layout, s)
|
t, err := time.Parse(layout, s)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return err
|
|
||||||
}
|
|
||||||
kt.Time = t
|
kt.Time = t
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to parse time from %s", s)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user