11use crate :: interval_norm:: IntervalNorm ;
22
33impl 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 {
5- let has_negative = self . years < 0
6- || self . months < 0
7- || self . days < 0
8- || self . hours < 0
9- || self . minutes < 0
10- || self . seconds < 0
11- || self . microseconds < 0 ;
12- let has_positive = self . years > 0
13- || self . months > 0
14- || self . days > 0
15- || self . hours > 0
16- || self . minutes > 0
17- || self . seconds > 0
18- || self . microseconds > 0 ;
19- let has_year_month = self . years != 0 || self . months != 0 ;
20- let has_day_time = self . days != 0
21- || self . hours != 0
22- || self . minutes != 0
23- || self . seconds != 0
24- || 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) ) ;
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) ;
3035
3136 if !has_negative && !has_positive {
3237 return "0" . to_owned ( ) ;
3338 }
3439
3540 if !sql_standard_value {
36- // Mixed signs or year-month + day-time: force signs on each component
3741 let year_sign = if self . years < 0 || self . months < 0 {
3842 '-'
3943 } else {
@@ -50,98 +54,51 @@ impl IntervalNorm {
5054 '+'
5155 } ;
5256
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 ! (
57+ let time_str = format ! (
7958 "{}{}:{:02}:{:02}" ,
80- sec_sign, hours_abs, minutes_abs, seconds_abs
59+ sec_sign,
60+ super :: safe_abs_u64( self . hours) ,
61+ super :: safe_abs_u64( self . minutes) ,
62+ super :: safe_abs_u64( self . seconds)
8163 ) ;
8264
83- if microseconds != 0 {
84- let microseconds_fmt = format ! ( ".{:06}" , super :: safe_abs_u64( microseconds) ) ;
85- time_str. push_str ( & microseconds_fmt) ;
86- }
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+ } ;
8770
8871 format ! (
8972 "{}{}-{} {}{} {}" ,
9073 year_sign,
91- years. abs( ) ,
92- months. abs( ) ,
74+ self . years. abs( ) ,
75+ self . months. abs( ) ,
9376 day_sign,
94- days. abs( ) ,
77+ self . days. abs( ) ,
9578 time_str
9679 )
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
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+ )
93+ )
14394 } else {
144- "0" . to_owned ( )
95+ Self :: format_time (
96+ self . hours ,
97+ self . minutes ,
98+ self . seconds ,
99+ self . microseconds ,
100+ has_negative,
101+ )
145102 }
146103 }
147104}
0 commit comments