Skip to content

Commit ea671ad

Browse files
committed
Fix PgInterval display formatting
Correct pluralization of years, months, and days in display. Correctly format the time part, including sign for negative intervals. Fix the test case to match the new format.
1 parent a002d41 commit ea671ad

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

sqlx-core/src/postgres/types/interval.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ impl std::fmt::Display for PgInterval {
9696
parts.push(format!(
9797
"{} year{}",
9898
years,
99-
if years != 1 { "s" } else { "" }
99+
if years.abs() != 1 { "s" } else { "" }
100100
));
101101
}
102102
if months != 0 {
103103
parts.push(format!(
104104
"{} mon{}",
105105
months,
106-
if months != 1 { "s" } else { "" }
106+
if months.abs() != 1 { "s" } else { "" }
107107
));
108108
}
109109
}
@@ -112,27 +112,29 @@ impl std::fmt::Display for PgInterval {
112112
parts.push(format!(
113113
"{} day{}",
114114
self.days,
115-
if self.days != 1 { "s" } else { "" }
115+
if self.days.abs() != 1 { "s" } else { "" }
116116
));
117117
}
118118

119-
let total_seconds = self.microseconds / 1_000_000;
120-
let microseconds = self.microseconds % 1_000_000;
121-
let hours = total_seconds / 3600;
122-
let minutes = (total_seconds % 3600) / 60;
123-
let seconds = total_seconds % 60;
119+
let time_us = self.microseconds;
120+
121+
if time_us != 0 || parts.is_empty() {
122+
let sign = if time_us < 0 { "-" } else { "" };
123+
let us = time_us.abs();
124+
125+
let total_seconds = us / 1_000_000;
126+
let microseconds = us % 1_000_000;
127+
let hours = total_seconds / 3600;
128+
let minutes = (total_seconds % 3600) / 60;
129+
let seconds = total_seconds % 60;
124130

125-
if hours != 0 || minutes != 0 || seconds != 0 || microseconds != 0 {
126131
let time_part = if microseconds != 0 {
127132
format!(
128-
"{:02}:{:02}:{:02}.{:06}",
129-
hours,
130-
minutes,
131-
seconds,
132-
microseconds.abs()
133+
"{}{:02}:{:02}:{:02}.{:06}",
134+
sign, hours, minutes, seconds, microseconds
133135
)
134136
} else {
135-
format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
137+
format!("{}{:02}:{:02}:{:02}", sign, hours, minutes, seconds)
136138
};
137139
parts.push(time_part);
138140
}
@@ -499,7 +501,7 @@ fn test_pginterval_display() {
499501
let interval = PgInterval {
500502
months: 0,
501503
days: 0,
502-
microseconds: 3_660_000_000 + 30_000, // 1 hour 1 minute 30 microseconds
504+
microseconds: 3_600 * 1_000_000 + 60 * 1_000_000 + 30, // 1 hour 1 minute 30 microseconds
503505
};
504506
assert_eq!(interval.to_string(), "01:01:00.000030");
505507

@@ -531,11 +533,11 @@ fn test_pginterval_display() {
531533
let interval = PgInterval {
532534
months: 14, // 1 year 2 months
533535
days: 27,
534-
microseconds: 43_200_000_000 + 180_000_000 + 30_000, // 12 hours 3 minutes 30 microseconds
536+
microseconds: 43_200_000_000 + 180_000_000 + 30_000, // 12 hours 3 minutes 30 milliseconds
535537
};
536538
assert_eq!(
537539
interval.to_string(),
538-
"1 year 2 mons 27 days 12:03:00.000030"
540+
"1 year 2 mons 27 days 12:03:00.030000"
539541
);
540542

541543
// Negative microseconds
@@ -544,5 +546,5 @@ fn test_pginterval_display() {
544546
days: 0,
545547
microseconds: -1_000_000, // -1 second
546548
};
547-
assert_eq!(interval.to_string(), "00:00:01.000000");
549+
assert_eq!(interval.to_string(), "-00:00:01");
548550
}

0 commit comments

Comments
 (0)