Skip to content

Commit

Permalink
Fix geneactiv timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed Apr 3, 2024
1 parent 53a4f45 commit 84cc54e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
38 changes: 19 additions & 19 deletions src/geneactiv/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,25 @@ pub mod id {
}
}

pub fn parse_date_time(date_time: &str) -> chrono::DateTime<chrono::Utc> {
chrono::NaiveDateTime::parse_from_str(date_time, "%Y-%m-%d %H:%M:%S:%3f")
.map(|dt| dt.and_utc())
.unwrap_or(chrono::DateTime::<chrono::Utc>::from_timestamp(0, 0).unwrap())
}

pub fn parse_date(date: &str) -> chrono::NaiveDate {
chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d")
.unwrap_or(chrono::NaiveDate::from_ymd_opt(1900, 1, 1).unwrap())
}

#[allow(dead_code)]
pub mod data {
use crate::geneactiv::id;
use chrono::{DateTime, NaiveDate, Utc};
use struct_iterable::Iterable;

use super::{parse_date, parse_date_time};

#[derive(Debug, Iterable)]
pub struct DeviceIdentity {
pub serial: String,
Expand All @@ -190,12 +203,7 @@ pub mod data {
id::identity::TYPE => self.device_type = value.to_string(),
id::identity::MODEL => self.model = value.to_string(),
id::identity::FIRMWARE => self.firmware = value.to_string(),
id::identity::CALIBRATION_DATE => {
self.calibration_date =
DateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S:%3f")
.unwrap()
.to_utc();
}
id::identity::CALIBRATION_DATE => self.calibration_date = parse_date_time(value),
_ => {}
}
}
Expand Down Expand Up @@ -286,9 +294,7 @@ pub mod data {
self.measurement_period = value.to_string()
}
id::configuration::START_TIME => {
self.start_time = DateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S:%3f")
.unwrap()
.to_utc();
self.start_time = parse_date_time(value)
}
id::configuration::TIME_ZONE => self.time_zone = value.to_string(),
_ => {}
Expand Down Expand Up @@ -334,16 +340,12 @@ pub mod data {
id::trial::EXERCISE_TYPE => self.exercise_type = value.to_string(),
id::trial::CONFIG_OPERATOR_ID => self.config_operator_id = value.to_string(),
id::trial::CONFIG_TIME => {
self.config_time = DateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S:%3f")
.unwrap()
.to_utc();
self.config_time = parse_date_time(value)
}
id::trial::CONFIG_NOTES => self.config_notes = value.to_string(),
id::trial::EXTRACT_OPERATOR_ID => self.extract_operator_id = value.to_string(),
id::trial::EXTRACT_TIME => {
self.extract_time = DateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S:%3f")
.unwrap()
.to_utc();
self.extract_time = parse_date_time(value)
}
id::trial::EXTRACT_NOTES => self.extract_notes = value.to_string(),
_ => {}
Expand Down Expand Up @@ -382,7 +384,7 @@ pub mod data {
id::subject::LOCATION_CODE => self.location_code = value.to_string(),
id::subject::CODE => self.code = value.to_string(),
id::subject::DATE_OF_BIRTH => {
self.date_of_birth = NaiveDate::parse_from_str(value, "%Y-%m-%d").unwrap();
self.date_of_birth = parse_date(value);
}
id::subject::SEX => self.sex = value.to_string(),
id::subject::HEIGHT => self.height = value.to_string(),
Expand Down Expand Up @@ -522,9 +524,7 @@ pub mod data {
id::record::SERIAL => self.device_unique_serial_code = value.to_string(),
id::record::SEQUENCE => self.sequence_number = value.parse::<i32>().unwrap(),
id::record::PAGE_TIME => {
self.page_time = DateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S:%3f")
.unwrap()
.to_utc();
self.page_time = parse_date_time(value);
}
id::record::UNASSIGNED => self.unassigned = value.to_string(),
id::record::TEMPERATURE => self.temperature = value.parse::<f32>().unwrap(),
Expand Down
9 changes: 5 additions & 4 deletions src/geneactiv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ pub fn load_data(path: String) -> AccelerometerData {
let line = lines_record[i].trim();
if let Some(measurement_frequency) = parse_value(line, id::record::MEASUREMENT_FREQUENCY) {
record_header.measurement_frequency = measurement_frequency;
} else if let Some(page_time) = parse_value(line, id::record::PAGE_TIME) {
record_header.page_time = page_time;
} else if let Some(page_time_str) = parse_value(line, id::record::PAGE_TIME) {
let _: String = page_time_str;
record_header.page_time = defs::parse_date_time(&page_time_str);
} else if let Some(temperature) = parse_value(line, id::record::TEMPERATURE) {
record_header.temperature = temperature;
} else if let Some(battery_voltage) = parse_value(line, id::record::BATTERY_VOLTAGE) {
record_header.battery_voltage = battery_voltage;
}
}

data.b_time.push(record_header.page_time.timestamp());
data.b_time.push(record_header.page_time.timestamp_nanos_opt().unwrap());
data.b_temperature.push(record_header.temperature);
data.b_battery_voltage.push(record_header.battery_voltage);

Expand All @@ -193,7 +194,7 @@ pub fn load_data(path: String) -> AccelerometerData {
let sample_time = record_header.page_time
+ chrono::Duration::nanoseconds((1_000_000_000.0 / record_header.measurement_frequency) as i64 * i as i64);

data.a_time.push(sample_time.timestamp());
data.a_time.push(sample_time.timestamp_nanos_opt().unwrap());
data.a3_acceleration.push(sample.x);
data.a3_acceleration.push(sample.y);
data.a3_acceleration.push(sample.z);
Expand Down

0 comments on commit 84cc54e

Please sign in to comment.