aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/sys/unix/timestruct.go
blob: 139fbbebbbda72a02fffef1d5ec65894dd27a095 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package unix

// TimespecToNsec converts a Timespec value into a number of
// nanoseconds since the Unix epoch.
func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }

// NsecToTimespec takes a number of nanoseconds since the Unix epoch
// and returns the corresponding Timespec value.
func NsecToTimespec(nsec int64) Timespec {
    sec := nsec / 1e9
    nsec = nsec % 1e9
    if nsec < 0 {
        nsec += 1e9
        sec--
    }
    return setTimespec(sec, nsec)
}

// TimevalToNsec converts a Timeval value into a number of nanoseconds
// since the Unix epoch.
func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 }

// NsecToTimeval takes a number of nanoseconds since the Unix epoch
// and returns the corresponding Timeval value.
func NsecToTimeval(nsec int64) Timeval {
    nsec += 999 // round up to microsecond
    usec := nsec % 1e9 / 1e3
    sec := nsec / 1e9
    if usec < 0 {
        usec += 1e6
        sec--
    }
    return setTimeval(sec, usec)
}

// Unix returns ts as the number of seconds and nanoseconds elapsed since the
// Unix epoch.
func (ts *Timespec) Unix() (sec int64, nsec int64) {
    return int64(ts.Sec), int64(ts.Nsec)
}

// Unix returns tv as the number of seconds and nanoseconds elapsed since the
// Unix epoch.
func (tv *Timeval) Unix() (sec int64, nsec int64) {
    return int64(tv.Sec), int64(tv.Usec) * 1000
}

// Nano returns ts as the number of nanoseconds elapsed since the Unix epoch.
func (ts *Timespec) Nano() int64 {
    return int64(ts.Sec)*1e9 + int64(ts.Nsec)
}

// Nano returns tv as the number of nanoseconds elapsed since the Unix epoch.
func (tv *Timeval) Nano() int64 {
    return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
}