|
1 | 1 | use crate::interval_norm::IntervalNorm; |
2 | 2 |
|
3 | 3 | impl IntervalNorm { |
| 4 | + fn format_time( |
| 5 | + hours: i64, |
| 6 | + minutes: i64, |
| 7 | + seconds: i64, |
| 8 | + microseconds: i64, |
| 9 | + sign: bool, |
| 10 | + ) -> String { |
| 11 | + let sign_str = if sign { "-" } else { "" }; |
| 12 | + let time_str = format!( |
| 13 | + "{}{}:{:02}:{:02}", |
| 14 | + sign_str, |
| 15 | + super::safe_abs_u64(hours), |
| 16 | + super::safe_abs_u64(minutes), |
| 17 | + super::safe_abs_u64(seconds) |
| 18 | + ); |
| 19 | + |
| 20 | + if microseconds != 0 { |
| 21 | + format!("{}.{:06}", time_str, super::safe_abs_u64(microseconds)) |
| 22 | + } else { |
| 23 | + time_str |
| 24 | + } |
| 25 | + } |
| 26 | + |
4 | 27 | pub fn into_sql(self) -> String { |
5 | | - if self.is_zeroed() { |
6 | | - "0".to_owned() |
7 | | - } else if !self.is_time_present() && !self.is_day_present() { |
8 | | - get_year_month(self.months, self.years, true) |
9 | | - } else if !self.is_time_present() && !self.is_year_month_present() { |
10 | | - format!("{} 0:00:00", self.days) |
11 | | - } else if !self.is_year_month_present() && !self.is_day_present() { |
12 | | - get_time_interval( |
13 | | - self.hours, |
14 | | - self.minutes, |
15 | | - self.seconds, |
16 | | - self.microseconds, |
17 | | - self.is_time_interval_pos(), |
18 | | - true, |
| 28 | + let has_negative = self.has_negative(); |
| 29 | + let has_positive = self.has_positive(); |
| 30 | + |
| 31 | + let has_year_month = self.is_year_month_present(); |
| 32 | + let has_day_time = self.is_day_present() || self.is_time_present(); |
| 33 | + |
| 34 | + let sql_standard_value = !(has_negative && has_positive || has_year_month && has_day_time); |
| 35 | + |
| 36 | + if !has_negative && !has_positive { |
| 37 | + return "0".to_owned(); |
| 38 | + } |
| 39 | + |
| 40 | + if !sql_standard_value { |
| 41 | + let year_sign = if self.years < 0 || self.months < 0 { |
| 42 | + '-' |
| 43 | + } else { |
| 44 | + '+' |
| 45 | + }; |
| 46 | + let day_sign = if self.days < 0 { '-' } else { '+' }; |
| 47 | + let sec_sign = if self.hours < 0 |
| 48 | + || self.minutes < 0 |
| 49 | + || self.seconds < 0 |
| 50 | + || self.microseconds < 0 |
| 51 | + { |
| 52 | + '-' |
| 53 | + } else { |
| 54 | + '+' |
| 55 | + }; |
| 56 | + |
| 57 | + let time_str = format!( |
| 58 | + "{}{}:{:02}:{:02}", |
| 59 | + sec_sign, |
| 60 | + super::safe_abs_u64(self.hours), |
| 61 | + super::safe_abs_u64(self.minutes), |
| 62 | + super::safe_abs_u64(self.seconds) |
| 63 | + ); |
| 64 | + |
| 65 | + let time_str = if self.microseconds != 0 { |
| 66 | + format!("{}.{:06}", time_str, super::safe_abs_u64(self.microseconds)) |
| 67 | + } else { |
| 68 | + time_str |
| 69 | + }; |
| 70 | + |
| 71 | + format!( |
| 72 | + "{}{}-{} {}{} {}", |
| 73 | + year_sign, |
| 74 | + self.years.abs(), |
| 75 | + self.months.abs(), |
| 76 | + day_sign, |
| 77 | + self.days.abs(), |
| 78 | + time_str |
| 79 | + ) |
| 80 | + } else if has_year_month { |
| 81 | + format!("{}-{}", self.years, super::safe_abs_u32(self.months)) |
| 82 | + } else if self.days != 0 { |
| 83 | + format!( |
| 84 | + "{} {}", |
| 85 | + self.days, |
| 86 | + Self::format_time( |
| 87 | + self.hours, |
| 88 | + self.minutes, |
| 89 | + self.seconds, |
| 90 | + self.microseconds, |
| 91 | + false |
| 92 | + ) |
19 | 93 | ) |
20 | 94 | } else { |
21 | | - let year_month = get_year_month(self.months, self.years, false); |
22 | | - let time_interval = get_time_interval( |
| 95 | + Self::format_time( |
23 | 96 | self.hours, |
24 | 97 | self.minutes, |
25 | 98 | self.seconds, |
26 | 99 | self.microseconds, |
27 | | - self.is_time_interval_pos(), |
28 | | - false, |
29 | | - ); |
30 | | - format!("{} {:+} {}", year_month, self.days, time_interval) |
| 100 | + has_negative, |
| 101 | + ) |
31 | 102 | } |
32 | 103 | } |
33 | 104 | } |
34 | | - |
35 | | -fn get_year_month(mons: i32, years: i32, is_only_year_month: bool) -> String { |
36 | | - let months = super::safe_abs_u32(mons); |
37 | | - if years == 0 || is_only_year_month { |
38 | | - format!("{}-{}", years, months) |
39 | | - } else { |
40 | | - format!("{:+}-{}", years, months) |
41 | | - } |
42 | | -} |
43 | | - |
44 | | -fn get_time_interval( |
45 | | - hours: i64, |
46 | | - mins: i64, |
47 | | - secs: i64, |
48 | | - micros: i64, |
49 | | - is_time_interval_pos: bool, |
50 | | - is_only_time: bool, |
51 | | -) -> String { |
52 | | - let mut interval = "".to_owned(); |
53 | | - if is_time_interval_pos && is_only_time { |
54 | | - interval.push_str(&format!("{}:{:02}:{:02}", hours, mins, secs)); |
55 | | - } else { |
56 | | - let minutes = super::safe_abs_u64(mins); |
57 | | - let seconds = super::safe_abs_u64(secs); |
58 | | - interval.push_str(&format!("{:+}:{:02}:{:02}", hours, minutes, seconds)); |
59 | | - } |
60 | | - if micros != 0 { |
61 | | - let microseconds = format!(".{:06}", super::safe_abs_u64(micros)); |
62 | | - interval.push_str(µseconds); |
63 | | - } |
64 | | - interval |
65 | | -} |
|
0 commit comments