Skip to content

Commit

Permalink
Implement UtcDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 13, 2024
1 parent 2785c11 commit 4a1d1e8
Show file tree
Hide file tree
Showing 22 changed files with 3,284 additions and 42 deletions.
9 changes: 9 additions & 0 deletions tests/compile-fail/invalid_utc_datetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use time::macros::utc_datetime;

fn main() {
let _ = utc_datetime!(2021-000 0:00);
let _ = utc_datetime!(2021-001 24:00);
let _ = utc_datetime!(2021-001 0:00 0);
let _ = utc_datetime!(2021-001 0:00 UTC);
let _ = utc_datetime!(2021-001 0:00 UTC x);
}
29 changes: 29 additions & 0 deletions tests/compile-fail/invalid_utc_datetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: invalid component: ordinal was 0
--> ../tests/compile-fail/invalid_utc_datetime.rs:4:32
|
4 | let _ = utc_datetime!(2021-000 0:00);
| ^^^

error: invalid component: hour was 24
--> ../tests/compile-fail/invalid_utc_datetime.rs:5:36
|
5 | let _ = utc_datetime!(2021-001 24:00);
| ^^

error: unexpected token: 0
--> ../tests/compile-fail/invalid_utc_datetime.rs:6:41
|
6 | let _ = utc_datetime!(2021-001 0:00 0);
| ^

error: unexpected token: UTC
--> ../tests/compile-fail/invalid_utc_datetime.rs:7:41
|
7 | let _ = utc_datetime!(2021-001 0:00 UTC);
| ^^^

error: unexpected token: UTC
--> ../tests/compile-fail/invalid_utc_datetime.rs:8:41
|
8 | let _ = utc_datetime!(2021-001 0:00 UTC x);
| ^^^
6 changes: 4 additions & 2 deletions tests/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use std::hash::Hash;
use time::error::{self, ConversionRange, IndeterminateOffset, TryFromParsed};
use time::ext::NumericalDuration;
use time::format_description::{self, modifier, well_known, Component, BorrowedFormatItem, OwnedFormatItem};
use time::macros::{date, offset, time};
use time::macros::{date, offset, time, utc_datetime, datetime};
use time::parsing::Parsed;
use time::{Duration, Error, Month, Time, Weekday};
#[allow(deprecated)]
use time::Instant;
use time_macros::datetime;

macro_rules! assert_cloned_eq {
($x:expr) => {
Expand All @@ -36,6 +35,7 @@ fn clone() {
assert_cloned_eq!(offset!(UTC));
assert_cloned_eq!(datetime!(2021-001 0:00));
assert_cloned_eq!(datetime!(2021-001 0:00 UTC));
assert_cloned_eq!(utc_datetime!(2021-001 0:00));
assert_cloned_eq!(Weekday::Monday);
assert_cloned_eq!(Month::January);
assert_cloned_eq!(Duration::ZERO);
Expand Down Expand Up @@ -96,6 +96,7 @@ fn hash() {
offset!(UTC).hash(&mut hasher);
datetime!(2021-001 0:00).hash(&mut hasher);
datetime!(2021-001 0:00 UTC).hash(&mut hasher);
utc_datetime!(2021-001 0:00).hash(&mut hasher);
Weekday::Monday.hash(&mut hasher);
Month::January.hash(&mut hasher);
#[allow(deprecated)]
Expand Down Expand Up @@ -146,6 +147,7 @@ fn debug() {
}

debug_all! {
utc_datetime!(2021-001 0:00);
Duration::ZERO;
IndeterminateOffset;
ConversionRange;
Expand Down
40 changes: 39 additions & 1 deletion tests/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::num::NonZeroU8;
use time::format_description::well_known::iso8601::{DateKind, OffsetPrecision, TimePrecision};
use time::format_description::well_known::{iso8601, Iso8601, Rfc2822, Rfc3339};
use time::format_description::{self, BorrowedFormatItem, OwnedFormatItem};
use time::macros::{date, datetime, format_description as fd, offset, time};
use time::macros::{date, datetime, format_description as fd, offset, time, utc_datetime};
use time::{OffsetDateTime, Time};

#[test]
Expand All @@ -13,6 +13,10 @@ fn rfc_2822() -> time::Result<()> {
datetime!(2021-01-02 03:04:05 UTC).format(&Rfc2822)?,
"Sat, 02 Jan 2021 03:04:05 +0000"
);
assert_eq!(
utc_datetime!(2021-01-02 03:04:05).format(&Rfc2822)?,
"Sat, 02 Jan 2021 03:04:05 +0000"
);
assert_eq!(
datetime!(2021-01-02 03:04:05 +06:07).format(&Rfc2822)?,
"Sat, 02 Jan 2021 03:04:05 +0607"
Expand All @@ -26,6 +30,10 @@ fn rfc_2822() -> time::Result<()> {
datetime!(1885-01-01 01:01:01 UTC).format(&Rfc2822),
Err(time::error::Format::InvalidComponent("year"))
));
assert!(matches!(
utc_datetime!(1885-01-01 01:01:01).format(&Rfc2822),
Err(time::error::Format::InvalidComponent("year"))
));
assert!(matches!(
datetime!(2000-01-01 00:00:00 +00:00:01).format(&Rfc2822),
Err(time::error::Format::InvalidComponent("offset_second"))
Expand Down Expand Up @@ -562,6 +570,36 @@ fn display_odt() {
);
}

#[test]
fn format_udt() -> time::Result<()> {
let format_description = fd!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]");

assert_eq!(
utc_datetime!(1970-01-01 0:00).format(format_description)?,
"1970-01-01 00:00:00.0"
);
assert!(utc_datetime!(1970-01-01 0:00)
.format_into(&mut io::sink(), format_description)
.is_ok());
assert_eq!(
utc_datetime!(1970-01-01 0:00).format(&OwnedFormatItem::from(format_description))?,
"1970-01-01 00:00:00.0"
);
assert!(utc_datetime!(1970-01-01 0:00)
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description))
.is_ok());

Ok(())
}

#[test]
fn display_udt() {
assert_eq!(
utc_datetime!(1970-01-01 0:00).to_string(),
"1970-01-01 0:00:00.0 +00"
);
}

#[test]
fn insufficient_type_information() {
let assert_insufficient_type_information = |res| {
Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ require_all_features! {
mod serde;
mod serde_helpers;
mod time;
mod utc_date_time;
mod utc_offset;
mod util;
mod weekday;
Expand Down
40 changes: 38 additions & 2 deletions tests/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use time::parsing::{Parsable, Parsed};
#[allow(deprecated)]
use time::Instant;
use time::{
error, ext, Date, Duration, Error, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset,
Weekday,
error, ext, Date, Duration, Error, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime,
UtcOffset, Weekday,
};

#[allow(clippy::cognitive_complexity)] // all test the same thing
Expand All @@ -44,6 +44,7 @@ fn alignment() {
assert_alignment!(Duration, 8);
assert_alignment!(OffsetDateTime, 4);
assert_alignment!(PrimitiveDateTime, 4);
assert_alignment!(UtcDateTime, 4);
assert_alignment!(Time, 4);
assert_alignment!(UtcOffset, 1);
assert_alignment!(error::ComponentRange, 8);
Expand Down Expand Up @@ -122,6 +123,7 @@ fn size() {
assert_size!(Duration, 16, 16);
assert_size!(OffsetDateTime, 16, 16);
assert_size!(PrimitiveDateTime, 12, 12);
assert_size!(UtcDateTime, 12, 12);
assert_size!(Time, 8, 8);
assert_size!(UtcOffset, 3, 4);
assert_size!(error::ComponentRange, 56, 56);
Expand Down Expand Up @@ -382,6 +384,40 @@ assert_impl! { @'a; PrimitiveDateTime:
Unpin,
UnwindSafe,
}
assert_impl! { @'a; UtcDateTime:
Add<Duration, Output = UtcDateTime>,
Add<StdDuration, Output = UtcDateTime>,
AddAssign<Duration>,
AddAssign<StdDuration>,
Arbitrary,
Clone,
Debug,
Deserialize<'a>,
Display,
Hash,
Ord,
PartialEq<UtcDateTime>,
PartialEq<OffsetDateTime>,
PartialEq<SystemTime>,
PartialOrd<UtcDateTime>,
PartialOrd<OffsetDateTime>,
PartialOrd<SystemTime>,
Serialize,
Sub<Duration, Output = UtcDateTime>,
Sub<StdDuration, Output = UtcDateTime>,
Sub<UtcDateTime>,
Sub<OffsetDateTime>,
SubAssign<Duration>,
SubAssign<StdDuration>,
TryFrom<Parsed, Error = error::TryFromParsed>,
Copy,
Eq,
RefUnwindSafe,
Send,
Sync,
Unpin,
UnwindSafe,
}
assert_impl! { @'a; Time:
Add<Duration, Output = Time>,
Add<StdDuration, Output = Time>,
Expand Down
29 changes: 28 additions & 1 deletion tests/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::Ordering;
use std::time::{Duration as StdDuration, SystemTime};

use time::ext::{NumericalDuration, NumericalStdDuration};
use time::macros::{date, datetime, offset, time};
use time::macros::{date, datetime, offset, time, utc_datetime};
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Weekday};

#[test]
Expand Down Expand Up @@ -96,6 +96,33 @@ fn checked_to_offset() {
);
}

#[test]
fn to_utc() {
assert_eq!(datetime!(2000-01-01 0:00 +1).to_utc().year(), 1999);
assert_eq!(
datetime!(0000-001 0:00 UTC).to_utc(),
utc_datetime!(0000-001 0:00),
);
}

#[test]
fn to_utc_panic() {
assert_panic!(datetime!(+999999-12-31 23:59:59 -1).to_utc());
assert_panic!(datetime!(-999999-01-01 00:00:00 +1).to_utc());
}

#[test]
fn checked_to_utc() {
assert_eq!(
datetime!(2000-01-01 0:00 +1)
.checked_to_utc()
.map(|udt| udt.year()),
Some(1999)
);
assert_eq!(datetime!(+999999-12-31 23:59:59 -1).checked_to_utc(), None);
assert_eq!(datetime!(-999999-01-01 00:00:00 +1).checked_to_utc(), None);
}

#[test]
fn from_unix_timestamp() {
assert_eq!(
Expand Down
Loading

0 comments on commit 4a1d1e8

Please sign in to comment.