You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
refactor: rename the timezone module to offset
This commit is contained in:
+13
-13
@@ -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,
|
||||
@@ -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>,
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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()?)?;
|
||||
@@ -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)?,
|
||||
}
|
||||
|
||||
+5
-5
@@ -21,10 +21,10 @@
|
||||
//! - [`combined`]
|
||||
//! - [`date`]
|
||||
//! - [`epoch`]
|
||||
//! - [`offset`]
|
||||
//! - [`pure`]
|
||||
//! - [`relative`]
|
||||
//! - [`time`]
|
||||
//! - [`timezone`]
|
||||
//! - [`weekday`]
|
||||
//! - [`year`]
|
||||
|
||||
@@ -32,10 +32,10 @@
|
||||
mod combined;
|
||||
mod date;
|
||||
mod epoch;
|
||||
mod offset;
|
||||
mod pure;
|
||||
mod relative;
|
||||
mod time;
|
||||
mod timezone;
|
||||
mod weekday;
|
||||
mod year;
|
||||
|
||||
@@ -66,7 +66,7 @@ enum Item {
|
||||
Time(time::Time),
|
||||
Weekday(weekday::Weekday),
|
||||
Relative(relative::Relative),
|
||||
TimeZone(timezone::Offset),
|
||||
Offset(offset::Offset),
|
||||
Pure(String),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)),
|
||||
)
|
||||
@@ -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());
|
||||
|
||||
@@ -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:
|
||||
//!
|
||||
+1
-1
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user