# README
UTC - A time.Time
Wrapper for ISO8601 / RFC3339
The package utc
consists of a single struct UTC
that is a small wrapper around the standard lib's time.Time
. It provides the following main functions:
- times in the UTC timezone (Coordinated Universal Time)
- consistent formatting and parsing of the ISO8601 / RFC3339 format '2006-01-02T15:04:05.000Z' with fixed milliseconds
- performance-optimized string formatting, binary marshal/unmarshal
- mocking of "Now()" for tests
Usage
Use just like time.Time:
import (
"fmt"
"time"
"github.com/eluv-io/utc-go"
)
func ExampleUTC() {
// standard lib time.Time
location, _ := time.LoadLocation("Indian/Mayotte")
d0std := time.Date(2000, 1, 1, 0, 0, 0, 0, location)
fmt.Println("d0 std ", d0std)
// utc.UTC
d0 := utc.New(d0std)
fmt.Println("d0 ", d0.String())
// utc.UTC.Time is the underlying time.Time (in UTC timezone)
fmt.Println("d0.Time", d0.Time)
// ISO8601 / RFC3339
d1 := utc.MustParse("2021-12-25T12:20:00.000Z")
fmt.Println("d1 ", d1)
// All methods of time.Time are available on utc.UTC
fmt.Println("d1-d0 ", d1.Sub(d0))
// JSON and text marshalling produce & parse ISO8601 / RFC3339
jsn, _ := d1.MarshalText()
fmt.Println("d1 ", string(jsn), "MarshalText()")
// Output:
//
// d0 std 2000-01-01 00:00:00 +0300 EAT
// d0 1999-12-31T21:00:00.000Z
// d0.Time 1999-12-31 21:00:00 +0000 UTC
// d1 2021-12-25T12:20:00.000Z
// d1-d0 192711h20m0s
// d1 2021-12-25T12:20:00.000Z MarshalText()
}
Mocking Now():
import (
"fmt"
"time"
"github.com/eluv-io/utc-go"
)
func ExampleMockNowFn() {
d0 := utc.MustParse("2020-01-01T00:00:00.000Z")
now := d0.Add(38*time.Hour + 30*time.Minute)
// replace Now() with a custom function that provides the mocked time
reset := utc.MockNowFn(func() utc.UTC {
return now
})
defer reset()
fmt.Println("now ", utc.Now(), "mocked")
time.Sleep(1 * time.Second)
fmt.Println("now ", utc.Now(), "one real second later: still the same")
fmt.Println("now-d0", utc.Now().Sub(d0))
now = now.Add(time.Second)
fmt.Println("now ", utc.Now(), "one mocked second later")
// Output:
//
// now 2020-01-02T14:30:00.000Z mocked
// now 2020-01-02T14:30:00.000Z one real second later: still the same
// now-d0 38h30m0s
// now 2020-01-02T14:30:01.000Z one mocked second later
}
# Functions
FromString parses the given time string.
MockNow allows to replace the Now func variable with a function that returns the given constant time and returns itself a function to restore the default Now() implementation.
MockNowClock mocks now with a test clock.
MockNowFn allows to replace the Now func variable with a mock function and returns a function to restore the default Now() implementation.
Mono returns the current time with the monotonic clock.
MustParse parses the given time string according to ISO 8601 format, panicking in case of errors.
New creates a new UTC instance from the given time.
NewMonoClock returns a TestClock with the monotonic clock reading.
NewWallClock returns a TestClock with the monotonic clock reading stripped.
NewWallClockMs returns a TestClock with the monotonic clock reading stripped and time rounded to the millisecond.
Now returns the current time as UTC instance.
Parse parses the given time value string with the provided layout - see Time.Parse().
ResetNow resets the Now func to the default implementation.
Since returns Now().Sub(t).
Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC.
UnixMilli returns the local Time corresponding to the given Unix time in milliseconds since January 1, 1970 UTC.
Until returns t.Sub(Now()).
WallClock returns the wall clock (i.e.
WallClockMs is like WallClock rounded to the millisecond.
WallNow returns Now as a wall clock, i.e.
WallNowMs returns Now as a wall clock rounded to the millisecond.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
TestClock is a Clock that can be set to a given UTC value or reset.
UTC is a standard time.Time in the UTC timezone with marshaling to and from ISO 8601 / RFC 3339 format with fixed milliseconds: 2006-01-02T15:04:05.000Z
Years smaller than "0000" and larger than "9999" cannot be marshaled to bytes, text, or JSON, and generate an error if attempted.
# Interfaces
No description provided by the author
# Type aliases
ClockFn is a function implementing Clock.