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

use NaiveDate where appropriate #118

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions rrule/src/iter/counter_date.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use chrono::{Datelike, TimeZone, Timelike, Utc, Weekday};
use chrono::{Datelike, NaiveDate, Timelike, Weekday};

use crate::{Frequency, RRule, RRuleError, Tz};

Expand Down Expand Up @@ -90,8 +90,7 @@ impl DateTimeIter {
};
let year_day = u32::from(month_range_mask[self.month as usize - 1]) + self.day - 1;
let year_day_mod = year_day % 7;
let year_start_weekday = Utc
.with_ymd_and_hms(self.year, 1, 1, 0, 0, 0)
let year_start_weekday = NaiveDate::from_ymd_opt(self.year, 1, 1)
// It should never fail, since there is always a 1st of January, is there?
.unwrap()
.weekday()
Expand Down
11 changes: 4 additions & 7 deletions rrule/src/iter/iterinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::easter::easter;
use super::{monthinfo::MonthInfo, yearinfo::YearInfo};
use crate::core::get_month;
use crate::{Frequency, NWeekday, RRule, Tz};
use chrono::{Datelike, NaiveTime, TimeZone};
use chrono::{Datelike, NaiveDate, NaiveTime};

#[derive(Debug, Clone)]
pub(crate) struct IterInfo {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl IterInfo {
self.year_info.year_len
}

pub fn year_ordinal(&self) -> i64 {
pub fn year_ordinal(&self) -> i32 {
self.year_info.year_ordinal
}

Expand Down Expand Up @@ -126,8 +126,7 @@ impl IterInfo {
let set_len = usize::from(self.year_len() + 7);

let mut date_ordinal = usize::try_from(
chrono::Utc
.with_ymd_and_hms(year, month, day, 0, 0, 0)
NaiveDate::from_ymd_opt(year, month, day)
.unwrap()
.ordinal0(),
)
Expand All @@ -152,11 +151,9 @@ impl IterInfo {
}

pub fn day_dayset(year: i32, month: u32, day: u32) -> Vec<usize> {
let date_ordinal = chrono::Utc
.with_ymd_and_hms(year, month, day, 0, 0, 0)
let date_ordinal = NaiveDate::from_ymd_opt(year, month, day)
.unwrap()
.ordinal0();

vec![usize::try_from(date_ordinal).expect("target arch should have at least 32 bits")]
}

Expand Down
6 changes: 3 additions & 3 deletions rrule/src/iter/pos_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) fn build_pos_list(
by_set_pos: &[i32],
dayset: &[usize],
timeset: &[NaiveTime],
year_ordinal: i64,
year_ordinal: i32,
tz: Tz,
) -> Vec<chrono::DateTime<Tz>> {
let mut pos_list = vec![];
Expand Down Expand Up @@ -41,8 +41,8 @@ pub(crate) fn build_pos_list(
Some(day) => day,
None => continue,
};
let day = i64::try_from(*day)
.expect("dayset is controlled by us and all elements are within range of i64");
let day = i32::try_from(*day)
.expect("dayset is controlled by us and all elements are within range of i32");

// Get ordinal which is UTC
let date = date_from_ordinal(year_ordinal + day);
Expand Down
4 changes: 2 additions & 2 deletions rrule/src/iter/rrule_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl RRuleIter {
if rrule.by_set_pos.is_empty() {
// Loop over `start..end`
for current_day in &dayset {
let current_day = i64::try_from(*current_day).expect(
"We control the dayset, and we know that it will always fit within an i64",
let current_day = i32::try_from(*current_day).expect(
"We control the dayset, and we know that it will always fit within i32",
);
let year_ordinal = self.ii.year_ordinal();
// Ordinal conversion uses UTC: if we apply local-TZ here, then
Expand Down
14 changes: 6 additions & 8 deletions rrule/src/iter/utils.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use std::ops;

use crate::core::{duration_from_midnight, Tz};
use chrono::{NaiveDate, NaiveTime, Utc};
use chrono::{Datelike, NaiveDate, NaiveTime};

const DAY_SECS: i64 = 24 * 60 * 60;
const UNIX_EPOCH_DAY: i32 = 719_163;

/// Converts number of days since unix epoch to a (naive) date.
pub(crate) fn date_from_ordinal(ordinal: i64) -> NaiveDate {
chrono::DateTime::<Utc>::from_timestamp(ordinal * DAY_SECS, 0)
.unwrap()
.date_naive()
pub(crate) fn date_from_ordinal(ordinal: i32) -> NaiveDate {
NaiveDate::from_num_days_from_ce_opt(UNIX_EPOCH_DAY + ordinal).unwrap()
}

/// Returns number of days since unix epoch (rounded down)
pub(crate) fn days_since_unix_epoch(date: &chrono::DateTime<Utc>) -> i64 {
date.timestamp() / DAY_SECS
pub(crate) fn days_since_unix_epoch(date: &NaiveDate) -> i32 {
date.num_days_from_ce() - UNIX_EPOCH_DAY
}

/// Returns true if given year is a leap year
Expand Down
8 changes: 4 additions & 4 deletions rrule/src/iter/yearinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
utils::{days_since_unix_epoch, get_year_len, pymod},
};
use crate::RRule;
use chrono::{Datelike, TimeZone, Utc};
use chrono::{Datelike, NaiveDate};

#[derive(Debug)]
pub(crate) struct BaseMasks {
Expand Down Expand Up @@ -43,7 +43,7 @@ pub(crate) struct YearInfo {
/// Number of days in the next year (365 or 366)
pub next_year_len: u16,
/// Number of days since Unix epoch
pub year_ordinal: i64,
pub year_ordinal: i32,
pub month_mask: &'static [u8],
pub month_day_mask: &'static [i8],
pub neg_month_day_mask: &'static [i8],
Expand All @@ -56,7 +56,7 @@ pub(crate) struct YearInfo {
impl YearInfo {
pub fn new(year: i32, rrule: &RRule) -> Self {
// It should never fail, since there is always a 1st of January, is there?
let first_year_day = Utc.with_ymd_and_hms(year, 1, 1, 0, 0, 0).unwrap();
let first_year_day = NaiveDate::from_ymd_opt(year, 1, 1).unwrap();

let year_len = get_year_len(year);
let next_year_len = get_year_len(year + 1);
Expand Down Expand Up @@ -167,7 +167,7 @@ impl YearInfo {
-1
} else {
let l_year_weekday = u16::try_from(
Utc.with_ymd_and_hms(year - 1, 1, 1, 0, 0, 0)
NaiveDate::from_ymd_opt(year - 1, 1, 1)
// It should never fail, since there is always a 1st of January, is there?
.unwrap()
.weekday()
Expand Down
Loading