aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gleam/os.gleam13
-rw-r--r--test/gleam/os_test.gleam13
2 files changed, 26 insertions, 0 deletions
diff --git a/src/gleam/os.gleam b/src/gleam/os.gleam
index 8ae522d..0c3f38a 100644
--- a/src/gleam/os.gleam
+++ b/src/gleam/os.gleam
@@ -45,3 +45,16 @@ pub fn delete_env(key: String) -> Nil {
os_unsetenv(string_to_char_list(key))
Nil
}
+
+pub type TimeUnit {
+ Second
+ Millisecond
+ Microsecond
+ Nanosecond
+}
+
+/// Return the current OS system time.
+///
+/// https://erlang.org/doc/apps/erts/time_correction.html#OS_System_Time
+pub external fn system_time(TimeUnit) -> Int =
+ "os" "system_time"
diff --git a/test/gleam/os_test.gleam b/test/gleam/os_test.gleam
index f248d2b..8a0b5f1 100644
--- a/test/gleam/os_test.gleam
+++ b/test/gleam/os_test.gleam
@@ -1,5 +1,6 @@
import gleam/map
import gleam/os
+import gleam/io
import gleam/should
pub fn env_test() {
@@ -13,3 +14,15 @@ pub fn env_test() {
|> map.get("GLEAM_TEST")
|> should.equal(Error(Nil))
}
+
+pub fn system_time_test() {
+ let june_12_2020 = 1591966971
+ os.system_time(os.Second) > june_12_2020
+ |> should.equal(True)
+ os.system_time(os.Second) < june_12_2020 * 1000
+ |> should.equal(True)
+ os.system_time(os.Millisecond) > june_12_2020 * 1000
+ |> should.equal(True)
+ os.system_time(os.Millisecond) < june_12_2020 * 1000000
+ |> should.equal(True)
+}