Skip to content

Commit 7d4e7a7

Browse files
committed
add Nanoseconds unit to time.Time, this will make it easier to support
`gleam_time` accurately
1 parent 05ae0d0 commit 7d4e7a7

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

src/humanise/time.gleam

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ import gleam/bool
1212

1313
import util
1414

15-
const millisecond = 1000.0
15+
const microsecond = 1000.0
1616

17-
const second = 1_000_000.0
17+
const millisecond = 1_000_000.0
1818

19-
const minute = 60_000_000.0
19+
const second = 1_000_000_000.0
2020

21-
const hour = 3_600_000_000.0
21+
const minute = 60_000_000_000.0
2222

23-
const day = 86_400_000_000.0
23+
const hour = 3_600_000_000_000.0
2424

25-
const week = 604_800_000_000.0
25+
const day = 86_400_000_000_000.0
26+
27+
const week = 604_800_000_000_000.0
2628

2729
/// The main type for holding time information.
2830
///
2931
/// Use its constructors directly to specify a unit for the value you want to format.
3032
pub type Time {
33+
Nanoseconds(Float)
3134
Microseconds(Float)
3235
Milliseconds(Float)
3336
Seconds(Float)
@@ -37,15 +40,16 @@ pub type Time {
3740
Weeks(Float)
3841
}
3942

40-
/// Convert a value to microseconds.
43+
/// Convert a value to nanoseconds.
4144
///
4245
/// Example:
4346
/// ```
44-
/// time.Milliseconds(1.0) |> time.as_microseconds // 1000.0
47+
/// time.Microseconds(1.0) |> time.as_nanoseconds // 1000.0
4548
/// ```
46-
pub fn as_microseconds(this time: Time) -> Float {
49+
pub fn as_nanoseconds(this time: Time) -> Float {
4750
case time {
48-
Microseconds(n) -> n
51+
Nanoseconds(n) -> n
52+
Microseconds(n) -> n *. microsecond
4953
Milliseconds(n) -> n *. millisecond
5054
Seconds(n) -> n *. second
5155
Minutes(n) -> n *. minute
@@ -55,14 +59,24 @@ pub fn as_microseconds(this time: Time) -> Float {
5559
}
5660
}
5761

62+
/// Convert a value to microseconds.
63+
///
64+
/// Example:
65+
/// ```
66+
/// time.Nanoseconds(1000.0) |> time.as_microseconds // 1.0
67+
/// ```
68+
pub fn as_microseconds(this time: Time) -> Float {
69+
as_nanoseconds(time) /. microsecond
70+
}
71+
5872
/// Convert a value to milliseconds.
5973
///
6074
/// Example:
6175
/// ```
6276
/// time.Microseconds(1000.0) |> time.as_milliseconds // 1.0
6377
/// ```
6478
pub fn as_milliseconds(this time: Time) -> Float {
65-
as_microseconds(time) /. millisecond
79+
as_nanoseconds(time) /. millisecond
6680
}
6781

6882
/// Convert a value to seconds.
@@ -72,7 +86,7 @@ pub fn as_milliseconds(this time: Time) -> Float {
7286
/// time.Milliseconds(1000.0) |> time.as_seconds // 1.0
7387
/// ```
7488
pub fn as_seconds(this time: Time) -> Float {
75-
as_microseconds(time) /. second
89+
as_nanoseconds(time) /. second
7690
}
7791

7892
/// Convert a value to minutes.
@@ -82,7 +96,7 @@ pub fn as_seconds(this time: Time) -> Float {
8296
/// time.Seconds(60.0) |> time.as_minutes // 1.0
8397
/// ```
8498
pub fn as_minutes(this time: Time) -> Float {
85-
as_microseconds(time) /. minute
99+
as_nanoseconds(time) /. minute
86100
}
87101

88102
/// Convert a value to hours.
@@ -92,7 +106,7 @@ pub fn as_minutes(this time: Time) -> Float {
92106
/// time.Minutes(60.0) |> time.as_hours // 1.0
93107
/// ```
94108
pub fn as_hours(this time: Time) -> Float {
95-
as_microseconds(time) /. hour
109+
as_nanoseconds(time) /. hour
96110
}
97111

98112
/// Convert a value to days.
@@ -102,7 +116,7 @@ pub fn as_hours(this time: Time) -> Float {
102116
/// time.Hours(24.0) |> time.as_days // 1.0
103117
/// ```
104118
pub fn as_days(this time: Time) -> Float {
105-
as_microseconds(time) /. day
119+
as_nanoseconds(time) /. day
106120
}
107121

108122
/// Convert a value to weeks.
@@ -112,7 +126,7 @@ pub fn as_days(this time: Time) -> Float {
112126
/// time.Days(7.0) |> time.as_weeks // 1.0
113127
/// ```
114128
pub fn as_weeks(this time: Time) -> Float {
115-
as_microseconds(time) /. week
129+
as_nanoseconds(time) /. week
116130
}
117131

118132
/// Convert a value to a more optimal unit, if possible.
@@ -122,16 +136,20 @@ pub fn as_weeks(this time: Time) -> Float {
122136
/// time.Seconds(120.0) |> time.humanise // time.Minutes(2.0)
123137
/// ```
124138
pub fn humanise(this time: Time) -> Time {
125-
let us = as_microseconds(time)
126-
127-
use <- bool.guard(when: us <. millisecond, return: Microseconds(us))
128-
use <- bool.guard(when: us <. second, return: Milliseconds(us /. millisecond))
129-
use <- bool.guard(when: us <. minute, return: Seconds(us /. second))
130-
use <- bool.guard(when: us <. hour, return: Minutes(us /. minute))
131-
use <- bool.guard(when: us <. day, return: Hours(us /. hour))
132-
use <- bool.guard(when: us <. week, return: Days(us /. day))
133-
134-
Weeks(us /. week)
139+
let ns = as_nanoseconds(time)
140+
141+
use <- bool.guard(when: ns <. microsecond, return: Nanoseconds(ns))
142+
use <- bool.guard(
143+
when: ns <. millisecond,
144+
return: Microseconds(ns /. microsecond),
145+
)
146+
use <- bool.guard(when: ns <. second, return: Milliseconds(ns /. millisecond))
147+
use <- bool.guard(when: ns <. minute, return: Seconds(ns /. second))
148+
use <- bool.guard(when: ns <. hour, return: Minutes(ns /. minute))
149+
use <- bool.guard(when: ns <. day, return: Hours(ns /. hour))
150+
use <- bool.guard(when: ns <. week, return: Days(ns /. day))
151+
152+
Weeks(ns /. week)
135153
}
136154

137155
/// Format a value as a `String`, rounded to at most 2 decimal places, followed by a unit suffix.
@@ -142,6 +160,7 @@ pub fn humanise(this time: Time) -> Time {
142160
/// ```
143161
pub fn to_string(this time: Time) -> String {
144162
let #(n, suffix) = case time {
163+
Nanoseconds(ns) -> #(ns, "ns")
145164
Microseconds(us) -> #(us, "us")
146165
Milliseconds(ms) -> #(ms, "ms")
147166
Seconds(s) -> #(s, "s")

test/humanise_test.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn time_humanise_test() {
5050

5151
time.Seconds(0.0)
5252
|> time.humanise
53-
|> should.equal(time.Microseconds(0.0))
53+
|> should.equal(time.Nanoseconds(0.0))
5454

5555
time.Minutes(120.0)
5656
|> time.humanise

0 commit comments

Comments
 (0)