Skip to content

Commit 1deef90

Browse files
committed
(WIP) Eliminate internal usage of custom macros
This has caused an ICE, so I can't accurately determine every instance without going through the code by hand.
1 parent 10e931b commit 1deef90

File tree

5 files changed

+113
-13
lines changed

5 files changed

+113
-13
lines changed

src/date.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,26 @@ mod test {
11211121
};
11221122
}
11231123

1124+
macro_rules! date {
1125+
($year:literal - $month:literal - $day:literal) => {
1126+
Date::try_from_ymd($year, $month, $day).unwrap()
1127+
};
1128+
1129+
($year:literal - $ordinal:literal) => {
1130+
Date::try_from_yo($year, $ordinal).unwrap()
1131+
};
1132+
}
1133+
1134+
macro_rules! time {
1135+
($hour:literal : $minute:literal : $second:literal) => {
1136+
Time::try_from_hms($hour, $minute, $second).unwrap()
1137+
};
1138+
1139+
($hour:literal : $minute:literal) => {
1140+
Time::try_from_hms($hour, $minute, 0).unwrap()
1141+
};
1142+
}
1143+
11241144
#[test]
11251145
fn weeks_in_year_exhaustive() {
11261146
let years_with_53 = &[
@@ -2174,7 +2194,7 @@ mod test {
21742194
assert_eq!(Date::parse("2019-002", "%Y-%j"), Ok(date!(2019-002)));
21752195
assert_eq!(
21762196
Date::parse("2019-W01-3", "%G-W%V-%u"),
2177-
Ok(date!(2019-W01-3))
2197+
Date::try_from_iso_ywd(2019, 1, Wednesday)
21782198
);
21792199
}
21802200

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ mod internal_prelude {
479479
vec,
480480
vec::Vec,
481481
};
482-
pub(crate) use time_macros::{date, offset, time};
483482
}
484483

485484
#[allow(clippy::missing_docs_in_private_items)]

src/offset_date_time.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,52 @@ impl From<OffsetDateTime> for SystemTime {
978978
mod test {
979979
use super::*;
980980

981+
macro_rules! date {
982+
($year:literal - $month:literal - $day:literal) => {
983+
Date::try_from_ymd($year, $month, $day).unwrap()
984+
};
985+
986+
($year:literal - $ordinal:literal) => {
987+
Date::try_from_yo($year, $ordinal).unwrap()
988+
};
989+
}
990+
991+
macro_rules! time {
992+
($hour:literal : $minute:literal : $second:literal . $nanosecond:literal) => {
993+
Time::try_from_hms_nano($hour, $minute, $second, $nanosecond).unwrap()
994+
};
995+
996+
($hour:literal : $minute:literal : $second:literal) => {
997+
Time::try_from_hms($hour, $minute, $second).unwrap()
998+
};
999+
1000+
($hour:literal : $minute:literal) => {
1001+
Time::try_from_hms($hour, $minute, 0).unwrap()
1002+
};
1003+
}
1004+
1005+
macro_rules! offset {
1006+
(UTC) => {
1007+
UtcOffset::UTC
1008+
};
1009+
1010+
($hours:literal) => {
1011+
UtcOffset::hours($hours)
1012+
};
1013+
1014+
(+ $hours:literal) => {
1015+
UtcOffset::east_hours($hours)
1016+
};
1017+
1018+
(+ $hours:literal : $minutes:literal) => {
1019+
UtcOffset::east_minutes($hours * 60 + $minutes)
1020+
};
1021+
1022+
(+ $hours:literal : $minutes:literal : $seconds:literal) => {
1023+
UtcOffset::east_seconds($hours * 3_600 + $minutes * 60 + $seconds)
1024+
};
1025+
}
1026+
9811027
#[test]
9821028
#[cfg(std)]
9831029
fn now() {
@@ -1239,7 +1285,7 @@ mod test {
12391285
assert_eq!(date!(2019-01-01).midnight().assume_utc().millisecond(), 0);
12401286
assert_eq!(
12411287
date!(2019-01-01)
1242-
.with_time(time!(23:59:59.999))
1288+
.with_time(time!(23:59:59 . 999_000_000))
12431289
.assume_utc()
12441290
.millisecond(),
12451291
999,
@@ -1251,7 +1297,7 @@ mod test {
12511297
assert_eq!(date!(2019-01-01).midnight().assume_utc().microsecond(), 0);
12521298
assert_eq!(
12531299
date!(2019-01-01)
1254-
.with_time(time!(23:59:59.999_999))
1300+
.with_time(time!(23:59:59 . 999_999_000))
12551301
.assume_utc()
12561302
.microsecond(),
12571303
999_999,
@@ -1263,7 +1309,7 @@ mod test {
12631309
assert_eq!(date!(2019-01-01).midnight().assume_utc().nanosecond(), 0);
12641310
assert_eq!(
12651311
date!(2019-01-01)
1266-
.with_time(time!(23:59:59.999_999_999))
1312+
.with_time(time!(23:59:59 . 999_999_999))
12671313
.assume_utc()
12681314
.nanosecond(),
12691315
999_999_999,
@@ -1290,7 +1336,9 @@ mod test {
12901336
);
12911337
assert_eq!(
12921338
OffsetDateTime::parse("2019-W01-3 12:00:00 pm +0000", "%G-W%V-%u %r %z"),
1293-
Ok(date!(2019-W01-3).with_time(time!(12:00)).assume_utc())
1339+
Ok(Date::try_from_iso_wyd(2019, 1, Wednesday)
1340+
.unwrap()
1341+
.with_time(time!(12:00)))
12941342
);
12951343
assert_eq!(
12961344
OffsetDateTime::parse("2019-01-02 03:04:05 +0600", "%F %T %z"),

src/primitive_date_time.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl PrimitiveDateTime {
8181
year: 1970,
8282
ordinal: 1,
8383
},
84-
time: time!(0:00),
84+
time: Time::midnight(),
8585
}
8686
}
8787

@@ -466,7 +466,7 @@ impl PrimitiveDateTime {
466466
pub const fn assume_utc(self) -> OffsetDateTime {
467467
OffsetDateTime {
468468
utc_datetime: self,
469-
offset: offset!(UTC),
469+
offset: UtcOffset::UTC,
470470
}
471471
}
472472
}
@@ -749,6 +749,36 @@ impl From<PrimitiveDateTime> for SystemTime {
749749
mod test {
750750
use super::*;
751751

752+
macro_rules! date {
753+
($year:literal - $month:literal - $day:literal) => {
754+
Date::try_from_ymd($year, $month, $day).unwrap()
755+
};
756+
757+
($year:literal - $ordinal:literal) => {
758+
Date::try_from_yo($year, $ordinal).unwrap()
759+
};
760+
}
761+
762+
macro_rules! time {
763+
($hour:literal : $minute:literal : $second:literal) => {
764+
Time::try_from_hms($hour, $minute, $second).unwrap()
765+
};
766+
767+
($hour:literal : $minute:literal) => {
768+
Time::try_from_hms($hour, $minute, 0).unwrap()
769+
};
770+
}
771+
772+
macro_rules! offset {
773+
(UTC) => {
774+
UtcOffset::UTC
775+
};
776+
777+
($hours:literal) => {
778+
UtcOffset::hours($hours)
779+
};
780+
}
781+
752782
#[test]
753783
fn new() {
754784
assert_eq!(
@@ -899,7 +929,8 @@ mod test {
899929
assert_eq!(date!(2019-01-01).midnight().millisecond(), 0);
900930
assert_eq!(
901931
date!(2019-01-01)
902-
.with_time(time!(23:59:59.999))
932+
.try_with_hms_milli(23, 59, 59, 999)
933+
.unwrap()
903934
.millisecond(),
904935
999
905936
);
@@ -910,7 +941,8 @@ mod test {
910941
assert_eq!(date!(2019-01-01).midnight().microsecond(), 0);
911942
assert_eq!(
912943
date!(2019-01-01)
913-
.with_time(time!(23:59:59.999_999))
944+
.try_with_hms_micro(23, 59, 59, 999_999)
945+
.unwrap()
914946
.microsecond(),
915947
999_999
916948
);
@@ -921,7 +953,8 @@ mod test {
921953
assert_eq!(date!(2019-01-01).midnight().nanosecond(), 0);
922954
assert_eq!(
923955
date!(2019-01-01)
924-
.with_time(time!(23:59:59.999_999_999))
956+
.try_with_hms_nano(23, 59, 59, 999_999_999)
957+
.unwrap()
925958
.nanosecond(),
926959
999_999_999
927960
);
@@ -985,7 +1018,7 @@ mod test {
9851018
);
9861019
assert_eq!(
9871020
PrimitiveDateTime::parse("2019-W01-3 12:00:00 pm", "%G-W%V-%u %r"),
988-
Ok(date!(2019-W01-3).with_time(time!(12:00))),
1021+
Date::try_from_iso_ywd(2019, 1, Wednesday).with_time(time!(12:00))
9891022
);
9901023
}
9911024

src/time_mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ mod test {
851851
assert_eq!(time.nanoseconds_since_midnight(), 0);
852852
assert_eq!(Time::from_nanoseconds_since_midnight(0), time);
853853

854-
let time = time!(23:59:59.999_999_999);
854+
let time = Time::try_from_hms_nano(23, 59, 59, 999_999_999).unwrap();
855855
assert_eq!(time.nanoseconds_since_midnight(), NANOS_PER_DAY - 1);
856856
assert_eq!(
857857
Time::from_nanoseconds_since_midnight(NANOS_PER_DAY - 1),

0 commit comments

Comments
 (0)