0 | module IotaTime.Instant
2 | import IotaTime.Duration
7 | nanosecondsPerSecond : Integer
8 | nanosecondsPerSecond = 1000000000
10 | secondsPerDay : Integer
11 | secondsPerDay = 86400
13 | unixEpochOffsetDays : Integer
14 | unixEpochOffsetDays = 11017
16 | unixEpochOffsetNanoseconds : Integer
17 | unixEpochOffsetNanoseconds =
18 | unixEpochOffsetDays * secondsPerDay * nanosecondsPerSecond
29 | record Instant where
30 | constructor MkInstant
31 | storedNanoseconds : Integer
35 | left == right = left.storedNanoseconds == right.storedNanoseconds
39 | compare left right = compare left.storedNanoseconds right.storedNanoseconds
43 | show value = "fromNanosecondsSinceEpoch " ++ show value.storedNanoseconds
47 | fromNanosecondsSinceEpoch : Integer -> Instant
48 | fromNanosecondsSinceEpoch = MkInstant
52 | toNanosecondsSinceEpoch : Instant -> Integer
53 | toNanosecondsSinceEpoch = storedNanoseconds
57 | ticks : Instant -> Integer
58 | ticks = toNanosecondsSinceEpoch
62 | instantNanosecondsRoundTrip : (value : Integer) ->
63 | toNanosecondsSinceEpoch (fromNanosecondsSinceEpoch value) = value
64 | instantNanosecondsRoundTrip value = Refl
68 | instantRoundTrip : (value : Instant) ->
69 | fromNanosecondsSinceEpoch (toNanosecondsSinceEpoch value) = value
70 | instantRoundTrip (MkInstant value) = Refl
74 | fromSecondsSinceUnixEpoch : Integer -> Instant
75 | fromSecondsSinceUnixEpoch seconds =
76 | MkInstant (seconds * nanosecondsPerSecond - unixEpochOffsetNanoseconds)
80 | fromNanosecondsSinceUnixEpoch : Integer -> Instant
81 | fromNanosecondsSinceUnixEpoch value =
82 | MkInstant (value - unixEpochOffsetNanoseconds)
86 | toNanosecondsSinceUnixEpoch : Instant -> Integer
87 | toNanosecondsSinceUnixEpoch value =
88 | value.storedNanoseconds + unixEpochOffsetNanoseconds
92 | toSecondsSinceUnixEpoch : Instant -> Integer
93 | toSecondsSinceUnixEpoch value =
94 | toNanosecondsSinceUnixEpoch value `div` nanosecondsPerSecond
98 | addDuration : Instant -> Duration -> Instant
99 | addDuration instant duration = MkInstant
100 | (instant.storedNanoseconds +
101 | toDurationNanoseconds duration)
104 | add : Instant -> Duration -> Instant
109 | subtractDuration : Instant -> Duration -> Instant
110 | subtractDuration instant duration = MkInstant
111 | (instant.storedNanoseconds -
112 | toDurationNanoseconds duration)
115 | minus : Instant -> Duration -> Instant
116 | minus = subtractDuration
120 | difference : Instant -> Instant -> Duration
121 | difference left right = durationFromNanoseconds
122 | (left.storedNanoseconds - right.storedNanoseconds)
128 | current <- clockTime UTC
129 | pure (fromNanosecondsSinceUnixEpoch (toNano current))
134 | epoch = MkInstant 0