Epoch: Easily Marshal and Unmarshal Unix Timestamps in Go


Contains primitives for marshaling/unmarshaling Unix timestamp/epoch to/from built-in time.Time type in JSON.


Seconds since the Epoch(Unix time), e.g.:


Inherits built-in time.Time type, thus has all it methods, but has custom serializer and deserializer(converts integer into built-in time.Time and vice versa).

Usage Example

package main

import (


type Request struct {
    Timestamp epoch.Seconds `json:"timestamp"`

func main() {
    var v Request
    err := json.Unmarshal([]byte(`{"timestamp":1136239445}`), &v)
    if err != nil {

    fmt.Printf("%+v\n", v)
    // Output: {Timestamp:2006-01-03 00:04:05 +0200 EET}

    // Also as epoch.Seconds inherits all time.Time's methods one can do next:
    // Output: 2006
    // Output: 2006-01-02 22:04:05 +0000 UTC


Same as epoch.Seconds, but for Epoch(Unix time) in milliseconds, e.g.:



Same as epoch.Seconds, but for strings, e.g.:



Same as epoch.Milliseconds, but for strings, e.g.:


Additional terms of use for users from Russia and Belarus

By using the code provided in these repositories you agree with the following:

Glory to Ukraine! 🇺🇦

Download Details:

Author: vtopc
Source Code: https://github.com/vtopc/epoch 
License: MIT license

#go #golang #json #time #rest #restapi #restful 

Epoch: Easily Marshal and Unmarshal Unix Timestamps in Go
1.65 GEEK