| 1 |
//! Code related to `DateTime` in zip files |
| 2 |
|
| 3 |
use crate::result::DateTimeRangeError; |
| 4 |
use core::fmt; |
| 5 |
|
| 6 |
/// Representation of a moment in time. |
| 7 |
/// |
| 8 |
/// Zip files use an old format from DOS to store timestamps, |
| 9 |
/// with its own set of peculiarities. |
| 10 |
/// For example, it has a resolution of 2 seconds! |
| 11 |
/// |
| 12 |
/// A [`DateTime`] can be stored directly in a zipfile with |
| 13 |
/// [`FileOptions::last_modified_time`](crate::read::ZipFile::last_modified), or |
| 14 |
/// read from one with [`ZipFile::last_modified`](crate::read::ZipFile::last_modified). |
| 15 |
/// |
| 16 |
/// # Warning |
| 17 |
/// |
| 18 |
/// Because there is no timezone associated with the [`DateTime`], they should ideally only |
| 19 |
/// be used for user-facing descriptions. |
| 20 |
/// |
| 21 |
/// Modern zip files store more precise timestamps; see [`crate::extra_fields::ExtendedTimestamp`] |
| 22 |
/// for details. |
| 23 |
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 24 |
pub struct DateTime { |
| 25 |
datepart: u16, |
| 26 |
timepart: u16, |
| 27 |
} |
| 28 |
|
| 29 |
impl fmt::Debug for DateTime { |
| 30 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 |
if *self == Self::default() { |
| 32 |
return f.write_str("DateTime::default()"); |
| 33 |
} |
| 34 |
f.write_fmt(format_args!( |
| 35 |
"DateTime::from_date_and_time({}, {}, {}, {}, {}, {})?", |
| 36 |
self.year(), |
| 37 |
self.month(), |
| 38 |
self.day(), |
| 39 |
self.hour(), |
| 40 |
self.minute(), |
| 41 |
self.second() |
| 42 |
)) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(feature = "_arbitrary")] |
| 47 |
impl arbitrary::Arbitrary<'_> for DateTime { |
| 48 |
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> { |
| 49 |
// DOS time format stores seconds divided by 2 in a 5-bit field (0..=29), |
| 50 |
// so the maximum representable second value is 58. |
| 51 |
const MAX_DOS_SECONDS: u16 = 58; |
| 52 |
|
| 53 |
let year: u16 = u.int_in_range(1980..=2107)?; |
| 54 |
let month: u16 = u.int_in_range(1..=12)?; |
| 55 |
let day: u16 = u.int_in_range(1..=31)?; |
| 56 |
let datepart = day | (month << 5) | ((year - 1980) << 9); |
| 57 |
let hour: u16 = u.int_in_range(0..=23)?; |
| 58 |
let minute: u16 = u.int_in_range(0..=59)?; |
| 59 |
let second: u16 = u.int_in_range(0..=MAX_DOS_SECONDS)?; |
| 60 |
let timepart = (second >> 1) | (minute << 5) | (hour << 11); |
| 61 |
Ok(DateTime { datepart, timepart }) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
#[cfg(feature = "chrono")] |
| 66 |
impl TryFrom<chrono::NaiveDateTime> for DateTime { |
| 67 |
type Error = DateTimeRangeError; |
| 68 |
|
| 69 |
fn try_from(value: chrono::NaiveDateTime) -> Result<Self, Self::Error> { |
| 70 |
use chrono::{Datelike, Timelike}; |
| 71 |
|
| 72 |
DateTime::from_date_and_time( |
| 73 |
value.year().try_into()?, |
| 74 |
value.month().try_into()?, |
| 75 |
value.day().try_into()?, |
| 76 |
value.hour().try_into()?, |
| 77 |
value.minute().try_into()?, |
| 78 |
value.second().try_into()?, |
| 79 |
) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(feature = "chrono")] |
| 84 |
impl TryFrom<DateTime> for chrono::NaiveDateTime { |
| 85 |
type Error = DateTimeRangeError; |
| 86 |
|
| 87 |
fn try_from(value: DateTime) -> Result<Self, Self::Error> { |
| 88 |
let date = chrono::NaiveDate::from_ymd_opt( |
| 89 |
value.year().into(), |
| 90 |
value.month().into(), |
| 91 |
value.day().into(), |
| 92 |
) |
| 93 |
.ok_or(DateTimeRangeError)?; |
| 94 |
let time = chrono::NaiveTime::from_hms_opt( |
| 95 |
value.hour().into(), |
| 96 |
value.minute().into(), |
| 97 |
value.second().into(), |
| 98 |
) |
| 99 |
.ok_or(DateTimeRangeError)?; |
| 100 |
Ok(chrono::NaiveDateTime::new(date, time)) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[cfg(feature = "jiff-02")] |
| 105 |
impl TryFrom<jiff::civil::DateTime> for DateTime { |
| 106 |
type Error = DateTimeRangeError; |
| 107 |
|
| 108 |
fn try_from(value: jiff::civil::DateTime) -> Result<Self, Self::Error> { |
| 109 |
Self::from_date_and_time( |
| 110 |
value.year().try_into()?, |
| 111 |
value.month() as u8, |
| 112 |
value.day() as u8, |
| 113 |
value.hour() as u8, |
| 114 |
value.minute() as u8, |
| 115 |
value.second() as u8, |
| 116 |
) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(feature = "jiff-02")] |
| 121 |
impl TryFrom<DateTime> for jiff::civil::DateTime { |
| 122 |
type Error = jiff::Error; |
| 123 |
|
| 124 |
fn try_from(value: DateTime) -> Result<Self, Self::Error> { |
| 125 |
Self::new( |
| 126 |
value.year() as i16, |
| 127 |
value.month() as i8, |
| 128 |
value.day() as i8, |
| 129 |
value.hour() as i8, |
| 130 |
value.minute() as i8, |
| 131 |
value.second() as i8, |
| 132 |
0, |
| 133 |
) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl TryFrom<(u16, u16)> for DateTime { |
| 138 |
type Error = DateTimeRangeError; |
| 139 |
|
| 140 |
#[inline] |
| 141 |
fn try_from(values: (u16, u16)) -> Result<Self, Self::Error> { |
| 142 |
Self::try_from_msdos(values.0, values.1) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl From<DateTime> for (u16, u16) { |
| 147 |
#[inline] |
| 148 |
fn from(dt: DateTime) -> Self { |
| 149 |
(dt.datepart(), dt.timepart()) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl Default for DateTime { |
| 154 |
/// Constructs an 'default' datetime of 1980-01-01 00:00:00 |
| 155 |
fn default() -> DateTime { |
| 156 |
DateTime::DEFAULT |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl fmt::Display for DateTime { |
| 161 |
#[inline] |
| 162 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 163 |
write!( |
| 164 |
f, |
| 165 |
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}", |
| 166 |
self.year(), |
| 167 |
self.month(), |
| 168 |
self.day(), |
| 169 |
self.hour(), |
| 170 |
self.minute(), |
| 171 |
self.second() |
| 172 |
) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
impl DateTime { |
| 177 |
/// Constructs a default datetime of 1980-01-01 00:00:00. |
| 178 |
pub const DEFAULT: Self = DateTime { |
| 179 |
datepart: 0b0000_0000_0010_0001, |
| 180 |
timepart: 0, |
| 181 |
}; |
| 182 |
|
| 183 |
/// Returns the current time if possible, otherwise the default of 1980-01-01. |
| 184 |
#[cfg(feature = "time")] |
| 185 |
#[must_use] |
| 186 |
pub fn default_for_write() -> Self { |
| 187 |
let now = time::OffsetDateTime::now_utc(); |
| 188 |
time::PrimitiveDateTime::new(now.date(), now.time()) |
| 189 |
.try_into() |
| 190 |
.unwrap_or_else(|_| DateTime::default()) |
| 191 |
} |
| 192 |
|
| 193 |
/// Returns the current time if possible, otherwise the default of 1980-01-01. |
| 194 |
#[cfg(not(feature = "time"))] |
| 195 |
#[must_use] |
| 196 |
pub fn default_for_write() -> Self { |
| 197 |
DateTime::default() |
| 198 |
} |
| 199 |
|
| 200 |
#[cfg(feature = "chrono")] |
| 201 |
/// Generate a `SystemTime` from a `DateTime`. |
| 202 |
pub(crate) fn datetime_to_systemtime(&self) -> Option<std::time::SystemTime> { |
| 203 |
if let Some(chrono_datetime) = self.generate_chrono_datetime() { |
| 204 |
let time = chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset( |
| 205 |
chrono_datetime, |
| 206 |
chrono::Utc, |
| 207 |
); |
| 208 |
return Some(time.into()); |
| 209 |
} |
| 210 |
None |
| 211 |
} |
| 212 |
|
| 213 |
#[cfg(feature = "chrono")] |
| 214 |
/// Generate a `NaiveDateTime` from a `DateTime`. |
| 215 |
fn generate_chrono_datetime(&self) -> Option<chrono::NaiveDateTime> { |
| 216 |
if let Some(chrono_date) = chrono::NaiveDate::from_ymd_opt( |
| 217 |
self.year().into(), |
| 218 |
self.month().into(), |
| 219 |
self.day().into(), |
| 220 |
) && let Some(chrono_datetime) = chrono_date.and_hms_opt( |
| 221 |
self.hour().into(), |
| 222 |
self.minute().into(), |
| 223 |
self.second().into(), |
| 224 |
) { |
| 225 |
return Some(chrono_datetime); |
| 226 |
} |
| 227 |
None |
| 228 |
} |
| 229 |
|
| 230 |
/// Converts an msdos (u16, u16) pair to a `DateTime` object |
| 231 |
/// |
| 232 |
/// # Safety |
| 233 |
/// The caller must ensure the date and time are valid. |
| 234 |
#[must_use] |
| 235 |
pub const unsafe fn from_msdos_unchecked(datepart: u16, timepart: u16) -> DateTime { |
| 236 |
DateTime { datepart, timepart } |
| 237 |
} |
| 238 |
|
| 239 |
pub(crate) fn is_leap_year(year: u16) -> bool { |
| 240 |
year.is_multiple_of(4) && (!year.is_multiple_of(100) || year.is_multiple_of(400)) |
| 241 |
} |
| 242 |
|
| 243 |
/// Converts an msdos (u16, u16) pair to a `DateTime` object if it represents a valid date and |
| 244 |
/// time. |
| 245 |
pub fn try_from_msdos(datepart: u16, timepart: u16) -> Result<DateTime, DateTimeRangeError> { |
| 246 |
let seconds = (timepart & 0b0000_0000_0001_1111) << 1; |
| 247 |
let minutes = (timepart & 0b0000_0111_1110_0000) >> 5; |
| 248 |
let hours = (timepart & 0b1111_1000_0000_0000) >> 11; |
| 249 |
let days = datepart & 0b0000_0000_0001_1111; |
| 250 |
let months = (datepart & 0b0000_0001_1110_0000) >> 5; |
| 251 |
let years = (datepart & 0b1111_1110_0000_0000) >> 9; |
| 252 |
Self::from_date_and_time( |
| 253 |
years.checked_add(1980).ok_or(DateTimeRangeError)?, |
| 254 |
months.try_into()?, |
| 255 |
days.try_into()?, |
| 256 |
hours.try_into()?, |
| 257 |
minutes.try_into()?, |
| 258 |
seconds.try_into()?, |
| 259 |
) |
| 260 |
} |
| 261 |
|
| 262 |
/// Constructs a `DateTime` from a specific date and time |
| 263 |
/// |
| 264 |
/// The bounds are: |
| 265 |
/// * year: [1980, 2107] |
| 266 |
/// * month: [1, 12] |
| 267 |
/// * day: [1, 28..=31] |
| 268 |
/// * hour: [0, 23] |
| 269 |
/// * minute: [0, 59] |
| 270 |
/// * second: [0, 60] (rounded down to even and to [0, 58] due to ZIP format limitation) |
| 271 |
pub fn from_date_and_time( |
| 272 |
year: u16, |
| 273 |
month: u8, |
| 274 |
day: u8, |
| 275 |
hour: u8, |
| 276 |
minute: u8, |
| 277 |
second: u8, |
| 278 |
) -> Result<DateTime, DateTimeRangeError> { |
| 279 |
if (1980..=2107).contains(&year) |
| 280 |
&& (1..=12).contains(&month) |
| 281 |
&& (1..=31).contains(&day) |
| 282 |
&& hour <= 23 |
| 283 |
&& minute <= 59 |
| 284 |
&& second <= 60 |
| 285 |
{ |
| 286 |
// DOS/ZIP timestamp stores seconds/2 in 5 bits and cannot represent 59 or 60 seconds (incl. leap seconds) |
| 287 |
let second = second.min(58); |
| 288 |
let max_day = match month { |
| 289 |
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, |
| 290 |
4 | 6 | 9 | 11 => 30, |
| 291 |
2 if Self::is_leap_year(year) => 29, |
| 292 |
2 => 28, |
| 293 |
_ => unreachable!(), |
| 294 |
}; |
| 295 |
if day > max_day { |
| 296 |
return Err(DateTimeRangeError); |
| 297 |
} |
| 298 |
let datepart = u16::from(day) | (u16::from(month) << 5) | ((year - 1980) << 9); |
| 299 |
let timepart = |
| 300 |
(u16::from(second) >> 1) | (u16::from(minute) << 5) | (u16::from(hour) << 11); |
| 301 |
Ok(DateTime { datepart, timepart }) |
| 302 |
} else { |
| 303 |
Err(DateTimeRangeError) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/// Indicates whether this date and time can be written to a zip archive. |
| 308 |
#[must_use] |
| 309 |
pub fn is_valid(&self) -> bool { |
| 310 |
Self::try_from_msdos(self.datepart, self.timepart).is_ok() |
| 311 |
} |
| 312 |
|
| 313 |
/// Gets the time portion of this datetime in the msdos representation |
| 314 |
#[must_use] |
| 315 |
pub const fn timepart(&self) -> u16 { |
| 316 |
self.timepart |
| 317 |
} |
| 318 |
|
| 319 |
/// Gets the date portion of this datetime in the msdos representation |
| 320 |
#[must_use] |
| 321 |
pub const fn datepart(&self) -> u16 { |
| 322 |
self.datepart |
| 323 |
} |
| 324 |
|
| 325 |
/// Get the year. There is no epoch, i.e. 2018 will be returned as 2018. |
| 326 |
#[must_use] |
| 327 |
pub const fn year(&self) -> u16 { |
| 328 |
(self.datepart >> 9) + 1980 |
| 329 |
} |
| 330 |
|
| 331 |
/// Get the month, where 1 = january and 12 = december |
| 332 |
/// |
| 333 |
/// # Warning |
| 334 |
/// |
| 335 |
/// When read from a zip file, this may not be a reasonable value |
| 336 |
#[must_use] |
| 337 |
pub const fn month(&self) -> u8 { |
| 338 |
((self.datepart & 0b0000_0001_1110_0000) >> 5) as u8 |
| 339 |
} |
| 340 |
|
| 341 |
/// Get the day |
| 342 |
/// |
| 343 |
/// # Warning |
| 344 |
/// |
| 345 |
/// When read from a zip file, this may not be a reasonable value |
| 346 |
#[must_use] |
| 347 |
pub const fn day(&self) -> u8 { |
| 348 |
(self.datepart & 0b0000_0000_0001_1111) as u8 |
| 349 |
} |
| 350 |
|
| 351 |
/// Get the hour |
| 352 |
/// |
| 353 |
/// # Warning |
| 354 |
/// |
| 355 |
/// When read from a zip file, this may not be a reasonable value |
| 356 |
#[must_use] |
| 357 |
pub const fn hour(&self) -> u8 { |
| 358 |
(self.timepart >> 11) as u8 |
| 359 |
} |
| 360 |
|
| 361 |
/// Get the minute |
| 362 |
/// |
| 363 |
/// # Warning |
| 364 |
/// |
| 365 |
/// When read from a zip file, this may not be a reasonable value |
| 366 |
#[must_use] |
| 367 |
pub const fn minute(&self) -> u8 { |
| 368 |
((self.timepart & 0b0000_0111_1110_0000) >> 5) as u8 |
| 369 |
} |
| 370 |
|
| 371 |
/// Get the second |
| 372 |
/// |
| 373 |
/// # Warning |
| 374 |
/// |
| 375 |
/// When read from a zip file, this may not be a reasonable value |
| 376 |
#[must_use] |
| 377 |
pub const fn second(&self) -> u8 { |
| 378 |
((self.timepart & 0b0000_0000_0001_1111) << 1) as u8 |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
#[cfg(all(feature = "time", feature = "deprecated-time"))] |
| 383 |
impl TryFrom<time::OffsetDateTime> for DateTime { |
| 384 |
type Error = DateTimeRangeError; |
| 385 |
|
| 386 |
fn try_from(dt: time::OffsetDateTime) -> Result<Self, Self::Error> { |
| 387 |
Self::try_from(time::PrimitiveDateTime::new(dt.date(), dt.time())) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
#[cfg(feature = "time")] |
| 392 |
impl TryFrom<time::PrimitiveDateTime> for DateTime { |
| 393 |
type Error = DateTimeRangeError; |
| 394 |
|
| 395 |
fn try_from(dt: time::PrimitiveDateTime) -> Result<Self, Self::Error> { |
| 396 |
Self::from_date_and_time( |
| 397 |
dt.year().try_into()?, |
| 398 |
dt.month().into(), |
| 399 |
dt.day(), |
| 400 |
dt.hour(), |
| 401 |
dt.minute(), |
| 402 |
dt.second(), |
| 403 |
) |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#[cfg(all(feature = "time", feature = "deprecated-time"))] |
| 408 |
impl TryFrom<DateTime> for time::OffsetDateTime { |
| 409 |
type Error = time::error::ComponentRange; |
| 410 |
|
| 411 |
fn try_from(dt: DateTime) -> Result<Self, Self::Error> { |
| 412 |
time::PrimitiveDateTime::try_from(dt).map(time::PrimitiveDateTime::assume_utc) |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
#[cfg(feature = "time")] |
| 417 |
impl TryFrom<DateTime> for time::PrimitiveDateTime { |
| 418 |
type Error = time::error::ComponentRange; |
| 419 |
|
| 420 |
fn try_from(dt: DateTime) -> Result<Self, Self::Error> { |
| 421 |
use time::{Date, Month, Time}; |
| 422 |
let date = |
| 423 |
Date::from_calendar_date(i32::from(dt.year()), Month::try_from(dt.month())?, dt.day())?; |
| 424 |
let time = Time::from_hms(dt.hour(), dt.minute(), dt.second())?; |
| 425 |
Ok(time::PrimitiveDateTime::new(date, time)) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
#[cfg(test)] |
| 430 |
mod tests { |
| 431 |
#[test] |
| 432 |
#[allow(clippy::unusual_byte_groupings)] |
| 433 |
fn datetime_default() { |
| 434 |
use super::DateTime; |
| 435 |
let dt = DateTime::default(); |
| 436 |
assert_eq!(dt.timepart(), 0); |
| 437 |
assert_eq!(dt.datepart(), 0b0000000_0001_00001); |
| 438 |
} |
| 439 |
|
| 440 |
#[test] |
| 441 |
#[allow(clippy::unusual_byte_groupings)] |
| 442 |
fn datetime_max() { |
| 443 |
use super::DateTime; |
| 444 |
let dt = DateTime::from_date_and_time(2107, 12, 31, 23, 59, 58).unwrap(); |
| 445 |
assert_eq!(dt.timepart(), 0b10111_111011_11101); |
| 446 |
assert_eq!(dt.datepart(), 0b1111111_1100_11111); |
| 447 |
} |
| 448 |
|
| 449 |
#[test] |
| 450 |
fn datetime_equality() { |
| 451 |
use super::DateTime; |
| 452 |
|
| 453 |
let dt = DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap(); |
| 454 |
assert_eq!( |
| 455 |
dt, |
| 456 |
DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap() |
| 457 |
); |
| 458 |
assert_ne!(dt, DateTime::default()); |
| 459 |
} |
| 460 |
|
| 461 |
#[test] |
| 462 |
fn datetime_order() { |
| 463 |
use std::cmp::Ordering; |
| 464 |
|
| 465 |
use super::DateTime; |
| 466 |
|
| 467 |
let dt = DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap(); |
| 468 |
assert_eq!( |
| 469 |
dt.cmp(&DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap()), |
| 470 |
Ordering::Equal |
| 471 |
); |
| 472 |
// year |
| 473 |
assert!(dt < DateTime::from_date_and_time(2019, 11, 17, 10, 38, 30).unwrap()); |
| 474 |
assert!(dt > DateTime::from_date_and_time(2017, 11, 17, 10, 38, 30).unwrap()); |
| 475 |
// month |
| 476 |
assert!(dt < DateTime::from_date_and_time(2018, 12, 17, 10, 38, 30).unwrap()); |
| 477 |
assert!(dt > DateTime::from_date_and_time(2018, 10, 17, 10, 38, 30).unwrap()); |
| 478 |
// day |
| 479 |
assert!(dt < DateTime::from_date_and_time(2018, 11, 18, 10, 38, 30).unwrap()); |
| 480 |
assert!(dt > DateTime::from_date_and_time(2018, 11, 16, 10, 38, 30).unwrap()); |
| 481 |
// hour |
| 482 |
assert!(dt < DateTime::from_date_and_time(2018, 11, 17, 11, 38, 30).unwrap()); |
| 483 |
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 9, 38, 30).unwrap()); |
| 484 |
// minute |
| 485 |
assert!(dt < DateTime::from_date_and_time(2018, 11, 17, 10, 39, 30).unwrap()); |
| 486 |
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 10, 37, 30).unwrap()); |
| 487 |
// second |
| 488 |
assert!(dt < DateTime::from_date_and_time(2018, 11, 17, 10, 38, 32).unwrap()); |
| 489 |
assert_eq!( |
| 490 |
dt.cmp(&DateTime::from_date_and_time(2018, 11, 17, 10, 38, 31).unwrap()), |
| 491 |
Ordering::Equal |
| 492 |
); |
| 493 |
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 10, 38, 29).unwrap()); |
| 494 |
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 10, 38, 28).unwrap()); |
| 495 |
} |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn datetime_display() { |
| 499 |
use super::DateTime; |
| 500 |
|
| 501 |
assert_eq!(format!("{}", DateTime::default()), "1980-01-01 00:00:00"); |
| 502 |
assert_eq!( |
| 503 |
format!( |
| 504 |
"{}", |
| 505 |
DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap() |
| 506 |
), |
| 507 |
"2018-11-17 10:38:30" |
| 508 |
); |
| 509 |
assert_eq!( |
| 510 |
format!( |
| 511 |
"{}", |
| 512 |
DateTime::from_date_and_time(2107, 12, 31, 23, 59, 58).unwrap() |
| 513 |
), |
| 514 |
"2107-12-31 23:59:58" |
| 515 |
); |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn datetime_bounds() { |
| 520 |
use super::DateTime; |
| 521 |
|
| 522 |
assert!(DateTime::from_date_and_time(2000, 1, 1, 23, 59, 60).is_ok()); |
| 523 |
assert!(DateTime::from_date_and_time(2000, 1, 1, 24, 0, 0).is_err()); |
| 524 |
assert!(DateTime::from_date_and_time(2000, 1, 1, 0, 60, 0).is_err()); |
| 525 |
assert!(DateTime::from_date_and_time(2000, 1, 1, 0, 0, 61).is_err()); |
| 526 |
|
| 527 |
assert!(DateTime::from_date_and_time(2107, 12, 31, 0, 0, 0).is_ok()); |
| 528 |
assert!(DateTime::from_date_and_time(1980, 1, 1, 0, 0, 0).is_ok()); |
| 529 |
assert!(DateTime::from_date_and_time(1979, 1, 1, 0, 0, 0).is_err()); |
| 530 |
assert!(DateTime::from_date_and_time(1980, 0, 1, 0, 0, 0).is_err()); |
| 531 |
assert!(DateTime::from_date_and_time(1980, 1, 0, 0, 0, 0).is_err()); |
| 532 |
assert!(DateTime::from_date_and_time(2108, 12, 31, 0, 0, 0).is_err()); |
| 533 |
assert!(DateTime::from_date_and_time(2107, 13, 31, 0, 0, 0).is_err()); |
| 534 |
assert!(DateTime::from_date_and_time(2107, 12, 32, 0, 0, 0).is_err()); |
| 535 |
|
| 536 |
assert!(DateTime::from_date_and_time(2018, 1, 31, 0, 0, 0).is_ok()); |
| 537 |
assert!(DateTime::from_date_and_time(2018, 2, 28, 0, 0, 0).is_ok()); |
| 538 |
assert!(DateTime::from_date_and_time(2018, 2, 29, 0, 0, 0).is_err()); |
| 539 |
assert!(DateTime::from_date_and_time(2018, 3, 31, 0, 0, 0).is_ok()); |
| 540 |
assert!(DateTime::from_date_and_time(2018, 4, 30, 0, 0, 0).is_ok()); |
| 541 |
assert!(DateTime::from_date_and_time(2018, 4, 31, 0, 0, 0).is_err()); |
| 542 |
assert!(DateTime::from_date_and_time(2018, 5, 31, 0, 0, 0).is_ok()); |
| 543 |
assert!(DateTime::from_date_and_time(2018, 6, 30, 0, 0, 0).is_ok()); |
| 544 |
assert!(DateTime::from_date_and_time(2018, 6, 31, 0, 0, 0).is_err()); |
| 545 |
assert!(DateTime::from_date_and_time(2018, 7, 31, 0, 0, 0).is_ok()); |
| 546 |
assert!(DateTime::from_date_and_time(2018, 8, 31, 0, 0, 0).is_ok()); |
| 547 |
assert!(DateTime::from_date_and_time(2018, 9, 30, 0, 0, 0).is_ok()); |
| 548 |
assert!(DateTime::from_date_and_time(2018, 9, 31, 0, 0, 0).is_err()); |
| 549 |
assert!(DateTime::from_date_and_time(2018, 10, 31, 0, 0, 0).is_ok()); |
| 550 |
assert!(DateTime::from_date_and_time(2018, 11, 30, 0, 0, 0).is_ok()); |
| 551 |
assert!(DateTime::from_date_and_time(2018, 11, 31, 0, 0, 0).is_err()); |
| 552 |
assert!(DateTime::from_date_and_time(2018, 12, 31, 0, 0, 0).is_ok()); |
| 553 |
|
| 554 |
// leap year: divisible by 4 |
| 555 |
assert!(DateTime::from_date_and_time(2024, 2, 29, 0, 0, 0).is_ok()); |
| 556 |
// leap year: divisible by 100 and by 400 |
| 557 |
assert!(DateTime::from_date_and_time(2000, 2, 29, 0, 0, 0).is_ok()); |
| 558 |
// common year: divisible by 100 but not by 400 |
| 559 |
assert!(DateTime::from_date_and_time(2100, 2, 29, 0, 0, 0).is_err()); |
| 560 |
} |
| 561 |
|
| 562 |
#[cfg(all(feature = "time", feature = "deprecated-time"))] |
| 563 |
#[test] |
| 564 |
fn datetime_try_from_offset_datetime() { |
| 565 |
use time::macros::datetime; |
| 566 |
|
| 567 |
use super::DateTime; |
| 568 |
|
| 569 |
// 2018-11-17 10:38:30 |
| 570 |
let dt = DateTime::try_from(datetime!(2018-11-17 10:38:30 UTC)).unwrap(); |
| 571 |
assert_eq!(dt.year(), 2018); |
| 572 |
assert_eq!(dt.month(), 11); |
| 573 |
assert_eq!(dt.day(), 17); |
| 574 |
assert_eq!(dt.hour(), 10); |
| 575 |
assert_eq!(dt.minute(), 38); |
| 576 |
assert_eq!(dt.second(), 30); |
| 577 |
} |
| 578 |
|
| 579 |
#[cfg(feature = "time")] |
| 580 |
#[test] |
| 581 |
fn datetime_try_from_primitive_datetime() { |
| 582 |
use time::macros::datetime; |
| 583 |
|
| 584 |
use super::DateTime; |
| 585 |
|
| 586 |
// 2018-11-17 10:38:30 |
| 587 |
let dt = DateTime::try_from(datetime!(2018-11-17 10:38:30)).unwrap(); |
| 588 |
assert_eq!(dt.year(), 2018); |
| 589 |
assert_eq!(dt.month(), 11); |
| 590 |
assert_eq!(dt.day(), 17); |
| 591 |
assert_eq!(dt.hour(), 10); |
| 592 |
assert_eq!(dt.minute(), 38); |
| 593 |
assert_eq!(dt.second(), 30); |
| 594 |
} |
| 595 |
|
| 596 |
#[cfg(feature = "time")] |
| 597 |
#[test] |
| 598 |
fn datetime_try_from_bounds() { |
| 599 |
use super::DateTime; |
| 600 |
use time::macros::datetime; |
| 601 |
|
| 602 |
// 1979-12-31 23:59:59 |
| 603 |
assert!(DateTime::try_from(datetime!(1979-12-31 23:59:59)).is_err()); |
| 604 |
|
| 605 |
// 1980-01-01 00:00:00 |
| 606 |
assert!(DateTime::try_from(datetime!(1980-01-01 00:00:00)).is_ok()); |
| 607 |
|
| 608 |
// 2107-12-31 23:59:59 |
| 609 |
assert!(DateTime::try_from(datetime!(2107-12-31 23:59:59)).is_ok()); |
| 610 |
|
| 611 |
// 2108-01-01 00:00:00 |
| 612 |
assert!(DateTime::try_from(datetime!(2108-01-01 00:00:00)).is_err()); |
| 613 |
} |
| 614 |
|
| 615 |
#[cfg(all(feature = "time", feature = "deprecated-time"))] |
| 616 |
#[test] |
| 617 |
fn offset_datetime_try_from_datetime() { |
| 618 |
use time::OffsetDateTime; |
| 619 |
use time::macros::datetime; |
| 620 |
|
| 621 |
use super::DateTime; |
| 622 |
|
| 623 |
// 2018-11-17 10:38:30 UTC |
| 624 |
let dt = |
| 625 |
OffsetDateTime::try_from(DateTime::try_from_msdos(0x4D71, 0x54CF).unwrap()).unwrap(); |
| 626 |
assert_eq!(dt, datetime!(2018-11-17 10:38:30 UTC)); |
| 627 |
} |
| 628 |
|
| 629 |
#[cfg(feature = "time")] |
| 630 |
#[test] |
| 631 |
fn primitive_datetime_try_from_datetime() { |
| 632 |
use time::PrimitiveDateTime; |
| 633 |
use time::macros::datetime; |
| 634 |
|
| 635 |
use super::DateTime; |
| 636 |
|
| 637 |
// 2018-11-17 10:38:30 |
| 638 |
let dt = |
| 639 |
PrimitiveDateTime::try_from(DateTime::try_from_msdos(0x4D71, 0x54CF).unwrap()).unwrap(); |
| 640 |
assert_eq!(dt, datetime!(2018-11-17 10:38:30)); |
| 641 |
} |
| 642 |
|
| 643 |
#[cfg(all(feature = "time", feature = "deprecated-time"))] |
| 644 |
#[test] |
| 645 |
fn offset_datetime_try_from_bounds() { |
| 646 |
use super::DateTime; |
| 647 |
use time::OffsetDateTime; |
| 648 |
|
| 649 |
// 1980-00-00 00:00:00 |
| 650 |
assert!( |
| 651 |
OffsetDateTime::try_from(unsafe { DateTime::from_msdos_unchecked(0x0000, 0x0000) }) |
| 652 |
.is_err() |
| 653 |
); |
| 654 |
|
| 655 |
// 2107-15-31 31:63:62 |
| 656 |
assert!( |
| 657 |
OffsetDateTime::try_from(unsafe { DateTime::from_msdos_unchecked(0xFFFF, 0xFFFF) }) |
| 658 |
.is_err() |
| 659 |
); |
| 660 |
} |
| 661 |
|
| 662 |
#[cfg(feature = "time")] |
| 663 |
#[test] |
| 664 |
fn primitive_datetime_try_from_bounds() { |
| 665 |
use super::DateTime; |
| 666 |
use time::PrimitiveDateTime; |
| 667 |
|
| 668 |
// 1980-00-00 00:00:00 |
| 669 |
assert!( |
| 670 |
PrimitiveDateTime::try_from(unsafe { DateTime::from_msdos_unchecked(0x0000, 0x0000) }) |
| 671 |
.is_err() |
| 672 |
); |
| 673 |
|
| 674 |
// 2107-15-31 31:63:62 |
| 675 |
assert!( |
| 676 |
PrimitiveDateTime::try_from(unsafe { DateTime::from_msdos_unchecked(0xFFFF, 0xFFFF) }) |
| 677 |
.is_err() |
| 678 |
); |
| 679 |
} |
| 680 |
|
| 681 |
#[cfg(feature = "jiff-02")] |
| 682 |
#[test] |
| 683 |
fn datetime_try_from_civil_datetime() { |
| 684 |
use jiff::civil; |
| 685 |
|
| 686 |
use super::DateTime; |
| 687 |
|
| 688 |
// 2018-11-17 10:38:30 |
| 689 |
let dt = DateTime::try_from(civil::datetime(2018, 11, 17, 10, 38, 30, 0)).unwrap(); |
| 690 |
assert_eq!(dt.year(), 2018); |
| 691 |
assert_eq!(dt.month(), 11); |
| 692 |
assert_eq!(dt.day(), 17); |
| 693 |
assert_eq!(dt.hour(), 10); |
| 694 |
assert_eq!(dt.minute(), 38); |
| 695 |
assert_eq!(dt.second(), 30); |
| 696 |
} |
| 697 |
|
| 698 |
#[cfg(feature = "jiff-02")] |
| 699 |
#[test] |
| 700 |
fn datetime_try_from_civil_datetime_bounds() { |
| 701 |
use jiff::civil; |
| 702 |
|
| 703 |
use super::DateTime; |
| 704 |
|
| 705 |
// 1979-12-31 23:59:59 |
| 706 |
assert!(DateTime::try_from(civil::datetime(1979, 12, 31, 23, 59, 59, 0)).is_err()); |
| 707 |
|
| 708 |
// 1980-01-01 00:00:00 |
| 709 |
assert!(DateTime::try_from(civil::datetime(1980, 1, 1, 0, 0, 0, 0)).is_ok()); |
| 710 |
|
| 711 |
// 2107-12-31 23:59:59 |
| 712 |
assert!(DateTime::try_from(civil::datetime(2107, 12, 31, 23, 59, 59, 0)).is_ok()); |
| 713 |
|
| 714 |
// 2108-01-01 00:00:00 |
| 715 |
assert!(DateTime::try_from(civil::datetime(2108, 1, 1, 0, 0, 0, 0)).is_err()); |
| 716 |
} |
| 717 |
|
| 718 |
#[cfg(feature = "jiff-02")] |
| 719 |
#[test] |
| 720 |
fn civil_datetime_try_from_datetime() { |
| 721 |
use jiff::civil; |
| 722 |
|
| 723 |
use super::DateTime; |
| 724 |
|
| 725 |
// 2018-11-17 10:38:30 UTC |
| 726 |
let dt = |
| 727 |
civil::DateTime::try_from(DateTime::try_from_msdos(0x4D71, 0x54CF).unwrap()).unwrap(); |
| 728 |
assert_eq!(dt, civil::datetime(2018, 11, 17, 10, 38, 30, 0)); |
| 729 |
} |
| 730 |
|
| 731 |
#[cfg(feature = "jiff-02")] |
| 732 |
#[test] |
| 733 |
fn civil_datetime_try_from_datetime_bounds() { |
| 734 |
use jiff::civil; |
| 735 |
|
| 736 |
use super::DateTime; |
| 737 |
|
| 738 |
// 1980-00-00 00:00:00 |
| 739 |
assert!( |
| 740 |
civil::DateTime::try_from(unsafe { DateTime::from_msdos_unchecked(0x0000, 0x0000) }) |
| 741 |
.is_err() |
| 742 |
); |
| 743 |
|
| 744 |
// 2107-15-31 31:63:62 |
| 745 |
assert!( |
| 746 |
civil::DateTime::try_from(unsafe { DateTime::from_msdos_unchecked(0xFFFF, 0xFFFF) }) |
| 747 |
.is_err() |
| 748 |
); |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn time_conversion() { |
| 753 |
use super::DateTime; |
| 754 |
let dt = DateTime::try_from_msdos(0x4D71, 0x54CF).unwrap(); |
| 755 |
assert_eq!(dt.year(), 2018); |
| 756 |
assert_eq!(dt.month(), 11); |
| 757 |
assert_eq!(dt.day(), 17); |
| 758 |
assert_eq!(dt.hour(), 10); |
| 759 |
assert_eq!(dt.minute(), 38); |
| 760 |
assert_eq!(dt.second(), 30); |
| 761 |
|
| 762 |
let dt = DateTime::try_from((0x4D71, 0x54CF)).unwrap(); |
| 763 |
assert_eq!(dt.year(), 2018); |
| 764 |
assert_eq!(dt.month(), 11); |
| 765 |
assert_eq!(dt.day(), 17); |
| 766 |
assert_eq!(dt.hour(), 10); |
| 767 |
assert_eq!(dt.minute(), 38); |
| 768 |
assert_eq!(dt.second(), 30); |
| 769 |
|
| 770 |
assert_eq!(<(u16, u16)>::from(dt), (0x4D71, 0x54CF)); |
| 771 |
} |
| 772 |
|
| 773 |
#[test] |
| 774 |
fn time_out_of_bounds() { |
| 775 |
use super::DateTime; |
| 776 |
let dt = unsafe { DateTime::from_msdos_unchecked(0xFFFF, 0xFFFF) }; |
| 777 |
assert_eq!(dt.year(), 2107); |
| 778 |
assert_eq!(dt.month(), 15); |
| 779 |
assert_eq!(dt.day(), 31); |
| 780 |
assert_eq!(dt.hour(), 31); |
| 781 |
assert_eq!(dt.minute(), 63); |
| 782 |
assert_eq!(dt.second(), 62); |
| 783 |
|
| 784 |
let dt = unsafe { DateTime::from_msdos_unchecked(0x0000, 0x0000) }; |
| 785 |
assert_eq!(dt.year(), 1980); |
| 786 |
assert_eq!(dt.month(), 0); |
| 787 |
assert_eq!(dt.day(), 0); |
| 788 |
assert_eq!(dt.hour(), 0); |
| 789 |
assert_eq!(dt.minute(), 0); |
| 790 |
assert_eq!(dt.second(), 0); |
| 791 |
} |
| 792 |
|
| 793 |
#[cfg(feature = "time")] |
| 794 |
#[test] |
| 795 |
fn time_at_january() { |
| 796 |
use super::DateTime; |
| 797 |
use time::{OffsetDateTime, PrimitiveDateTime}; |
| 798 |
|
| 799 |
// 2020-01-01 00:00:00 |
| 800 |
let clock = OffsetDateTime::from_unix_timestamp(1_577_836_800).unwrap(); |
| 801 |
|
| 802 |
assert!(DateTime::try_from(PrimitiveDateTime::new(clock.date(), clock.time())).is_ok()); |
| 803 |
} |
| 804 |
|
| 805 |
#[test] |
| 806 |
fn test_is_leap_year() { |
| 807 |
use crate::DateTime; |
| 808 |
assert!(DateTime::is_leap_year(2000)); |
| 809 |
assert!(!DateTime::is_leap_year(2026)); |
| 810 |
assert!(!DateTime::is_leap_year(2027)); |
| 811 |
assert!(DateTime::is_leap_year(2028)); |
| 812 |
assert!(DateTime::is_leap_year(1600)); |
| 813 |
assert!(DateTime::is_leap_year(2400)); |
| 814 |
assert!(!DateTime::is_leap_year(1900)); |
| 815 |
assert!(!DateTime::is_leap_year(2100)); |
| 816 |
} |
| 817 |
} |