Skip to main content

deep_causality_physics/units/
time.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
4 */
5
6use crate::PhysicsError;
7
8/// Time (Seconds).
9#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
10pub struct Time(f64);
11
12impl Time {
13    pub fn new(val: f64) -> Result<Self, PhysicsError> {
14        if val < 0.0 {
15            return Err(PhysicsError::PhysicalInvariantBroken(
16                "Time cannot be negative (relative time duration assumed positive)".into(),
17            ));
18        }
19        Ok(Self(val))
20    }
21
22    pub fn from_minutes(minutes: f64) -> Result<Self, PhysicsError> {
23        Self::new(minutes * 60.0)
24    }
25
26    pub fn from_hours(hours: f64) -> Result<Self, PhysicsError> {
27        Self::new(hours * 3600.0)
28    }
29
30    pub fn from_days(days: f64) -> Result<Self, PhysicsError> {
31        Self::new(days * 86400.0)
32    }
33
34    pub fn from_years(years: f64) -> Result<Self, PhysicsError> {
35        // Julian year = 365.25 days
36        Self::new(years * 31_557_600.0)
37    }
38
39    pub fn new_unchecked(val: f64) -> Self {
40        Self(val)
41    }
42
43    pub fn value(&self) -> f64 {
44        self.0
45    }
46
47    pub fn as_minutes(&self) -> f64 {
48        self.0 / 60.0
49    }
50
51    pub fn as_hours(&self) -> f64 {
52        self.0 / 3600.0
53    }
54
55    pub fn as_days(&self) -> f64 {
56        self.0 / 86400.0
57    }
58
59    pub fn as_years(&self) -> f64 {
60        self.0 / 31_557_600.0
61    }
62}
63
64impl From<Time> for f64 {
65    fn from(val: Time) -> Self {
66        val.0
67    }
68}