Skip to content

Commit b831e74

Browse files
committed
refactor: simplify sql format
1 parent 767265f commit b831e74

1 file changed

Lines changed: 60 additions & 87 deletions

File tree

src/interval_fmt/sql.rs

Lines changed: 60 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
use crate::interval_norm::IntervalNorm;
22

33
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+
427
pub fn into_sql(self) -> String {
528
let has_negative = self.years < 0
629
|| self.months < 0
@@ -16,24 +39,21 @@ impl IntervalNorm {
1639
|| self.minutes > 0
1740
|| self.seconds > 0
1841
|| self.microseconds > 0;
42+
1943
let has_year_month = self.years != 0 || self.months != 0;
2044
let has_day_time = self.days != 0
2145
|| self.hours != 0
2246
|| self.minutes != 0
2347
|| self.seconds != 0
2448
|| self.microseconds != 0;
25-
let has_day = self.days != 0;
26-
let is_year_month_only = has_year_month && !has_day_time;
27-
let is_day_time_only = has_day_time && !has_year_month;
28-
let sql_standard_value =
29-
!((has_negative && has_positive) || (has_year_month && has_day_time));
49+
50+
let sql_standard_value = !(has_negative && has_positive || has_year_month && has_day_time);
3051

3152
if !has_negative && !has_positive {
3253
return "0".to_owned();
3354
}
3455

3556
if !sql_standard_value {
36-
// Mixed signs or year-month + day-time: force signs on each component
3757
let year_sign = if self.years < 0 || self.months < 0 {
3858
'-'
3959
} else {
@@ -50,98 +70,51 @@ impl IntervalNorm {
5070
'+'
5171
};
5272

53-
let (years, months, days, hours, minutes, seconds, microseconds) = if year_sign == '-' {
54-
(
55-
-self.years,
56-
-self.months,
57-
-self.days,
58-
-self.hours,
59-
-self.minutes,
60-
-self.seconds,
61-
-self.microseconds,
62-
)
63-
} else {
64-
(
65-
self.years,
66-
self.months,
67-
self.days,
68-
self.hours,
69-
self.minutes,
70-
self.seconds,
71-
self.microseconds,
72-
)
73-
};
74-
75-
let hours_abs = super::safe_abs_u64(hours);
76-
let minutes_abs = super::safe_abs_u64(minutes);
77-
let seconds_abs = super::safe_abs_u64(seconds);
78-
let mut time_str = format!(
73+
let time_str = format!(
7974
"{}{}:{:02}:{:02}",
80-
sec_sign, hours_abs, minutes_abs, seconds_abs
75+
sec_sign,
76+
super::safe_abs_u64(self.hours),
77+
super::safe_abs_u64(self.minutes),
78+
super::safe_abs_u64(self.seconds)
8179
);
8280

83-
if microseconds != 0 {
84-
let microseconds_fmt = format!(".{:06}", super::safe_abs_u64(microseconds));
85-
time_str.push_str(&microseconds_fmt);
86-
}
81+
let time_str = if self.microseconds != 0 {
82+
format!("{}.{:06}", time_str, super::safe_abs_u64(self.microseconds))
83+
} else {
84+
time_str
85+
};
8786

8887
format!(
8988
"{}{}-{} {}{} {}",
9089
year_sign,
91-
years.abs(),
92-
months.abs(),
90+
self.years.abs(),
91+
self.months.abs(),
9392
day_sign,
94-
days.abs(),
93+
self.days.abs(),
9594
time_str
9695
)
97-
} else if is_year_month_only {
98-
// Only year-month
99-
if has_negative {
100-
format!("-{}-{}", self.years.abs(), super::safe_abs_u32(self.months))
101-
} else {
102-
format!("{}-{}", self.years, super::safe_abs_u32(self.months))
103-
}
104-
} else if is_day_time_only && has_day {
105-
// Day-time with day
106-
let hours_abs = super::safe_abs_u64(self.hours);
107-
let minutes = super::safe_abs_u64(self.minutes);
108-
let seconds = super::safe_abs_u64(self.seconds);
109-
let mut time_str = if self.hours == 0
110-
&& self.minutes == 0
111-
&& self.seconds == 0
112-
&& self.microseconds == 0
113-
{
114-
"0:00:00".to_owned()
115-
} else {
116-
format!("{}:{:02}:{:02}", hours_abs, minutes, seconds)
117-
};
118-
119-
if self.microseconds != 0 {
120-
let microseconds_fmt = format!(".{:06}", super::safe_abs_u64(self.microseconds));
121-
time_str.push_str(&microseconds_fmt);
122-
}
123-
124-
if has_negative {
125-
format!("-{} {}", self.days.abs(), time_str)
126-
} else {
127-
format!("{} {}", self.days, time_str)
128-
}
129-
} else if is_day_time_only {
130-
// Time only
131-
let hours_abs = super::safe_abs_u64(self.hours);
132-
let minutes = super::safe_abs_u64(self.minutes);
133-
let seconds = super::safe_abs_u64(self.seconds);
134-
let sign = if self.hours < 0 { "-" } else { "" };
135-
let mut result = format!("{}{}:{:02}:{:02}", sign, hours_abs, minutes, seconds);
136-
137-
if self.microseconds != 0 {
138-
let microseconds_fmt = format!(".{:06}", super::safe_abs_u64(self.microseconds));
139-
result.push_str(&microseconds_fmt);
140-
}
141-
142-
result
96+
} else if has_year_month {
97+
format!("{}-{}", self.years, super::safe_abs_u32(self.months))
98+
} else if self.days != 0 {
99+
format!(
100+
"{} {}",
101+
self.days,
102+
Self::format_time(
103+
self.hours,
104+
self.minutes,
105+
self.seconds,
106+
self.microseconds,
107+
false
108+
)
109+
)
143110
} else {
144-
"0".to_owned()
111+
Self::format_time(
112+
self.hours,
113+
self.minutes,
114+
self.seconds,
115+
self.microseconds,
116+
has_negative,
117+
)
145118
}
146119
}
147120
}

0 commit comments

Comments
 (0)