Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: Update example #295

Merged
merged 1 commit into from
Feb 24, 2025
Merged
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
32 changes: 25 additions & 7 deletions examples/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! An example of printing a human-readable date and time as the file time.

use std::{ops::Deref, str::FromStr};

use anyhow::Context;
use clap::{Parser, ValueEnum};
use nt_time::{
Expand Down Expand Up @@ -33,8 +35,8 @@ struct Opt {
///
/// <DATE> is a string representing a date and time in either ISO 8601, RFC
/// 2822, or RFC 3339 format.
#[arg(value_parser(parse_offset_date_time), value_name("DATE"))]
dt: OffsetDateTime,
#[arg(value_name("DATE"))]
dt: DateTime,
}

#[derive(Clone, Debug, Default, ValueEnum)]
Expand All @@ -56,16 +58,32 @@ enum Format {
HighLow,
}

fn parse_offset_date_time(dt: &str) -> Result<OffsetDateTime, Parse> {
OffsetDateTime::parse(dt, &Iso8601::DEFAULT)
.or_else(|_| OffsetDateTime::parse(dt, &Rfc2822))
.or_else(|_| OffsetDateTime::parse(dt, &Rfc3339))
#[derive(Clone, Debug)]
struct DateTime(OffsetDateTime);

impl Deref for DateTime {
type Target = OffsetDateTime;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl FromStr for DateTime {
type Err = Parse;

fn from_str(dt: &str) -> Result<Self, Parse> {
OffsetDateTime::parse(dt, &Iso8601::DEFAULT)
.or_else(|_| OffsetDateTime::parse(dt, &Rfc2822))
.or_else(|_| OffsetDateTime::parse(dt, &Rfc3339))
.map(Self)
}
}

fn main() -> anyhow::Result<()> {
let opt = Opt::parse();

let ft = FileTime::try_from(opt.dt).context("could not convert time")?;
let ft = FileTime::try_from(*opt.dt).context("could not convert time")?;
match opt.format {
Format::Raw => println!("{}", ft.to_raw()),
Format::BeBytes => println!("{:#04x?}", ft.to_be_bytes()),
Expand Down