Skip to content
Open
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
14 changes: 11 additions & 3 deletions vrp-pragmatic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod discovery;
pub mod regression;

pub use vrp_core as core;
use vrp_core::models::common::Timestamp;

mod utils;

Expand All @@ -36,8 +37,8 @@ pub mod validation;

use crate::format::problem::Problem;
use crate::format::{CoordIndex, Location};
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
use time::{Date, OffsetDateTime, Time, UtcOffset};
use vrp_core::prelude::{Float, GenericError};

/// Get lists of unique locations in the problem. Use it to request routing matrix from outside.
Expand All @@ -46,9 +47,16 @@ pub fn get_unique_locations(problem: &Problem) -> Vec<Location> {
CoordIndex::new(problem).unique()
}

fn format_time(time: Float) -> String {
// vrp_core's Timestamps are f64s that could go far beyond what unix timestamps support
const MIN_TIMESTAMP: i64 = OffsetDateTime::new_in_offset(Date::MIN, Time::MIDNIGHT, UtcOffset::UTC).unix_timestamp();
const MAX_TIMESTAMP: i64 = OffsetDateTime::new_in_offset(Date::MAX, Time::MAX, UtcOffset::UTC).unix_timestamp();

fn format_time(time: Timestamp) -> String {
let time: i64 = (time as i64).clamp(MIN_TIMESTAMP, MAX_TIMESTAMP);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't seen anything near MIN_TIMESTAMP but the handling is the same and it reduces the window of failure

// TODO avoid using implicitly unwrap
OffsetDateTime::from_unix_timestamp(time as i64).map(|time| time.format(&Rfc3339).unwrap()).unwrap()
// (a priori the above clamping should prevent any potential failure here...)
let ts = OffsetDateTime::from_unix_timestamp(time).expect("Could not convert value to timestamp");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not to get too in the weeds but the error message should be a &'static str and this expect call ... shouldn't really cost much/anythig comp[are to unwrap

return ts.format(&Rfc3339).expect("Error formatting timestamp to time");
}

fn parse_time(time: &str) -> Float {
Expand Down