|
| 1 | +use pg_interval::Interval; |
| 2 | + |
| 3 | +pub struct IntervalNorm { |
| 4 | + pub years: i32, |
| 5 | + pub months: i32, |
| 6 | + pub days: i32, |
| 7 | + pub hours: i64, |
| 8 | + pub minutes: i64, |
| 9 | + pub seconds: i64, |
| 10 | + pub microseconds: i64, |
| 11 | +} |
| 12 | + |
| 13 | +impl<'a> From<&'a Interval> for IntervalNorm { |
| 14 | + fn from(val: &Interval) -> IntervalNorm { |
| 15 | + // grab the base values from the interval |
| 16 | + let months = val.months; |
| 17 | + let days = val.days; |
| 18 | + let microseconds = val.microseconds; |
| 19 | + // calc the year and get the remaining months |
| 20 | + let years = (months - (months % 12)) / 12; |
| 21 | + let months = months - years * 12; |
| 22 | + // calc the hours from the microseconds and update |
| 23 | + // the remaining microseconds. |
| 24 | + let hours = (microseconds - (microseconds % 3_600_000_000)) / 3_600_000_000; |
| 25 | + let microseconds = microseconds - hours * 3_600_000_000; |
| 26 | + // calc the minutes from remaining microseconds and |
| 27 | + // update the remaining microseconds. |
| 28 | + let minutes = (microseconds - (microseconds % 60_000_000)) / 60_000_000; |
| 29 | + let microseconds = microseconds - minutes * 60_000_000; |
| 30 | + // calc the seconds and update the remaining microseconds. |
| 31 | + let seconds = (microseconds - (microseconds % 1_000_000)) / 1_000_000; |
| 32 | + let microseconds = microseconds - seconds * 1_000_000; |
| 33 | + IntervalNorm { |
| 34 | + years, |
| 35 | + months, |
| 36 | + days, |
| 37 | + hours, |
| 38 | + minutes, |
| 39 | + seconds, |
| 40 | + microseconds, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl IntervalNorm { |
| 46 | + /// Is all the values in the interval set to 0? |
| 47 | + pub fn is_zeroed(&self) -> bool { |
| 48 | + self.years == 0 |
| 49 | + && self.months == 0 |
| 50 | + && self.hours == 0 |
| 51 | + && self.minutes == 0 |
| 52 | + && self.seconds == 0 |
| 53 | + && self.microseconds == 0 |
| 54 | + } |
| 55 | + |
| 56 | + /// Is the years or month value set? |
| 57 | + pub fn is_year_month_present(&self) -> bool { |
| 58 | + self.years != 0 || self.months != 0 |
| 59 | + } |
| 60 | + |
| 61 | + /// Is the day value set? |
| 62 | + pub fn is_day_present(&self) -> bool { |
| 63 | + self.days != 0 |
| 64 | + } |
| 65 | + |
| 66 | + /// Is at least one of hours,minutes,seconds,microseconds values |
| 67 | + /// postive. There are no mixed intervals so we can assume that |
| 68 | + /// if one value is postive the rest are at least >= 0 |
| 69 | + pub fn is_time_interval_pos(&self) -> bool { |
| 70 | + self.hours > 0 || self.minutes > 0 || self.seconds > 0 || self.microseconds > 0 |
| 71 | + } |
| 72 | + |
| 73 | + /// Is the hours,minutes, seconds, microseconds values set? |
| 74 | + pub fn is_time_present(&self) -> bool { |
| 75 | + self.hours != 0 || self.minutes != 0 || self.seconds != 0 || self.microseconds != 0 |
| 76 | + } |
| 77 | +} |
0 commit comments