package
1.3.4
Repository: https://github.com/avakarev/go-util.git
Documentation: pkg.go.dev

# README

timeutil

Utilities around time with respect of TZ environment variable

Install

go get github.com/avakarev/go-util/timeutil

Usage

package main

import (
	"fmt"
	"time"

	"github.com/avakarev/go-util/timeutil"
)

func respectTimezone(t time.Time) {
	// `TZ` env var is set to "Europe/Berlin"
	fmt.Println(t.Format(time.RFC3339))                 // => 2022-06-03T16:26:15Z
	fmt.Println(timeutil.Local(t).Format(time.RFC3339)) // => 2022-06-03T18:26:15+02:00
}

func mockTimeNow(t time.Time) {
	timeutil.MockNow(func() time.Time {
		return t
	})
	defer timeutil.UnmockNow()
	fmt.Println(timeutil.Now().Format(time.RFC3339)) // => 2022-06-03T16:26:15Z
}

func main() {
	t, _ := time.Parse(time.RFC3339, "2022-06-03T16:26:15Z")

	respectTimezone(t)
	mockTimeNow(t)
}

License

go-timeutil is licensed under MIT license. (see LICENSE)

# Functions

Init initializes timeutil.
IsDay checks whether it is day.
IsSameDay checks whether given time values are in same day.
IsToday checks whether given time represents today's date.
IsTomorrow checks whether given time represents tomorrow's date.
IsYesterday checks whether given time represents yesterday's date.
Local converts UTC time to local (timezone is taken from "TZ" env var).
MockNow replaces the Now() function with given implementation.
MustInit is like Init but panics in case of error.
NewClock returns an new clock value.
NewMock returns new clock mock value.
Now returns the current local time or mocked time if mock is active.
UnmockNow restores Now() function to original time.Now().

# Variables

Location represents time offset in use in a geographical area.

# Structs

Mock implements clock mock that can adjust time on demand.

# Interfaces

Clock defines interface to the time functions.