-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathduration.go
More file actions
39 lines (32 loc) · 768 Bytes
/
duration.go
File metadata and controls
39 lines (32 loc) · 768 Bytes
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
package opencypher
import (
"fmt"
"time"
)
// Duration represents a duration in opencypher. Copied from neo4j driver.
type Duration struct {
Months int64
Days int64
Seconds int64
Nanos int
}
func (d Duration) String() string {
sign := ""
if d.Seconds < 0 && d.Nanos > 0 {
d.Seconds++
d.Nanos = int(time.Second) - d.Nanos
if d.Seconds == 0 {
sign = "-"
}
}
timePart := ""
if d.Nanos == 0 {
timePart = fmt.Sprintf("%s%d", sign, d.Seconds)
} else {
timePart = fmt.Sprintf("%s%d.%09d", sign, d.Seconds, d.Nanos)
}
return fmt.Sprintf("P%dM%dDT%sS", d.Months, d.Days, timePart)
}
func (d1 Duration) Equal(d2 Duration) bool {
return d1.Months == d2.Months && d1.Days == d2.Days && d1.Seconds == d2.Seconds && d1.Nanos == d2.Nanos
}