Skip to content

Commit

Permalink
Remove num-integer dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 23, 2023
1 parent 84bbea5 commit 267e59d
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 43 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -29,7 +29,6 @@ __doctest = []

[dependencies]
time = { version = "0.1.43", optional = true }
num-integer = { version = "0.1.36", default-features = false }
num-traits = { version = "0.2", default-features = false }
rustc-serialize = { version = "0.3.20", optional = true }
serde = { version = "1.0.99", default-features = false, optional = true }
Expand Down
10 changes: 4 additions & 6 deletions src/format/mod.rs
Expand Up @@ -510,8 +510,6 @@ fn format_inner(
) -> fmt::Result {
let locale = Locales::new(locale);

use num_integer::{div_floor, mod_floor};

match *item {
Item::Literal(s) | Item::Space(s) => result.push_str(s),
#[cfg(any(feature = "alloc", feature = "std", test))]
Expand All @@ -525,11 +523,11 @@ fn format_inner(

let (width, v) = match *spec {
Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
YearDiv100 => (2, date.map(|d| i64::from(d.year()).div_euclid(100))),
YearMod100 => (2, date.map(|d| i64::from(d.year()).rem_euclid(100))),
IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))),
IsoYearDiv100 => (2, date.map(|d| div_floor(i64::from(d.iso_week().year()), 100))),
IsoYearMod100 => (2, date.map(|d| mod_floor(i64::from(d.iso_week().year()), 100))),
IsoYearDiv100 => (2, date.map(|d| i64::from(d.iso_week().year()).div_euclid(100))),
IsoYearMod100 => (2, date.map(|d| i64::from(d.iso_week().year()).rem_euclid(100))),
Month => (2, date.map(|d| i64::from(d.month()))),
Day => (2, date.map(|d| i64::from(d.day()))),
WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),
Expand Down
10 changes: 4 additions & 6 deletions src/format/parsed.rs
Expand Up @@ -4,7 +4,6 @@
//! A collection of parsed date and time items.
//! They can be constructed incrementally while being checked for consistency.

use num_integer::div_rem;
use num_traits::ToPrimitive;

use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
Expand Down Expand Up @@ -310,7 +309,8 @@ impl Parsed {
if y < 0 {
return Err(OUT_OF_RANGE);
}
let (q_, r_) = div_rem(y, 100);
let q_ = y / 100;
let r_ = y % 100;
if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ {
Ok(Some(y))
} else {
Expand Down Expand Up @@ -345,8 +345,7 @@ impl Parsed {
let verify_ymd = |date: NaiveDate| {
let year = date.year();
let (year_div_100, year_mod_100) = if year >= 0 {
let (q, r) = div_rem(year, 100);
(Some(q), Some(r))
(Some(year / 100), Some(year % 100))
} else {
(None, None) // they should be empty to be consistent
};
Expand All @@ -366,8 +365,7 @@ impl Parsed {
let isoweek = week.week();
let weekday = date.weekday();
let (isoyear_div_100, isoyear_mod_100) = if isoyear >= 0 {
let (q, r) = div_rem(isoyear, 100);
(Some(q), Some(r))
(Some(isoyear / 100), Some(isoyear % 100))
} else {
(None, None) // they should be empty to be consistent
};
Expand Down
5 changes: 4 additions & 1 deletion src/naive/date.rs
Expand Up @@ -9,7 +9,6 @@ use core::convert::TryFrom;
use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
use core::{fmt, str};

use num_integer::div_mod_floor;
use num_traits::ToPrimitive;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};
Expand Down Expand Up @@ -56,6 +55,10 @@ const MIN_DAYS_FROM_YEAR_0: i32 = (MIN_YEAR + 400_000) * 365 + (MIN_YEAR + 400_0
#[cfg(test)] // only used for testing, but duplicated in naive::datetime
const MAX_BITS: usize = 44;

fn div_mod_floor(val: i32, div: i32) -> (i32, i32) {
(val.div_euclid(div), val.rem_euclid(div))
}

/// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first
/// day of the week.
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/mod.rs
Expand Up @@ -10,7 +10,6 @@ use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, str};

use num_integer::div_mod_floor;
use num_traits::ToPrimitive;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};
Expand Down Expand Up @@ -239,7 +238,8 @@ impl NaiveDateTime {
#[inline]
#[must_use]
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
let (days, secs) = div_mod_floor(secs, 86_400);
let days = secs.div_euclid(86_400);
let secs = secs.rem_euclid(86_400);
let date = days
.to_i32()
.and_then(|days| days.checked_add(719_163))
Expand Down
6 changes: 3 additions & 3 deletions src/naive/internals.rs
Expand Up @@ -17,7 +17,6 @@

use crate::Weekday;
use core::{fmt, i32};
use num_integer::{div_rem, mod_floor};
use num_traits::FromPrimitive;

/// The internal date representation. This also includes the packed `Mdf` value.
Expand Down Expand Up @@ -95,7 +94,8 @@ static YEAR_DELTAS: [u8; 401] = [
];

pub(super) fn cycle_to_yo(cycle: u32) -> (u32, u32) {
let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365);
let mut year_mod_400 = cycle / 365;
let mut ordinal0 = cycle % 365;
let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]);
if ordinal0 < delta {
year_mod_400 -= 1;
Expand All @@ -116,7 +116,7 @@ impl YearFlags {
#[inline]
#[must_use]
pub fn from_year(year: i32) -> YearFlags {
let year = mod_floor(year, 400);
let year = year.rem_euclid(400);
YearFlags::from_year_mod_400(year)
}

Expand Down
7 changes: 4 additions & 3 deletions src/naive/time/mod.rs
Expand Up @@ -8,7 +8,6 @@ use core::borrow::Borrow;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, str};

use num_integer::div_mod_floor;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -762,8 +761,10 @@ impl NaiveTime {

/// Returns a triple of the hour, minute and second numbers.
fn hms(&self) -> (u32, u32, u32) {
let (mins, sec) = div_mod_floor(self.secs, 60);
let (hour, min) = div_mod_floor(mins, 60);
let sec = self.secs % 60;
let mins = self.secs / 60;
let min = mins % 60;
let hour = mins / 60;
(hour, min, sec)
}

Expand Down
7 changes: 4 additions & 3 deletions src/offset/fixed.rs
Expand Up @@ -6,7 +6,6 @@
use core::fmt;
use core::ops::{Add, Sub};

use num_integer::div_mod_floor;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -140,8 +139,10 @@ impl fmt::Debug for FixedOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let offset = self.local_minus_utc;
let (sign, offset) = if offset < 0 { ('-', -offset) } else { ('+', offset) };
let (mins, sec) = div_mod_floor(offset, 60);
let (hour, min) = div_mod_floor(mins, 60);
let sec = offset.rem_euclid(60);
let mins = offset.div_euclid(60);
let min = mins.rem_euclid(60);
let hour = mins.div_euclid(60);
if sec == 0 {
write!(f, "{}{:02}:{:02}", sign, hour, min)
} else {
Expand Down
19 changes: 1 addition & 18 deletions src/oldtime.rs
Expand Up @@ -462,26 +462,9 @@ impl Error for OutOfRangeError {
}
}

// Copied from libnum
#[inline]
const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
(div_floor_64(this, other), mod_floor_64(this, other))
}

#[inline]
const fn div_floor_64(this: i64, other: i64) -> i64 {
match div_rem_64(this, other) {
(d, r) if (r > 0 && other < 0) || (r < 0 && other > 0) => d - 1,
(d, _) => d,
}
}

#[inline]
const fn mod_floor_64(this: i64, other: i64) -> i64 {
match this % other {
r if (r > 0 && other < 0) || (r < 0 && other > 0) => r + other,
r => r,
}
(this.div_euclid(other), this.rem_euclid(other))
}

#[inline]
Expand Down

0 comments on commit 267e59d

Please sign in to comment.