Skip to content

Commit

Permalink
chore: Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Feb 24, 2025
1 parent 0db82d5 commit 87b120e
Showing 1 changed file with 25 additions and 7 deletions.
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

0 comments on commit 87b120e

Please # to comment.