Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
galene
Commits
8fb8f7b7
Commit
8fb8f7b7
authored
May 02, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement NTP time.
parent
303d1553
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
mono/mono.go
mono/mono.go
+22
-0
mono/mono_test.go
mono/mono_test.go
+38
-0
No files found.
mono/mono.go
View file @
8fb8f7b7
...
@@ -10,6 +10,10 @@ func fromDuration(d time.Duration, hz uint32) uint64 {
...
@@ -10,6 +10,10 @@ func fromDuration(d time.Duration, hz uint32) uint64 {
return
uint64
(
d
)
*
uint64
(
hz
)
/
uint64
(
time
.
Second
)
return
uint64
(
d
)
*
uint64
(
hz
)
/
uint64
(
time
.
Second
)
}
}
func
toDuration
(
tm
uint64
,
hz
uint32
)
time
.
Duration
{
return
time
.
Duration
(
tm
*
uint64
(
time
.
Second
)
/
uint64
(
hz
))
}
func
Now
(
hz
uint32
)
uint64
{
func
Now
(
hz
uint32
)
uint64
{
return
fromDuration
(
time
.
Since
(
epoch
),
hz
)
return
fromDuration
(
time
.
Since
(
epoch
),
hz
)
}
}
...
@@ -17,3 +21,21 @@ func Now(hz uint32) uint64 {
...
@@ -17,3 +21,21 @@ func Now(hz uint32) uint64 {
func
Microseconds
()
uint64
{
func
Microseconds
()
uint64
{
return
Now
(
1000000
)
return
Now
(
1000000
)
}
}
var
ntpEpoch
=
time
.
Date
(
1900
,
1
,
1
,
0
,
0
,
0
,
0
,
time
.
UTC
)
func
NTPToTime
(
ntp
uint64
)
time
.
Time
{
sec
:=
uint32
(
ntp
>>
32
)
frac
:=
uint32
(
ntp
&
0xFFFFFFFF
)
return
ntpEpoch
.
Add
(
time
.
Duration
(
sec
)
*
time
.
Second
+
((
time
.
Duration
(
frac
)
*
time
.
Second
)
>>
32
),
)
}
func
TimeToNTP
(
tm
time
.
Time
)
uint64
{
d
:=
tm
.
Sub
(
ntpEpoch
)
sec
:=
uint32
(
d
/
time
.
Second
)
frac
:=
uint32
(
d
%
time
.
Second
)
return
(
uint64
(
sec
)
<<
32
)
+
(
uint64
(
frac
)
<<
32
)
/
uint64
(
time
.
Second
)
}
mono/mono_test.go
View file @
8fb8f7b7
...
@@ -5,6 +5,18 @@ import (
...
@@ -5,6 +5,18 @@ import (
"time"
"time"
)
)
func
TestDuration
(
t
*
testing
.
T
)
{
a
:=
fromDuration
(
time
.
Second
,
48000
)
if
a
!=
48000
{
t
.
Errorf
(
"Expected 48000, got %v"
,
a
)
}
b
:=
toDuration
(
48000
,
48000
)
if
b
!=
time
.
Second
{
t
.
Errorf
(
"Expected %v, got %v"
,
time
.
Second
,
b
)
}
}
func
differs
(
a
,
b
,
delta
uint64
)
bool
{
func
differs
(
a
,
b
,
delta
uint64
)
bool
{
if
a
<
b
{
if
a
<
b
{
a
,
b
=
b
,
a
a
,
b
=
b
,
a
...
@@ -27,3 +39,29 @@ func TestMono(t *testing.T) {
...
@@ -27,3 +39,29 @@ func TestMono(t *testing.T) {
t
.
Errorf
(
"Expected %v, got %v"
,
4000
,
d
)
t
.
Errorf
(
"Expected %v, got %v"
,
4000
,
d
)
}
}
}
}
func
TestNTP
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
ntp
:=
TimeToNTP
(
now
)
now2
:=
NTPToTime
(
ntp
)
ntp2
:=
TimeToNTP
(
now2
)
diff1
:=
now2
.
Sub
(
now
)
if
diff1
<
0
{
diff1
=
-
diff1
}
if
diff1
>
time
.
Nanosecond
{
t
.
Errorf
(
"Expected %v, got %v (diff=%v)"
,
now
,
now2
,
diff1
)
}
diff2
:=
int64
(
ntp2
-
ntp
)
if
diff2
<
0
{
diff2
=
-
diff2
}
if
diff2
>
(
1
<<
8
)
{
t
.
Errorf
(
"Expected %v, got %v (diff=%v)"
,
ntp
,
ntp2
,
float64
(
diff2
)
/
float64
(
1
<<
32
))
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment