Skip to content

Files

Latest commit

 

History

History
53 lines (35 loc) · 1.25 KB

README.md

File metadata and controls

53 lines (35 loc) · 1.25 KB

UnixTime

Test Go Reference

Simple module to handle unix timestamp json encoding/decoding.

Example

package main

import (
    "encoding/json"
    "fmt"
    "time"

    "github.com/eosswedenorg-go/unixtime"
)

type MyStruct struct {
    Timestamp unixtime.Time
}

func main() {
    var s MyStruct

    input := `{"timestamp": 1205647965800}`

    if err := json.Unmarshal([]byte(input), &s); err != nil {
        panic(err)
    }

    // Output: Decoded: 2008-03-16 06:12:45.800 +0000 UTC
    fmt.Println("Decoded:", s.Timestamp.Time().Format("2006-01-02 15:04:05.000 -0700 MST"))

    s.Timestamp.FromTime(time.Date(2014, 6, 11, 14, 3, 55, int(time.Millisecond) * 625, time.UTC))

    data, err := json.Marshal(s)
    if err != nil {
        panic(err)
    }

    // Output: Encoded: {"Timestamp":1402495435625}
    fmt.Println("Encoded:", string(data))
}

Author

Henrik Hautakoski - Sw/eden - henrik@eossweden.org