diff --git a/assets/core_team.nu b/assets/core_team.nu new file mode 100644 index 0000000..ed623d9 --- /dev/null +++ b/assets/core_team.nu @@ -0,0 +1,12 @@ +def "nu core-team" [] { + [ + [ 'name', 'tz']; + [ 'andres', 'America/Guayaquil'] + [ 'fdncred', 'US/Central'] + [ 'gedge', 'US/Eastern'] + [ 'jt', 'NZ'] + [ 'wycats', 'US/Pacific'] + [ 'kubouch', 'Europe/Helsinki'] + ['elferherrera', 'Europe/London'] + ] +} diff --git a/examples/date_in_local_timezones.nu b/examples/date_in_local_timezones.nu new file mode 100644 index 0000000..2b8eb2c --- /dev/null +++ b/examples/date_in_local_timezones.nu @@ -0,0 +1,25 @@ +source lib/scripts.nu +source assets/core_team.nu + +let next_call = ("2021-08-31 15:00:21.290597200 -05:00" | str to-datetime); +let people = (nu core-team | date local $next_call); + +def say [block] { + each {|person| + do $block ( + $person | merge { + [[name]; [($person.name | str capitalize)]] + } + ) + } | str collect (char newline) +} + +$people | say {|person| $"($person.name)'s timezone is ($person.tz)"} + +$" + +for the next call happening on ($next_call | date format '%c').. in their timezones they would be: + +" + +$people | say {|person| $"($person.name)'s: ($person.time)"} diff --git a/language/playground.nu b/language/playground.nu new file mode 100644 index 0000000..656bc73 --- /dev/null +++ b/language/playground.nu @@ -0,0 +1 @@ +source language/playground/lib.nu \ No newline at end of file diff --git a/language/playground/lib.nu b/language/playground/lib.nu new file mode 100644 index 0000000..3b6f944 --- /dev/null +++ b/language/playground/lib.nu @@ -0,0 +1,55 @@ +def playground [topic, block] { + with-env [N 5 REJECT slow] { + echo $topic " tests" (char newline) | str collect + + do $block + } +} + +def scene [ + topic: any + --tag: string + block: block +] { + $" ($topic)(char newline)" + do $block +} + +def play [ + topic: any + --tag: string + block: block +] { + let title = $topic; + + let is_tag_empty = ($tag | empty?); + let should_run_all = ($nu.env | default RUN_ALL $false | get RUN_ALL); + + if $is_tag_empty { + do $block + } { + if $tag == $nu.env.REJECT && $should_run_all { + $" ($topic) ... (ansi yellow)skipped(ansi reset) (char newline)" + } { do $block } + } +} + +def expect [ + actual: any + --to-be: any +] { + let are_equal = ($actual | zip { $to-be } | pivot header_names values | each {|case| + let values = $case.values; + + $values.0 == $values.1 + } + | all? $it) && (($actual | get | length) == ($to-be | get | length)); + + let line = (if $true == $are_equal { + $"(ansi green)ok(ansi reset)(char newline)" + } { + $"(ansi red)failed(ansi reset)(char newline)" + }); + + $" ($title) ... ($line)" +} \ No newline at end of file diff --git a/language/std.nu b/language/std.nu new file mode 100644 index 0000000..4f3cd15 --- /dev/null +++ b/language/std.nu @@ -0,0 +1 @@ +source language/std/date.nu diff --git a/language/std/date.nu b/language/std/date.nu new file mode 100644 index 0000000..380997e --- /dev/null +++ b/language/std/date.nu @@ -0,0 +1,7 @@ +def "date local" [now] { + insert time {|value| + let converted = ($now | date to-timezone $value.tz); + + $converted | date format '%c' + } +} diff --git a/lib/scripts.nu b/lib/scripts.nu new file mode 100644 index 0000000..1d49ee5 --- /dev/null +++ b/lib/scripts.nu @@ -0,0 +1 @@ +source language/std.nu diff --git a/maintainer_time.nu b/maintainer_time.nu deleted file mode 100644 index 22bc65f..0000000 --- a/maintainer_time.nu +++ /dev/null @@ -1,18 +0,0 @@ -let m_table = ( - [ - ['name', 'tz', 'time']; - ['andres' 'America/Guayaquil' ' '] - ['fdncred' 'US/Central' ' '] - ['gedge' 'US/Eastern' ' '] - ['jt' 'NZ' ' '] - ['wycats' 'US/Pacific' ' '] - ['kubouch' 'Europe/Helsinki' ' '] - ['elferherrera' 'Europe/London' ' '] - ] -) -let now = (date now) -$m_table | update time { - each { |name| - $now | date to-timezone ($name | get tz) | date format '%c' - } -} diff --git a/tests/language/std/date.nu b/tests/language/std/date.nu new file mode 100644 index 0000000..cb8e62b --- /dev/null +++ b/tests/language/std/date.nu @@ -0,0 +1,38 @@ +source lib/scripts.nu + +def mock-now [] { + "2021-08-29 03:31:21.290597200 -05:00" | str to-datetime +} + +def people [] { + [ + [ 'name', 'tz']; + [ 'andres', 'America/Guayaquil'] + [ 'fdncred', 'US/Central'] + ] +} + +playground "std/date" { + + scene 'command: "date local"' { + + play "adds times in local timezone" { + + let expected_times = [ + [time]; + ["Sun Aug 29 03:31:21 2021"] + ["Sun Aug 29 03:31:21 2021"] + ]; + + let actual = (people | + date local (mock-now) | + select time + ); + + expect $actual --to-be $expected_times + + } + + } + +} diff --git a/tests/main.nu b/tests/main.nu new file mode 100644 index 0000000..346b35f --- /dev/null +++ b/tests/main.nu @@ -0,0 +1,2 @@ +source language/playground.nu +source tests/language/std/date.nu