Skip to content

Commit 642bbbf

Browse files
authored
Merge pull request #255 from ChrisDryden/fix-tz-prefix-with-base-date
fix: TZ prefix should override base date timezone
1 parent 6396c07 commit 642bbbf

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/items/builder.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,13 @@ impl DateTimeBuilder {
187187
/// - e. Apply final fixed offset if present.
188188
pub(super) fn build(self) -> Result<Zoned, error::Error> {
189189
// 1. Choose the base instant.
190-
let base = match (self.base, &self.timezone) {
191-
(Some(b), _) => b,
192-
(None, Some(tz)) => jiff::Timestamp::now().to_zoned(tz.clone()),
190+
// If a TZ="..." prefix was parsed, it should override the base's timezone
191+
// while keeping the base's timestamp for relative date calculations.
192+
let has_timezone = self.timezone.is_some();
193+
let base = match (self.base, self.timezone) {
194+
(Some(b), Some(tz)) => b.timestamp().to_zoned(tz),
195+
(Some(b), None) => b,
196+
(None, Some(tz)) => jiff::Timestamp::now().to_zoned(tz),
193197
(None, None) => Zoned::now(),
194198
};
195199

@@ -204,7 +208,7 @@ impl DateTimeBuilder {
204208
|| self.time.is_some()
205209
|| self.weekday.is_some()
206210
|| self.offset.is_some()
207-
|| self.timezone.is_some();
211+
|| has_timezone;
208212

209213
let mut dt = if need_midnight {
210214
base.with().time(civil::time(0, 0, 0, 0)).build()?

tests/date.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,20 @@ fn test_date_omitting_year(#[case] input: &str, #[case] year: u32, #[case] expec
9595
.unwrap();
9696
check_relative(now, input, expected);
9797
}
98+
99+
#[rstest]
100+
#[case::tz_prefix_est5("TZ=\"EST5\" 1970-01-01 00:00", "1970-01-01 00:00:00-05:00")]
101+
#[case::tz_prefix_pst8("TZ=\"PST8\" 1970-01-01 00:00", "1970-01-01 00:00:00-08:00")]
102+
#[case::tz_prefix_utc("TZ=\"UTC\" 1970-01-01 12:00", "1970-01-01 12:00:00+00:00")]
103+
#[case::tz_prefix_europe_paris(
104+
r#"TZ="Europe/Paris" 2025-01-02 03:04:05"#,
105+
"2025-01-02 03:04:05+01:00"
106+
)]
107+
fn test_tz_prefix_with_base_date(#[case] input: &str, #[case] expected: &str) {
108+
let base = "2020-06-15 12:00:00"
109+
.parse::<DateTime>()
110+
.unwrap()
111+
.to_zoned(TimeZone::UTC)
112+
.unwrap();
113+
check_relative(base, input, expected);
114+
}

0 commit comments

Comments
 (0)