diff options
author | Lucas Rosa <lrosa008@gmail.com> | 2020-11-03 05:20:11 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-03 10:20:11 +0000 |
commit | aa1450bda6666dc22412e937deccffebaf82733c (patch) | |
tree | aced4858ff1a541ec04f422af30911c9b381d6b0 | |
parent | 33fd0fb3130bf1f11947385c42e8bfadb3c753e1 (diff) | |
download | gleam_stdlib-aa1450bda6666dc22412e937deccffebaf82733c.tar.gz gleam_stdlib-aa1450bda6666dc22412e937deccffebaf82733c.zip |
gleam/os add timestamp function (#117)
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/os.gleam | 6 | ||||
-rw-r--r-- | test/gleam/os_test.gleam | 12 |
3 files changed, 19 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 06389e9..3ea63f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - The `result` module gains the `all` function. - The `dynamic` module gains the `option` function. - The `uri` module gains the `percent_encode` and `percent_decode` functions. +- The `os` module gains the `erlang_timestamp` function. ## v0.11.0 - 2020-08-22 diff --git a/src/gleam/os.gleam b/src/gleam/os.gleam index 0c3f38a..0623dd5 100644 --- a/src/gleam/os.gleam +++ b/src/gleam/os.gleam @@ -58,3 +58,9 @@ pub type TimeUnit { /// https://erlang.org/doc/apps/erts/time_correction.html#OS_System_Time pub external fn system_time(TimeUnit) -> Int = "os" "system_time" + +/// Return the current OS system time as a tuple of Ints +/// +/// http://erlang.org/doc/man/os.html#timestamp-0 +pub external fn erlang_timestamp() -> tuple(Int, Int, Int) = + "os" "timestamp" diff --git a/test/gleam/os_test.gleam b/test/gleam/os_test.gleam index 45fc0ef..815a9e0 100644 --- a/test/gleam/os_test.gleam +++ b/test/gleam/os_test.gleam @@ -26,3 +26,15 @@ pub fn system_time_test() { { os.system_time(os.Millisecond) < june_12_2020 * 1000000 } |> should.equal(True) } + +pub fn erlang_timestamp_test() { + // in microseconds + let june_12_2020 = 1591966971000000 + let tuple(mega_seconds, seconds, micro_seconds) = os.erlang_timestamp() + + let stamp_as_micro = + { mega_seconds * 1_000_000 + seconds } * 1_000_000 + micro_seconds + + { stamp_as_micro > june_12_2020 } + |> should.be_true() +} |