Skip to content

Commit 91f069b

Browse files
committed
Cleanup utils module
1 parent bca9957 commit 91f069b

File tree

5 files changed

+257
-296
lines changed

5 files changed

+257
-296
lines changed

src/builtins/core/year_month.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This module implements `YearMonth` and any directly related algorithms.
22
3-
use alloc::string::String;
3+
use alloc::{format, string::String};
44
use core::{cmp::Ordering, str::FromStr};
55

66
use tinystr::TinyAsciiStr;
@@ -9,7 +9,6 @@ use crate::{
99
iso::{year_month_within_limits, IsoDate},
1010
options::{ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar},
1111
parsers::{FormattableCalendar, FormattableDate, FormattableYearMonth},
12-
utils::pad_iso_year,
1312
Calendar, MonthCode, TemporalError, TemporalResult, TemporalUnwrap,
1413
};
1514

@@ -103,11 +102,19 @@ impl PlainYearMonth {
103102
self.iso.year
104103
}
105104

106-
/// Returns the padded ISO year string
105+
/// 3.5.11 PadISOYear ( y )
106+
///
107+
/// Returns a String representation of y suitable for inclusion in an ISO 8601 string.
107108
#[inline]
108109
#[must_use]
109110
pub fn padded_iso_year_string(&self) -> String {
110-
pad_iso_year(self.iso.year)
111+
let year = self.iso.year;
112+
if (0..9999).contains(&year) {
113+
return format!("{:04}", year);
114+
}
115+
let year_sign = if year > 0 { "+" } else { "-" };
116+
let year_string = format!("{:06}", year.abs());
117+
format!("{year_sign}{year_string}",)
111118
}
112119

113120
/// Returns the iso month value for this `YearMonth`.

src/iso.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl IsoDateTime {
9191
// 3. Let epochMilliseconds be 𝔽((epochNanoseconds - remainderNs) / 10^6).
9292
let epoch_millis = (mathematical_nanos - remainder_nanos) / 1_000_000;
9393

94-
let (year, month, day) = utils::ymd_from_epoch_milliseconds(epoch_millis);
94+
let (year, month, day) = utils::Epoch::new(epoch_millis).ymd();
9595

9696
// 7. Let hour be ℝ(! HourFromTime(epochMilliseconds)).
9797
let hour = epoch_millis.div_euclid(3_600_000).rem_euclid(24);
@@ -344,8 +344,7 @@ impl IsoDate {
344344
/// Equivalent to `BalanceISODate`.
345345
pub(crate) fn balance(year: i32, month: i32, day: i32) -> Self {
346346
let epoch_days = iso_date_to_epoch_days(year, month, day);
347-
let ms = utils::epoch_days_to_epoch_ms(epoch_days, 0);
348-
let (year, month, day) = utils::ymd_from_epoch_milliseconds(ms);
347+
let (year, month, day) = utils::Epoch::from_days(epoch_days).ymd();
349348
Self::new_unchecked(year, month, day)
350349
}
351350

@@ -367,7 +366,7 @@ impl IsoDate {
367366
/// Equivalent to `IsoDateToEpochDays`
368367
#[inline]
369368
pub(crate) fn to_epoch_days(self) -> i32 {
370-
utils::epoch_days_from_gregorian_date(self.year, self.month, self.day)
369+
utils::Epoch::from_gregorian_date(self.year, self.month, self.day).days()
371370
}
372371

373372
/// Returns if the current `IsoDate` is valid.
@@ -488,12 +487,13 @@ impl IsoDate {
488487

489488
// NOTE: Below is adapted from the polyfill. Preferring this as it avoids looping.
490489
// 11. Let weeks be 0.
491-
let days = utils::epoch_days_from_gregorian_date(other.year, other.month, other.day)
492-
- utils::epoch_days_from_gregorian_date(
490+
let days = utils::Epoch::from_gregorian_date(other.year, other.month, other.day).days()
491+
- utils::Epoch::from_gregorian_date(
493492
constrained.year,
494493
constrained.month,
495494
constrained.day,
496-
);
495+
)
496+
.days();
497497

498498
let (weeks, days) = if largest_unit == TemporalUnit::Week {
499499
(days / 7, days % 7)
@@ -905,8 +905,7 @@ const MAX_EPOCH_DAYS: i32 = 10i32.pow(8) + 1;
905905
#[inline]
906906
/// Utility function to determine if a `DateTime`'s components create a `DateTime` within valid limits
907907
fn iso_dt_within_valid_limits(date: IsoDate, time: &IsoTime) -> bool {
908-
if utils::epoch_days_from_gregorian_date(date.year, date.month, date.day).abs() > MAX_EPOCH_DAYS
909-
{
908+
if utils::Epoch::from_gregorian_date(date.year, date.month, date.day).days() > MAX_EPOCH_DAYS {
910909
return false;
911910
}
912911

@@ -927,7 +926,7 @@ fn utc_epoch_nanos(date: IsoDate, time: &IsoTime) -> TemporalResult<EpochNanosec
927926
#[inline]
928927
fn to_unchecked_epoch_nanoseconds(date: IsoDate, time: &IsoTime) -> i128 {
929928
let ms = time.to_epoch_ms();
930-
let epoch_ms = utils::epoch_days_to_epoch_ms(date.to_epoch_days(), ms);
929+
let epoch_ms = utils::Epoch::from_days(date.to_epoch_days()).millis() + ms;
931930
epoch_ms as i128 * 1_000_000 + time.microsecond as i128 * 1_000 + time.nanosecond as i128
932931
}
933932

@@ -943,10 +942,10 @@ pub(crate) fn iso_date_to_epoch_days(year: i32, month: i32, day: i32) -> i32 {
943942
let resolved_month = month.rem_euclid(12) as u8;
944943
// 3. Find a time t such that EpochTimeToEpochYear(t) is resolvedYear,
945944
// EpochTimeToMonthInYear(t) is resolvedMonth, and EpochTimeToDate(t) is 1.
946-
let epoch_days = utils::epoch_days_from_gregorian_date(resolved_year, resolved_month, 1);
945+
let epoch_days = utils::Epoch::from_gregorian_date(resolved_year, resolved_month, 1);
947946

948947
// 4. Return EpochTimeToDayNumber(t) + date - 1.
949-
epoch_days + day - 1
948+
epoch_days.days() + day - 1
950949
}
951950

952951
#[inline]
@@ -997,13 +996,13 @@ fn balance_iso_year_month(year: i32, month: i32) -> (i32, u8) {
997996
/// Note: month is 1 based.
998997
#[inline]
999998
pub(crate) fn constrain_iso_day(year: i32, month: u8, day: u8) -> u8 {
1000-
let days_in_month = utils::iso_days_in_month(year, month);
999+
let days_in_month = utils::Epoch::from_gregorian_date(year, month, 1).days_in_month();
10011000
day.clamp(1, days_in_month)
10021001
}
10031002

10041003
#[inline]
10051004
pub(crate) fn is_valid_iso_day(year: i32, month: u8, day: u8) -> bool {
1006-
let days_in_month = utils::iso_days_in_month(year, month);
1005+
let days_in_month = utils::Epoch::from_gregorian_date(year, month, 1).days_in_month();
10071006
(1..=days_in_month).contains(&day)
10081007
}
10091008

0 commit comments

Comments
 (0)