Skip to content

Commit 0429d2c

Browse files
committed
date: consolidate date parsing logic
Removed the `parse_offset` function and rely on `parse_date` only.
1 parent c7b02d6 commit 0429d2c

File tree

1 file changed

+11
-43
lines changed

1 file changed

+11
-43
lines changed

src/uu/date/src/date.rs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use clap::{Arg, ArgAction, Command};
99
use jiff::fmt::strtime;
1010
use jiff::tz::TimeZone;
11-
use jiff::{SignedDuration, Timestamp, Zoned};
11+
use jiff::{Timestamp, Zoned};
1212
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
1313
use libc::{CLOCK_REALTIME, clock_settime, timespec};
1414
use std::fs::File;
@@ -64,10 +64,9 @@ enum Format {
6464
/// Various places that dates can come from
6565
enum DateSource {
6666
Now,
67-
Custom(String),
6867
File(PathBuf),
6968
Stdin,
70-
Human(SignedDuration),
69+
Human(String),
7170
}
7271

7372
enum Iso8601Format {
@@ -141,11 +140,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
141140
};
142141

143142
let date_source = if let Some(date) = matches.get_one::<String>(OPT_DATE) {
144-
if let Ok(duration) = parse_offset(date.as_str()) {
145-
DateSource::Human(duration)
146-
} else {
147-
DateSource::Custom(date.into())
148-
}
143+
DateSource::Human(date.into())
149144
} else if let Some(file) = matches.get_one::<String>(OPT_FILE) {
150145
match file.as_ref() {
151146
"-" => DateSource::Stdin,
@@ -176,7 +171,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
176171
if let Some(date) = settings.set_to {
177172
// All set time functions expect UTC datetimes.
178173
let date = if settings.utc {
179-
date.with_time_zone(TimeZone::UTC)
174+
date.datetime().to_zoned(TimeZone::UTC).map_err(|e| {
175+
USimpleError::new(1, translate!("date-error-invalid-date", "error" => e))
176+
})?
180177
} else {
181178
date
182179
};
@@ -193,27 +190,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
193190

194191
// Iterate over all dates - whether it's a single date or a file.
195192
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
196-
DateSource::Custom(ref input) => {
193+
DateSource::Human(ref input) => {
197194
let date = parse_date(input);
198195
let iter = std::iter::once(date);
199196
Box::new(iter)
200197
}
201-
DateSource::Human(relative_time) => {
202-
// Double check the result is overflow or not of the current_time + relative_time
203-
// it may cause a panic of chrono::datetime::DateTime add
204-
match now.checked_add(relative_time) {
205-
Ok(date) => {
206-
let iter = std::iter::once(Ok(date));
207-
Box::new(iter)
208-
}
209-
Err(_) => {
210-
return Err(USimpleError::new(
211-
1,
212-
translate!("date-error-date-overflow", "date" => relative_time),
213-
));
214-
}
215-
}
216-
}
217198
DateSource::Stdin => {
218199
let lines = BufReader::new(std::io::stdin()).lines();
219200
let iter = lines.map_while(Result::ok).map(parse_date);
@@ -391,28 +372,15 @@ fn parse_date<S: AsRef<str> + Clone>(
391372
Ok(date) => {
392373
let timestamp =
393374
Timestamp::new(date.timestamp(), date.timestamp_subsec_nanos() as i32).unwrap();
394-
Ok(Zoned::new(timestamp, TimeZone::UTC))
375+
Ok(Zoned::new(
376+
timestamp,
377+
TimeZone::try_system().unwrap_or(TimeZone::UTC),
378+
))
395379
}
396380
Err(e) => Err((s.as_ref().into(), e)),
397381
}
398382
}
399383

400-
// TODO: Convert `parse_datetime` to jiff and remove wrapper from chrono to jiff structures.
401-
// Also, consider whether parse_datetime::parse_datetime_at_date can be renamed to something
402-
// like parse_datetime::parse_offset, instead of doing some addition/subtraction.
403-
fn parse_offset(date: &str) -> Result<SignedDuration, ()> {
404-
let ref_time = chrono::Local::now();
405-
if let Ok(new_time) = parse_datetime::parse_datetime_at_date(ref_time, date) {
406-
let duration = new_time.signed_duration_since(ref_time);
407-
Ok(SignedDuration::new(
408-
duration.num_seconds(),
409-
duration.subsec_nanos(),
410-
))
411-
} else {
412-
Err(())
413-
}
414-
}
415-
416384
#[cfg(not(any(unix, windows)))]
417385
fn set_system_datetime(_date: Zoned) -> UResult<()> {
418386
unimplemented!("setting date not implemented (unsupported target)");

0 commit comments

Comments
 (0)