Something went wrong. Try again.
Unofficial implementation of analysis for activPAL™ data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159// Copyright (C) 2026 Jannik Schürg <jannik.schuerg@posteo.de>//// SPDX-License-Identifier: Apache-2.0// SPDX-License-Identifier: MIT
use std::ops::{Add, AddAssign};
use chrono::{Duration, NaiveDate, NaiveDateTime};
use crate::{GIGA, HZ, NANO, S_PER_DAY, S_PER_HOUR, S_PER_MIN, SMPS_PER_DAY};
#[derive(Debug, Copy, Clone, PartialEq, Default)]pub struct NumSamples(pub(crate) u32);
impl NumSamples { pub fn zero() -> Self { Self(0) }
pub fn hours(self) -> f64 { self.0 as f64 / (S_PER_HOUR * HZ) as f64 } pub fn minutes(self) -> f64 { self.0 as f64 / (S_PER_MIN * HZ) as f64 } pub fn seconds(self) -> f64 { self.0 as f64 / HZ as f64 }}
impl Add for NumSamples { type Output = Self;
#[inline] fn add(self, rhs: Self) -> Self::Output { Self(self.0 + rhs.0) }}
impl AddAssign for NumSamples { #[inline] fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; }}
impl PartialOrd for NumSamples { #[inline] fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { self.0.partial_cmp(&other.0) }}
impl From<u32> for NumSamples { fn from(value: u32) -> Self { Self(value) }}
impl From<NumSamples> for u32 { fn from(value: NumSamples) -> Self { value.0 }}
impl From<NumSamples> for Duration { fn from(value: NumSamples) -> Self { let seconds = value.0 / HZ; let subsec = (value.0 % HZ) as u64 * GIGA / HZ as u64; Duration::new(seconds as i64, subsec as u32).unwrap() }}
/// ActivePal files store time as decimal days starting from/// 30.12.1899. We use this struct so we can distinguish different/// types of float or time information more easily.#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]pub struct ActivPalTime(pub(crate) f64);
impl ActivPalTime { pub fn activpal_epoch() -> NaiveDateTime { NaiveDate::from_ymd_opt(1899, 12, 30) .unwrap() .and_hms_opt(0, 0, 0) .unwrap() }
pub fn sample_diff_since(&self, rhs: &Self) -> Result<NumSamples, String> { let delta = (self.0 - rhs.0) * (SMPS_PER_DAY as f64); if delta < 0.0 { return Err("Cannot produce negative sample difference".into()); } Ok(NumSamples::from(delta.round() as u32)) }}
impl From<f64> for ActivPalTime { fn from(value: f64) -> Self { Self(value) }}
impl From<ActivPalTime> for f64 { fn from(value: ActivPalTime) -> Self { value.0 }}
impl From<ActivPalTime> for NaiveDateTime { fn from(value: ActivPalTime) -> NaiveDateTime { let total_seconds = S_PER_DAY as f64 * value.0; let seconds = total_seconds.floor(); let subsec = (total_seconds - seconds) * GIGA as f64; ActivPalTime::activpal_epoch() + Duration::new(seconds as i64, subsec as u32).unwrap() }}
impl From<NaiveDateTime> for ActivPalTime { fn from(value: NaiveDateTime) -> ActivPalTime { let delta = value - ActivPalTime::activpal_epoch(); let days = (delta.num_seconds() as f64 + delta.subsec_nanos() as f64 * NANO) / (S_PER_DAY as f64); ActivPalTime::from(days) }}
impl Add<NumSamples> for ActivPalTime { type Output = ActivPalTime; fn add(self, rhs: NumSamples) -> ActivPalTime { let delta = rhs.0 as f64 / (SMPS_PER_DAY as f64); ActivPalTime(self.0 + delta) }}
impl Add<ActivPalTime> for ActivPalTime { type Output = ActivPalTime; fn add(self, rhs: ActivPalTime) -> ActivPalTime { ActivPalTime(self.0 + rhs.0) }}
impl ActivPalTime { pub fn round(&self) -> Self { Self(self.0.round()) }
pub fn midnight_prev(&self) -> Self { Self(self.0.floor()) }
pub fn midnight_succ(&self) -> Self { Self(self.0.ceil()) }
pub fn midnight_closest(&self) -> Self { self.round() }}