Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/items/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use jiff::{civil, Span, Zoned};

use super::{date, epoch, error, relative, time, timezone, weekday, year, Item};
use super::{date, epoch, error, offset, relative, time, weekday, year, Item};

/// The builder is used to construct a DateTime object from various components.
/// The parser creates a `DateTimeBuilder` object with the parsed components,
Expand All @@ -17,7 +17,7 @@ pub(crate) struct DateTimeBuilder {
date: Option<date::Date>,
time: Option<time::Time>,
weekday: Option<weekday::Weekday>,
timezone: Option<timezone::Offset>,
offset: Option<offset::Offset>,
relative: Vec<relative::Relative>,
}

Expand All @@ -41,7 +41,7 @@ impl DateTimeBuilder {
} else if self.date.is_some()
|| self.time.is_some()
|| self.weekday.is_some()
|| self.timezone.is_some()
|| self.offset.is_some()
|| !self.relative.is_empty()
{
return Err("timestamp cannot be combined with other date/time items");
Expand All @@ -67,7 +67,7 @@ impl DateTimeBuilder {
return Err("timestamp cannot be combined with other date/time items");
} else if self.time.is_some() {
return Err("time cannot appear more than once");
} else if self.timezone.is_some() && time.offset.is_some() {
} else if self.offset.is_some() && time.offset.is_some() {
return Err("time offset and timezone are mutually exclusive");
}

Expand All @@ -86,16 +86,16 @@ impl DateTimeBuilder {
Ok(self)
}

pub(super) fn set_timezone(mut self, timezone: timezone::Offset) -> Result<Self, &'static str> {
pub(super) fn set_offset(mut self, timezone: offset::Offset) -> Result<Self, &'static str> {
if self.timestamp.is_some() {
return Err("timestamp cannot be combined with other date/time items");
} else if self.timezone.is_some() {
return Err("timezone cannot appear more than once");
} else if self.time.as_ref().and_then(|t| t.offset.as_ref()).is_some() {
return Err("time offset and timezone are mutually exclusive");
} else if self.offset.is_some()
|| self.time.as_ref().and_then(|t| t.offset.as_ref()).is_some()
{
return Err("time offset cannot appear more than once");
}

self.timezone = Some(timezone);
self.offset = Some(timezone);
Ok(self)
}

Expand Down Expand Up @@ -162,7 +162,7 @@ impl DateTimeBuilder {
&& self.date.is_none()
&& self.time.is_none()
&& self.weekday.is_none()
&& self.timezone.is_none()
&& self.offset.is_none()
{
base
} else {
Expand Down Expand Up @@ -239,7 +239,7 @@ impl DateTimeBuilder {
})?;
}

if let Some(offset) = self.timezone {
if let Some(offset) = self.offset {
let (offset, hour_adjustment) = offset.normalize();
dt = dt.checked_add(Span::new().hours(hour_adjustment))?;
dt = dt.datetime().to_zoned((&offset).try_into()?)?;
Expand All @@ -262,7 +262,7 @@ impl TryFrom<Vec<Item>> for DateTimeBuilder {
Item::Date(d) => builder.set_date(d)?,
Item::Time(t) => builder.set_time(t)?,
Item::Weekday(weekday) => builder.set_weekday(weekday)?,
Item::TimeZone(tz) => builder.set_timezone(tz)?,
Item::Offset(offset) => builder.set_offset(offset)?,
Item::Relative(rel) => builder.push_relative(rel)?,
Item::Pure(pure) => builder.set_pure(pure)?,
}
Expand Down
10 changes: 5 additions & 5 deletions src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
//! - [`combined`]
//! - [`date`]
//! - [`epoch`]
//! - [`offset`]
//! - [`pure`]
//! - [`relative`]
//! - [`time`]
//! - [`timezone`]
//! - [`weekday`]
//! - [`year`]

// date and time items
mod combined;
mod date;
mod epoch;
mod offset;
mod pure;
mod relative;
mod time;
mod timezone;
mod weekday;
mod year;

Expand Down Expand Up @@ -66,7 +66,7 @@ enum Item {
Time(time::Time),
Weekday(weekday::Weekday),
Relative(relative::Relative),
TimeZone(timezone::Offset),
Offset(offset::Offset),
Pure(String),
}

Expand Down Expand Up @@ -231,7 +231,7 @@ fn parse_item(input: &mut &str) -> ModalResult<Item> {
time::parse.map(Item::Time),
relative::parse.map(Item::Relative),
weekday::parse.map(Item::Weekday),
timezone::parse.map(Item::TimeZone),
offset::parse.map(Item::Offset),
pure::parse.map(Item::Pure),
)),
)
Expand Down Expand Up @@ -369,7 +369,7 @@ mod tests {
assert!(result
.unwrap_err()
.to_string()
.contains("timezone cannot appear more than once"));
.contains("time offset cannot appear more than once"));

let result = parse(&mut "2025-05-19 abcdef");
assert!(result.is_err());
Expand Down
2 changes: 1 addition & 1 deletion src/items/timezone.rs → src/items/offset.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! Parse a timezone item.
//! Parse an offset item.
//!
//! From the GNU docs:
//!
Expand Down
2 changes: 1 addition & 1 deletion src/items/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ use winnow::{

use super::{
epoch::sec_and_nsec,
offset::{timezone_offset, Offset},
primitive::{colon, ctx_err, dec_uint, s},
timezone::{timezone_offset, Offset},
};

#[derive(PartialEq, Clone, Debug, Default)]
Expand Down
Loading