unixtime

package module
v0.0.0-...-0107044 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2017 License: BSD-3-Clause Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

unixtime

unixtime is a go package that makes it easier to convert between unix timestamps and go objects. Its primary purpose is so you don't have to remember (and properly type) the number of nanoseconds in a millisecond. It also properly handles the seconds/nanoseconds division in time objects so it avoids overflows of times above math.MaxInt64 nanoseconds since 1 January 1970 (times after year 2262).

Documentation

http://godoc.org/github.com/retailnext/unixtime

Usage
t0 := time.Now()
unixMicroseconds := unixtime.ToUnix(t0, time.Microsecond)

fmt.Printf("Microseconds: %d\n", unixMicroseconds)
// prints Microseconds: 1507158285672039

t1 := unixtime.ToTime(unixMicroseconds, time.Microsecond)

fmt.Printf("t0=%q t1=%q\n", t0.Format(time.RFC3339Nano), t1.Format(time.RFC3339Nano))
// prints t0="2017-10-04T23:04:45.672039138Z" t1="2017-10-04T23:04:45.672039Z"
Installation
go get github.com/retailnext/unixtime

Documentation

Overview

Package unixtime provides functions that make it easier to convert between go time.Time objects and Unix timestamps.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToTime

func ToTime(unixTime int64, unit time.Duration) time.Time

ToTime converts an int64 representing the number of unit {nano,mirco,milli,}seconds since 1 January 1970 utc. to a time. This function has the same imput limitations as time.Unix. ToTime panics if your unix is something other than 1 {Nano,Micro,Milli}second.

Example
var unixMilliseconds int64 = 1507156635642

t := ToTime(unixMilliseconds, time.Second)

fmt.Printf("time=%s\n", t.Format(time.RFC3339Nano))

func ToUnix

func ToUnix(t time.Time, unit time.Duration) int64

ToUnix converts a time object to number of {Nano,Mirco,Milli,}seconds since 1 January 1970 utc. ToUnix panics if your unix is something other than 1 {Nano,Micro,Milli}second.

Example
now := time.Now()

s := ToUnix(now, time.Second)
ms := ToUnix(now, time.Millisecond)
us := ToUnix(now, time.Microsecond)

fmt.Printf("seconds=%d milliseconds=%d microseconds=%d\n", s, ms, us)

Types

This section is empty.

Jump to

Keyboard shortcuts

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