diff options
Diffstat (limited to 'third_party/rust/time-core/src')
-rw-r--r-- | third_party/rust/time-core/src/lib.rs | 52 | ||||
-rw-r--r-- | third_party/rust/time-core/src/util.rs | 52 |
2 files changed, 104 insertions, 0 deletions
diff --git a/third_party/rust/time-core/src/lib.rs b/third_party/rust/time-core/src/lib.rs new file mode 100644 index 0000000000..d962578b59 --- /dev/null +++ b/third_party/rust/time-core/src/lib.rs @@ -0,0 +1,52 @@ +//! Core items for `time`. +//! +//! This crate is an implementation detail of `time` and should not be relied upon directly. + +#![no_std] +#![deny( + anonymous_parameters, + clippy::all, + clippy::alloc_instead_of_core, + clippy::explicit_auto_deref, + clippy::obfuscated_if_else, + clippy::std_instead_of_core, + clippy::undocumented_unsafe_blocks, + const_err, + illegal_floating_point_literal_pattern, + late_bound_lifetime_arguments, + path_statements, + patterns_in_fns_without_body, + rust_2018_idioms, + trivial_casts, + trivial_numeric_casts, + unreachable_pub, + unsafe_op_in_unsafe_fn, + unused_extern_crates, + rustdoc::broken_intra_doc_links, + rustdoc::private_intra_doc_links +)] +#![warn( + clippy::dbg_macro, + clippy::decimal_literal_representation, + clippy::get_unwrap, + clippy::missing_docs_in_private_items, + clippy::nursery, + clippy::print_stdout, + clippy::todo, + clippy::unimplemented, + clippy::unnested_or_patterns, + clippy::unwrap_in_result, + clippy::unwrap_used, + clippy::use_debug, + deprecated_in_future, + missing_copy_implementations, + missing_debug_implementations, + unused_qualifications, + variant_size_differences +)] +#![allow(clippy::redundant_pub_crate)] +#![doc(html_favicon_url = "https://avatars0.githubusercontent.com/u/55999857")] +#![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/55999857")] +#![doc(test(attr(deny(warnings))))] + +pub mod util; diff --git a/third_party/rust/time-core/src/util.rs b/third_party/rust/time-core/src/util.rs new file mode 100644 index 0000000000..26ced0638c --- /dev/null +++ b/third_party/rust/time-core/src/util.rs @@ -0,0 +1,52 @@ +//! Utility functions. + +/// Returns if the provided year is a leap year in the proleptic Gregorian calendar. Uses +/// [astronomical year numbering](https://en.wikipedia.org/wiki/Astronomical_year_numbering). +/// +/// ```rust +/// # use time::util::is_leap_year; +/// assert!(!is_leap_year(1900)); +/// assert!(is_leap_year(2000)); +/// assert!(is_leap_year(2004)); +/// assert!(!is_leap_year(2005)); +/// assert!(!is_leap_year(2100)); +/// ``` +pub const fn is_leap_year(year: i32) -> bool { + year % 4 == 0 && (year % 25 != 0 || year % 16 == 0) +} + +/// Get the number of calendar days in a given year. +/// +/// The returned value will always be either 365 or 366. +/// +/// ```rust +/// # use time::util::days_in_year; +/// assert_eq!(days_in_year(1900), 365); +/// assert_eq!(days_in_year(2000), 366); +/// assert_eq!(days_in_year(2004), 366); +/// assert_eq!(days_in_year(2005), 365); +/// assert_eq!(days_in_year(2100), 365); +/// ``` +pub const fn days_in_year(year: i32) -> u16 { + if is_leap_year(year) { 366 } else { 365 } +} + +/// Get the number of weeks in the ISO year. +/// +/// The returned value will always be either 52 or 53. +/// +/// ```rust +/// # use time::util::weeks_in_year; +/// assert_eq!(weeks_in_year(2019), 52); +/// assert_eq!(weeks_in_year(2020), 53); +/// ``` +pub const fn weeks_in_year(year: i32) -> u8 { + match year.rem_euclid(400) { + 4 | 9 | 15 | 20 | 26 | 32 | 37 | 43 | 48 | 54 | 60 | 65 | 71 | 76 | 82 | 88 | 93 | 99 + | 105 | 111 | 116 | 122 | 128 | 133 | 139 | 144 | 150 | 156 | 161 | 167 | 172 | 178 + | 184 | 189 | 195 | 201 | 207 | 212 | 218 | 224 | 229 | 235 | 240 | 246 | 252 | 257 + | 263 | 268 | 274 | 280 | 285 | 291 | 296 | 303 | 308 | 314 | 320 | 325 | 331 | 336 + | 342 | 348 | 353 | 359 | 364 | 370 | 376 | 381 | 387 | 392 | 398 => 53, + _ => 52, + } +} |