Giter Site home page Giter Site logo

vtopc / epoch Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 3.0 748 KB

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

License: MIT License

Makefile 2.73% Go 97.27%
go golang time json date serialization deserialization timestamp unix-timestamp epoch

epoch's Introduction

Stand With Ukraine

epoch

Mentioned in Awesome Go Godoc Reference build codecov goreportcard

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

Seconds

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

  {"timestamp":1136239445}

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 (
	"encoding/json"
	"fmt"

	"github.com/vtopc/epoch"
)

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

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

	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:
	fmt.Println(v.Timestamp.Year())
	// Output: 2006
	fmt.Println(v.Timestamp.UTC().String())
	// Output: 2006-01-02 22:04:05 +0000 UTC
}

Milliseconds

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

  {"timestamp":1136239445999}

StrSeconds

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

  {"timestamp":"1136239445"}

StrMilliseconds

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

  {"timestamp":"1136239445999"}

FloatMS

Integer part of timestamp represents seconds and fractional - milliseconds since the Epoch(Unix time), e.g.:

  {"timestamp":1136239445.999}

Installation

go get github.com/vtopc/epoch@latest

This will update yours go.mod file.

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! πŸ‡ΊπŸ‡¦

epoch's People

Contributors

vtopc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

epoch's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.