unixtime

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2020 License: BSD-3-Clause Imports: 1 Imported by: 2

README

Package unixtime

This package has been created to provide functions to convert between Go time values and integers representing the Unix time in micro- or milliseconds.

Ian Lance Taylor suggested to create such a package in the discussion of Go issue #27782. The package should be used to measure, whether there is really a need for this functionality that would justify the inclusion in the standard library. Note that the issue was a duplicate of #18935.

Installing the package

Use the get get command to download the module.

$ go get -u github.com/ulikunitz/unixtime

Using the package

import (
        "fmt"
        "log"
        "time"

        "github.com/ulikunitz/unixtime"
)

func Example() {
        t, err := time.Parse(time.RFC3339Nano, "1961-04-12T09:06:59.7+03:00")
        if err != nil {
                log.Fatalf("Parse error %s", err)
        }

        ms := unixtime.Milli(t)
        fmt.Printf("Unix time: %d ms\n", ms)

        tms := unixtime.FromMilli(ms)
        fmt.Printf("FromMilli: %s\n", tms.Format(time.RFC3339Nano))

        µs := unixtime.Micro(t)
        fmt.Printf("Unix time: %d µs\n", µs)

        tµs := unixtime.FromMicro(µs)
        fmt.Printf("FromMicro: %s\n", tµs.Format(time.RFC3339Nano))
}

Documentation

Overview

Package unixtime provides helper functions to convert between Go time values and Unix time values in milli- and microseconds.

The package has been created in response to Ian Lance Taylor's suggestion in the discussion of the Go issue #27782. A former issue discussing the same functionality has been #18935.

https://github.com/golang/go/issues/27782
https://github.com/golang/go/issues/18935
Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/ulikunitz/unixtime"
)

func main() {
	t, err := time.Parse(time.RFC3339Nano, "1961-04-12T09:06:59.7+03:00")
	if err != nil {
		log.Fatalf("Parse error %s", err)
	}

	ms := unixtime.Milli(t)
	fmt.Printf("Unix time: %d ms\n", ms)

	tms := unixtime.FromMilli(ms)
	fmt.Printf("FromMilli: %s\n", tms.Format(time.RFC3339Nano))

	µs := unixtime.Micro(t)
	fmt.Printf("Unix time: %d µs\n", µs)

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

}
Output:

Unix time: -275248380300 ms
FromMilli: 1961-04-12T07:06:59.7+01:00
Unix time: -275248380300000 µs
FromMicro: 1961-04-12T07:06:59.7+01:00

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromMicro

func FromMicro(µs int64) time.Time

FromMicro converts the Unix time in microseconds to a time value.

func FromMilli

func FromMilli(ms int64) time.Time

FromMilli converts the Unix time in milliseconds to a time value.

func Micro

func Micro(t time.Time) int64

Micro converts a time value to the Unix time in microseconds.

func Milli

func Milli(t time.Time) int64

Milli converts a time value to the Unix time in milliseconds.

Types

This section is empty.

Jump to

Keyboard shortcuts

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