Skip to content

Commit aa1450b

Browse files
authored
gleam/os add timestamp function (#117)
1 parent 33fd0fb commit aa1450b

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- The `result` module gains the `all` function.
1010
- The `dynamic` module gains the `option` function.
1111
- The `uri` module gains the `percent_encode` and `percent_decode` functions.
12+
- The `os` module gains the `erlang_timestamp` function.
1213

1314
## v0.11.0 - 2020-08-22
1415

src/gleam/os.gleam

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ pub type TimeUnit {
5858
/// https://erlang.org/doc/apps/erts/time_correction.html#OS_System_Time
5959
pub external fn system_time(TimeUnit) -> Int =
6060
"os" "system_time"
61+
62+
/// Return the current OS system time as a tuple of Ints
63+
///
64+
/// http://erlang.org/doc/man/os.html#timestamp-0
65+
pub external fn erlang_timestamp() -> tuple(Int, Int, Int) =
66+
"os" "timestamp"

test/gleam/os_test.gleam

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ pub fn system_time_test() {
2626
{ os.system_time(os.Millisecond) < june_12_2020 * 1000000 }
2727
|> should.equal(True)
2828
}
29+
30+
pub fn erlang_timestamp_test() {
31+
// in microseconds
32+
let june_12_2020 = 1591966971000000
33+
let tuple(mega_seconds, seconds, micro_seconds) = os.erlang_timestamp()
34+
35+
let stamp_as_micro =
36+
{ mega_seconds * 1_000_000 + seconds } * 1_000_000 + micro_seconds
37+
38+
{ stamp_as_micro > june_12_2020 }
39+
|> should.be_true()
40+
}

0 commit comments

Comments
 (0)