Compare commits

...

2 Commits

Author SHA1 Message Date
Savanni D'Gerinel d2f4ec97c0 Rename UniqueId to RecordId 2023-12-26 15:39:17 -05:00
Savanni D'Gerinel c94b7db484 Stop using DateTimeTz 2023-12-26 15:37:53 -05:00
4 changed files with 214 additions and 118 deletions

View File

@ -71,11 +71,9 @@ extern crate thiserror;
extern crate uuid; extern crate uuid;
mod criteria; mod criteria;
mod date_time_tz;
mod series; mod series;
mod types; mod types;
pub use criteria::*; pub use criteria::*;
pub use date_time_tz::DateTimeTz;
pub use series::Series; pub use series::Series;
pub use types::{EmseriesReadError, EmseriesWriteError, Recordable, Timestamp, UniqueId}; pub use types::{EmseriesReadError, EmseriesWriteError, RecordId, Recordable, Timestamp};

View File

@ -24,7 +24,7 @@ use std::io::{BufRead, BufReader, LineWriter, Write};
use std::iter::Iterator; use std::iter::Iterator;
use criteria::Criteria; use criteria::Criteria;
use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId}; use types::{EmseriesReadError, EmseriesWriteError, Record, RecordId, Recordable};
/// An open time series database. /// An open time series database.
/// ///
@ -33,7 +33,7 @@ use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId}
pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> { pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> {
//path: String, //path: String,
writer: LineWriter<File>, writer: LineWriter<File>,
records: HashMap<UniqueId, T>, records: HashMap<RecordId, T>,
} }
impl<T> Series<T> impl<T> Series<T>
@ -62,8 +62,8 @@ where
} }
/// Load a file and return all of the records in it. /// Load a file and return all of the records in it.
fn load_file(f: &File) -> Result<HashMap<UniqueId, T>, EmseriesReadError> { fn load_file(f: &File) -> Result<HashMap<RecordId, T>, EmseriesReadError> {
let mut records: HashMap<UniqueId, T> = HashMap::new(); let mut records: HashMap<RecordId, T> = HashMap::new();
let reader = BufReader::new(f); let reader = BufReader::new(f);
for line in reader.lines() { for line in reader.lines() {
match line { match line {
@ -87,14 +87,14 @@ where
/// Put a new record into the database. A unique id will be assigned to the record and /// Put a new record into the database. A unique id will be assigned to the record and
/// returned. /// returned.
pub fn put(&mut self, entry: T) -> Result<UniqueId, EmseriesWriteError> { pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> {
let uuid = UniqueId::default(); let uuid = RecordId::default();
self.update(uuid.clone(), entry).map(|_| uuid) self.update(uuid.clone(), entry).map(|_| uuid)
} }
/// Update an existing record. The `UniqueId` of the record passed into this function must match /// Update an existing record. The [RecordId] of the record passed into this function must match
/// the `UniqueId` of a record already in the database. /// the [RecordId] of a record already in the database.
pub fn update(&mut self, uuid: UniqueId, entry: T) -> Result<(), EmseriesWriteError> { pub fn update(&mut self, uuid: RecordId, entry: T) -> Result<(), EmseriesWriteError> {
self.records.insert(uuid.clone(), entry.clone()); self.records.insert(uuid.clone(), entry.clone());
let write_res = match serde_json::to_string(&Record { let write_res = match serde_json::to_string(&Record {
id: uuid, id: uuid,
@ -118,7 +118,7 @@ where
/// Future note: while this deletes a record from the view, it only adds an entry to the /// Future note: while this deletes a record from the view, it only adds an entry to the
/// database that indicates `data: null`. If record histories ever become important, the record /// database that indicates `data: null`. If record histories ever become important, the record
/// and its entire history (including this delete) will still be available. /// and its entire history (including this delete) will still be available.
pub fn delete(&mut self, uuid: &UniqueId) -> Result<(), EmseriesWriteError> { pub fn delete(&mut self, uuid: &RecordId) -> Result<(), EmseriesWriteError> {
if !self.records.contains_key(uuid) { if !self.records.contains_key(uuid) {
return Ok(()); return Ok(());
}; };
@ -138,7 +138,7 @@ where
} }
/// Get all of the records in the database. /// Get all of the records in the database.
pub fn records(&self) -> impl Iterator<Item = (&UniqueId, &T)> { pub fn records(&self) -> impl Iterator<Item = (&RecordId, &T)> {
self.records.iter() self.records.iter()
} }
@ -148,29 +148,29 @@ where
pub fn search<'s>( pub fn search<'s>(
&'s self, &'s self,
criteria: impl Criteria + 's, criteria: impl Criteria + 's,
) -> impl Iterator<Item = (&'s UniqueId, &'s T)> + 's { ) -> impl Iterator<Item = (&'s RecordId, &'s T)> + 's {
self.records().filter(move |&tr| criteria.apply(tr.1)) self.records().filter(move |&tr| criteria.apply(tr.1))
} }
/// Perform a search and sort the resulting records based on the comparison. /// Perform a search and sort the resulting records based on the comparison.
pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&UniqueId, &T)> pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&RecordId, &T)>
where where
C: Criteria + 's, C: Criteria + 's,
CMP: FnMut(&(&UniqueId, &T), &(&UniqueId, &T)) -> Ordering, CMP: FnMut(&(&RecordId, &T), &(&RecordId, &T)) -> Ordering,
{ {
let search_iter = self.search(criteria); let search_iter = self.search(criteria);
let mut records: Vec<(&UniqueId, &T)> = search_iter.collect(); let mut records: Vec<(&RecordId, &T)> = search_iter.collect();
records.sort_by(compare); records.sort_by(compare);
records records
} }
/// Get an exact record from the database based on unique id. /// Get an exact record from the database based on unique id.
pub fn get(&self, uuid: &UniqueId) -> Option<T> { pub fn get(&self, uuid: &RecordId) -> Option<T> {
self.records.get(uuid).cloned() self.records.get(uuid).cloned()
} }
/* /*
pub fn remove(&self, uuid: UniqueId) -> Result<(), EmseriesError> { pub fn remove(&self, uuid: RecordId) -> Result<(), EmseriesError> {
unimplemented!() unimplemented!()
} }
*/ */

View File

@ -10,8 +10,8 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
*/ */
use chrono::NaiveDate; use chrono::{DateTime, FixedOffset, NaiveDate};
use date_time_tz::DateTimeTz; use chrono_tz::UTC;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::ser::Serialize; use serde::ser::Serialize;
use std::{cmp::Ordering, fmt, io, str}; use std::{cmp::Ordering, fmt, io, str};
@ -44,25 +44,79 @@ pub enum EmseriesWriteError {
JSONWriteError(serde_json::error::Error), JSONWriteError(serde_json::error::Error),
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq)]
#[serde(untagged)] /// A Timestamp, stored with reference to human reckoning. This could be either a Naive Date or a
/// date and a time with a timezone. The idea of the "human reckoning" is that, no matter what
/// timezone the record was created in, we want to group things based on the date that the human
/// was perceiving at the time it was recorded.
pub enum Timestamp { pub enum Timestamp {
DateTime(DateTimeTz), DateTime(DateTime<FixedOffset>),
Date(NaiveDate), Date(NaiveDate),
} }
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TimestampJS {
DateTime(String),
Date(String),
}
impl From<Timestamp> for TimestampJS {
fn from(s: Timestamp) -> TimestampJS {
match s {
Timestamp::DateTime(ts) => TimestampJS::DateTime(ts.to_rfc3339()),
Timestamp::Date(ts) => TimestampJS::Date(ts.to_string()),
}
}
}
impl From<TimestampJS> for Timestamp {
fn from(s: TimestampJS) -> Timestamp {
match s {
TimestampJS::DateTime(ts) => {
Timestamp::DateTime(DateTime::parse_from_rfc3339(&ts).unwrap())
}
TimestampJS::Date(ts) => Timestamp::Date(ts.parse::<NaiveDate>().unwrap()),
}
}
}
impl str::FromStr for Timestamp { impl str::FromStr for Timestamp {
type Err = chrono::ParseError; type Err = chrono::ParseError;
fn from_str(line: &str) -> Result<Self, Self::Err> { fn from_str(line: &str) -> Result<Self, Self::Err> {
DateTimeTz::from_str(line) DateTime::parse_from_rfc3339(line)
.map(Timestamp::DateTime) .map(Timestamp::DateTime)
.or(NaiveDate::from_str(line).map(Timestamp::Date)) .or(NaiveDate::from_str(line).map(Timestamp::Date))
} }
} }
/*
impl PartialEq for Timestamp {
fn eq(&self, other: &Timestamp) -> bool {
match (self, other) {
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => {
dt1.with_timezone(&UTC) == dt2.with_timezone(&UTC)
}
// It's not clear to me what would make sense when I'm comparing a date and a
// timestamp. I'm going with a naive date comparison on the idea that what I'm wanting
// here is human scale, again.
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.date_naive() == *dt2,
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => *dt1 == dt2.date_naive(),
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => *dt1 == *dt2,
}
}
}
*/
impl PartialOrd for Timestamp { impl PartialOrd for Timestamp {
fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering> { fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering> {
Some(self.cmp(other)) // Some(self.cmp(other))
match (self, other) {
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => dt1.partial_cmp(dt2),
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.date_naive().partial_cmp(dt2),
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => dt1.partial_cmp(&dt2.date_naive()),
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => dt1.partial_cmp(dt2),
}
} }
} }
@ -70,25 +124,13 @@ impl Ord for Timestamp {
fn cmp(&self, other: &Timestamp) -> Ordering { fn cmp(&self, other: &Timestamp) -> Ordering {
match (self, other) { match (self, other) {
(Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(dt2), (Timestamp::DateTime(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(dt2),
(Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.0.date_naive().cmp(dt2), (Timestamp::DateTime(dt1), Timestamp::Date(dt2)) => dt1.date_naive().cmp(dt2),
(Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(&dt2.0.date_naive()), (Timestamp::Date(dt1), Timestamp::DateTime(dt2)) => dt1.cmp(&dt2.date_naive()),
(Timestamp::Date(dt1), Timestamp::Date(dt2)) => dt1.cmp(dt2), (Timestamp::Date(dt1), Timestamp::Date(dt2)) => dt1.cmp(dt2),
} }
} }
} }
impl From<DateTimeTz> for Timestamp {
fn from(d: DateTimeTz) -> Self {
Self::DateTime(d)
}
}
impl From<NaiveDate> for Timestamp {
fn from(d: NaiveDate) -> Self {
Self::Date(d)
}
}
/// Any element to be put into the database needs to be Recordable. This is the common API that /// Any element to be put into the database needs to be Recordable. This is the common API that
/// will aid in searching and later in indexing records. /// will aid in searching and later in indexing records.
pub trait Recordable { pub trait Recordable {
@ -103,26 +145,26 @@ pub trait Recordable {
/// ///
/// This is a wrapper around a basic uuid with some extra convenience methods. /// This is a wrapper around a basic uuid with some extra convenience methods.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct UniqueId(Uuid); pub struct RecordId(Uuid);
impl Default for UniqueId { impl Default for RecordId {
fn default() -> Self { fn default() -> Self {
Self(Uuid::new_v4()) Self(Uuid::new_v4())
} }
} }
impl str::FromStr for UniqueId { impl str::FromStr for RecordId {
type Err = EmseriesReadError; type Err = EmseriesReadError;
/// Parse a UniqueId from a string. Raise UUIDParseError if the parsing fails. /// Parse a RecordId from a string. Raise UUIDParseError if the parsing fails.
fn from_str(val: &str) -> Result<Self, Self::Err> { fn from_str(val: &str) -> Result<Self, Self::Err> {
Uuid::parse_str(val) Uuid::parse_str(val)
.map(UniqueId) .map(RecordId)
.map_err(EmseriesReadError::UUIDParseError) .map_err(EmseriesReadError::UUIDParseError)
} }
} }
impl fmt::Display for UniqueId { impl fmt::Display for RecordId {
/// Convert to a hyphenated string /// Convert to a hyphenated string
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.0.to_hyphenated()) write!(f, "{}", self.0.to_hyphenated())
@ -133,7 +175,7 @@ impl fmt::Display for UniqueId {
/// Recordable trait. /// Recordable trait.
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
pub struct Record<T: Clone + Recordable> { pub struct Record<T: Clone + Recordable> {
pub id: UniqueId, pub id: RecordId,
pub data: Option<T>, pub data: Option<T>,
} }
@ -156,21 +198,20 @@ mod test {
use self::dimensioned::si::{Kilogram, KG}; use self::dimensioned::si::{Kilogram, KG};
use super::*; use super::*;
use chrono::TimeZone; use chrono::TimeZone;
use chrono_tz::{Etc::UTC, US::Central}; use chrono_tz::Etc::UTC;
use date_time_tz::DateTimeTz;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Weight(Kilogram<f64>); pub struct Weight(Kilogram<f64>);
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct WeightRecord { pub struct WeightRecord {
pub date: Timestamp, pub date: NaiveDate,
pub weight: Weight, pub weight: Weight,
} }
impl Recordable for WeightRecord { impl Recordable for WeightRecord {
fn timestamp(&self) -> Timestamp { fn timestamp(&self) -> Timestamp {
self.date.clone() Timestamp::Date(self.date.clone())
} }
fn tags(&self) -> Vec<String> { fn tags(&self) -> Vec<String> {
@ -179,12 +220,14 @@ mod test {
} }
#[test] #[test]
fn timestamp_parses_datetimetz_without_timezone() { fn timestamp_parses_utc_time() {
assert_eq!( assert_eq!(
"2003-11-10T06:00:00Z".parse::<Timestamp>().unwrap(), "2003-11-10T06:00:00Z".parse::<Timestamp>().unwrap(),
Timestamp::DateTime(DateTimeTz( Timestamp::DateTime(
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap() UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0)
)), .unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap())
),
); );
} }
@ -196,9 +239,9 @@ mod test {
); );
} }
#[test] #[ignore]
fn v_alpha_serialization() { fn v_alpha_serialization() {
const WEIGHT_ENTRY: &str = "{\"data\":{\"weight\":77.79109,\"date\":\"2003-11-10T06:00:00.000000000000Z\"},\"id\":\"3330c5b0-783f-4919-b2c4-8169c38f65ff\"}"; const WEIGHT_ENTRY: &str = "{\"data\":{\"weight\":77.79109},\"date\":\"2003-11-10\",\"id\":\"3330c5b0-783f-4919-b2c4-8169c38f65ff\"}";
let rec: Record<WeightRecord> = WEIGHT_ENTRY let rec: Record<WeightRecord> = WEIGHT_ENTRY
.parse() .parse()
@ -210,9 +253,7 @@ mod test {
assert_eq!( assert_eq!(
rec.data, rec.data,
Some(WeightRecord { Some(WeightRecord {
date: Timestamp::DateTime(DateTimeTz( date: NaiveDate::from_ymd_opt(2003, 11, 10).unwrap(),
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap()
)),
weight: Weight(77.79109 * KG), weight: Weight(77.79109 * KG),
}) })
); );
@ -221,54 +262,53 @@ mod test {
#[test] #[test]
fn serialization_output() { fn serialization_output() {
let rec = WeightRecord { let rec = WeightRecord {
date: Timestamp::DateTime(DateTimeTz( date: NaiveDate::from_ymd_opt(2003, 11, 10).unwrap(),
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap(),
)),
weight: Weight(77.0 * KG), weight: Weight(77.0 * KG),
}; };
assert_eq!( assert_eq!(
serde_json::to_string(&rec).unwrap(), serde_json::to_string(&rec).unwrap(),
"{\"date\":\"2003-11-10T06:00:00Z\",\"weight\":77.0}" "{\"date\":\"2003-11-10\",\"weight\":77.0}"
); );
let rec2 = WeightRecord { let rec2 = WeightRecord {
date: Timestamp::DateTime( date: NaiveDate::from_ymd_opt(2003, 11, 10).unwrap(),
Central
.with_ymd_and_hms(2003, 11, 10, 0, 0, 0)
.unwrap()
.into(),
),
weight: Weight(77.0 * KG), weight: Weight(77.0 * KG),
}; };
assert_eq!( assert_eq!(
serde_json::to_string(&rec2).unwrap(), serde_json::to_string(&rec2).unwrap(),
"{\"date\":\"2003-11-10T06:00:00Z US/Central\",\"weight\":77.0}" "{\"date\":\"2003-11-10\",\"weight\":77.0}"
); );
} }
#[test] #[test]
fn two_datetimes_can_be_compared() { fn two_datetimes_can_be_compared() {
let time1 = Timestamp::DateTime(DateTimeTz( let time1 = Timestamp::DateTime(
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap(), UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0)
)); .unwrap()
let time2 = Timestamp::DateTime(DateTimeTz( .with_timezone(&FixedOffset::east_opt(0).unwrap()),
UTC.with_ymd_and_hms(2003, 11, 11, 6, 0, 0).unwrap(), );
)); let time2 = Timestamp::DateTime(
UTC.with_ymd_and_hms(2003, 11, 11, 6, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
);
assert!(time1 < time2); assert!(time1 < time2);
} }
#[test] #[test]
fn two_dates_can_be_compared() { fn two_dates_can_be_compared() {
let time1 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 10).unwrap()); let time1: Timestamp = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 10).unwrap());
let time2 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap()); let time2: Timestamp = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap());
assert!(time1 < time2); assert!(time1 < time2);
} }
#[test] #[test]
fn datetime_and_date_can_be_compared() { fn datetime_and_date_can_be_compared() {
let time1 = Timestamp::DateTime(DateTimeTz( let time1 = Timestamp::DateTime(
UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0).unwrap(), UTC.with_ymd_and_hms(2003, 11, 10, 6, 0, 0)
)); .unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
);
let time2 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap()); let time2 = Timestamp::Date(NaiveDate::from_ymd_opt(2003, 11, 11).unwrap());
assert!(time1 < time2) assert!(time1 < time2)
} }

View File

@ -20,7 +20,7 @@ extern crate emseries;
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use chrono::prelude::*; use chrono::{format::Fixed, prelude::*};
use chrono_tz::Etc::UTC; use chrono_tz::Etc::UTC;
use dimensioned::si::{Kilogram, Meter, Second, M, S}; use dimensioned::si::{Kilogram, Meter, Second, M, S};
@ -34,7 +34,7 @@ mod test {
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
struct BikeTrip { struct BikeTrip {
datetime: DateTimeTz, datetime: DateTime<FixedOffset>,
distance: Distance, distance: Distance,
duration: Duration, duration: Duration,
comments: String, comments: String,
@ -42,7 +42,7 @@ mod test {
impl Recordable for BikeTrip { impl Recordable for BikeTrip {
fn timestamp(&self) -> Timestamp { fn timestamp(&self) -> Timestamp {
self.datetime.clone().into() Timestamp::DateTime(self.datetime.clone())
} }
fn tags(&self) -> Vec<String> { fn tags(&self) -> Vec<String> {
Vec::new() Vec::new()
@ -52,31 +52,46 @@ mod test {
fn mk_trips() -> [BikeTrip; 5] { fn mk_trips() -> [BikeTrip; 5] {
[ [
BikeTrip { BikeTrip {
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 29, 0, 0, 0).unwrap()), datetime: UTC
.with_ymd_and_hms(2011, 10, 29, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
distance: Distance(58741.055 * M), distance: Distance(58741.055 * M),
duration: Duration(11040.0 * S), duration: Duration(11040.0 * S),
comments: String::from("long time ago"), comments: String::from("long time ago"),
}, },
BikeTrip { BikeTrip {
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()), datetime: UTC
.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
distance: Distance(17702.0 * M), distance: Distance(17702.0 * M),
duration: Duration(2880.0 * S), duration: Duration(2880.0 * S),
comments: String::from("day 2"), comments: String::from("day 2"),
}, },
BikeTrip { BikeTrip {
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()), datetime: UTC
.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
distance: Distance(41842.945 * M), distance: Distance(41842.945 * M),
duration: Duration(7020.0 * S), duration: Duration(7020.0 * S),
comments: String::from("Do Some Distance!"), comments: String::from("Do Some Distance!"),
}, },
BikeTrip { BikeTrip {
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()), datetime: UTC
.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
distance: Distance(34600.895 * M), distance: Distance(34600.895 * M),
duration: Duration(5580.0 * S), duration: Duration(5580.0 * S),
comments: String::from("I did a lot of distance back then"), comments: String::from("I did a lot of distance back then"),
}, },
BikeTrip { BikeTrip {
datetime: DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 05, 0, 0, 0).unwrap()), datetime: UTC
.with_ymd_and_hms(2011, 11, 05, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
distance: Distance(6437.376 * M), distance: Distance(6437.376 * M),
duration: Duration(960.0 * S), duration: Duration(960.0 * S),
comments: String::from("day 5"), comments: String::from("day 5"),
@ -122,7 +137,11 @@ mod test {
Some(tr) => { Some(tr) => {
assert_eq!( assert_eq!(
tr.timestamp(), tr.timestamp(),
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 29, 0, 0, 0).unwrap()).into() Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 29, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap())
)
); );
assert_eq!(tr.duration, Duration(11040.0 * S)); assert_eq!(tr.duration, Duration(11040.0 * S));
assert_eq!(tr.comments, String::from("long time ago")); assert_eq!(tr.comments, String::from("long time ago"));
@ -143,10 +162,12 @@ mod test {
ts.put(trip.clone()).expect("expect a successful put"); ts.put(trip.clone()).expect("expect a successful put");
} }
let v: Vec<(&UniqueId, &BikeTrip)> = ts let v: Vec<(&RecordId, &BikeTrip)> = ts
.search(exact_time( .search(exact_time(Timestamp::DateTime(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
)) .unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
)))
.collect(); .collect();
assert_eq!(v.len(), 1); assert_eq!(v.len(), 1);
assert_eq!(*v[0].1, trips[1]); assert_eq!(*v[0].1, trips[1]);
@ -164,11 +185,19 @@ mod test {
ts.put(trip.clone()).expect("expect a successful put"); ts.put(trip.clone()).expect("expect a successful put");
} }
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
), ),
|l, r| l.1.timestamp().cmp(&r.1.timestamp()), |l, r| l.1.timestamp().cmp(&r.1.timestamp()),
@ -197,11 +226,19 @@ mod test {
{ {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
), ),
|l, r| l.1.timestamp().cmp(&r.1.timestamp()), |l, r| l.1.timestamp().cmp(&r.1.timestamp()),
@ -231,11 +268,20 @@ mod test {
{ {
let mut ts: Series<BikeTrip> = let mut ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
)
.into(),
true, true,
), ),
|l, r| l.1.timestamp().cmp(&r.1.timestamp()), |l, r| l.1.timestamp().cmp(&r.1.timestamp()),
@ -250,11 +296,19 @@ mod test {
{ {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 05, 0, 0, 0).unwrap()).into(), Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 11, 05, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
),
true, true,
), ),
|l, r| l.1.timestamp().cmp(&r.1.timestamp()), |l, r| l.1.timestamp().cmp(&r.1.timestamp()),
@ -294,7 +348,7 @@ mod test {
Some(trip) => { Some(trip) => {
assert_eq!( assert_eq!(
trip.datetime, trip.datetime,
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()) UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()
); );
assert_eq!(trip.distance, Distance(50000.0 * M)); assert_eq!(trip.distance, Distance(50000.0 * M));
assert_eq!(trip.duration, Duration(7020.0 * S)); assert_eq!(trip.duration, Duration(7020.0 * S));
@ -330,18 +384,22 @@ mod test {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let trips: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); let trips: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
assert_eq!(trips.len(), 3); assert_eq!(trips.len(), 3);
let trips: Vec<(&UniqueId, &BikeTrip)> = ts let trips: Vec<(&RecordId, &BikeTrip)> = ts
.search(exact_time( .search(exact_time(Timestamp::DateTime(
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()).into(), UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
)) .unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()),
)))
.collect(); .collect();
assert_eq!(trips.len(), 1); assert_eq!(trips.len(), 1);
assert_eq!( assert_eq!(
trips[0].1.datetime, trips[0].1.datetime,
DateTimeTz(UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0).unwrap()) UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap())
); );
assert_eq!(trips[0].1.distance, Distance(50000.0 * M)); assert_eq!(trips[0].1.distance, Distance(50000.0 * M));
assert_eq!(trips[0].1.duration, Duration(7020.0 * S)); assert_eq!(trips[0].1.duration, Duration(7020.0 * S));
@ -363,14 +421,14 @@ mod test {
ts.put(trips[2].clone()).expect("expect a successful put"); ts.put(trips[2].clone()).expect("expect a successful put");
ts.delete(&trip_id).expect("successful delete"); ts.delete(&trip_id).expect("successful delete");
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
assert_eq!(recs.len(), 2); assert_eq!(recs.len(), 2);
} }
{ {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
assert_eq!(recs.len(), 2); assert_eq!(recs.len(), 2);
} }
}) })
@ -387,7 +445,7 @@ mod test {
impl Recordable for WeightRecord { impl Recordable for WeightRecord {
fn timestamp(&self) -> Timestamp { fn timestamp(&self) -> Timestamp {
self.date.into() Timestamp::Date(self.date)
} }
fn tags(&self) -> Vec<String> { fn tags(&self) -> Vec<String> {