| 1 |
use super::{Days, MAX_YEAR, MIN_YEAR, Months, NaiveDate}; |
| 2 |
use crate::naive::internals::{A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF, YearFlags}; |
| 3 |
use crate::{Datelike, TimeDelta, Weekday}; |
| 4 |
|
| 5 |
// as it is hard to verify year flags in `NaiveDate::MIN` and `NaiveDate::MAX`, |
| 6 |
// we use a separate run-time test. |
| 7 |
#[test] |
| 8 |
fn test_date_bounds() { |
| 9 |
let calculated_min = NaiveDate::from_ymd_opt(MIN_YEAR, 1, 1).unwrap(); |
| 10 |
let calculated_max = NaiveDate::from_ymd_opt(MAX_YEAR, 12, 31).unwrap(); |
| 11 |
assert!( |
| 12 |
NaiveDate::MIN == calculated_min, |
| 13 |
"`NaiveDate::MIN` should have year flag {:?}", |
| 14 |
calculated_min.year_flags() |
| 15 |
); |
| 16 |
assert!( |
| 17 |
NaiveDate::MAX == calculated_max, |
| 18 |
"`NaiveDate::MAX` should have year flag {:?} and ordinal {}", |
| 19 |
calculated_max.year_flags(), |
| 20 |
calculated_max.ordinal() |
| 21 |
); |
| 22 |
|
| 23 |
// let's also check that the entire range do not exceed 2^44 seconds |
| 24 |
// (sometimes used for bounding `TimeDelta` against overflow) |
| 25 |
let maxsecs = NaiveDate::MAX.signed_duration_since(NaiveDate::MIN).num_seconds(); |
| 26 |
let maxsecs = maxsecs + 86401; // also take care of DateTime |
| 27 |
assert!( |
| 28 |
maxsecs < (1 << MAX_BITS), |
| 29 |
"The entire `NaiveDate` range somehow exceeds 2^{MAX_BITS} seconds" |
| 30 |
); |
| 31 |
|
| 32 |
const BEFORE_MIN: NaiveDate = NaiveDate::BEFORE_MIN; |
| 33 |
assert_eq!(BEFORE_MIN.year_flags(), YearFlags::from_year(BEFORE_MIN.year())); |
| 34 |
assert_eq!((BEFORE_MIN.month(), BEFORE_MIN.day()), (12, 31)); |
| 35 |
|
| 36 |
const AFTER_MAX: NaiveDate = NaiveDate::AFTER_MAX; |
| 37 |
assert_eq!(AFTER_MAX.year_flags(), YearFlags::from_year(AFTER_MAX.year())); |
| 38 |
assert_eq!((AFTER_MAX.month(), AFTER_MAX.day()), (1, 1)); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn diff_months() { |
| 43 |
// identity |
| 44 |
assert_eq!( |
| 45 |
NaiveDate::from_ymd_opt(2022, 8, 3).unwrap().checked_add_months(Months::new(0)), |
| 46 |
Some(NaiveDate::from_ymd_opt(2022, 8, 3).unwrap()) |
| 47 |
); |
| 48 |
|
| 49 |
// add with months exceeding `i32::MAX` |
| 50 |
assert_eq!( |
| 51 |
NaiveDate::from_ymd_opt(2022, 8, 3) |
| 52 |
.unwrap() |
| 53 |
.checked_add_months(Months::new(i32::MAX as u32 + 1)), |
| 54 |
None |
| 55 |
); |
| 56 |
|
| 57 |
// sub with months exceeding `i32::MIN` |
| 58 |
assert_eq!( |
| 59 |
NaiveDate::from_ymd_opt(2022, 8, 3) |
| 60 |
.unwrap() |
| 61 |
.checked_sub_months(Months::new(i32::MIN.unsigned_abs() + 1)), |
| 62 |
None |
| 63 |
); |
| 64 |
|
| 65 |
// add overflowing year |
| 66 |
assert_eq!(NaiveDate::MAX.checked_add_months(Months::new(1)), None); |
| 67 |
|
| 68 |
// add underflowing year |
| 69 |
assert_eq!(NaiveDate::MIN.checked_sub_months(Months::new(1)), None); |
| 70 |
|
| 71 |
// sub crossing year 0 boundary |
| 72 |
assert_eq!( |
| 73 |
NaiveDate::from_ymd_opt(2022, 8, 3).unwrap().checked_sub_months(Months::new(2050 * 12)), |
| 74 |
Some(NaiveDate::from_ymd_opt(-28, 8, 3).unwrap()) |
| 75 |
); |
| 76 |
|
| 77 |
// add crossing year boundary |
| 78 |
assert_eq!( |
| 79 |
NaiveDate::from_ymd_opt(2022, 8, 3).unwrap().checked_add_months(Months::new(6)), |
| 80 |
Some(NaiveDate::from_ymd_opt(2023, 2, 3).unwrap()) |
| 81 |
); |
| 82 |
|
| 83 |
// sub crossing year boundary |
| 84 |
assert_eq!( |
| 85 |
NaiveDate::from_ymd_opt(2022, 8, 3).unwrap().checked_sub_months(Months::new(10)), |
| 86 |
Some(NaiveDate::from_ymd_opt(2021, 10, 3).unwrap()) |
| 87 |
); |
| 88 |
|
| 89 |
// add clamping day, non-leap year |
| 90 |
assert_eq!( |
| 91 |
NaiveDate::from_ymd_opt(2022, 1, 29).unwrap().checked_add_months(Months::new(1)), |
| 92 |
Some(NaiveDate::from_ymd_opt(2022, 2, 28).unwrap()) |
| 93 |
); |
| 94 |
|
| 95 |
// add to leap day |
| 96 |
assert_eq!( |
| 97 |
NaiveDate::from_ymd_opt(2022, 10, 29).unwrap().checked_add_months(Months::new(16)), |
| 98 |
Some(NaiveDate::from_ymd_opt(2024, 2, 29).unwrap()) |
| 99 |
); |
| 100 |
|
| 101 |
// add into december |
| 102 |
assert_eq!( |
| 103 |
NaiveDate::from_ymd_opt(2022, 10, 31).unwrap().checked_add_months(Months::new(2)), |
| 104 |
Some(NaiveDate::from_ymd_opt(2022, 12, 31).unwrap()) |
| 105 |
); |
| 106 |
|
| 107 |
// sub into december |
| 108 |
assert_eq!( |
| 109 |
NaiveDate::from_ymd_opt(2022, 10, 31).unwrap().checked_sub_months(Months::new(10)), |
| 110 |
Some(NaiveDate::from_ymd_opt(2021, 12, 31).unwrap()) |
| 111 |
); |
| 112 |
|
| 113 |
// add into january |
| 114 |
assert_eq!( |
| 115 |
NaiveDate::from_ymd_opt(2022, 8, 3).unwrap().checked_add_months(Months::new(5)), |
| 116 |
Some(NaiveDate::from_ymd_opt(2023, 1, 3).unwrap()) |
| 117 |
); |
| 118 |
|
| 119 |
// sub into january |
| 120 |
assert_eq!( |
| 121 |
NaiveDate::from_ymd_opt(2022, 8, 3).unwrap().checked_sub_months(Months::new(7)), |
| 122 |
Some(NaiveDate::from_ymd_opt(2022, 1, 3).unwrap()) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_readme_doomsday() { |
| 128 |
for y in NaiveDate::MIN.year()..=NaiveDate::MAX.year() { |
| 129 |
// even months |
| 130 |
let d4 = NaiveDate::from_ymd_opt(y, 4, 4).unwrap(); |
| 131 |
let d6 = NaiveDate::from_ymd_opt(y, 6, 6).unwrap(); |
| 132 |
let d8 = NaiveDate::from_ymd_opt(y, 8, 8).unwrap(); |
| 133 |
let d10 = NaiveDate::from_ymd_opt(y, 10, 10).unwrap(); |
| 134 |
let d12 = NaiveDate::from_ymd_opt(y, 12, 12).unwrap(); |
| 135 |
|
| 136 |
// nine to five, seven-eleven |
| 137 |
let d59 = NaiveDate::from_ymd_opt(y, 5, 9).unwrap(); |
| 138 |
let d95 = NaiveDate::from_ymd_opt(y, 9, 5).unwrap(); |
| 139 |
let d711 = NaiveDate::from_ymd_opt(y, 7, 11).unwrap(); |
| 140 |
let d117 = NaiveDate::from_ymd_opt(y, 11, 7).unwrap(); |
| 141 |
|
| 142 |
// "March 0" |
| 143 |
let d30 = NaiveDate::from_ymd_opt(y, 3, 1).unwrap().pred_opt().unwrap(); |
| 144 |
|
| 145 |
let weekday = d30.weekday(); |
| 146 |
let other_dates = [d4, d6, d8, d10, d12, d59, d95, d711, d117]; |
| 147 |
assert!(other_dates.iter().all(|d| d.weekday() == weekday)); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn test_date_from_ymd() { |
| 153 |
let from_ymd = NaiveDate::from_ymd_opt; |
| 154 |
|
| 155 |
assert!(from_ymd(2012, 0, 1).is_none()); |
| 156 |
assert!(from_ymd(2012, 1, 1).is_some()); |
| 157 |
assert!(from_ymd(2012, 2, 29).is_some()); |
| 158 |
assert!(from_ymd(2014, 2, 29).is_none()); |
| 159 |
assert!(from_ymd(2014, 3, 0).is_none()); |
| 160 |
assert!(from_ymd(2014, 3, 1).is_some()); |
| 161 |
assert!(from_ymd(2014, 3, 31).is_some()); |
| 162 |
assert!(from_ymd(2014, 3, 32).is_none()); |
| 163 |
assert!(from_ymd(2014, 12, 31).is_some()); |
| 164 |
assert!(from_ymd(2014, 13, 1).is_none()); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn test_date_from_yo() { |
| 169 |
let from_yo = NaiveDate::from_yo_opt; |
| 170 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 171 |
|
| 172 |
assert_eq!(from_yo(2012, 0), None); |
| 173 |
assert_eq!(from_yo(2012, 1), Some(ymd(2012, 1, 1))); |
| 174 |
assert_eq!(from_yo(2012, 2), Some(ymd(2012, 1, 2))); |
| 175 |
assert_eq!(from_yo(2012, 32), Some(ymd(2012, 2, 1))); |
| 176 |
assert_eq!(from_yo(2012, 60), Some(ymd(2012, 2, 29))); |
| 177 |
assert_eq!(from_yo(2012, 61), Some(ymd(2012, 3, 1))); |
| 178 |
assert_eq!(from_yo(2012, 100), Some(ymd(2012, 4, 9))); |
| 179 |
assert_eq!(from_yo(2012, 200), Some(ymd(2012, 7, 18))); |
| 180 |
assert_eq!(from_yo(2012, 300), Some(ymd(2012, 10, 26))); |
| 181 |
assert_eq!(from_yo(2012, 366), Some(ymd(2012, 12, 31))); |
| 182 |
assert_eq!(from_yo(2012, 367), None); |
| 183 |
assert_eq!(from_yo(2012, (1 << 28) | 60), None); |
| 184 |
|
| 185 |
assert_eq!(from_yo(2014, 0), None); |
| 186 |
assert_eq!(from_yo(2014, 1), Some(ymd(2014, 1, 1))); |
| 187 |
assert_eq!(from_yo(2014, 2), Some(ymd(2014, 1, 2))); |
| 188 |
assert_eq!(from_yo(2014, 32), Some(ymd(2014, 2, 1))); |
| 189 |
assert_eq!(from_yo(2014, 59), Some(ymd(2014, 2, 28))); |
| 190 |
assert_eq!(from_yo(2014, 60), Some(ymd(2014, 3, 1))); |
| 191 |
assert_eq!(from_yo(2014, 100), Some(ymd(2014, 4, 10))); |
| 192 |
assert_eq!(from_yo(2014, 200), Some(ymd(2014, 7, 19))); |
| 193 |
assert_eq!(from_yo(2014, 300), Some(ymd(2014, 10, 27))); |
| 194 |
assert_eq!(from_yo(2014, 365), Some(ymd(2014, 12, 31))); |
| 195 |
assert_eq!(from_yo(2014, 366), None); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn test_date_from_isoywd() { |
| 200 |
let from_isoywd = NaiveDate::from_isoywd_opt; |
| 201 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 202 |
|
| 203 |
assert_eq!(from_isoywd(2004, 0, Weekday::Sun), None); |
| 204 |
assert_eq!(from_isoywd(2004, 1, Weekday::Mon), Some(ymd(2003, 12, 29))); |
| 205 |
assert_eq!(from_isoywd(2004, 1, Weekday::Sun), Some(ymd(2004, 1, 4))); |
| 206 |
assert_eq!(from_isoywd(2004, 2, Weekday::Mon), Some(ymd(2004, 1, 5))); |
| 207 |
assert_eq!(from_isoywd(2004, 2, Weekday::Sun), Some(ymd(2004, 1, 11))); |
| 208 |
assert_eq!(from_isoywd(2004, 52, Weekday::Mon), Some(ymd(2004, 12, 20))); |
| 209 |
assert_eq!(from_isoywd(2004, 52, Weekday::Sun), Some(ymd(2004, 12, 26))); |
| 210 |
assert_eq!(from_isoywd(2004, 53, Weekday::Mon), Some(ymd(2004, 12, 27))); |
| 211 |
assert_eq!(from_isoywd(2004, 53, Weekday::Sun), Some(ymd(2005, 1, 2))); |
| 212 |
assert_eq!(from_isoywd(2004, 54, Weekday::Mon), None); |
| 213 |
|
| 214 |
assert_eq!(from_isoywd(2011, 0, Weekday::Sun), None); |
| 215 |
assert_eq!(from_isoywd(2011, 1, Weekday::Mon), Some(ymd(2011, 1, 3))); |
| 216 |
assert_eq!(from_isoywd(2011, 1, Weekday::Sun), Some(ymd(2011, 1, 9))); |
| 217 |
assert_eq!(from_isoywd(2011, 2, Weekday::Mon), Some(ymd(2011, 1, 10))); |
| 218 |
assert_eq!(from_isoywd(2011, 2, Weekday::Sun), Some(ymd(2011, 1, 16))); |
| 219 |
|
| 220 |
assert_eq!(from_isoywd(2018, 51, Weekday::Mon), Some(ymd(2018, 12, 17))); |
| 221 |
assert_eq!(from_isoywd(2018, 51, Weekday::Sun), Some(ymd(2018, 12, 23))); |
| 222 |
assert_eq!(from_isoywd(2018, 52, Weekday::Mon), Some(ymd(2018, 12, 24))); |
| 223 |
assert_eq!(from_isoywd(2018, 52, Weekday::Sun), Some(ymd(2018, 12, 30))); |
| 224 |
assert_eq!(from_isoywd(2018, 53, Weekday::Mon), None); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn test_date_from_isoywd_and_iso_week() { |
| 229 |
for year in 2000..2401 { |
| 230 |
for week in 1..54 { |
| 231 |
for &weekday in [ |
| 232 |
Weekday::Mon, |
| 233 |
Weekday::Tue, |
| 234 |
Weekday::Wed, |
| 235 |
Weekday::Thu, |
| 236 |
Weekday::Fri, |
| 237 |
Weekday::Sat, |
| 238 |
Weekday::Sun, |
| 239 |
] |
| 240 |
.iter() |
| 241 |
{ |
| 242 |
let d = NaiveDate::from_isoywd_opt(year, week, weekday); |
| 243 |
if let Some(d) = d { |
| 244 |
assert_eq!(d.weekday(), weekday); |
| 245 |
let w = d.iso_week(); |
| 246 |
assert_eq!(w.year(), year); |
| 247 |
assert_eq!(w.week(), week); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
for year in 2000..2401 { |
| 254 |
for month in 1..13 { |
| 255 |
for day in 1..32 { |
| 256 |
let d = NaiveDate::from_ymd_opt(year, month, day); |
| 257 |
if let Some(d) = d { |
| 258 |
let w = d.iso_week(); |
| 259 |
let d_ = NaiveDate::from_isoywd_opt(w.year(), w.week(), d.weekday()); |
| 260 |
assert_eq!(d, d_.unwrap()); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn test_date_from_num_days_from_ce() { |
| 269 |
let from_ndays_from_ce = NaiveDate::from_num_days_from_ce_opt; |
| 270 |
assert_eq!(from_ndays_from_ce(1), Some(NaiveDate::from_ymd_opt(1, 1, 1).unwrap())); |
| 271 |
assert_eq!(from_ndays_from_ce(2), Some(NaiveDate::from_ymd_opt(1, 1, 2).unwrap())); |
| 272 |
assert_eq!(from_ndays_from_ce(31), Some(NaiveDate::from_ymd_opt(1, 1, 31).unwrap())); |
| 273 |
assert_eq!(from_ndays_from_ce(32), Some(NaiveDate::from_ymd_opt(1, 2, 1).unwrap())); |
| 274 |
assert_eq!(from_ndays_from_ce(59), Some(NaiveDate::from_ymd_opt(1, 2, 28).unwrap())); |
| 275 |
assert_eq!(from_ndays_from_ce(60), Some(NaiveDate::from_ymd_opt(1, 3, 1).unwrap())); |
| 276 |
assert_eq!(from_ndays_from_ce(365), Some(NaiveDate::from_ymd_opt(1, 12, 31).unwrap())); |
| 277 |
assert_eq!(from_ndays_from_ce(365 + 1), Some(NaiveDate::from_ymd_opt(2, 1, 1).unwrap())); |
| 278 |
assert_eq!(from_ndays_from_ce(365 * 2 + 1), Some(NaiveDate::from_ymd_opt(3, 1, 1).unwrap())); |
| 279 |
assert_eq!(from_ndays_from_ce(365 * 3 + 1), Some(NaiveDate::from_ymd_opt(4, 1, 1).unwrap())); |
| 280 |
assert_eq!(from_ndays_from_ce(365 * 4 + 2), Some(NaiveDate::from_ymd_opt(5, 1, 1).unwrap())); |
| 281 |
assert_eq!(from_ndays_from_ce(146097 + 1), Some(NaiveDate::from_ymd_opt(401, 1, 1).unwrap())); |
| 282 |
assert_eq!( |
| 283 |
from_ndays_from_ce(146097 * 5 + 1), |
| 284 |
Some(NaiveDate::from_ymd_opt(2001, 1, 1).unwrap()) |
| 285 |
); |
| 286 |
assert_eq!(from_ndays_from_ce(719163), Some(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())); |
| 287 |
assert_eq!(from_ndays_from_ce(0), Some(NaiveDate::from_ymd_opt(0, 12, 31).unwrap())); // 1 BCE |
| 288 |
assert_eq!(from_ndays_from_ce(-365), Some(NaiveDate::from_ymd_opt(0, 1, 1).unwrap())); |
| 289 |
assert_eq!(from_ndays_from_ce(-366), Some(NaiveDate::from_ymd_opt(-1, 12, 31).unwrap())); // 2 BCE |
| 290 |
|
| 291 |
for days in (-9999..10001).map(|x| x * 100) { |
| 292 |
assert_eq!(from_ndays_from_ce(days).map(|d| d.num_days_from_ce()), Some(days)); |
| 293 |
} |
| 294 |
|
| 295 |
assert_eq!(from_ndays_from_ce(NaiveDate::MIN.num_days_from_ce()), Some(NaiveDate::MIN)); |
| 296 |
assert_eq!(from_ndays_from_ce(NaiveDate::MIN.num_days_from_ce() - 1), None); |
| 297 |
assert_eq!(from_ndays_from_ce(NaiveDate::MAX.num_days_from_ce()), Some(NaiveDate::MAX)); |
| 298 |
assert_eq!(from_ndays_from_ce(NaiveDate::MAX.num_days_from_ce() + 1), None); |
| 299 |
|
| 300 |
assert_eq!(from_ndays_from_ce(i32::MIN), None); |
| 301 |
assert_eq!(from_ndays_from_ce(i32::MAX), None); |
| 302 |
} |
| 303 |
|
| 304 |
#[test] |
| 305 |
fn test_date_from_epoch_days() { |
| 306 |
let from_epoch_days = NaiveDate::from_epoch_days; |
| 307 |
assert_eq!(from_epoch_days(-719_162), Some(NaiveDate::from_ymd_opt(1, 1, 1).unwrap())); |
| 308 |
assert_eq!(from_epoch_days(0), Some(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())); |
| 309 |
assert_eq!(from_epoch_days(1), Some(NaiveDate::from_ymd_opt(1970, 1, 2).unwrap())); |
| 310 |
assert_eq!(from_epoch_days(2), Some(NaiveDate::from_ymd_opt(1970, 1, 3).unwrap())); |
| 311 |
assert_eq!(from_epoch_days(30), Some(NaiveDate::from_ymd_opt(1970, 1, 31).unwrap())); |
| 312 |
assert_eq!(from_epoch_days(31), Some(NaiveDate::from_ymd_opt(1970, 2, 1).unwrap())); |
| 313 |
assert_eq!(from_epoch_days(58), Some(NaiveDate::from_ymd_opt(1970, 2, 28).unwrap())); |
| 314 |
assert_eq!(from_epoch_days(59), Some(NaiveDate::from_ymd_opt(1970, 3, 1).unwrap())); |
| 315 |
assert_eq!(from_epoch_days(364), Some(NaiveDate::from_ymd_opt(1970, 12, 31).unwrap())); |
| 316 |
assert_eq!(from_epoch_days(365), Some(NaiveDate::from_ymd_opt(1971, 1, 1).unwrap())); |
| 317 |
assert_eq!(from_epoch_days(365 * 2), Some(NaiveDate::from_ymd_opt(1972, 1, 1).unwrap())); |
| 318 |
assert_eq!(from_epoch_days(365 * 3 + 1), Some(NaiveDate::from_ymd_opt(1973, 1, 1).unwrap())); |
| 319 |
assert_eq!(from_epoch_days(365 * 4 + 1), Some(NaiveDate::from_ymd_opt(1974, 1, 1).unwrap())); |
| 320 |
assert_eq!(from_epoch_days(13036), Some(NaiveDate::from_ymd_opt(2005, 9, 10).unwrap())); |
| 321 |
assert_eq!(from_epoch_days(-365), Some(NaiveDate::from_ymd_opt(1969, 1, 1).unwrap())); |
| 322 |
assert_eq!(from_epoch_days(-366), Some(NaiveDate::from_ymd_opt(1968, 12, 31).unwrap())); |
| 323 |
|
| 324 |
for days in (-9999..10001).map(|x| x * 100) { |
| 325 |
assert_eq!(from_epoch_days(days).map(|d| d.to_epoch_days()), Some(days)); |
| 326 |
} |
| 327 |
|
| 328 |
assert_eq!(from_epoch_days(NaiveDate::MIN.to_epoch_days()), Some(NaiveDate::MIN)); |
| 329 |
assert_eq!(from_epoch_days(NaiveDate::MIN.to_epoch_days() - 1), None); |
| 330 |
assert_eq!(from_epoch_days(NaiveDate::MAX.to_epoch_days()), Some(NaiveDate::MAX)); |
| 331 |
assert_eq!(from_epoch_days(NaiveDate::MAX.to_epoch_days() + 1), None); |
| 332 |
|
| 333 |
assert_eq!(from_epoch_days(i32::MIN), None); |
| 334 |
assert_eq!(from_epoch_days(i32::MAX), None); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn test_date_from_weekday_of_month_opt() { |
| 339 |
let ymwd = NaiveDate::from_weekday_of_month_opt; |
| 340 |
assert_eq!(ymwd(2018, 8, Weekday::Tue, 0), None); |
| 341 |
assert_eq!(ymwd(2018, 8, Weekday::Wed, 1), Some(NaiveDate::from_ymd_opt(2018, 8, 1).unwrap())); |
| 342 |
assert_eq!(ymwd(2018, 8, Weekday::Thu, 1), Some(NaiveDate::from_ymd_opt(2018, 8, 2).unwrap())); |
| 343 |
assert_eq!(ymwd(2018, 8, Weekday::Sun, 1), Some(NaiveDate::from_ymd_opt(2018, 8, 5).unwrap())); |
| 344 |
assert_eq!(ymwd(2018, 8, Weekday::Mon, 1), Some(NaiveDate::from_ymd_opt(2018, 8, 6).unwrap())); |
| 345 |
assert_eq!(ymwd(2018, 8, Weekday::Tue, 1), Some(NaiveDate::from_ymd_opt(2018, 8, 7).unwrap())); |
| 346 |
assert_eq!(ymwd(2018, 8, Weekday::Wed, 2), Some(NaiveDate::from_ymd_opt(2018, 8, 8).unwrap())); |
| 347 |
assert_eq!(ymwd(2018, 8, Weekday::Sun, 2), Some(NaiveDate::from_ymd_opt(2018, 8, 12).unwrap())); |
| 348 |
assert_eq!(ymwd(2018, 8, Weekday::Thu, 3), Some(NaiveDate::from_ymd_opt(2018, 8, 16).unwrap())); |
| 349 |
assert_eq!(ymwd(2018, 8, Weekday::Thu, 4), Some(NaiveDate::from_ymd_opt(2018, 8, 23).unwrap())); |
| 350 |
assert_eq!(ymwd(2018, 8, Weekday::Thu, 5), Some(NaiveDate::from_ymd_opt(2018, 8, 30).unwrap())); |
| 351 |
assert_eq!(ymwd(2018, 8, Weekday::Fri, 5), Some(NaiveDate::from_ymd_opt(2018, 8, 31).unwrap())); |
| 352 |
assert_eq!(ymwd(2018, 8, Weekday::Sat, 5), None); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn test_date_fields() { |
| 357 |
fn check(year: i32, month: u32, day: u32, ordinal: u32) { |
| 358 |
let d1 = NaiveDate::from_ymd_opt(year, month, day).unwrap(); |
| 359 |
assert_eq!(d1.year(), year); |
| 360 |
assert_eq!(d1.month(), month); |
| 361 |
assert_eq!(d1.day(), day); |
| 362 |
assert_eq!(d1.ordinal(), ordinal); |
| 363 |
|
| 364 |
let d2 = NaiveDate::from_yo_opt(year, ordinal).unwrap(); |
| 365 |
assert_eq!(d2.year(), year); |
| 366 |
assert_eq!(d2.month(), month); |
| 367 |
assert_eq!(d2.day(), day); |
| 368 |
assert_eq!(d2.ordinal(), ordinal); |
| 369 |
|
| 370 |
assert_eq!(d1, d2); |
| 371 |
} |
| 372 |
|
| 373 |
check(2012, 1, 1, 1); |
| 374 |
check(2012, 1, 2, 2); |
| 375 |
check(2012, 2, 1, 32); |
| 376 |
check(2012, 2, 29, 60); |
| 377 |
check(2012, 3, 1, 61); |
| 378 |
check(2012, 4, 9, 100); |
| 379 |
check(2012, 7, 18, 200); |
| 380 |
check(2012, 10, 26, 300); |
| 381 |
check(2012, 12, 31, 366); |
| 382 |
|
| 383 |
check(2014, 1, 1, 1); |
| 384 |
check(2014, 1, 2, 2); |
| 385 |
check(2014, 2, 1, 32); |
| 386 |
check(2014, 2, 28, 59); |
| 387 |
check(2014, 3, 1, 60); |
| 388 |
check(2014, 4, 10, 100); |
| 389 |
check(2014, 7, 19, 200); |
| 390 |
check(2014, 10, 27, 300); |
| 391 |
check(2014, 12, 31, 365); |
| 392 |
} |
| 393 |
|
| 394 |
#[test] |
| 395 |
fn test_date_weekday() { |
| 396 |
assert_eq!(NaiveDate::from_ymd_opt(1582, 10, 15).unwrap().weekday(), Weekday::Fri); |
| 397 |
// May 20, 1875 = ISO 8601 reference date |
| 398 |
assert_eq!(NaiveDate::from_ymd_opt(1875, 5, 20).unwrap().weekday(), Weekday::Thu); |
| 399 |
assert_eq!(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().weekday(), Weekday::Sat); |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn test_date_with_fields() { |
| 404 |
let d = NaiveDate::from_ymd_opt(2000, 2, 29).unwrap(); |
| 405 |
assert_eq!(d.with_year(-400), Some(NaiveDate::from_ymd_opt(-400, 2, 29).unwrap())); |
| 406 |
assert_eq!(d.with_year(-100), None); |
| 407 |
assert_eq!(d.with_year(1600), Some(NaiveDate::from_ymd_opt(1600, 2, 29).unwrap())); |
| 408 |
assert_eq!(d.with_year(1900), None); |
| 409 |
assert_eq!(d.with_year(2000), Some(NaiveDate::from_ymd_opt(2000, 2, 29).unwrap())); |
| 410 |
assert_eq!(d.with_year(2001), None); |
| 411 |
assert_eq!(d.with_year(2004), Some(NaiveDate::from_ymd_opt(2004, 2, 29).unwrap())); |
| 412 |
assert_eq!(d.with_year(i32::MAX), None); |
| 413 |
|
| 414 |
let d = NaiveDate::from_ymd_opt(2000, 4, 30).unwrap(); |
| 415 |
assert_eq!(d.with_month(0), None); |
| 416 |
assert_eq!(d.with_month(1), Some(NaiveDate::from_ymd_opt(2000, 1, 30).unwrap())); |
| 417 |
assert_eq!(d.with_month(2), None); |
| 418 |
assert_eq!(d.with_month(3), Some(NaiveDate::from_ymd_opt(2000, 3, 30).unwrap())); |
| 419 |
assert_eq!(d.with_month(4), Some(NaiveDate::from_ymd_opt(2000, 4, 30).unwrap())); |
| 420 |
assert_eq!(d.with_month(12), Some(NaiveDate::from_ymd_opt(2000, 12, 30).unwrap())); |
| 421 |
assert_eq!(d.with_month(13), None); |
| 422 |
assert_eq!(d.with_month(u32::MAX), None); |
| 423 |
|
| 424 |
let d = NaiveDate::from_ymd_opt(2000, 2, 8).unwrap(); |
| 425 |
assert_eq!(d.with_day(0), None); |
| 426 |
assert_eq!(d.with_day(1), Some(NaiveDate::from_ymd_opt(2000, 2, 1).unwrap())); |
| 427 |
assert_eq!(d.with_day(29), Some(NaiveDate::from_ymd_opt(2000, 2, 29).unwrap())); |
| 428 |
assert_eq!(d.with_day(30), None); |
| 429 |
assert_eq!(d.with_day(u32::MAX), None); |
| 430 |
} |
| 431 |
|
| 432 |
#[test] |
| 433 |
fn test_date_with_ordinal() { |
| 434 |
let d = NaiveDate::from_ymd_opt(2000, 5, 5).unwrap(); |
| 435 |
assert_eq!(d.with_ordinal(0), None); |
| 436 |
assert_eq!(d.with_ordinal(1), Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap())); |
| 437 |
assert_eq!(d.with_ordinal(60), Some(NaiveDate::from_ymd_opt(2000, 2, 29).unwrap())); |
| 438 |
assert_eq!(d.with_ordinal(61), Some(NaiveDate::from_ymd_opt(2000, 3, 1).unwrap())); |
| 439 |
assert_eq!(d.with_ordinal(366), Some(NaiveDate::from_ymd_opt(2000, 12, 31).unwrap())); |
| 440 |
assert_eq!(d.with_ordinal(367), None); |
| 441 |
assert_eq!(d.with_ordinal((1 << 28) | 60), None); |
| 442 |
let d = NaiveDate::from_ymd_opt(1999, 5, 5).unwrap(); |
| 443 |
assert_eq!(d.with_ordinal(366), None); |
| 444 |
assert_eq!(d.with_ordinal(u32::MAX), None); |
| 445 |
} |
| 446 |
|
| 447 |
#[test] |
| 448 |
fn test_date_num_days_from_ce() { |
| 449 |
assert_eq!(NaiveDate::from_ymd_opt(1, 1, 1).unwrap().num_days_from_ce(), 1); |
| 450 |
|
| 451 |
for year in -9999..10001 { |
| 452 |
assert_eq!( |
| 453 |
NaiveDate::from_ymd_opt(year, 1, 1).unwrap().num_days_from_ce(), |
| 454 |
NaiveDate::from_ymd_opt(year - 1, 12, 31).unwrap().num_days_from_ce() + 1 |
| 455 |
); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
#[test] |
| 460 |
fn test_date_to_epoch_days() { |
| 461 |
assert_eq!(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().to_epoch_days(), 0); |
| 462 |
|
| 463 |
for year in -9999..10001 { |
| 464 |
assert_eq!( |
| 465 |
NaiveDate::from_ymd_opt(year, 1, 1).unwrap().to_epoch_days(), |
| 466 |
NaiveDate::from_ymd_opt(year - 1, 12, 31).unwrap().to_epoch_days() + 1 |
| 467 |
); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn test_date_succ() { |
| 473 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 474 |
assert_eq!(ymd(2014, 5, 6).succ_opt(), Some(ymd(2014, 5, 7))); |
| 475 |
assert_eq!(ymd(2014, 5, 31).succ_opt(), Some(ymd(2014, 6, 1))); |
| 476 |
assert_eq!(ymd(2014, 12, 31).succ_opt(), Some(ymd(2015, 1, 1))); |
| 477 |
assert_eq!(ymd(2016, 2, 28).succ_opt(), Some(ymd(2016, 2, 29))); |
| 478 |
assert_eq!(ymd(NaiveDate::MAX.year(), 12, 31).succ_opt(), None); |
| 479 |
} |
| 480 |
|
| 481 |
#[test] |
| 482 |
fn test_date_pred() { |
| 483 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 484 |
assert_eq!(ymd(2016, 3, 1).pred_opt(), Some(ymd(2016, 2, 29))); |
| 485 |
assert_eq!(ymd(2015, 1, 1).pred_opt(), Some(ymd(2014, 12, 31))); |
| 486 |
assert_eq!(ymd(2014, 6, 1).pred_opt(), Some(ymd(2014, 5, 31))); |
| 487 |
assert_eq!(ymd(2014, 5, 7).pred_opt(), Some(ymd(2014, 5, 6))); |
| 488 |
assert_eq!(ymd(NaiveDate::MIN.year(), 1, 1).pred_opt(), None); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn test_date_checked_add_signed() { |
| 493 |
fn check(lhs: Option<NaiveDate>, delta: TimeDelta, rhs: Option<NaiveDate>) { |
| 494 |
assert_eq!(lhs.unwrap().checked_add_signed(delta), rhs); |
| 495 |
assert_eq!(lhs.unwrap().checked_sub_signed(-delta), rhs); |
| 496 |
} |
| 497 |
let ymd = NaiveDate::from_ymd_opt; |
| 498 |
|
| 499 |
check(ymd(2014, 1, 1), TimeDelta::zero(), ymd(2014, 1, 1)); |
| 500 |
check(ymd(2014, 1, 1), TimeDelta::try_seconds(86399).unwrap(), ymd(2014, 1, 1)); |
| 501 |
// always round towards zero |
| 502 |
check(ymd(2014, 1, 1), TimeDelta::try_seconds(-86399).unwrap(), ymd(2014, 1, 1)); |
| 503 |
check(ymd(2014, 1, 1), TimeDelta::try_days(1).unwrap(), ymd(2014, 1, 2)); |
| 504 |
check(ymd(2014, 1, 1), TimeDelta::try_days(-1).unwrap(), ymd(2013, 12, 31)); |
| 505 |
check(ymd(2014, 1, 1), TimeDelta::try_days(364).unwrap(), ymd(2014, 12, 31)); |
| 506 |
check(ymd(2014, 1, 1), TimeDelta::try_days(365 * 4 + 1).unwrap(), ymd(2018, 1, 1)); |
| 507 |
check(ymd(2014, 1, 1), TimeDelta::try_days(365 * 400 + 97).unwrap(), ymd(2414, 1, 1)); |
| 508 |
|
| 509 |
check(ymd(-7, 1, 1), TimeDelta::try_days(365 * 12 + 3).unwrap(), ymd(5, 1, 1)); |
| 510 |
|
| 511 |
// overflow check |
| 512 |
check( |
| 513 |
ymd(0, 1, 1), |
| 514 |
TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64).unwrap(), |
| 515 |
ymd(MAX_YEAR, 12, 31), |
| 516 |
); |
| 517 |
check(ymd(0, 1, 1), TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64 + 1).unwrap(), None); |
| 518 |
check(ymd(0, 1, 1), TimeDelta::MAX, None); |
| 519 |
check( |
| 520 |
ymd(0, 1, 1), |
| 521 |
TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap(), |
| 522 |
ymd(MIN_YEAR, 1, 1), |
| 523 |
); |
| 524 |
check(ymd(0, 1, 1), TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64 - 1).unwrap(), None); |
| 525 |
check(ymd(0, 1, 1), TimeDelta::MIN, None); |
| 526 |
} |
| 527 |
|
| 528 |
#[test] |
| 529 |
fn test_date_signed_duration_since() { |
| 530 |
fn check(lhs: Option<NaiveDate>, rhs: Option<NaiveDate>, delta: TimeDelta) { |
| 531 |
assert_eq!(lhs.unwrap().signed_duration_since(rhs.unwrap()), delta); |
| 532 |
assert_eq!(rhs.unwrap().signed_duration_since(lhs.unwrap()), -delta); |
| 533 |
} |
| 534 |
let ymd = NaiveDate::from_ymd_opt; |
| 535 |
|
| 536 |
check(ymd(2014, 1, 1), ymd(2014, 1, 1), TimeDelta::zero()); |
| 537 |
check(ymd(2014, 1, 2), ymd(2014, 1, 1), TimeDelta::try_days(1).unwrap()); |
| 538 |
check(ymd(2014, 12, 31), ymd(2014, 1, 1), TimeDelta::try_days(364).unwrap()); |
| 539 |
check(ymd(2015, 1, 3), ymd(2014, 1, 1), TimeDelta::try_days(365 + 2).unwrap()); |
| 540 |
check(ymd(2018, 1, 1), ymd(2014, 1, 1), TimeDelta::try_days(365 * 4 + 1).unwrap()); |
| 541 |
check(ymd(2414, 1, 1), ymd(2014, 1, 1), TimeDelta::try_days(365 * 400 + 97).unwrap()); |
| 542 |
|
| 543 |
check( |
| 544 |
ymd(MAX_YEAR, 12, 31), |
| 545 |
ymd(0, 1, 1), |
| 546 |
TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64).unwrap(), |
| 547 |
); |
| 548 |
check( |
| 549 |
ymd(MIN_YEAR, 1, 1), |
| 550 |
ymd(0, 1, 1), |
| 551 |
TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap(), |
| 552 |
); |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn test_date_add_days() { |
| 557 |
fn check(lhs: Option<NaiveDate>, days: Days, rhs: Option<NaiveDate>) { |
| 558 |
assert_eq!(lhs.unwrap().checked_add_days(days), rhs); |
| 559 |
} |
| 560 |
let ymd = NaiveDate::from_ymd_opt; |
| 561 |
|
| 562 |
check(ymd(2014, 1, 1), Days::new(0), ymd(2014, 1, 1)); |
| 563 |
// always round towards zero |
| 564 |
check(ymd(2014, 1, 1), Days::new(1), ymd(2014, 1, 2)); |
| 565 |
check(ymd(2014, 1, 1), Days::new(364), ymd(2014, 12, 31)); |
| 566 |
check(ymd(2014, 1, 1), Days::new(365 * 4 + 1), ymd(2018, 1, 1)); |
| 567 |
check(ymd(2014, 1, 1), Days::new(365 * 400 + 97), ymd(2414, 1, 1)); |
| 568 |
|
| 569 |
check(ymd(-7, 1, 1), Days::new(365 * 12 + 3), ymd(5, 1, 1)); |
| 570 |
|
| 571 |
// overflow check |
| 572 |
check(ymd(0, 1, 1), Days::new(MAX_DAYS_FROM_YEAR_0.try_into().unwrap()), ymd(MAX_YEAR, 12, 31)); |
| 573 |
check(ymd(0, 1, 1), Days::new(u64::try_from(MAX_DAYS_FROM_YEAR_0).unwrap() + 1), None); |
| 574 |
} |
| 575 |
|
| 576 |
#[test] |
| 577 |
fn test_date_sub_days() { |
| 578 |
fn check(lhs: Option<NaiveDate>, days: Days, rhs: Option<NaiveDate>) { |
| 579 |
assert_eq!(lhs.unwrap().checked_sub_days(days), rhs); |
| 580 |
} |
| 581 |
let ymd = NaiveDate::from_ymd_opt; |
| 582 |
|
| 583 |
check(ymd(2014, 1, 1), Days::new(0), ymd(2014, 1, 1)); |
| 584 |
check(ymd(2014, 1, 2), Days::new(1), ymd(2014, 1, 1)); |
| 585 |
check(ymd(2014, 12, 31), Days::new(364), ymd(2014, 1, 1)); |
| 586 |
check(ymd(2015, 1, 3), Days::new(365 + 2), ymd(2014, 1, 1)); |
| 587 |
check(ymd(2018, 1, 1), Days::new(365 * 4 + 1), ymd(2014, 1, 1)); |
| 588 |
check(ymd(2414, 1, 1), Days::new(365 * 400 + 97), ymd(2014, 1, 1)); |
| 589 |
|
| 590 |
check(ymd(MAX_YEAR, 12, 31), Days::new(MAX_DAYS_FROM_YEAR_0.try_into().unwrap()), ymd(0, 1, 1)); |
| 591 |
check( |
| 592 |
ymd(0, 1, 1), |
| 593 |
Days::new((-MIN_DAYS_FROM_YEAR_0).try_into().unwrap()), |
| 594 |
ymd(MIN_YEAR, 1, 1), |
| 595 |
); |
| 596 |
} |
| 597 |
|
| 598 |
#[test] |
| 599 |
fn test_date_addassignment() { |
| 600 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 601 |
let mut date = ymd(2016, 10, 1); |
| 602 |
date += TimeDelta::try_days(10).unwrap(); |
| 603 |
assert_eq!(date, ymd(2016, 10, 11)); |
| 604 |
date += TimeDelta::try_days(30).unwrap(); |
| 605 |
assert_eq!(date, ymd(2016, 11, 10)); |
| 606 |
} |
| 607 |
|
| 608 |
#[test] |
| 609 |
fn test_date_subassignment() { |
| 610 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 611 |
let mut date = ymd(2016, 10, 11); |
| 612 |
date -= TimeDelta::try_days(10).unwrap(); |
| 613 |
assert_eq!(date, ymd(2016, 10, 1)); |
| 614 |
date -= TimeDelta::try_days(2).unwrap(); |
| 615 |
assert_eq!(date, ymd(2016, 9, 29)); |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn test_date_fmt() { |
| 620 |
assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(2012, 3, 4).unwrap()), "2012-03-04"); |
| 621 |
assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(0, 3, 4).unwrap()), "0000-03-04"); |
| 622 |
assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(-307, 3, 4).unwrap()), "-0307-03-04"); |
| 623 |
assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(12345, 3, 4).unwrap()), "+12345-03-04"); |
| 624 |
|
| 625 |
assert_eq!(NaiveDate::from_ymd_opt(2012, 3, 4).unwrap().to_string(), "2012-03-04"); |
| 626 |
assert_eq!(NaiveDate::from_ymd_opt(0, 3, 4).unwrap().to_string(), "0000-03-04"); |
| 627 |
assert_eq!(NaiveDate::from_ymd_opt(-307, 3, 4).unwrap().to_string(), "-0307-03-04"); |
| 628 |
assert_eq!(NaiveDate::from_ymd_opt(12345, 3, 4).unwrap().to_string(), "+12345-03-04"); |
| 629 |
|
| 630 |
// the format specifier should have no effect on `NaiveTime` |
| 631 |
assert_eq!(format!("{:+30?}", NaiveDate::from_ymd_opt(1234, 5, 6).unwrap()), "1234-05-06"); |
| 632 |
assert_eq!(format!("{:30?}", NaiveDate::from_ymd_opt(12345, 6, 7).unwrap()), "+12345-06-07"); |
| 633 |
} |
| 634 |
|
| 635 |
#[test] |
| 636 |
fn test_date_from_str() { |
| 637 |
// valid cases |
| 638 |
let valid = [ |
| 639 |
"-0000000123456-1-2", |
| 640 |
" -123456 - 1 - 2 ", |
| 641 |
"-12345-1-2", |
| 642 |
"-1234-12-31", |
| 643 |
"-7-6-5", |
| 644 |
"350-2-28", |
| 645 |
"360-02-29", |
| 646 |
"0360-02-29", |
| 647 |
"2015-2 -18", |
| 648 |
"2015-02-18", |
| 649 |
"+70-2-18", |
| 650 |
"+70000-2-18", |
| 651 |
"+00007-2-18", |
| 652 |
]; |
| 653 |
for &s in &valid { |
| 654 |
eprintln!("test_date_from_str valid {s:?}"); |
| 655 |
let d = match s.parse::<NaiveDate>() { |
| 656 |
Ok(d) => d, |
| 657 |
Err(e) => panic!("parsing `{s}` has failed: {e}"), |
| 658 |
}; |
| 659 |
eprintln!("d {d:?} (NaiveDate)"); |
| 660 |
let s_ = format!("{d:?}"); |
| 661 |
eprintln!("s_ {s_:?}"); |
| 662 |
// `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same |
| 663 |
let d_ = match s_.parse::<NaiveDate>() { |
| 664 |
Ok(d) => d, |
| 665 |
Err(e) => { |
| 666 |
panic!("`{s}` is parsed into `{d:?}`, but reparsing that has failed: {e}") |
| 667 |
} |
| 668 |
}; |
| 669 |
eprintln!("d_ {d_:?} (NaiveDate)"); |
| 670 |
assert!( |
| 671 |
d == d_, |
| 672 |
"`{s}` is parsed into `{d:?}`, but reparsed result \ |
| 673 |
`{d_:?}` does not match" |
| 674 |
); |
| 675 |
} |
| 676 |
|
| 677 |
// some invalid cases |
| 678 |
// since `ParseErrorKind` is private, all we can do is to check if there was an error |
| 679 |
let invalid = [ |
| 680 |
"", // empty |
| 681 |
"x", // invalid |
| 682 |
"Fri, 09 Aug 2013 GMT", // valid date, wrong format |
| 683 |
"Sat Jun 30 2012", // valid date, wrong format |
| 684 |
"1441497364.649", // valid datetime, wrong format |
| 685 |
"+1441497364.649", // valid datetime, wrong format |
| 686 |
"+1441497364", // valid datetime, wrong format |
| 687 |
"2014/02/03", // valid date, wrong format |
| 688 |
"2014", // datetime missing data |
| 689 |
"2014-01", // datetime missing data |
| 690 |
"2014-01-00", // invalid day |
| 691 |
"2014-11-32", // invalid day |
| 692 |
"2014-13-01", // invalid month |
| 693 |
"2014-13-57", // invalid month, day |
| 694 |
"9999999-9-9", // invalid year (out of bounds) |
| 695 |
]; |
| 696 |
for &s in &invalid { |
| 697 |
eprintln!("test_date_from_str invalid {s:?}"); |
| 698 |
assert!(s.parse::<NaiveDate>().is_err()); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
#[test] |
| 703 |
fn test_date_parse_from_str() { |
| 704 |
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); |
| 705 |
assert_eq!( |
| 706 |
NaiveDate::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"), |
| 707 |
Ok(ymd(2014, 5, 7)) |
| 708 |
); // ignore time and offset |
| 709 |
assert_eq!( |
| 710 |
NaiveDate::parse_from_str("2015-W06-1=2015-033 Q1", "%G-W%V-%u = %Y-%j Q%q"), |
| 711 |
Ok(ymd(2015, 2, 2)) |
| 712 |
); |
| 713 |
assert_eq!(NaiveDate::parse_from_str("Fri, 09 Aug 13", "%a, %d %b %y"), Ok(ymd(2013, 8, 9))); |
| 714 |
assert!(NaiveDate::parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err()); |
| 715 |
assert!(NaiveDate::parse_from_str("2014-57", "%Y-%m-%d").is_err()); |
| 716 |
assert!(NaiveDate::parse_from_str("2014", "%Y").is_err()); // insufficient |
| 717 |
|
| 718 |
assert!(NaiveDate::parse_from_str("2014-5-7 Q3", "%Y-%m-%d Q%q").is_err()); // mismatched quarter |
| 719 |
|
| 720 |
assert_eq!( |
| 721 |
NaiveDate::parse_from_str("2020-01-0", "%Y-%W-%w").ok(), |
| 722 |
NaiveDate::from_ymd_opt(2020, 1, 12), |
| 723 |
); |
| 724 |
|
| 725 |
assert_eq!( |
| 726 |
NaiveDate::parse_from_str("2019-01-0", "%Y-%W-%w").ok(), |
| 727 |
NaiveDate::from_ymd_opt(2019, 1, 13), |
| 728 |
); |
| 729 |
} |
| 730 |
|
| 731 |
#[test] |
| 732 |
fn test_day_iterator_limit() { |
| 733 |
assert_eq!(NaiveDate::from_ymd_opt(MAX_YEAR, 12, 29).unwrap().iter_days().take(4).count(), 2); |
| 734 |
assert_eq!( |
| 735 |
NaiveDate::from_ymd_opt(MIN_YEAR, 1, 3).unwrap().iter_days().rev().take(4).count(), |
| 736 |
2 |
| 737 |
); |
| 738 |
} |
| 739 |
|
| 740 |
#[test] |
| 741 |
fn test_week_iterator_limit() { |
| 742 |
assert_eq!(NaiveDate::from_ymd_opt(MAX_YEAR, 12, 12).unwrap().iter_weeks().take(4).count(), 2); |
| 743 |
assert_eq!( |
| 744 |
NaiveDate::from_ymd_opt(MIN_YEAR, 1, 15).unwrap().iter_weeks().rev().take(4).count(), |
| 745 |
2 |
| 746 |
); |
| 747 |
} |
| 748 |
|
| 749 |
#[test] |
| 750 |
fn test_weeks_from() { |
| 751 |
// tests per: https://github.com/chronotope/chrono/issues/961 |
| 752 |
// these internally use `weeks_from` via the parsing infrastructure |
| 753 |
assert_eq!( |
| 754 |
NaiveDate::parse_from_str("2020-01-0", "%Y-%W-%w").ok(), |
| 755 |
NaiveDate::from_ymd_opt(2020, 1, 12), |
| 756 |
); |
| 757 |
assert_eq!( |
| 758 |
NaiveDate::parse_from_str("2019-01-0", "%Y-%W-%w").ok(), |
| 759 |
NaiveDate::from_ymd_opt(2019, 1, 13), |
| 760 |
); |
| 761 |
|
| 762 |
// direct tests |
| 763 |
for (y, starts_on) in &[ |
| 764 |
(2019, Weekday::Tue), |
| 765 |
(2020, Weekday::Wed), |
| 766 |
(2021, Weekday::Fri), |
| 767 |
(2022, Weekday::Sat), |
| 768 |
(2023, Weekday::Sun), |
| 769 |
(2024, Weekday::Mon), |
| 770 |
(2025, Weekday::Wed), |
| 771 |
(2026, Weekday::Thu), |
| 772 |
] { |
| 773 |
for day in &[ |
| 774 |
Weekday::Mon, |
| 775 |
Weekday::Tue, |
| 776 |
Weekday::Wed, |
| 777 |
Weekday::Thu, |
| 778 |
Weekday::Fri, |
| 779 |
Weekday::Sat, |
| 780 |
Weekday::Sun, |
| 781 |
] { |
| 782 |
assert_eq!( |
| 783 |
NaiveDate::from_ymd_opt(*y, 1, 1).map(|d| d.weeks_from(*day)), |
| 784 |
Some(if day == starts_on { 1 } else { 0 }) |
| 785 |
); |
| 786 |
|
| 787 |
// last day must always be in week 52 or 53 |
| 788 |
assert!( |
| 789 |
[52, 53].contains(&NaiveDate::from_ymd_opt(*y, 12, 31).unwrap().weeks_from(*day)), |
| 790 |
); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
let base = NaiveDate::from_ymd_opt(2019, 1, 1).unwrap(); |
| 795 |
|
| 796 |
// 400 years covers all year types |
| 797 |
for day in &[ |
| 798 |
Weekday::Mon, |
| 799 |
Weekday::Tue, |
| 800 |
Weekday::Wed, |
| 801 |
Weekday::Thu, |
| 802 |
Weekday::Fri, |
| 803 |
Weekday::Sat, |
| 804 |
Weekday::Sun, |
| 805 |
] { |
| 806 |
// must always be below 54 |
| 807 |
for dplus in 1..(400 * 366) { |
| 808 |
assert!((base + Days::new(dplus)).weeks_from(*day) < 54) |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
#[test] |
| 814 |
fn test_with_0_overflow() { |
| 815 |
let dt = NaiveDate::from_ymd_opt(2023, 4, 18).unwrap(); |
| 816 |
assert!(dt.with_month0(4294967295).is_none()); |
| 817 |
assert!(dt.with_day0(4294967295).is_none()); |
| 818 |
assert!(dt.with_ordinal0(4294967295).is_none()); |
| 819 |
} |
| 820 |
|
| 821 |
#[test] |
| 822 |
fn test_leap_year() { |
| 823 |
for year in 0..=MAX_YEAR { |
| 824 |
let date = NaiveDate::from_ymd_opt(year, 1, 1).unwrap(); |
| 825 |
let is_leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); |
| 826 |
assert_eq!(date.leap_year(), is_leap); |
| 827 |
assert_eq!(date.leap_year(), date.with_ordinal(366).is_some()); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
#[test] |
| 832 |
fn test_date_yearflags() { |
| 833 |
for (year, year_flags, _) in YEAR_FLAGS { |
| 834 |
assert_eq!(NaiveDate::from_yo_opt(year, 1).unwrap().year_flags(), year_flags); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
#[test] |
| 839 |
fn test_weekday_with_yearflags() { |
| 840 |
for (year, year_flags, first_weekday) in YEAR_FLAGS { |
| 841 |
let first_day_of_year = NaiveDate::from_yo_opt(year, 1).unwrap(); |
| 842 |
dbg!(year); |
| 843 |
assert_eq!(first_day_of_year.year_flags(), year_flags); |
| 844 |
assert_eq!(first_day_of_year.weekday(), first_weekday); |
| 845 |
|
| 846 |
let mut prev = first_day_of_year.weekday(); |
| 847 |
for ordinal in 2u32..=year_flags.ndays() { |
| 848 |
let date = NaiveDate::from_yo_opt(year, ordinal).unwrap(); |
| 849 |
let expected = prev.succ(); |
| 850 |
assert_eq!(date.weekday(), expected); |
| 851 |
prev = expected; |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
#[test] |
| 857 |
fn test_isoweekdate_with_yearflags() { |
| 858 |
for (year, year_flags, _) in YEAR_FLAGS { |
| 859 |
// January 4 should be in the first week |
| 860 |
let jan4 = NaiveDate::from_ymd_opt(year, 1, 4).unwrap(); |
| 861 |
let iso_week = jan4.iso_week(); |
| 862 |
assert_eq!(jan4.year_flags(), year_flags); |
| 863 |
assert_eq!(iso_week.week(), 1); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
#[test] |
| 868 |
fn test_date_to_mdf_to_date() { |
| 869 |
for (year, year_flags, _) in YEAR_FLAGS { |
| 870 |
for ordinal in 1..=year_flags.ndays() { |
| 871 |
let date = NaiveDate::from_yo_opt(year, ordinal).unwrap(); |
| 872 |
assert_eq!(date, NaiveDate::from_mdf(date.year(), date.mdf()).unwrap()); |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
// Used for testing some methods with all combinations of `YearFlags`. |
| 878 |
// (year, flags, first weekday of year) |
| 879 |
const YEAR_FLAGS: [(i32, YearFlags, Weekday); 14] = [ |
| 880 |
(2006, A, Weekday::Sun), |
| 881 |
(2005, B, Weekday::Sat), |
| 882 |
(2010, C, Weekday::Fri), |
| 883 |
(2009, D, Weekday::Thu), |
| 884 |
(2003, E, Weekday::Wed), |
| 885 |
(2002, F, Weekday::Tue), |
| 886 |
(2001, G, Weekday::Mon), |
| 887 |
(2012, AG, Weekday::Sun), |
| 888 |
(2000, BA, Weekday::Sat), |
| 889 |
(2016, CB, Weekday::Fri), |
| 890 |
(2004, DC, Weekday::Thu), |
| 891 |
(2020, ED, Weekday::Wed), |
| 892 |
(2008, FE, Weekday::Tue), |
| 893 |
(2024, GF, Weekday::Mon), |
| 894 |
]; |
| 895 |
|
| 896 |
#[test] |
| 897 |
#[cfg(feature = "rkyv-validation")] |
| 898 |
fn test_rkyv_validation() { |
| 899 |
let date_min = NaiveDate::MIN; |
| 900 |
let bytes = rkyv::to_bytes::<_, 4>(&date_min).unwrap(); |
| 901 |
assert_eq!(rkyv::from_bytes::<NaiveDate>(&bytes).unwrap(), date_min); |
| 902 |
|
| 903 |
let date_max = NaiveDate::MAX; |
| 904 |
let bytes = rkyv::to_bytes::<_, 4>(&date_max).unwrap(); |
| 905 |
assert_eq!(rkyv::from_bytes::<NaiveDate>(&bytes).unwrap(), date_max); |
| 906 |
} |
| 907 |
|
| 908 |
// MAX_YEAR-12-31 minus 0000-01-01 |
| 909 |
// = (MAX_YEAR-12-31 minus 0000-12-31) + (0000-12-31 - 0000-01-01) |
| 910 |
// = MAX_YEAR * 365 + (# of leap years from 0001 to MAX_YEAR) + 365 |
| 911 |
// = (MAX_YEAR + 1) * 365 + (# of leap years from 0001 to MAX_YEAR) |
| 912 |
const MAX_DAYS_FROM_YEAR_0: i32 = |
| 913 |
(MAX_YEAR + 1) * 365 + MAX_YEAR / 4 - MAX_YEAR / 100 + MAX_YEAR / 400; |
| 914 |
|
| 915 |
// MIN_YEAR-01-01 minus 0000-01-01 |
| 916 |
// = MIN_YEAR * 365 + (# of leap years from MIN_YEAR to 0000) |
| 917 |
const MIN_DAYS_FROM_YEAR_0: i32 = MIN_YEAR * 365 + MIN_YEAR / 4 - MIN_YEAR / 100 + MIN_YEAR / 400; |
| 918 |
|
| 919 |
// only used for testing, but duplicated in naive::datetime |
| 920 |
const MAX_BITS: usize = 44; |