mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-05 00:34:41 +00:00
Use overflowing_naive_local
in methods that don't return DateTime
This commit is contained in:
parent
4ca5a5292f
commit
e6580db095
@ -22,7 +22,7 @@ use crate::format::{
|
|||||||
StrftimeItems, TOO_LONG,
|
StrftimeItems, TOO_LONG,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
use crate::format::{write_rfc3339, DelayedFormat};
|
use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat};
|
||||||
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
|
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
|
||||||
#[cfg(feature = "clock")]
|
#[cfg(feature = "clock")]
|
||||||
use crate::offset::Local;
|
use crate::offset::Local;
|
||||||
@ -561,7 +561,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn to_rfc2822(&self) -> String {
|
pub fn to_rfc2822(&self) -> String {
|
||||||
let mut result = String::with_capacity(32);
|
let mut result = String::with_capacity(32);
|
||||||
crate::format::write_rfc2822(&mut result, self.naive_local(), self.offset.fix())
|
write_rfc2822(&mut result, self.overflowing_naive_local(), self.offset.fix())
|
||||||
.expect("writing rfc2822 datetime to string should never fail");
|
.expect("writing rfc2822 datetime to string should never fail");
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
@ -572,7 +572,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
|||||||
pub fn to_rfc3339(&self) -> String {
|
pub fn to_rfc3339(&self) -> String {
|
||||||
// For some reason a string with a capacity less than 32 is ca 20% slower when benchmarking.
|
// For some reason a string with a capacity less than 32 is ca 20% slower when benchmarking.
|
||||||
let mut result = String::with_capacity(32);
|
let mut result = String::with_capacity(32);
|
||||||
let naive = self.naive_local();
|
let naive = self.overflowing_naive_local();
|
||||||
let offset = self.offset.fix();
|
let offset = self.offset.fix();
|
||||||
write_rfc3339(&mut result, naive, offset, SecondsFormat::AutoSi, false)
|
write_rfc3339(&mut result, naive, offset, SecondsFormat::AutoSi, false)
|
||||||
.expect("writing rfc3339 datetime to string should never fail");
|
.expect("writing rfc3339 datetime to string should never fail");
|
||||||
@ -888,7 +888,7 @@ where
|
|||||||
I: Iterator<Item = B> + Clone,
|
I: Iterator<Item = B> + Clone,
|
||||||
B: Borrow<Item<'a>>,
|
B: Borrow<Item<'a>>,
|
||||||
{
|
{
|
||||||
let local = self.naive_local();
|
let local = self.overflowing_naive_local();
|
||||||
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
|
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -924,7 +924,7 @@ where
|
|||||||
I: Iterator<Item = B> + Clone,
|
I: Iterator<Item = B> + Clone,
|
||||||
B: Borrow<Item<'a>>,
|
B: Borrow<Item<'a>>,
|
||||||
{
|
{
|
||||||
let local = self.naive_local();
|
let local = self.overflowing_naive_local();
|
||||||
DelayedFormat::new_with_offset_and_locale(
|
DelayedFormat::new_with_offset_and_locale(
|
||||||
Some(local.date()),
|
Some(local.date()),
|
||||||
Some(local.time()),
|
Some(local.time()),
|
||||||
@ -954,39 +954,39 @@ where
|
|||||||
impl<Tz: TimeZone> Datelike for DateTime<Tz> {
|
impl<Tz: TimeZone> Datelike for DateTime<Tz> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn year(&self) -> i32 {
|
fn year(&self) -> i32 {
|
||||||
self.naive_local().year()
|
self.overflowing_naive_local().year()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn month(&self) -> u32 {
|
fn month(&self) -> u32 {
|
||||||
self.naive_local().month()
|
self.overflowing_naive_local().month()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn month0(&self) -> u32 {
|
fn month0(&self) -> u32 {
|
||||||
self.naive_local().month0()
|
self.overflowing_naive_local().month0()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn day(&self) -> u32 {
|
fn day(&self) -> u32 {
|
||||||
self.naive_local().day()
|
self.overflowing_naive_local().day()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn day0(&self) -> u32 {
|
fn day0(&self) -> u32 {
|
||||||
self.naive_local().day0()
|
self.overflowing_naive_local().day0()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn ordinal(&self) -> u32 {
|
fn ordinal(&self) -> u32 {
|
||||||
self.naive_local().ordinal()
|
self.overflowing_naive_local().ordinal()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn ordinal0(&self) -> u32 {
|
fn ordinal0(&self) -> u32 {
|
||||||
self.naive_local().ordinal0()
|
self.overflowing_naive_local().ordinal0()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn weekday(&self) -> Weekday {
|
fn weekday(&self) -> Weekday {
|
||||||
self.naive_local().weekday()
|
self.overflowing_naive_local().weekday()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn iso_week(&self) -> IsoWeek {
|
fn iso_week(&self) -> IsoWeek {
|
||||||
self.naive_local().iso_week()
|
self.overflowing_naive_local().iso_week()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -1105,19 +1105,19 @@ impl<Tz: TimeZone> Datelike for DateTime<Tz> {
|
|||||||
impl<Tz: TimeZone> Timelike for DateTime<Tz> {
|
impl<Tz: TimeZone> Timelike for DateTime<Tz> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn hour(&self) -> u32 {
|
fn hour(&self) -> u32 {
|
||||||
self.naive_local().hour()
|
self.overflowing_naive_local().hour()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn minute(&self) -> u32 {
|
fn minute(&self) -> u32 {
|
||||||
self.naive_local().minute()
|
self.overflowing_naive_local().minute()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn second(&self) -> u32 {
|
fn second(&self) -> u32 {
|
||||||
self.naive_local().second()
|
self.overflowing_naive_local().second()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn nanosecond(&self) -> u32 {
|
fn nanosecond(&self) -> u32 {
|
||||||
self.naive_local().nanosecond()
|
self.overflowing_naive_local().nanosecond()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes a new `DateTime` with the hour number changed.
|
/// Makes a new `DateTime` with the hour number changed.
|
||||||
@ -1521,7 +1521,7 @@ impl<Tz: TimeZone> Sub<Days> for DateTime<Tz> {
|
|||||||
|
|
||||||
impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
|
impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.naive_local().fmt(f)?;
|
self.overflowing_naive_local().fmt(f)?;
|
||||||
self.offset.fmt(f)
|
self.offset.fmt(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1531,7 +1531,7 @@ where
|
|||||||
Tz::Offset: fmt::Display,
|
Tz::Offset: fmt::Display,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.naive_local().fmt(f)?;
|
self.overflowing_naive_local().fmt(f)?;
|
||||||
f.write_char(' ')?;
|
f.write_char(' ')?;
|
||||||
self.offset.fmt(f)
|
self.offset.fmt(f)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use crate::naive::{NaiveDate, NaiveTime};
|
|||||||
use crate::offset::{FixedOffset, TimeZone, Utc};
|
use crate::offset::{FixedOffset, TimeZone, Utc};
|
||||||
#[cfg(feature = "clock")]
|
#[cfg(feature = "clock")]
|
||||||
use crate::offset::{Local, Offset};
|
use crate::offset::{Local, Offset};
|
||||||
use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime, Timelike};
|
use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime, Timelike, Weekday};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct DstTester;
|
struct DstTester;
|
||||||
@ -1331,6 +1331,64 @@ fn test_datetime_sub_assign() {
|
|||||||
assert_eq!(datetime_sub, datetime - OldDuration::minutes(90));
|
assert_eq!(datetime_sub, datetime - OldDuration::minutes(90));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_min_max_getters() {
|
||||||
|
let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
|
||||||
|
let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
|
||||||
|
let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
|
||||||
|
let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
|
||||||
|
|
||||||
|
assert_eq!(format!("{:?}", beyond_min), "-262144-12-31T22:00:00-02:00");
|
||||||
|
// RFC 2822 doesn't support years with more than 4 digits.
|
||||||
|
// assert_eq!(beyond_min.to_rfc2822(), "");
|
||||||
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
||||||
|
assert_eq!(beyond_min.to_rfc3339(), "-262144-12-31T22:00:00-02:00");
|
||||||
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
||||||
|
assert_eq!(
|
||||||
|
beyond_min.format("%Y-%m-%dT%H:%M:%S%:z").to_string(),
|
||||||
|
"-262144-12-31T22:00:00-02:00"
|
||||||
|
);
|
||||||
|
assert_eq!(beyond_min.year(), -262144);
|
||||||
|
assert_eq!(beyond_min.month(), 12);
|
||||||
|
assert_eq!(beyond_min.month0(), 11);
|
||||||
|
assert_eq!(beyond_min.day(), 31);
|
||||||
|
assert_eq!(beyond_min.day0(), 30);
|
||||||
|
assert_eq!(beyond_min.ordinal(), 366);
|
||||||
|
assert_eq!(beyond_min.ordinal0(), 365);
|
||||||
|
assert_eq!(beyond_min.weekday(), Weekday::Wed);
|
||||||
|
assert_eq!(beyond_min.iso_week().year(), -262143);
|
||||||
|
assert_eq!(beyond_min.iso_week().week(), 1);
|
||||||
|
assert_eq!(beyond_min.hour(), 22);
|
||||||
|
assert_eq!(beyond_min.minute(), 0);
|
||||||
|
assert_eq!(beyond_min.second(), 0);
|
||||||
|
assert_eq!(beyond_min.nanosecond(), 0);
|
||||||
|
|
||||||
|
assert_eq!(format!("{:?}", beyond_max), "+262143-01-01T01:59:59.999999999+02:00");
|
||||||
|
// RFC 2822 doesn't support years with more than 4 digits.
|
||||||
|
// assert_eq!(beyond_max.to_rfc2822(), "");
|
||||||
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
||||||
|
assert_eq!(beyond_max.to_rfc3339(), "+262143-01-01T01:59:59.999999999+02:00");
|
||||||
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
||||||
|
assert_eq!(
|
||||||
|
beyond_max.format("%Y-%m-%dT%H:%M:%S%.9f%:z").to_string(),
|
||||||
|
"+262143-01-01T01:59:59.999999999+02:00"
|
||||||
|
);
|
||||||
|
assert_eq!(beyond_max.year(), 262143);
|
||||||
|
assert_eq!(beyond_max.month(), 1);
|
||||||
|
assert_eq!(beyond_max.month0(), 0);
|
||||||
|
assert_eq!(beyond_max.day(), 1);
|
||||||
|
assert_eq!(beyond_max.day0(), 0);
|
||||||
|
assert_eq!(beyond_max.ordinal(), 1);
|
||||||
|
assert_eq!(beyond_max.ordinal0(), 0);
|
||||||
|
assert_eq!(beyond_max.weekday(), Weekday::Tue);
|
||||||
|
assert_eq!(beyond_max.iso_week().year(), 262143);
|
||||||
|
assert_eq!(beyond_max.iso_week().week(), 1);
|
||||||
|
assert_eq!(beyond_max.hour(), 1);
|
||||||
|
assert_eq!(beyond_max.minute(), 59);
|
||||||
|
assert_eq!(beyond_max.second(), 59);
|
||||||
|
assert_eq!(beyond_max.nanosecond(), 999_999_999);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_local_beyond_min_datetime() {
|
fn test_local_beyond_min_datetime() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user