epoch

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2024 License: MIT Imports: 5 Imported by: 2

README

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! 🇺🇦

Documentation

Overview

Package epoch contains primitives for marshaling/unmarshaling Unix timestamp/epoch to/from build-in time.Time type in JSON.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FloatMS added in v1.5.0

type FloatMS struct {
	time.Time
}

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

1136239445.999

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

func NewFloatMS added in v1.5.0

func NewFloatMS(t time.Time) FloatMS

NewFloatMS - returns FloatMS

func (FloatMS) MarshalJSON added in v1.5.0

func (s FloatMS) MarshalJSON() ([]byte, error)

MarshalJSON - implements JSON marshaling interface

func (*FloatMS) UnmarshalJSON added in v1.5.0

func (s *FloatMS) UnmarshalJSON(data []byte) error

type Milliseconds added in v1.1.0

type Milliseconds struct {
	time.Time
}

Milliseconds - same as epoch.Seconds, but for Epoch(Unix time) in milliseconds.

func NewMilliseconds added in v1.1.0

func NewMilliseconds(t time.Time) Milliseconds

NewMilliseconds - returns Milliseconds

func (Milliseconds) MarshalJSON added in v1.1.0

func (m Milliseconds) MarshalJSON() ([]byte, error)

MarshalJSON - implements JSON marshaling interface

func (*Milliseconds) UnmarshalJSON added in v1.1.0

func (m *Milliseconds) UnmarshalJSON(data []byte) error

UnmarshalJSON - implements JSON unmarshaling interface

type Seconds

type Seconds struct {
	time.Time
}

Seconds - seconds since the Epoch(Unix time). 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).

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)
	}

	// Also as epoch.Seconds inherits all time.Time's methods:
	fmt.Println(v.Timestamp.Year())
	fmt.Println(v.Timestamp.UTC().String())
}
Output:

2006
2006-01-02 22:04:05 +0000 UTC

func NewSeconds

func NewSeconds(t time.Time) Seconds

NewSeconds - returns Seconds

func (Seconds) MarshalJSON

func (s Seconds) MarshalJSON() ([]byte, error)

MarshalJSON - implements JSON marshaling interface

func (*Seconds) UnmarshalJSON

func (s *Seconds) UnmarshalJSON(data []byte) error

UnmarshalJSON - implements JSON unmarshaling interface

type StrMilliseconds added in v1.2.0

type StrMilliseconds struct {
	time.Time
}

StrMilliseconds - same as epoch.Milliseconds, but for strings.

func NewStrMilliseconds added in v1.2.0

func NewStrMilliseconds(t time.Time) StrMilliseconds

NewStrMilliseconds - returns Milliseconds

func (StrMilliseconds) MarshalJSON added in v1.2.0

func (m StrMilliseconds) MarshalJSON() ([]byte, error)

MarshalJSON - implements JSON marshaling interface

func (*StrMilliseconds) UnmarshalJSON added in v1.2.0

func (m *StrMilliseconds) UnmarshalJSON(data []byte) error

UnmarshalJSON - implements JSON unmarshaling interface

type StrSeconds added in v1.2.0

type StrSeconds struct {
	time.Time
}

StrSeconds - seconds since the Epoch(Unix time) as string. 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).

func NewStrSeconds added in v1.2.0

func NewStrSeconds(t time.Time) StrSeconds

NewStrSeconds - returns StrSeconds

func (StrSeconds) MarshalJSON added in v1.2.0

func (s StrSeconds) MarshalJSON() ([]byte, error)

MarshalJSON - implements JSON marshaling interface

func (*StrSeconds) UnmarshalJSON added in v1.2.0

func (s *StrSeconds) UnmarshalJSON(data []byte) error

UnmarshalJSON - implements JSON unmarshaling interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL