Last active
February 17, 2022 11:52
-
-
Save alexmcroberts/219127816e7a16c7bd70 to your computer and use it in GitHub Desktop.
Revisions
-
alexmcroberts revised this gist
Oct 1, 2015 . 1 changed file with 1 addition and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,15 +36,7 @@ func (t jsonTime) MarshalJSON() ([]byte, error) { } func (t *jsonTime) UnmarshalJSON(s []byte) (err error) { r := strings.Replace(string(s), `"`, ``, -1) q, err := strconv.ParseInt(r, 10, 64) if err != nil { -
alexmcroberts created this gist
Oct 1, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ package main import ( "encoding/json" "fmt" "strconv" "strings" "time" ) func main() { b := []byte(`{"timestamp": 1436150027000}`) var ep EpochParent err := json.Unmarshal(b, &ep) fmt.Println(ep, err) fmt.Println(ep.Epoch) fmt.Println("=======") c := []byte(`{"timestamp": "1436150027000"}`) var dp EpochParent err = json.Unmarshal(c, &dp) fmt.Println(dp, err) fmt.Println(dp.Epoch) fmt.Println("=======") } type EpochParent struct { Epoch jsonTime `json:"timestamp"` } type jsonTime time.Time func (t jsonTime) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatInt(time.Time(t).Unix(), 10)), nil } func (t *jsonTime) UnmarshalJSON(s []byte) (err error) { var r string if strings.ContainsAny(string(s), "\"") { r, err = strconv.Unquote(string(s)) if err != nil { return err } } else { r = string(s) } q, err := strconv.ParseInt(r, 10, 64) if err != nil { return err } *(*time.Time)(t) = time.Unix(q/1000, 0) return } func (t jsonTime) String() string { return time.Time(t).String() } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ Golang gist for unmarshalling JSON containing an epoch timestamp with the value in milliseconds, stored as a string: `{"timestamp": "1436150027000"}` Note the three zeroes at the end of the value, and also note that the value is stored as a string – not as a number string: `{"timestamp": "1436150027000"}` number: `{"timestamp": 1436150027000}` The example code above will process both samples listed where epoch time in milliseconds is stored as either string or number