rtime

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: MIT Imports: 5 Imported by: 3

README

rtime

GoDoc

Retrieve the current time from remote servers.

It works by requesting timestamps from twelve very popular hosts over https. As soon as it gets at least three responses, it takes the two that have the smallest difference in time. And from those two it picks the one that is the oldest. Finally it ensures that the time is monotonic.

Getting

go get -u github.com/tidwall/rtime

Using

Get the remote time with rtime.Now().

tm := rtime.Now()
if tm.IsZero() {
    panic("internet offline")
}
println(tm.String())
// output: 2020-03-29 10:27:00 -0700 MST
}

Stay in sync

The rtime.Now() will be a little slow, usually 200 ms or more, because it must make a round trip to three or more remote servers to determine the correct time.

You make it fast like the built-in time.Now() by calling rtime.Sync() once at the start of your application.

if err := rtime.Sync(); err != nil {
    panic(err)
}
// All following rtime.Now() calls will now be quick and without the need for
// checking its result.
tm := rtime.Now()
println(tm.String())

It's a good idea to call rtime.Sync() at the top of the main() or init() functions.

Contact

Josh Baker @tidwall

License

Source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustSync added in v0.2.0

func MustSync()

MustSync is like Sync but panics if the internet is offline.

func Now

func Now() time.Time

Now returns the current remote time. If the remote time cannot be retrieved then the zero value for Time is returned. It's a good idea to test for zero after every call, such as:

now := rtime.Now()
if now.IsZero() {
    ... handle failure ...
}

func Sync added in v0.2.0

func Sync() error

Sync tells the application to keep rtime in sync with internet time. This ensures that all following rtime.Now() calls are fast, accurate, and without the need to check the result.

Ideally you would call this at the top of your main() or init() function.

if err := rtime.Sync(); err != nil {
    ... internet offline, handle error or try again ...
    return
}
rtime.Now() // guaranteed to be a valid time

Types

This section is empty.

Jump to

Keyboard shortcuts

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