diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-03-13 07:33:12 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-03-13 07:33:12 -0400 |
commit | 9160c2fd5dcf3db9a44539be0ea41eb69eb06b21 (patch) | |
tree | 0a02f4fe2dae19dfb4e4cc1bddee587606cccbaa /cisa/date.go | |
parent | 8ffd821fd1d5e227f15ca3e6a0f428cfdbd45398 (diff) | |
download | cvez-9160c2fd5dcf3db9a44539be0ea41eb69eb06b21.tar.bz2 cvez-9160c2fd5dcf3db9a44539be0ea41eb69eb06b21.zip |
cisa: add date json marshall/unmarshall
Diffstat (limited to 'cisa/date.go')
-rw-r--r-- | cisa/date.go | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/cisa/date.go b/cisa/date.go index b36b73c..b3b6528 100644 --- a/cisa/date.go +++ b/cisa/date.go @@ -1,6 +1,7 @@ package cisa import ( + "encoding/json" "fmt" "regexp" "strconv" @@ -135,5 +136,29 @@ func (me Date) String() string { y, m, d := me.GetComponents() // return string - return fmt.Sprintf("%d-%d-%d", y, m, d) + return fmt.Sprintf("%04d-%02d-%02d", y, m, d) +} + +// Unmarshal date from JSON string. +func (me *Date) UnmarshalJSON(b []byte) error { + // decode json string + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + + // create date + d, err := NewDate([]byte(s)) + if err != nil { + return err + } + + // save result, return success + *me = d + return nil +} + +// Marshal Date as JSON string. +func (d Date) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) } |