-
Notifications
You must be signed in to change notification settings - Fork 595
Description
I found that getting specific with time measurements from TimeDelta was difficult. for example if I used num_minutes(); and there was an hour on the TimeDelta, I would get 60 instead of zero. I also thought that creating a TimeDelta from a combination of time measurements would be fun to add so I did. It is not necessary to use f128 (that was something I did because it was easier for me to work with logically).
struct FakeDelta {
seconds: f128,
}
#[derive(Debug)]
enum TimeUnits {
Seconds(f128),
Minutes(i64),
Hours(i64),
Days(i64),
Weeks(i64),
Months(i64),
Years(i64)
}
impl FakeDelta {
fn from_seconds(secs: f128) -> FakeDelta {
FakeDelta {
seconds: secs
}
}
fn all_seconds(&self) -> f128 {
self.seconds
}
fn seconds(&self) -> f128 {
self.seconds % 60.0
}
fn from_minutes(mins: i64) -> FakeDelta {
FakeDelta {
seconds: (mins * 60) as f128
}
}
fn all_minutes(&self) -> f128 {
self.seconds / 60.0
}
fn minutes(&self) -> f128 {
(self.seconds / 60.0) % 60.0
}
fn from_hours(hours: i64) -> FakeDelta {
FakeDelta {
seconds: (hours * 3600) as f128
}
}
fn all_hours(&self) -> f128 {
self.seconds / 3600.0
}
fn hours(&self) -> f128 {
(self.seconds / 3600.0) % 24.0
}
fn from_days(days: i64) -> FakeDelta {
FakeDelta {
seconds: (days * 86400) as f128
}
}
fn all_days(&self) -> f128 {
self.seconds / 86400.0
}
fn days(&self) -> f128 {
(self.seconds / 86400.0) % 52.1428571429
}
fn from_weeks(weeks: i64) -> FakeDelta {
FakeDelta {
seconds: (weeks * 604800) as f128
}
}
fn all_weeks(&self) -> f128 {
self.seconds / 604800.0
}
fn weeks(&self) -> f128 {
(self.seconds / 604800.0) % 4.345242857142857
}
fn from_months(months: i64) -> FakeDelta {
FakeDelta {
seconds: (months * 2592000) as f128
}
}
fn all_months(&self) -> f128 {
self.seconds / 604800.0
}
fn months(&self) -> f128 {
(self.seconds / 604800.0) % 12.0
}
fn from_years(years: i64) -> FakeDelta {
FakeDelta {
seconds: (years * 31536000) as f128
}
}
fn from_mixed(input: Vec) {
let mut output: f128 = 0.0;
for i in 0..input.len() {
output += match input [i] {
Seconds(val) => val,
Minutes(val) => (val * 60) as f128,
Hours(val) => (val * 3600) as f128,
Days(val) => (val * 86400) as f128,
Weeks(val) => (val * 604800) as f128,
Months(val) => (val * 2592000) as f128,
Years(val) => (val * 31536000) as f128
}
}
}
}