From cf3090e7921d5cd2d69ffe83f59b28bca8104f4c Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 24 Jul 2024 15:39:58 +0200 Subject: [PATCH 1/8] feat(std): add date function --- src/std/date.ab | 103 +++++++++++++++++++++++ src/tests/stdlib/date.ab | 7 ++ src/tests/stdlib/date.output.txt | 1 + src/tests/stdlib/date_add.ab | 7 ++ src/tests/stdlib/date_add.output.txt | 1 + src/tests/stdlib/date_compare.ab | 5 ++ src/tests/stdlib/date_compare.output.txt | 1 + src/tests/stdlib/now.ab | 5 ++ src/tests/stdlib/now.output.txt | 1 + 9 files changed, 131 insertions(+) create mode 100644 src/std/date.ab create mode 100644 src/tests/stdlib/date.ab create mode 100644 src/tests/stdlib/date.output.txt create mode 100644 src/tests/stdlib/date_add.ab create mode 100644 src/tests/stdlib/date_add.output.txt create mode 100644 src/tests/stdlib/date_compare.ab create mode 100644 src/tests/stdlib/date_compare.output.txt create mode 100644 src/tests/stdlib/now.ab create mode 100644 src/tests/stdlib/now.output.txt diff --git a/src/std/date.ab b/src/std/date.ab new file mode 100644 index 00000000..2923115e --- /dev/null +++ b/src/std/date.ab @@ -0,0 +1,103 @@ +/// format a date with a special format +/// if no date is specified, the current date is used +/// if no format is specified, "%FT%T%Z" format is used +/// for more info about format type "man date" on your shell or go to https://www.gnu.org/software/coreutils/date +/// format : +/// %% a literal % +/// %a locale's abbreviated weekday name (e.g., Sun) +/// %A locale's full weekday name (e.g., Sunday) +/// %b locale's abbreviated month name (e.g., Jan) +/// %B locale's full month name (e.g., January) +/// %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) +/// %C century; like %Y, except omit last two digits (e.g., 20) +/// %d day of month (e.g., 01) +/// %D date; same as %m/%d/%y +/// %e day of month, space padded; same as %_d +/// %F full date; like %+4Y-%m-%d +/// %g last two digits of year of ISO week number (see %G) +/// %G year of ISO week number (see %V); normally useful only with %V +/// %h same as %b +/// %H hour (00..23) +/// %I hour (01..12) +/// %j day of year (001..366) +/// %k hour, space padded ( 0..23); same as %_H +/// %l hour, space padded ( 1..12); same as %_I +/// %m month (01..12) +/// %M minute (00..59) +/// %n a newline +/// %N nanoseconds (000000000..999999999) +/// %p locale's equivalent of either AM or PM; blank if not known +/// %P like %p, but lower case +/// %q quarter of year (1..4) +/// %r locale's 12-hour clock time (e.g., 11:11:04 PM) +/// %R 24-hour hour and minute; same as %H:%M +/// %s seconds since the Epoch (1970-01-01 00:00 UTC) +/// %S second (00..60) +/// %t a tab +/// %T time; same as %H:%M:%S +/// %u day of week (1..7); 1 is Monday +/// %U week number of year, with Sunday as first day of week (00..53) +/// %V ISO week number, with Monday as first day of week (01..53) +/// %w day of week (0..6); 0 is Sunday +/// %W week number of year, with Monday as first day of week (00..53) +/// %x locale's date representation (e.g., 12/31/99) +/// %X locale's time representation (e.g., 23:13:48) +/// %y last two digits of year (00..99) +/// %Y year +/// %z +hhmm numeric time zone (e.g., -0400) +/// %:z +hh:mm numeric time zone (e.g., -04:00) +/// %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) +/// %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) +/// %Z alphabetic time zone abbreviation (e.g., EDT) +/// +/// By default, date pads numeric fields with zeroes. The following +/// optional flags may follow '%': +/// +/// - (hyphen) do not pad the field +/// _ (underscore) pad with spaces +/// 0 (zero) pad with zeros +/// + pad with zeros, and put '+' before future years with >4 digits +/// ^ use upper case if possible +/// # use opposite case if possible +pub fun date(format: Text = "", date: Text = ""): Text { + if format == "": format = "%FT%T%Z" + if date == "": date = unsafe $date +"%FT%T%Z"$ + return $date -d "{date}" +"{format}"$? +} + +/// return current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)) +#[allow_absurd_cast] +pub fun now(): Num { + return unsafe $date +%s$ as Num +} + +/// add value to date. +/// if no date is specified, the current date is used +/// ex : date_add("+3 days") +/// you can use : +/// (+/-) +/// years +/// months +/// days +/// hours +/// minutes +/// seconds +pub fun date_add(add:Text, date:Text = ""): Text { + if date == "": date = unsafe $date +"%FT%T%Z"$ + return date("", "{date("%F", date)?} {add} {date("%T", date)?}")? +} + +// compare 2 date +// return 1 if date_a is after date_b +// return 0 if date_a and date_b is the same +// return -1 if date_b is after date_a +// if date_b is not provided, current date will be used +#[allow_absurd_cast] +pub fun date_compare(date_a: Text, date_b: Text = ""): Num { + if date_b == "": date_b = unsafe date() + let timestamp_a = date("%s", date_a)? as Num + let timestamp_b = date("%s", date_b)? as Num + if timestamp_a > timestamp_b: return 1 + if timestamp_a == timestamp_b: return 0 + if timestamp_a < timestamp_b: return -1 +} \ No newline at end of file diff --git a/src/tests/stdlib/date.ab b/src/tests/stdlib/date.ab new file mode 100644 index 00000000..f2352ade --- /dev/null +++ b/src/tests/stdlib/date.ab @@ -0,0 +1,7 @@ +import * from "std/date" + +main { + echo date("", "@1234567890") failed { + echo "error" + } +} \ No newline at end of file diff --git a/src/tests/stdlib/date.output.txt b/src/tests/stdlib/date.output.txt new file mode 100644 index 00000000..01cf2f2f --- /dev/null +++ b/src/tests/stdlib/date.output.txt @@ -0,0 +1 @@ +2009-02-14T00:31:30CET \ No newline at end of file diff --git a/src/tests/stdlib/date_add.ab b/src/tests/stdlib/date_add.ab new file mode 100644 index 00000000..d113ef85 --- /dev/null +++ b/src/tests/stdlib/date_add.ab @@ -0,0 +1,7 @@ +import * from "std/date" + +main { + echo date_add("+17 Days", "2009-02-14T00:31:30CET") failed { + echo "error" + } +} \ No newline at end of file diff --git a/src/tests/stdlib/date_add.output.txt b/src/tests/stdlib/date_add.output.txt new file mode 100644 index 00000000..9bb59de3 --- /dev/null +++ b/src/tests/stdlib/date_add.output.txt @@ -0,0 +1 @@ +2009-03-03T00:31:30CET \ No newline at end of file diff --git a/src/tests/stdlib/date_compare.ab b/src/tests/stdlib/date_compare.ab new file mode 100644 index 00000000..268ae939 --- /dev/null +++ b/src/tests/stdlib/date_compare.ab @@ -0,0 +1,5 @@ +import * from "std/date" + +main { + if (unsafe date_compare("2000-01-01") == -1 and unsafe date_compare("2100-01-01") == 1) : echo "Hello, Amber!" +} \ No newline at end of file diff --git a/src/tests/stdlib/date_compare.output.txt b/src/tests/stdlib/date_compare.output.txt new file mode 100644 index 00000000..335af0d8 --- /dev/null +++ b/src/tests/stdlib/date_compare.output.txt @@ -0,0 +1 @@ +Hello, Amber! \ No newline at end of file diff --git a/src/tests/stdlib/now.ab b/src/tests/stdlib/now.ab new file mode 100644 index 00000000..131cd853 --- /dev/null +++ b/src/tests/stdlib/now.ab @@ -0,0 +1,5 @@ +import * from "std/date" + +main { + if now() == (unsafe $date +%s$ as Num): echo "Hello, Amber!" +} \ No newline at end of file diff --git a/src/tests/stdlib/now.output.txt b/src/tests/stdlib/now.output.txt new file mode 100644 index 00000000..335af0d8 --- /dev/null +++ b/src/tests/stdlib/now.output.txt @@ -0,0 +1 @@ +Hello, Amber! \ No newline at end of file From 39049e100e438e2a5317e22a366ac53eb31a5189 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 24 Jul 2024 16:00:43 +0200 Subject: [PATCH 2/8] feat(std): add utc support to date function --- src/std/date.ab | 20 ++++++++++++-------- src/tests/stdlib/date.ab | 2 +- src/tests/stdlib/date.output.txt | 2 +- src/tests/stdlib/date_add.ab | 2 +- src/tests/stdlib/date_add.output.txt | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/std/date.ab b/src/std/date.ab index 2923115e..88557435 100644 --- a/src/std/date.ab +++ b/src/std/date.ab @@ -59,10 +59,14 @@ /// + pad with zeros, and put '+' before future years with >4 digits /// ^ use upper case if possible /// # use opposite case if possible -pub fun date(format: Text = "", date: Text = ""): Text { +pub fun date(format: Text = "", date: Text = "", utc: Bool = false): Text { if format == "": format = "%FT%T%Z" if date == "": date = unsafe $date +"%FT%T%Z"$ - return $date -d "{date}" +"{format}"$? + if (utc) { + return $date --utc -d "{date}" +"{format}"$? + } else { + return $date -d "{date}" +"{format}"$? + } } /// return current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)) @@ -82,9 +86,9 @@ pub fun now(): Num { /// hours /// minutes /// seconds -pub fun date_add(add:Text, date:Text = ""): Text { +pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text { if date == "": date = unsafe $date +"%FT%T%Z"$ - return date("", "{date("%F", date)?} {add} {date("%T", date)?}")? + return date("", "{date("%F", date)?} {add} {date("%T", date)?}", utc)? } // compare 2 date @@ -93,10 +97,10 @@ pub fun date_add(add:Text, date:Text = ""): Text { // return -1 if date_b is after date_a // if date_b is not provided, current date will be used #[allow_absurd_cast] -pub fun date_compare(date_a: Text, date_b: Text = ""): Num { - if date_b == "": date_b = unsafe date() - let timestamp_a = date("%s", date_a)? as Num - let timestamp_b = date("%s", date_b)? as Num +pub fun date_compare(date_a: Text, date_b: Text = "", utc: Bool = false): Num { + if date_b == "": date_b = unsafe date("", "", utc) + let timestamp_a = date("%s", date_a, utc)? as Num + let timestamp_b = date("%s", date_b, utc)? as Num if timestamp_a > timestamp_b: return 1 if timestamp_a == timestamp_b: return 0 if timestamp_a < timestamp_b: return -1 diff --git a/src/tests/stdlib/date.ab b/src/tests/stdlib/date.ab index f2352ade..0989a5d3 100644 --- a/src/tests/stdlib/date.ab +++ b/src/tests/stdlib/date.ab @@ -1,7 +1,7 @@ import * from "std/date" main { - echo date("", "@1234567890") failed { + echo date("", "@1234567890", true) failed { echo "error" } } \ No newline at end of file diff --git a/src/tests/stdlib/date.output.txt b/src/tests/stdlib/date.output.txt index 01cf2f2f..6ecc1e2e 100644 --- a/src/tests/stdlib/date.output.txt +++ b/src/tests/stdlib/date.output.txt @@ -1 +1 @@ -2009-02-14T00:31:30CET \ No newline at end of file +2009-02-13T23:31:30UTC \ No newline at end of file diff --git a/src/tests/stdlib/date_add.ab b/src/tests/stdlib/date_add.ab index d113ef85..869b3ba8 100644 --- a/src/tests/stdlib/date_add.ab +++ b/src/tests/stdlib/date_add.ab @@ -1,7 +1,7 @@ import * from "std/date" main { - echo date_add("+17 Days", "2009-02-14T00:31:30CET") failed { + echo date_add("+17 Days", "2009-02-14T00:31:30UTC", true) failed { echo "error" } } \ No newline at end of file diff --git a/src/tests/stdlib/date_add.output.txt b/src/tests/stdlib/date_add.output.txt index 9bb59de3..85a9ef6f 100644 --- a/src/tests/stdlib/date_add.output.txt +++ b/src/tests/stdlib/date_add.output.txt @@ -1 +1 @@ -2009-03-03T00:31:30CET \ No newline at end of file +2009-03-03T01:31:30UTC \ No newline at end of file From 59a122fa378b542a81651c7c3ab9f0b64c4d351e Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 24 Jul 2024 16:04:32 +0200 Subject: [PATCH 3/8] feat(std): replace tab by space --- src/tests/stdlib/date.ab | 6 +++--- src/tests/stdlib/date_add.ab | 6 +++--- src/tests/stdlib/date_add.output.txt | 2 +- src/tests/stdlib/date_compare.ab | 2 +- src/tests/stdlib/now.ab | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/tests/stdlib/date.ab b/src/tests/stdlib/date.ab index 0989a5d3..12aae37a 100644 --- a/src/tests/stdlib/date.ab +++ b/src/tests/stdlib/date.ab @@ -1,7 +1,7 @@ import * from "std/date" main { - echo date("", "@1234567890", true) failed { - echo "error" - } + echo date("", "@1234567890", true) failed { + echo "error" + } } \ No newline at end of file diff --git a/src/tests/stdlib/date_add.ab b/src/tests/stdlib/date_add.ab index 869b3ba8..e91e323b 100644 --- a/src/tests/stdlib/date_add.ab +++ b/src/tests/stdlib/date_add.ab @@ -1,7 +1,7 @@ import * from "std/date" main { - echo date_add("+17 Days", "2009-02-14T00:31:30UTC", true) failed { - echo "error" - } + echo date_add("+17 Days", "2009-02-14T00:31:30UTC", true) failed { + echo "error" + } } \ No newline at end of file diff --git a/src/tests/stdlib/date_add.output.txt b/src/tests/stdlib/date_add.output.txt index 85a9ef6f..51beec1e 100644 --- a/src/tests/stdlib/date_add.output.txt +++ b/src/tests/stdlib/date_add.output.txt @@ -1 +1 @@ -2009-03-03T01:31:30UTC \ No newline at end of file +2009-03-03T00:31:30UTC \ No newline at end of file diff --git a/src/tests/stdlib/date_compare.ab b/src/tests/stdlib/date_compare.ab index 268ae939..fb07ce2a 100644 --- a/src/tests/stdlib/date_compare.ab +++ b/src/tests/stdlib/date_compare.ab @@ -1,5 +1,5 @@ import * from "std/date" main { - if (unsafe date_compare("2000-01-01") == -1 and unsafe date_compare("2100-01-01") == 1) : echo "Hello, Amber!" + if (unsafe date_compare("2000-01-01") == -1 and unsafe date_compare("2100-01-01") == 1) : echo "Hello, Amber!" } \ No newline at end of file diff --git a/src/tests/stdlib/now.ab b/src/tests/stdlib/now.ab index 131cd853..965937e6 100644 --- a/src/tests/stdlib/now.ab +++ b/src/tests/stdlib/now.ab @@ -1,5 +1,5 @@ import * from "std/date" main { - if now() == (unsafe $date +%s$ as Num): echo "Hello, Amber!" + if now() == (unsafe $date +%s$ as Num): echo "Hello, Amber!" } \ No newline at end of file From 165f8206454e3b39870933ee1d700eaff7b96466 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 24 Jul 2024 16:10:35 +0200 Subject: [PATCH 4/8] Update date.ab --- src/std/date.ab | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/std/date.ab b/src/std/date.ab index 88557435..5d4c1a6f 100644 --- a/src/std/date.ab +++ b/src/std/date.ab @@ -1,8 +1,8 @@ -/// format a date with a special format -/// if no date is specified, the current date is used -/// if no format is specified, "%FT%T%Z" format is used -/// for more info about format type "man date" on your shell or go to https://www.gnu.org/software/coreutils/date -/// format : +/// Format a date with a special format +/// If no date is specified, the current date is used +/// If no format is specified, "%FT%T%Z" format is used +/// For more info about format type "man date" on your shell or go to https://www.gnu.org/software/coreutils/date +/// Format : /// %% a literal % /// %a locale's abbreviated weekday name (e.g., Sun) /// %A locale's full weekday name (e.g., Sunday) @@ -50,8 +50,7 @@ /// %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) /// %Z alphabetic time zone abbreviation (e.g., EDT) /// -/// By default, date pads numeric fields with zeroes. The following -/// optional flags may follow '%': +/// By default, date pads numeric fields with zeroes. The following optional flags may follow '%': /// /// - (hyphen) do not pad the field /// _ (underscore) pad with spaces @@ -69,16 +68,16 @@ pub fun date(format: Text = "", date: Text = "", utc: Bool = false): Text { } } -/// return current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)) +/// Return current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)) #[allow_absurd_cast] pub fun now(): Num { return unsafe $date +%s$ as Num } -/// add value to date. -/// if no date is specified, the current date is used -/// ex : date_add("+3 days") -/// you can use : +/// Add value to date. +/// If no date is specified, the current date is used +/// Ex : date_add("+3 days") +/// You can use : /// (+/-) /// years /// months @@ -91,11 +90,11 @@ pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text { return date("", "{date("%F", date)?} {add} {date("%T", date)?}", utc)? } -// compare 2 date -// return 1 if date_a is after date_b -// return 0 if date_a and date_b is the same -// return -1 if date_b is after date_a -// if date_b is not provided, current date will be used +// Compare 2 date +// Return 1 if date_a is after date_b +// Return 0 if date_a and date_b is the same +// Return -1 if date_b is after date_a +// If date_b is not provided, current date will be used #[allow_absurd_cast] pub fun date_compare(date_a: Text, date_b: Text = "", utc: Bool = false): Num { if date_b == "": date_b = unsafe date("", "", utc) From 7b5800a7ccda60da58c7035d6393f648fdd74c91 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 24 Jul 2024 16:22:25 +0200 Subject: [PATCH 5/8] feat(std) : remove *.outpu.txt in date function test --- src/tests/stdlib/date.ab | 4 +--- src/tests/stdlib/date.output.txt | 1 - src/tests/stdlib/date_add.ab | 4 +--- src/tests/stdlib/date_add.output.txt | 1 - src/tests/stdlib/date_compare.ab | 2 +- src/tests/stdlib/date_compare.output.txt | 1 - src/tests/stdlib/now.ab | 2 +- src/tests/stdlib/now.output.txt | 1 - 8 files changed, 4 insertions(+), 12 deletions(-) delete mode 100644 src/tests/stdlib/date.output.txt delete mode 100644 src/tests/stdlib/date_add.output.txt delete mode 100644 src/tests/stdlib/date_compare.output.txt delete mode 100644 src/tests/stdlib/now.output.txt diff --git a/src/tests/stdlib/date.ab b/src/tests/stdlib/date.ab index 12aae37a..b8f39c40 100644 --- a/src/tests/stdlib/date.ab +++ b/src/tests/stdlib/date.ab @@ -1,7 +1,5 @@ import * from "std/date" main { - echo date("", "@1234567890", true) failed { - echo "error" - } + if unsafe date("", "@1234567890", true) == "2009-02-13T23:31:30UTC": echo "Succeded" } \ No newline at end of file diff --git a/src/tests/stdlib/date.output.txt b/src/tests/stdlib/date.output.txt deleted file mode 100644 index 6ecc1e2e..00000000 --- a/src/tests/stdlib/date.output.txt +++ /dev/null @@ -1 +0,0 @@ -2009-02-13T23:31:30UTC \ No newline at end of file diff --git a/src/tests/stdlib/date_add.ab b/src/tests/stdlib/date_add.ab index e91e323b..7c4f4f45 100644 --- a/src/tests/stdlib/date_add.ab +++ b/src/tests/stdlib/date_add.ab @@ -1,7 +1,5 @@ import * from "std/date" main { - echo date_add("+17 Days", "2009-02-14T00:31:30UTC", true) failed { - echo "error" - } + if unsafe date_add("+17 Days", "2009-02-14T00:31:30UTC", true) == "2009-03-03T00:31:30UTC": echo "Succeded" } \ No newline at end of file diff --git a/src/tests/stdlib/date_add.output.txt b/src/tests/stdlib/date_add.output.txt deleted file mode 100644 index 51beec1e..00000000 --- a/src/tests/stdlib/date_add.output.txt +++ /dev/null @@ -1 +0,0 @@ -2009-03-03T00:31:30UTC \ No newline at end of file diff --git a/src/tests/stdlib/date_compare.ab b/src/tests/stdlib/date_compare.ab index fb07ce2a..04bca715 100644 --- a/src/tests/stdlib/date_compare.ab +++ b/src/tests/stdlib/date_compare.ab @@ -1,5 +1,5 @@ import * from "std/date" main { - if (unsafe date_compare("2000-01-01") == -1 and unsafe date_compare("2100-01-01") == 1) : echo "Hello, Amber!" + if (unsafe date_compare("2000-01-01") == -1 and unsafe date_compare("2100-01-01") == 1) : echo "Succeded" } \ No newline at end of file diff --git a/src/tests/stdlib/date_compare.output.txt b/src/tests/stdlib/date_compare.output.txt deleted file mode 100644 index 335af0d8..00000000 --- a/src/tests/stdlib/date_compare.output.txt +++ /dev/null @@ -1 +0,0 @@ -Hello, Amber! \ No newline at end of file diff --git a/src/tests/stdlib/now.ab b/src/tests/stdlib/now.ab index 965937e6..e37a550d 100644 --- a/src/tests/stdlib/now.ab +++ b/src/tests/stdlib/now.ab @@ -1,5 +1,5 @@ import * from "std/date" main { - if now() == (unsafe $date +%s$ as Num): echo "Hello, Amber!" + if now() == (unsafe $date +%s$ as Num): echo "Succeded" } \ No newline at end of file diff --git a/src/tests/stdlib/now.output.txt b/src/tests/stdlib/now.output.txt deleted file mode 100644 index 335af0d8..00000000 --- a/src/tests/stdlib/now.output.txt +++ /dev/null @@ -1 +0,0 @@ -Hello, Amber! \ No newline at end of file From 6e11c022e81b90386502327ad43b5be0ec25fb31 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 24 Jul 2024 16:31:10 +0200 Subject: [PATCH 6/8] fix(std): utc support in date function --- src/std/date.ab | 2 +- src/tests/stdlib/date.ab | 2 +- src/tests/stdlib/date_add.ab | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/std/date.ab b/src/std/date.ab index 5d4c1a6f..0273712f 100644 --- a/src/std/date.ab +++ b/src/std/date.ab @@ -87,7 +87,7 @@ pub fun now(): Num { /// seconds pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text { if date == "": date = unsafe $date +"%FT%T%Z"$ - return date("", "{date("%F", date)?} {add} {date("%T", date)?}", utc)? + return date("", "{date("%F", date, utc)?} {add} {date("%T", date, utc)?}", utc)? } // Compare 2 date diff --git a/src/tests/stdlib/date.ab b/src/tests/stdlib/date.ab index b8f39c40..6d8f35a4 100644 --- a/src/tests/stdlib/date.ab +++ b/src/tests/stdlib/date.ab @@ -1,5 +1,5 @@ import * from "std/date" main { - if unsafe date("", "@1234567890", true) == "2009-02-13T23:31:30UTC": echo "Succeded" + if unsafe date("", "@1234000000", true) == "2009-02-07T09:46:40UTC": echo "Succeded" } \ No newline at end of file diff --git a/src/tests/stdlib/date_add.ab b/src/tests/stdlib/date_add.ab index 7c4f4f45..6c8404b1 100644 --- a/src/tests/stdlib/date_add.ab +++ b/src/tests/stdlib/date_add.ab @@ -1,5 +1,5 @@ import * from "std/date" main { - if unsafe date_add("+17 Days", "2009-02-14T00:31:30UTC", true) == "2009-03-03T00:31:30UTC": echo "Succeded" + if unsafe date_add("+17 Days", "2009-02-07T09:46:40UTC", true) == "2009-02-24T09:46:40UTC": echo "Succeded" } \ No newline at end of file From 66d7f697ccb37f9da826537ed6700b3abd9b0f18 Mon Sep 17 00:00:00 2001 From: Yann Date: Sat, 27 Jul 2024 11:30:48 +0200 Subject: [PATCH 7/8] feat(review) : review by @Ph0enixKM change date to date_posix --- src/std/date.ab | 10 +++++----- src/tests/stdlib/date.ab | 5 ----- src/tests/stdlib/date_posix.ab | 5 +++++ 3 files changed, 10 insertions(+), 10 deletions(-) delete mode 100644 src/tests/stdlib/date.ab create mode 100644 src/tests/stdlib/date_posix.ab diff --git a/src/std/date.ab b/src/std/date.ab index 0273712f..eab9c258 100644 --- a/src/std/date.ab +++ b/src/std/date.ab @@ -58,7 +58,7 @@ /// + pad with zeros, and put '+' before future years with >4 digits /// ^ use upper case if possible /// # use opposite case if possible -pub fun date(format: Text = "", date: Text = "", utc: Bool = false): Text { +pub fun date_posix(format: Text = "", date: Text = "", utc: Bool = false): Text { if format == "": format = "%FT%T%Z" if date == "": date = unsafe $date +"%FT%T%Z"$ if (utc) { @@ -87,7 +87,7 @@ pub fun now(): Num { /// seconds pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text { if date == "": date = unsafe $date +"%FT%T%Z"$ - return date("", "{date("%F", date, utc)?} {add} {date("%T", date, utc)?}", utc)? + return date_posix("", "{date_posix("%F", date, utc)?} {add} {date_posix("%T", date, utc)?}", utc)? } // Compare 2 date @@ -97,9 +97,9 @@ pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text { // If date_b is not provided, current date will be used #[allow_absurd_cast] pub fun date_compare(date_a: Text, date_b: Text = "", utc: Bool = false): Num { - if date_b == "": date_b = unsafe date("", "", utc) - let timestamp_a = date("%s", date_a, utc)? as Num - let timestamp_b = date("%s", date_b, utc)? as Num + if date_b == "": date_b = unsafe date_posix("", "", utc) + let timestamp_a = date_posix("%s", date_a, utc)? as Num + let timestamp_b = date_posix("%s", date_b, utc)? as Num if timestamp_a > timestamp_b: return 1 if timestamp_a == timestamp_b: return 0 if timestamp_a < timestamp_b: return -1 diff --git a/src/tests/stdlib/date.ab b/src/tests/stdlib/date.ab deleted file mode 100644 index 6d8f35a4..00000000 --- a/src/tests/stdlib/date.ab +++ /dev/null @@ -1,5 +0,0 @@ -import * from "std/date" - -main { - if unsafe date("", "@1234000000", true) == "2009-02-07T09:46:40UTC": echo "Succeded" -} \ No newline at end of file diff --git a/src/tests/stdlib/date_posix.ab b/src/tests/stdlib/date_posix.ab new file mode 100644 index 00000000..1180f986 --- /dev/null +++ b/src/tests/stdlib/date_posix.ab @@ -0,0 +1,5 @@ +import * from "std/date" + +main { + if unsafe date_posix("", "@1234000000", true) == "2009-02-07T09:46:40UTC": echo "Succeded" +} \ No newline at end of file From 7d5844e6d4356d0e998580735f8516904a5b922b Mon Sep 17 00:00:00 2001 From: Yann Date: Sat, 27 Jul 2024 12:40:54 +0200 Subject: [PATCH 8/8] feat(review): add /// EXPERIMENTAL in comment --- src/std/date.ab | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/std/date.ab b/src/std/date.ab index eab9c258..6d4d5ec3 100644 --- a/src/std/date.ab +++ b/src/std/date.ab @@ -1,3 +1,4 @@ +/// EXPERIMENTAL /// Format a date with a special format /// If no date is specified, the current date is used /// If no format is specified, "%FT%T%Z" format is used @@ -74,6 +75,7 @@ pub fun now(): Num { return unsafe $date +%s$ as Num } +/// EXPERIMENTAL /// Add value to date. /// If no date is specified, the current date is used /// Ex : date_add("+3 days") @@ -90,6 +92,7 @@ pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text { return date_posix("", "{date_posix("%F", date, utc)?} {add} {date_posix("%T", date, utc)?}", utc)? } +/// EXPERIMENTAL // Compare 2 date // Return 1 if date_a is after date_b // Return 0 if date_a and date_b is the same