Skip to content
Open
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
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: EUPL-1.2
[toolchain]
# NOTE: don't forget to update the unit-tests workflow when changing this
channel = "1.83"
channel = "1.85.0"
components = [
"rustfmt",
"rustc",
Expand Down
1 change: 1 addition & 0 deletions src/options/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub fn get_command() -> clap::Command {
.arg(arg!(-u --accessed "show the accessed timestamp field (replace default field, combinable)"))
.arg(arg!(--changed "show the changed timestamp field (replace default field, combinable)"))
.arg(arg!(-U --created "show the created timestamp field (replace default field, combinable)"))
.arg(arg!(--utc "show the time in the UTC timezone"))
.arg(arg!(--"time-style" <STYLE>)
.help(format!("how to format timestamps {FORMAT_STYLE_FIELDS_HELP}"))
.value_parser(TimeFormatParser)
Expand Down
2 changes: 2 additions & 0 deletions src/options/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,15 @@ impl TableOptions {
let user_format = UserFormat::deduce(matches);
let group_format = GroupFormat::deduce(matches);
let columns = Columns::deduce(matches, vars)?;
let use_utc = matches.get_flag("utc");
Ok(Self {
size_format,
time_format,
user_format,
group_format,
flags_format,
columns,
use_utc,
})
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/output/render/times.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ use chrono::prelude::*;
use nu_ansi_term::Style;

pub trait Render {
fn render(self, style: Style, time_offset: FixedOffset, time_format: TimeFormat) -> TextCell;
fn render(
self,
style: Style,
time_offset: FixedOffset,
time_format: TimeFormat,
use_utc: bool,
) -> TextCell;
}

impl Render for Option<NaiveDateTime> {
fn render(self, style: Style, time_offset: FixedOffset, time_format: TimeFormat) -> TextCell {
fn render(
self,
style: Style,
time_offset: FixedOffset,
time_format: TimeFormat,
use_utc: bool,
) -> TextCell {
let datestamp = if let Some(time) = self {
time_format.format(&DateTime::<FixedOffset>::from_naive_utc_and_offset(
time,
time_offset,
))
time_format.format(
&DateTime::<FixedOffset>::from_naive_utc_and_offset(time, time_offset),
use_utc,
)
} else {
String::from("-")
};
Expand Down
10 changes: 9 additions & 1 deletion src/output/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Options {
pub group_format: GroupFormat,
pub flags_format: FlagsFormat,
pub columns: Columns,
pub use_utc: bool,
}

/// Extra columns to display in the table.
Expand Down Expand Up @@ -417,6 +418,7 @@ pub struct Table<'a> {
group_format: GroupFormat,
flags_format: FlagsFormat,
git: Option<&'a GitCache>,
use_utc: bool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -451,6 +453,7 @@ impl<'a> Table<'a> {
#[cfg(unix)]
group_format: options.group_format,
flags_format: options.flags_format,
use_utc: options.use_utc,
}
}

Expand Down Expand Up @@ -575,8 +578,13 @@ impl<'a> Table<'a> {
} else {
self.theme.ui.date.unwrap_or_default()
},
self.env.time_offset,
if self.use_utc {
FixedOffset::east_opt(0).unwrap()
} else {
self.env.time_offset
},
self.time_format.clone(),
self.use_utc,
),
}
}
Expand Down
90 changes: 80 additions & 10 deletions src/output/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ pub enum TimeFormat {

impl TimeFormat {
#[must_use]
pub fn format(self, time: &DateTime<FixedOffset>) -> String {
pub fn format(self, time: &DateTime<FixedOffset>, use_utc: bool) -> String {
#[rustfmt::skip]
return match self {
Self::DefaultFormat => default(time),
Self::ISOFormat => iso(time),
Self::LongISO => long(time),
Self::FullISO => full(time),
Self::FullISO => full(time, use_utc),
Self::Relative => relative(time),
Self::Custom { non_recent, recent } => custom(
time, non_recent.as_str(), recent.as_deref()
time, non_recent.as_str(), recent.as_deref(), use_utc
),
};
}
Expand Down Expand Up @@ -125,22 +125,92 @@ fn relative(time: &DateTime<FixedOffset>) -> String {
))
}

fn full(time: &DateTime<FixedOffset>) -> String {
time.format("%Y-%m-%d %H:%M:%S.%f %z").to_string()
fn full(time: &DateTime<FixedOffset>, use_utc: bool) -> String {
format_with_tz(time, "%Y-%m-%d %H:%M:%S.%f %z", use_utc)
}

fn custom(time: &DateTime<FixedOffset>, non_recent_fmt: &str, recent_fmt: Option<&str>) -> String {
if let Some(recent_fmt) = recent_fmt {
fn custom(
time: &DateTime<FixedOffset>,
non_recent_fmt: &str,
recent_fmt: Option<&str>,
use_utc: bool,
) -> String {
let format = if let Some(recent_fmt) = recent_fmt {
if time.year() == *CURRENT_YEAR {
time.format(recent_fmt).to_string()
recent_fmt
} else {
time.format(non_recent_fmt).to_string()
non_recent_fmt
}
} else {
time.format(non_recent_fmt).to_string()
non_recent_fmt
};

format_with_tz(time, format, use_utc)
}

fn format_with_tz(time: &DateTime<FixedOffset>, format: &str, use_utc: bool) -> String {
if !format.contains("%Z") {
return time.format(format).to_string();
}

let tz_name = if use_utc {
String::from("UTC")
} else {
TZ_HANDLER.get_abbreviation(time.timestamp())
};
let mut result = String::new();
let mut last_end = 0;
for (start, part) in format.match_indices("%Z") {
result.push_str(&time.format(&format[last_end..start]).to_string());
result.push_str(&tz_name);
last_end = start + part.len();
}
result.push_str(&time.format(&format[last_end..]).to_string());
result
}

struct TimezoneHandler;

impl TimezoneHandler {
fn get_abbreviation(&self, timestamp: i64) -> String {
#[cfg(unix)]
{
unsafe {
extern "C" {
fn tzset();
fn localtime_r(
timep: *const libc::time_t,
result: *mut libc::tm,
) -> *mut libc::tm;
static tzname: [*const libc::c_char; 2];
}

tzset();
let mut tm: libc::tm = std::mem::zeroed();
#[allow(trivial_numeric_casts)]
let t = timestamp as libc::time_t;
if localtime_r(&t, &mut tm).is_null() {
return String::new();
}

let idx = usize::from(tm.tm_isdst > 0);
let ptr = tzname[idx];
if ptr.is_null() {
return String::new();
}

std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned()
}
}
#[cfg(not(unix))]
{
String::new()
}
}
}

static TZ_HANDLER: LazyLock<TimezoneHandler> = LazyLock::new(|| TimezoneHandler);

static CURRENT_YEAR: LazyLock<i32> = LazyLock::new(|| Local::now().year());

static LOCALE: LazyLock<locale::Time> =
Expand Down
Loading