Simple module to handle unix timestamp json encoding/decoding.
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))
}
Henrik Hautakoski - Sw/eden - henrik@eossweden.org