timex

package module
v0.0.0-...-2524b63 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2014 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Timex

A simple go package for accessing current time, clock status, maximum and estimated error. This is accomplished by calling ntp_gettime from sys/timex.h via cgo. Timex should work on any system with sys/timex.h including modern Linux and BSD distributions but not Mac OS X.

Status

Timex is my first cgo package and as such there may be errors, leaks, or other bad behavior. It also may not be altogether idomatic. Please open an issue or get in touch if you see anything that could use improvement. Also, you should probably use Adjtimex in the syscall package instead:

package main

import (
        "log"
        "syscall"
)

func main() {
        var timex = new(syscall.Timex)
        status, err := syscall.Adjtimex(timex)
        if err != nil {
                log.Fatalf("Adjtimex error: %v", err)
        }
        if status != 0 {
                log.Fatalf("Unexpected syscall status %v", status)
        }
        log.Printf("time: %v max: %v est: %v status: %v\n", timex.Time, timex.Maxerror, timex.Esterror, timex.Status)
}

Installation and use

Timex can be installed using go get:

$ go get github.com/mcroydon/timex

From there you can import and use timex:

import (
    "fmt"
    "github.com/mcroydon/timex"
)
tmx := timex.Now()
fmt.Printf("Current time is %v and status is %v", tmx.Time, tmx.StatusInfo())

You can also install and use timexinfo:

$ go install github.com/mcroydon/timex/timexinfo
$ timexinfo
Current time: 2014-02-23 05:38:34.777437699 +0000 UTC
Status: Everything is ok.
Max error: 224774
Estimated error: 24376

Testing

Run the tests:

$ go test github.com/mcroydon/timex

Run the tests and a benchmark that keeps track of the min, max, and mean values of timex.Now():

$ go test -bench . github.com/mcroydon/timex

To see the current values of timex.Now() run go test with -v.

License

Released under a 3-clause BSD license. The effective license may change based on the license of sys/timex.h and related files on your system. Maybe.

Documentation

Overview

Package timex provides access to current system time along with clock synchronization status and clock error maximum and estimated. This information is provided with cgo calls to ntp_gettime() in sys/timex.h. This package will only work on systems with cgo and sys/timex.h (which includes modern Linux and many BSD distributions but not Mac OS X).

This information is readily avaialble via Adjtimex in the syscall package (http://golang.org/pkg/syscall/#Adjtimex) and you should probably use that instead.

Note: This package is distributed under a BSD license but the effective license may change based on the license of sys/timex.h and related files on your system.

Index

Constants

View Source
const (
	// Ok means that eveything is okay with no leap second warning.
	Ok = C.TIME_OK
	// Ins indicates a positive leap second warning. An additional leap second will be added after
	// 23:59:59 today.
	Ins = C.TIME_INS
	// Del indicates a negative leap second warning. Skip 23:59:59 at the end of the day.
	Del = C.TIME_DEL
	// Oop is when a leap second is occuring.
	Oop = C.TIME_OOP
	// Wait means that leap second has occured.
	Wait = C.TIME_WAIT
	// Error signifies that the clock is not synchronized.
	Error = C.TIME_ERROR
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Timex

type Timex struct {
	// The status returned by the underlying call to get_ntptimeval().
	Status int
	// The current time according to get_ntptimeval().
	Time time.Time
	// The maximum clock error.
	Maxerror int64
	// The estimated clock error.
	Esterror int64
}

Timex contains the response to a local query of NTP on the local system.

func Now

func Now() (timex *Timex)

Now returns a Timex struct based on a query to get_ntptimeval() via cgo.

func (*Timex) StatusInfo

func (timex *Timex) StatusInfo() string

StatusInfo returns a human-readble version of the status for a Timex.

Directories

Path Synopsis
Show the current time, clock status, maximum and estimated clock errors (in nanoseconds).
Show the current time, clock status, maximum and estimated clock errors (in nanoseconds).

Jump to

Keyboard shortcuts

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