Skip to content

Instantly share code, notes, and snippets.

@alexmcroberts
Last active February 17, 2022 11:52
Show Gist options
  • Select an option

  • Save alexmcroberts/219127816e7a16c7bd70 to your computer and use it in GitHub Desktop.

Select an option

Save alexmcroberts/219127816e7a16c7bd70 to your computer and use it in GitHub Desktop.

Revisions

  1. alexmcroberts revised this gist Oct 1, 2015. 1 changed file with 1 addition and 9 deletions.
    10 changes: 1 addition & 9 deletions main.go
    Original 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) {
    var r string
    if strings.ContainsAny(string(s), "\"") {
    r, err = strconv.Unquote(string(s))
    if err != nil {
    return err
    }
    } else {
    r = string(s)
    }
    r := strings.Replace(string(s), `"`, ``, -1)

    q, err := strconv.ParseInt(r, 10, 64)
    if err != nil {
  2. alexmcroberts created this gist Oct 1, 2015.
    57 changes: 57 additions & 0 deletions main.go
    Original 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() }
    10 changes: 10 additions & 0 deletions readme.md
    Original 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