mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-29 22:24:02 +00:00
fix(time): avoid i32 overflow when converting an out-of-range date (#10299)
Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
(cherry picked from commit 402e4a97fc)
Signed-off-by: WenyXu <wenymedia@gmail.com>
This commit is contained in:
@@ -55,7 +55,7 @@ impl From<NaiveDate> for Date {
|
||||
impl Display for Date {
|
||||
/// [Date] is formatted according to ISO-8601 standard.
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
if let Some(abs_date) = NaiveDate::from_num_days_from_ce_opt(UNIX_EPOCH_FROM_CE + self.0) {
|
||||
if let Some(abs_date) = self.to_chrono_date() {
|
||||
write!(f, "{}", abs_date.format("%F"))
|
||||
} else {
|
||||
write!(f, "Date({})", self.0)
|
||||
@@ -93,7 +93,9 @@ impl Date {
|
||||
}
|
||||
|
||||
pub fn to_chrono_date(&self) -> Option<NaiveDate> {
|
||||
NaiveDate::from_num_days_from_ce_opt(UNIX_EPOCH_FROM_CE + self.0)
|
||||
UNIX_EPOCH_FROM_CE
|
||||
.checked_add(self.0)
|
||||
.and_then(NaiveDate::from_num_days_from_ce_opt)
|
||||
}
|
||||
|
||||
/// Format Date for given format and timezone.
|
||||
@@ -217,6 +219,12 @@ mod tests {
|
||||
assert_eq!("1970-02-12", Date::new(42).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_chrono_date_out_of_range() {
|
||||
assert_eq!(None, Date::new(i32::MAX).to_chrono_date());
|
||||
assert_eq!("Date(2147483647)", Date::new(i32::MAX).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_date_parse() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user