libdit: implement dit to timestamp conversion
This commit is contained in:
parent
cb032d11fc
commit
1e8c065b75
|
@ -3,6 +3,7 @@ pub mod libdit {
|
|||
use std::str::FromStr;
|
||||
|
||||
/// Represents a time in a StarDIT day
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Dit {
|
||||
pub deca: u16,
|
||||
pub decim: u16,
|
||||
|
@ -49,6 +50,12 @@ pub mod libdit {
|
|||
(unix - (12.0 * 3600.0)) % 86400.0 / 86400.0
|
||||
}
|
||||
|
||||
/// Returns relative progress in the day, between 0 and 1 corresponding to `dit`
|
||||
pub fn dit_struct_to_dit(dit: Dit) -> f64 {
|
||||
((dit.deca as f32 / 10.0) + (dit.decim as f32 / 1000.0) + (dit.desec as f32 / 100000.0))
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Returns a struct containing deca, decim and desec corresponding to `unix`
|
||||
pub fn unix_to_dit_struct(unix: f64) -> Dit {
|
||||
let dit = unix_to_dit(unix);
|
||||
|
@ -59,6 +66,11 @@ pub mod libdit {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns a unix timestamp corresponding to `dit`
|
||||
pub fn dit_struct_to_unix(dit: Dit) -> f64 {
|
||||
dit_struct_to_dit(dit) * 86400.0 + (12.0 * 3600.0)
|
||||
}
|
||||
|
||||
/// Returns a string containing "deca.decim.desec" corresponding to `unix`
|
||||
///
|
||||
/// # Examples
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use libdit::libdit::dit_struct_to_unix;
|
||||
use libdit::libdit::unix_to_dit_string;
|
||||
use libdit::libdit::unix_to_dit_struct;
|
||||
use libdit::libdit::Dit;
|
||||
|
||||
#[test]
|
||||
fn test_unix_to_dit_string() {
|
||||
|
@ -7,3 +10,27 @@ fn test_unix_to_dit_string() {
|
|||
assert_eq!("9.99.99", unix_to_dit_string(1672574399.9999));
|
||||
assert_eq!("0.00.00", unix_to_dit_string(1672574400.0000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unix_to_dit_struct() {
|
||||
assert_eq!(
|
||||
Dit {
|
||||
deca: 0,
|
||||
decim: 0,
|
||||
desec: 0
|
||||
},
|
||||
unix_to_dit_struct(43200.0)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dit_struct_to_unix() {
|
||||
assert_eq!(
|
||||
43200.0,
|
||||
dit_struct_to_unix(Dit {
|
||||
deca: 0,
|
||||
decim: 0,
|
||||
desec: 0
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue