Skip to content

Commit

Permalink
datefmt: microoptimize formatting
Browse files Browse the repository at this point in the history
add logic to skip formatting DateTime<UTC> to UTC format using `with_timezone()` when the desired format is already UTC
  • Loading branch information
jqnatividad committed Apr 3, 2024
1 parent 0fe8871 commit 0ee27e7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/cmd/datefmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
flag_formatstr = "%Y-%m-%dT%H:%M:%SZ".to_string();
}

let is_output_utc = output_tz == chrono_tz::UTC;

// set RAYON_NUM_THREADS
util::njobs(args.flag_jobs);

Expand Down Expand Up @@ -358,9 +360,14 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
parse_with_preference_and_timezone(&cell, prefer_dmy, &input_tz)
};
if let Ok(format_date) = parsed_date {
format_date_with_tz = format_date.with_timezone(&output_tz);
formatted_date =
format_date_with_tz.format(&flag_formatstr).to_string();
// don't need to call with_timezone() if output_tz is UTC
// as format_date is already in UTC
formatted_date = if is_output_utc {
format_date.format(&flag_formatstr).to_string()
} else {
format_date_with_tz = format_date.with_timezone(&output_tz);
format_date_with_tz.format(&flag_formatstr).to_string()
};
if !keep_zero_time && formatted_date.ends_with("T00:00:00+00:00") {
formatted_date[..10].clone_into(&mut cell);
} else {
Expand Down

0 comments on commit 0ee27e7

Please sign in to comment.