Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace chrono with jiff #511

Merged
merged 11 commits into from
Jul 31, 2024
175 changes: 29 additions & 146 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build.env]
# 'cross' Docker images do not have the tzdata package installed. Add a volume mount to pass it in from the host
volumes = ["ZONEINFO=/usr/share/zoneinfo"]
2 changes: 1 addition & 1 deletion book/src/date-and-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The following functions are available for date and time handling:
- `datetime(input: String) -> DateTime`: Parses a string (date and time) into a `DateTime` object.
- `date(input: String) -> DateTime`: Parses a string (only date) into a `DateTime` object.
- `time(input: String) -> DateTime`: Parses a string (only time) into a `DateTime` object.
- `format_datetime(format: String, dt: DateTime) -> String`: Formats a `DateTime` object as a string. See [this page](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers) for possible format specifiers.
- `format_datetime(format: String, dt: DateTime) -> String`: Formats a `DateTime` object as a string. See [this page](https://docs.rs/jiff/latest/jiff/fmt/strtime/index.html#conversion-specifications) for possible format specifiers.
- `tz(tz: String) -> Fn[(DateTime) -> DateTime]`: Returns a timezone conversion function, typically used with the conversion operator (`datetime -> tz("Europe/Berlin")`)
- `local(dt: DateTime) -> DateTime`: Timezone conversion function targeting the users local timezone (`datetime -> local`)
- `get_local_timezone() -> String`: Returns the users local timezone
Expand Down
98 changes: 86 additions & 12 deletions examples/tests/datetime.nbt
Original file line number Diff line number Diff line change
@@ -1,22 +1,96 @@
# General datetime parsing

let test_date1 = datetime("2024-07-31T13:24:00Z")
let test_date2 = datetime("2024-01-02T13:24:56Z")
let test_date3 = datetime("2024-01-02T13:24:56.789Z")

# RFC 2822
assert_eq(datetime("Wed, 31 Jul 2024 13:24:00 UTC"), test_date1)
assert_eq(datetime("Wed, 31 Jul 2024 13:24 UTC"), test_date1)
assert_eq(datetime("Wed, 31 Jul 2024 13:24 Z"), test_date1)

assert_eq(datetime("2024-07-31 13:24:00 UTC"), test_date1)
assert_eq(datetime("2024-07-31 13:24 UTC"), test_date1)
assert_eq(datetime("2024-07-31 01:24 pm UTC"), test_date1)
assert_eq(datetime("2024/07/31 13:24:00 UTC"), test_date1)
assert_eq(datetime("2024/07/31 13:24 UTC"), test_date1)
assert_eq(datetime("2024/07/31 01:24 pm UTC"), test_date1)

assert_eq(datetime("2024-01-02 13:24:56 UTC"), test_date2)
assert_eq(datetime("2024-01-02 01:24:56 pm UTC"), test_date2)
assert_eq(datetime("2024/01/02 13:24:56 UTC"), test_date2)
assert_eq(datetime("2024/01/02 01:24:56 pm UTC"), test_date2)

assert_eq(datetime("2024-01-02 13:24:56.789 UTC"), test_date3)
assert_eq(datetime("2024/01/02 13:24:56.789 UTC"), test_date3)



# Parsing with offsets / timezones

assert_eq(datetime("2024-01-02 13:24:56.789 +0000"), test_date3)
assert_eq(datetime("2024-01-02 15:24:56.789 +0200"), test_date3)
assert_eq(datetime("2024-01-02 07:24:56.789 -0600"), test_date3)

assert_eq(datetime("2024-07-31 15:24:00 Europe/Berlin"), test_date1) # CEST (UTC+2)
assert_eq(datetime("2024-01-02 14:24:56 Europe/Berlin"), test_date2) # CET (UTC+1)

assert_eq(datetime("2024-07-31 09:24:00 US/Eastern"), test_date1) # UTC-4
assert_eq(datetime("2024-01-02 08:24:56 US/Eastern"), test_date2) # UTC-5



# Formatting

assert_eq(format_datetime("%Y-%m-%dT%H:%M:%S%:z", test_date1), "2024-07-31T13:24:00+00:00")
assert_eq(format_datetime("%Y-%m-%dT%H:%M:%S%:z", test_date2), "2024-01-02T13:24:56+00:00")
assert_eq(format_datetime("%Y-%m-%dT%H:%M:%S.%3f%:z", test_date3), "2024-01-02T13:24:56.789+00:00")



# Time zone conversions

fn as_string(dt: DateTime) -> String = "{dt}"

assert_eq(as_string(test_date3), "2024-01-02 13:24:56 UTC")
assert_eq(as_string(test_date3 -> UTC),
"2024-01-02 13:24:56 UTC")

assert_eq(as_string(test_date3 -> tz("Europe/Berlin")),
"2024-01-02 14:24:56 CET (UTC +01), Europe/Berlin")
assert_eq(as_string(test_date3 -> tz("US/Eastern")),
"2024-01-02 08:24:56 EST (UTC -05), US/Eastern")
assert_eq(as_string(test_date3 -> tz("Asia/Kathmandu")),
"2024-01-02 19:09:56 (UTC +05:45), Asia/Kathmandu")





# Test leap years (2020 was a leap year)
let dt_leap = datetime("2020-02-28 20:00 UTC")
assert_eq(format_datetime("%Y/%m/%d", dt_leap + 12 hours), "2020/02/29")
let dt_no_leap = datetime("2021-02-28 20:00 UTC")
assert_eq(format_datetime("%Y/%m/%d", dt_no_leap + 12 hours), "2021/03/01")

# Regression test for #376
let dt_issue_376 = datetime("Fri, 23 Feb 2024 14:01:54 -0800")
assert_eq(format_datetime("%Y-%m-%dT%H:%M:%S%:z", dt_issue_376), "2024-02-23T14:01:54-08:00")



# Unix time

let epoch = datetime("1970-01-01T00:00:00Z")
assert_eq(epoch -> unixtime, 0)

assert_eq(epoch + 1000 milliseconds + 2 seconds -> unixtime, 3)

let x = datetime("Wed, 20 Jul 2022 21:52:05 +0200")
assert_eq(x -> unixtime, 1658346725)
let dt_unixtime_1 = datetime("Wed, 20 Jul 2022 21:52:05 +0200")
assert_eq(dt_unixtime_1 -> unixtime, 1658346725)

assert_eq(from_unixtime(1658346725) -> unixtime, 1658346725)
assert_eq(from_unixtime(1658346725), dt_unixtime_1)

# 2020 was a leap year
let y = datetime("2020-02-28 20:00 UTC")
assert_eq(format_datetime("%Y/%m/%d", y + 12 hours), "2020/02/29")
let z = datetime("2021-02-28 20:00 UTC")
assert_eq(format_datetime("%Y/%m/%d", z + 12 hours), "2021/03/01")

# Regression test for #376
let dt_376 = datetime("Fri, 23 Feb 2024 14:01:54 -0800")
assert_eq(format_datetime("%Y-%m-%dT%H:%M:%S%:z", dt_376), "2024-02-23T14:01:54-08:00")


# Julian date
Expand Down
Loading
Loading