gtime

package module
v0.0.0-...-fae00ad Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2020 License: MIT Imports: 6 Imported by: 0

README

This project has been archived. Please check out tidwall/rtime for a better internet time library.

gtime

GoDoc

A Go package that helps to keep your application time in sync with Google server time.

This can be very useful for long running applications which reside on systems that may have a higher risk for clock drift. Such as containers, virtual servers, and cloud providers.

Getting

go get github.com/tidwall/gtime

API

// Now returns the current Google time. 
// Local system time is returned until Sync or MustSync has been succesfully called.
gtime.Now() time.Time

// Sync will sync the time with Google servers. 
gtime.Sync(timeout time.Duration) error

// MustSync will attempt to sync with Google servers. 
// This operation will try over and over again until the time has successfully 
// synced or the timeout has been reached. A timeout will panic.
gtime.MustSync(timeout time.Duration)

Example

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/tidwall/gtime"
)

func main() {
	// Require a sync with Google time. This will panic if it does not
	// succeed in one minute. Optionally you can just call gtime.Sync() and
	// handle the error.
	gtime.MustSync(time.Minute)

	// Spin up a background routine to keep the system in sync. This example 
	// syncs with Google every 5 minutes.
	go func() {
		for range time.NewTicker(time.Minute * 5).C {
			gtime.Sync(time.Minute)
		}
	}()

	// Create a little HTTP server that responds to all requests with
	// Google and Local time.
	http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprintf(w, "Google: %v\n Local: %v\n", gtime.Now(), time.Now())
	})
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}

Then from a terminal:

$ curl http://localhost:8080
Google: 2017-01-07 15:45:02.288466989 -0700 MST
 Local: 2017-01-07 15:45:02.529567671 -0700 MST

Contact

Josh Baker @tidwall

License

Gtime source code is available under the MIT License.

Documentation

Overview

package gtime allows for syncing with Google time. This is useful for applications that run on servers that have a high risk for time drift, such as containers, virtual servers, and cloud providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustSync

func MustSync(timeout time.Duration)

MustSync will attempt to sync with Google servers. It will try over and over again until the timeout has been reached. It will panic if the timeout is reached. If the operation was successful then every following Now() call will return Google time.

func Now

func Now() time.Time

Now returns the current Google time. Local system time is returned if Sync or MustSync has not been succesfully called.

func Sync

func Sync(timeout time.Duration) error

Sync will sync the time with Google servers. If the operation was successful then every following Now() call will return Google time. Returns an error if time cannot be fetched or the timeout has been reached.

Types

This section is empty.

Jump to

Keyboard shortcuts

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