Browse Source

Add helper for absolute time "Never".

Bernd Fix 3 months ago
parent
commit
9cbf4e0cd9
1 changed files with 17 additions and 0 deletions
  1. 17
    0
      src/gnunet/util/time.go

+ 17
- 0
src/gnunet/util/time.go View File

@@ -9,10 +9,14 @@ import (
9 9
 // Absolute time
10 10
 //----------------------------------------------------------------------
11 11
 
12
+// AbsoluteTime refers to a unique point in time.
13
+// The value is the elapsed time in milliseconds (Unix epoch), so no timestamp
14
+// before January 1st, 1970 is possible (not a restriction for GNUnet).
12 15
 type AbsoluteTime struct {
13 16
 	Val uint64 `order:"big"`
14 17
 }
15 18
 
19
+// NewAbsoluteTime set the point in time to the given time value
16 20
 func NewAbsoluteTime(t time.Time) AbsoluteTime {
17 21
 	secs := t.Unix()
18 22
 	usecs := t.Nanosecond() / 1000
@@ -21,10 +25,17 @@ func NewAbsoluteTime(t time.Time) AbsoluteTime {
21 25
 	}
22 26
 }
23 27
 
28
+// AbsoluteTimeNow returns the current point in time.
24 29
 func AbsoluteTimeNow() AbsoluteTime {
25 30
 	return NewAbsoluteTime(time.Now())
26 31
 }
27 32
 
33
+// AbsoluteTimeNever returns the time defined as "never"
34
+func AbsoluteTimeNever() AbsoluteTime {
35
+	return AbsoluteTime{math.MaxUint64}
36
+}
37
+
38
+// String returns a human-readable notation of an absolute time.
28 39
 func (t AbsoluteTime) String() string {
29 40
 	if t.Val == math.MaxUint64 {
30 41
 		return "Never"
@@ -33,12 +44,14 @@ func (t AbsoluteTime) String() string {
33 44
 	return ts.Format(time.RFC3339Nano)
34 45
 }
35 46
 
47
+// Add a duration to an absolute time yielding a new absolute time.
36 48
 func (t AbsoluteTime) Add(d time.Duration) AbsoluteTime {
37 49
 	return AbsoluteTime{
38 50
 		Val: t.Val + uint64(d.Milliseconds()),
39 51
 	}
40 52
 }
41 53
 
54
+// Expired returns true if the timestamp is in the past.
42 55
 func (t AbsoluteTime) Expired() bool {
43 56
 	// check for "never"
44 57
 	if t.Val == math.MaxUint64 {
@@ -51,16 +64,20 @@ func (t AbsoluteTime) Expired() bool {
51 64
 // Relative time
52 65
 //----------------------------------------------------------------------
53 66
 
67
+// Relative time is a timestamp defined relative to the current time.
68
+// It actually is more like a duration than a time...
54 69
 type RelativeTime struct {
55 70
 	Val uint64 `order:"big"`
56 71
 }
57 72
 
73
+// NewRelativeTime is initialized with a given duration.
58 74
 func NewRelativeTime(d time.Duration) RelativeTime {
59 75
 	return RelativeTime{
60 76
 		Val: uint64(d.Milliseconds()),
61 77
 	}
62 78
 }
63 79
 
80
+// String returns a human-readble representation of a relative time (duration).
64 81
 func (t RelativeTime) String() string {
65 82
 	if t.Val == math.MaxUint64 {
66 83
 		return "Forever"

Loading…
Cancel
Save