Skip to content

Commit

Permalink
Merge pull request #943 from hove-io/clippy/chrono
Browse files Browse the repository at this point in the history
[tech] clippy/chrono : remove warnings "deprecated..."
  • Loading branch information
datanel committed Mar 7, 2024
2 parents 42c466e + 24f919a commit 763e4e9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/calendars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ use crate::serde_utils::*;
use crate::vptranslator::translate;
use crate::Result;
use anyhow::{anyhow, bail, Context};
use chrono::{self, Datelike, Weekday};
use chrono::{self, Datelike, Days, Weekday};
use serde::{Deserialize, Serialize};
use skip_error::skip_error_and_warn;
use std::collections::BTreeSet;
use std::path;
use std::{collections::BTreeSet, convert::TryFrom, path};
use tracing::info;
use typed_index_collection::*;

Expand Down Expand Up @@ -121,7 +120,11 @@ impl Calendar {
let valid_days = self.get_valid_days();
let duration = self.end_date - self.start_date;
(0..=duration.num_days())
.map(|i| self.start_date + chrono::Duration::days(i))
.filter_map(|i| {
u64::try_from(i)
.ok()
.map(|i| self.start_date + Days::new(i))
})
.filter(|d| valid_days.contains(&d.weekday()))
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ impl Collections {
// the following handles generated trip starting after midnight, we need to generate a
// new service in case the next day is not covered
let service_id = if start_time.hours() >= 24 {
let nb_days = start_time.hours() / 24;
let nb_days = u64::from(start_time.hours() / 24);
let service = self
.calendars
.get(&corresponding_vj.service_id)
Expand All @@ -1368,7 +1368,7 @@ impl Collections {
let new_dates: BTreeSet<_> = service
.dates
.iter()
.map(|d| *d + chrono::Duration::days(i64::from(nb_days)))
.map(|d| *d + chrono::Days::new(nb_days))
.collect();

let new_service = Calendar {
Expand Down
4 changes: 2 additions & 2 deletions src/ntfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::ntfs::{has_fares_v1, has_fares_v2};
use crate::objects::*;
use crate::NTFS_VERSION;
use anyhow::{anyhow, bail, Context};
use chrono::{DateTime, Duration, FixedOffset};
use chrono::{DateTime, Days, FixedOffset};
use csv::Writer;
use rust_decimal::{prelude::ToPrimitive, Decimal};
use std::collections::{BTreeSet, HashMap};
Expand Down Expand Up @@ -271,7 +271,7 @@ fn build_price_v1(id: &str, ticket: &Ticket, price: &TicketPrice) -> Result<Pric
let price_v1 = PriceV1 {
id: id.to_string(),
start_date: price.ticket_validity_start,
end_date: price.ticket_validity_end + Duration::days(1), //in fare v1 end_date is excluded, whereas in fare v2 ticket_validity_end is included
end_date: price.ticket_validity_end + Days::new(1), //in fare v1 end_date is excluded, whereas in fare v2 ticket_validity_end is included
price: cents_price,
name: ticket.name.clone(),
ignored: String::new(),
Expand Down
6 changes: 3 additions & 3 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![allow(missing_docs)]

use crate::{serde_utils::*, AddPrefix, PrefixConfiguration};
use chrono::NaiveDate;
use chrono::{Days, NaiveDate};
use chrono_tz::Tz;
use derivative::Derivative;
use geo::{Geometry as GeoGeometry, Point as GeoPoint};
Expand Down Expand Up @@ -197,8 +197,8 @@ pub struct ValidityPeriod {

impl Default for ValidityPeriod {
fn default() -> ValidityPeriod {
use chrono::{Duration, Utc};
let duration = Duration::days(15);
use chrono::Utc;
let duration = Days::new(15);
let today = Utc::now().date_naive();
let start_date = today - duration;
let end_date = today + duration;
Expand Down
15 changes: 8 additions & 7 deletions src/vptranslator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
//! See function translate

use crate::objects::{Date, ExceptionType, ValidityPeriod};
use chrono::{Datelike, Duration, Weekday};
use chrono::{Datelike, Days, Weekday};
use num_traits::cast::FromPrimitive;
use std::collections::BTreeSet;
use std::vec::Vec;

///Indicates whether service is available on the date specified.
#[derive(Debug, Eq, PartialEq)]
Expand All @@ -41,7 +40,7 @@ pub struct BlockPattern {
}

fn get_prev_monday(date: Date) -> Date {
date - Duration::days(i64::from(date.weekday().num_days_from_monday()))
date - Days::new(u64::from(date.weekday().num_days_from_monday()))
}

fn compute_validity_pattern(start_date: Date, end_date: Date, dates: &BTreeSet<Date>) -> Vec<u8> {
Expand Down Expand Up @@ -84,9 +83,9 @@ fn fill_exceptions(
exception_type: ExceptionType,
exception_list: &mut Vec<ExceptionDate>,
) {
for i in 0..7 {
for i in 0_u64..7_u64 {
if exception & (1 << i) == (1 << i) {
let date = start_date + Duration::days(i64::from(6 - i));
let date = start_date + Days::new(6 - i);
exception_list.push(ExceptionDate {
date,
exception_type: exception_type.clone(),
Expand Down Expand Up @@ -142,7 +141,7 @@ pub fn translate(dates: &BTreeSet<Date>) -> BlockPattern {
&mut exceptions_list,
);
}
monday_ref += Duration::days(7);
monday_ref = monday_ref + Days::new(7);
}
clean_extra_dates(start_date, end_date, &mut exceptions_list);
BlockPattern {
Expand All @@ -167,7 +166,9 @@ pub fn translate(dates: &BTreeSet<Date>) -> BlockPattern {
#[cfg(test)]
mod tests {
use super::*;
use chrono::Days;
use pretty_assertions::assert_eq;
use std::convert::TryFrom;

fn compute_between(start_date: Date, end_date: Date) -> Vec<u8> {
let mut dates = BTreeSet::new();
Expand Down Expand Up @@ -276,7 +277,7 @@ mod tests {
let mut res = BTreeSet::new();
for (i, c) in vpattern.chars().enumerate() {
if c == '1' {
res.insert(start_date + Duration::days(i as i64));
res.insert(start_date + Days::new(u64::try_from(i).unwrap()));
}
}
res
Expand Down

0 comments on commit 763e4e9

Please sign in to comment.