Skip to content

Commit

Permalink
Added repr:four modifier support to the [year] format item
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisorlando committed Sep 25, 2024
1 parent 2e68a36 commit aba2bc2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions time-macros/src/format_description/format_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ modifier! {
enum YearRepr {
#[default]
Full = b"full",
Four = b"four",
LastTwo = b"last_two",
}
}
Expand Down
1 change: 1 addition & 0 deletions time-macros/src/format_description/public/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ to_tokens! {
to_tokens! {
pub(crate) enum YearRepr {
Full,
Four,
LastTwo,
}
}
Expand Down
2 changes: 2 additions & 0 deletions time/src/format_description/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub struct WeekNumber {
pub enum YearRepr {
/// The full value of the year.
Full,
/// Standard 4 digit format, to be used when `large-dates` feature is enabled
Four,
/// Only the last two digits of the year.
LastTwo,
}
Expand Down
1 change: 1 addition & 0 deletions time/src/format_description/parse/format_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ modifier! {
#[default]
Full = b"full",
LastTwo = b"last_two",
Four = b"four",
}
}

Expand Down
8 changes: 7 additions & 1 deletion time/src/formatting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,24 @@ fn fmt_year(
};
let value = match repr {
modifier::YearRepr::Full => full_year,
modifier::YearRepr::Four => (full_year % 10000).abs(),
modifier::YearRepr::LastTwo => (full_year % 100).abs(),
};
let format_number = match repr {

#[cfg(feature = "large-dates")]
modifier::YearRepr::Full if value.abs() >= 100_000 => format_number::<6>,
#[cfg(feature = "large-dates")]
modifier::YearRepr::Four if value.abs() >= 1_000 => format_number::<4>,
#[cfg(feature = "large-dates")]
modifier::YearRepr::Full if value.abs() >= 10_000 => format_number::<5>,

modifier::YearRepr::Full => format_number::<4>,
modifier::YearRepr::Four => format_number::<4>,
modifier::YearRepr::LastTwo => format_number::<2>,
};
let mut bytes = 0;
if repr != modifier::YearRepr::LastTwo {
if repr == modifier::YearRepr::Full {
if full_year < 0 {
bytes += write(output, b"-")?;
} else if sign_is_mandatory || cfg!(feature = "large-dates") && full_year >= 10_000 {
Expand Down
3 changes: 3 additions & 0 deletions time/src/parsing/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub(crate) fn parse_year(input: &[u8], modifiers: modifier::Year) -> Option<Pars
_ => Some(ParsedItem(input, year.cast_signed())),
}
}
modifier::YearRepr::Four => Some (
exactly_n_digits_padded::<4, u32>(modifiers.padding)(input)?.map(|v| v.cast_signed()),
),
modifier::YearRepr::LastTwo => Some(
exactly_n_digits_padded::<2, u32>(modifiers.padding)(input)?.map(|v| v.cast_signed()),
),
Expand Down
8 changes: 7 additions & 1 deletion time/src/parsing/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use deranged::{
OptionRangedI128, OptionRangedI32, OptionRangedI8, OptionRangedU16, OptionRangedU32,
OptionRangedU8, RangedI128, RangedI32, RangedI8, RangedU16, RangedU32, RangedU8,
};
use num_conv::prelude::*;
use num_conv::{prelude::*, CastUnsigned, Truncate};

use crate::convert::{Day, Hour, Minute, Nanosecond, Second};
use crate::date::{MAX_YEAR, MIN_YEAR};
Expand Down Expand Up @@ -296,10 +296,16 @@ impl Parsed {
parse_year(input, modifiers).ok_or(InvalidComponent("year"))?;
match (modifiers.iso_week_based, modifiers.repr) {
(false, modifier::YearRepr::Full) => self.set_year(value),
(false, modifier::YearRepr::Four) => {
self.set_year(value)
}
(false, modifier::YearRepr::LastTwo) => {
self.set_year_last_two(value.cast_unsigned().truncate())
}
(true, modifier::YearRepr::Full) => self.set_iso_year(value),
(true, modifier::YearRepr::Four) => {
self.set_iso_year(value)
}
(true, modifier::YearRepr::LastTwo) => {
self.set_iso_year_last_two(value.cast_unsigned().truncate())
}
Expand Down

0 comments on commit aba2bc2

Please sign in to comment.