leafTime

package module
v0.0.0-...-0829b1e Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: MIT Imports: 2 Imported by: 8

README

Time

Time Helper for Go

Time helper for golang package, mocking time without using monkeypatch 🙈.

Usage

Import the package

import (
    ...
    "github.com/paulusrobin/leaf-utilities/time"
    ...
)

To get current time:

var now = leafTime.Now()

To mock / freeze current time and reset the mock:

var desiredTime = leafTime.Now()
leafTime.Mock(desiredTime)
defer leafTime.ResetMock()

To convert time to UTC:

var utcTime = leafTime.ToUTCTime(leafTime.Now())

To convert time to Server Time:

var serverTime = leafTime.ToServerTime(leafTime.Now())

To convert time using Common Location (List Common Location)

wibTime, err := leafTime.ToClientTimeByLocation(leafTime.Now(), leafTime.WIB)
witaTime, err := leafTime.ToClientTimeByLocation(leafTime.Now(), leafTime.WITA)
witTime, err := leafTime.ToClientTimeByLocation(leafTime.Now(), leafTime.WIT)

To convert time using location string (IANA)

amsterdamTime, err := leafTime.ToClientTimeByLocationString(leafTime.Now(), "Europe/Amsterdam")
tokyoTime, err := leafTime.ToClientTimeByLocationString(leafTime.Now(), "Asia/Tokyo")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidLocation = fmt.Errorf("invalid location")
)

Functions

func Mock

func Mock(data time.Time)

func Now

func Now() time.Time

func ResetMock

func ResetMock()

func ToClientTimeByLocation

func ToClientTimeByLocation(t time.Time, clientLoc Location) (time.Time, error)

func ToClientTimeByLocationString

func ToClientTimeByLocationString(t time.Time, clientLoc string) (time.Time, error)

func ToServerTime

func ToServerTime(clientTime time.Time) time.Time

func ToUTCTime

func ToUTCTime(clientTime time.Time) time.Time

Types

type Location

type Location struct {
	// contains filtered or unexported fields
}
var (
	WIB  Location
	WITA Location
	WIT  Location
)

func (Location) Location

func (l Location) Location() *time.Location

func (Location) Valid

func (l Location) Valid() bool

Jump to

Keyboard shortcuts

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