-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtimestamp.go
44 lines (37 loc) · 1.04 KB
/
timestamp.go
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
package twamp
import (
"time"
)
type TwampTimestamp struct {
Integer uint32
Fraction uint32
}
// offset from UNIX epoch to NTP epoch
const epochOffset = time.Duration(0x83aa7e80 * time.Second)
/*
Converts a UNIX epoch time time.Time object into an RFC 1305 compliant time.
*/
func NewTwampTimestamp(t time.Time) *TwampTimestamp {
// convert epoch from 1970 to 1900 per RFC 1305
t = t.Add(epochOffset)
return &TwampTimestamp{
Integer: uint32(t.Unix()),
Fraction: uint32(t.Nanosecond()),
}
}
func NewTimestamp(twampTimestamp TwampTimestamp) time.Time {
t := time.Unix(int64(twampTimestamp.Integer), int64(twampTimestamp.Fraction))
// convert epoch from 1970 to 1900 per RFC 1305
t = t.Add(-1 * epochOffset)
return t
}
/*
Return a time.Time object representing Unix Epoch time since January 1st, 1970.
*/
func (t *TwampTimestamp) GetTime() time.Time {
// convert epoch from 1900 back to 1970
return time.Unix(int64(t.Integer), int64(t.Fraction)).Add(-1 * epochOffset)
}
func (t *TwampTimestamp) String() string {
return t.GetTime().String()
}