Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implement support for to_string and implement PlainDate::to_string #153

Merged
merged 9 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ combine = { workspace = true, optional = true }

# System time feature
web-time = { workspace = true, optional = true }
writeable = "0.6.0"

[features]
default = ["now"]
Expand Down
33 changes: 26 additions & 7 deletions src/components/date.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! This module implements `Date` and any directly related algorithms.

use tinystr::TinyAsciiStr;

use crate::{
components::{
calendar::{Calendar, CalendarDateLike, GetTemporalCalendar},
Expand All @@ -10,15 +8,14 @@ use crate::{
},
iso::{IsoDate, IsoDateTime, IsoTime},
options::{
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, ResolvedRoundingOptions,
TemporalUnit,
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar,
ResolvedRoundingOptions, TemporalUnit,
},
parsers::parse_date_time,
parsers::{parse_date_time, FormattableCalendar, FormattableDate, FormattableIxdtf},
primitive::FiniteF64,
Sign, TemporalError, TemporalResult, TemporalUnwrap, TimeZone,
};

use alloc::format;
use alloc::{format, string::String};
use core::str::FromStr;

use super::{
Expand All @@ -27,6 +24,7 @@ use super::{
timezone::NeverProvider,
PlainMonthDay, PlainTime, PlainYearMonth,
};
use tinystr::TinyAsciiStr;

// TODO (potentially): Bump era up to TinyAsciiStr<18> to accomodate
// "ethiopic-amete-alem". TODO: PrepareTemporalFields expects a type
Expand Down Expand Up @@ -137,6 +135,12 @@ pub struct PlainDate {
calendar: Calendar,
}

impl core::fmt::Display for PlainDate {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.to_ixdtf_string(DisplayCalendar::Auto))
}
}

impl Ord for PlainDate {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.iso.cmp(&other.iso)
Expand Down Expand Up @@ -586,6 +590,21 @@ impl PlainDate {
ArithmeticOverflow::Constrain,
)
}

#[inline]
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {
let ixdtf = FormattableIxdtf {
date: Some(FormattableDate(self.iso.year, self.iso.month, self.iso.day)),
time: None,
utc_offset: None,
timezone: None,
calendar: Some(FormattableCalendar {
show: display_calendar,
calendar: self.calendar.identifier(),
}),
};
ixdtf.to_string()
}
}

// ==== Trait impls ====
Expand Down
4 changes: 2 additions & 2 deletions src/components/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TimeZone {
return Ok(Self::OffsetMinutes(0));
}
let mut cursor = source.chars().peekable();
if cursor.peek().map_or(false, is_ascii_sign) {
if cursor.peek().is_some_and(is_ascii_sign) {
return parse_offset(&mut cursor);
} else if provider.check_identifier(source) {
return Ok(Self::IanaIdentifier(source.to_owned()));
Expand Down Expand Up @@ -366,7 +366,7 @@ pub(crate) fn parse_offset(chars: &mut Peekable<Chars<'_>>) -> TemporalResult<Ti
// First offset portion
let hours = parse_digit_pair(chars)?;

let sep = chars.peek().map_or(false, |ch| *ch == ':');
let sep = chars.peek().is_some_and(|ch| *ch == ':');
if sep {
let _ = chars.next();
}
Expand Down
80 changes: 71 additions & 9 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ impl fmt::Display for TemporalRoundingMode {

/// values for `CalendarName`, whether to show the calendar in toString() methods
/// <https://tc39.es/proposal-temporal/#sec-temporal-gettemporalshowcalendarnameoption>
#[derive(Debug, Clone, Copy)]
pub enum CalendarName {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DisplayCalendar {
/// `Auto` option
Auto,
/// `Always` option
Expand All @@ -722,19 +722,19 @@ pub enum CalendarName {
Critical,
}

impl fmt::Display for CalendarName {
impl fmt::Display for DisplayCalendar {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
CalendarName::Auto => "auto",
CalendarName::Always => "always",
CalendarName::Never => "never",
CalendarName::Critical => "critical",
DisplayCalendar::Auto => "auto",
DisplayCalendar::Always => "always",
DisplayCalendar::Never => "never",
DisplayCalendar::Critical => "critical",
}
.fmt(f)
}
}

impl FromStr for CalendarName {
impl FromStr for DisplayCalendar {
type Err = TemporalError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -743,7 +743,69 @@ impl FromStr for CalendarName {
"always" => Ok(Self::Always),
"never" => Ok(Self::Never),
"critical" => Ok(Self::Critical),
_ => Err(TemporalError::range().with_message("Invalid CalendarName provided.")),
_ => Err(TemporalError::range().with_message("Invalid calendarName provided.")),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DisplayOffset {
Auto,
Never,
}

impl fmt::Display for DisplayOffset {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
DisplayOffset::Auto => "auto",
DisplayOffset::Never => "never",
}
.fmt(f)
}
}

impl FromStr for DisplayOffset {
type Err = TemporalError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"auto" => Ok(Self::Auto),
"never" => Ok(Self::Never),
_ => Err(TemporalError::range().with_message("Invalid offset option provided.")),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DisplayTimeZone {
/// `Auto` option
Auto,
/// `Never` option
Never,
// `Critical` option
Critical,
}

impl fmt::Display for DisplayTimeZone {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
DisplayTimeZone::Auto => "auto",
DisplayTimeZone::Never => "never",
DisplayTimeZone::Critical => "critical",
}
.fmt(f)
}
}

impl FromStr for DisplayTimeZone {
type Err = TemporalError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"auto" => Ok(Self::Auto),
"never" => Ok(Self::Never),
"critical" => Ok(Self::Critical),
_ => Err(TemporalError::range().with_message("Invalid timeZoneName option provided.")),
}
}
}
Loading
Loading