1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/*! # UTC2K UTC2K is a fast and lean date/time library that only cares about UTC happenings in _this century_ (between `2000-01-01 00:00:00` and `2099-12-31 23:59:59`). With that very significant constraint in mind, UTC2K can: * Convert to/from Unix timestamps (`u32`); * Convert to/from date strings of the `YYYY-MM-DD hh:mm:ss` variety; That's it! Compared to more robust libraries like [`chrono`](https://crates.io/crates/chrono) and [`time`](https://crates.io/crates/time), UTC2K can be magnitudes faster, particularly in regards to string parsing and printing. This library is still a work in progress and there is certainly room to improve performance further. If you have any suggestions for improvement, feel free to open [an issue](https://github.com/Blobfolio/utc2k/issues) on Github! ## Examples The main date object is [`Utc2k`]. ``` use utc2k::Utc2k; use std::convert::TryFrom; let date = Utc2k::default(); // 2000-01-01 00:00:00 let date = Utc2k::now(); // The current time. let date = Utc2k::from(4_102_444_799_u32); // 2099-12-31 23:59:59 let date = Utc2k::new(2010, 10, 31, 15, 30, 0); // 2010-10-31 15:30:00 // String parsing is fallible, but flexible. So long as the numbers we // need are in the right place, it will be fine. (At least, it won't error // out; if the date string is trying to communicate a time zone, that won't // be listened to.) assert!(Utc2k::try_from("2099-12-31 23:59:59").is_ok()); // Fine. assert!(Utc2k::try_from("2099-12-31T23:59:59.0000Z").is_ok()); // Also fine. assert!(Utc2k::try_from("January 1, 2010 @ Eleven O'Clock").is_err()); // Nope! ``` There is also [`FmtUtc2k`], used for string representation. ``` use utc2k::{FmtUtc2k, Utc2k}; use std::convert::TryFrom; // You can generate it from an existing Utc2k with either: assert_eq!(Utc2k::default().formatted(), FmtUtc2k::from(Utc2k::default())); // You could also skip `Utc2k` and seed directly from a timestamp or date/time // string. let fmt = FmtUtc2k::from(4_102_444_799_u32); let fmt = FmtUtc2k::try_from("2099-12-31 23:59:59").unwrap(); ``` Once you have a [`FmtUtc2k`], you can turn it into a string with: ``` use utc2k::{FmtUtc2k, Utc2k}; use std::borrow::Borrow; let fmt = FmtUtc2k::from(4_102_444_799_u32); let s: &str = &fmt; let s: &str = fmt.as_ref(); let s: &str = fmt.as_str(); let s: &str = fmt.borrow(); ``` ## Installation Add `utc2k` to your `dependencies` in `Cargo.toml`, like: ```ignore,text [dependencies] utc2k = "0.1.*" ``` */ #![warn(clippy::filetype_is_file)] #![warn(clippy::integer_division)] #![warn(clippy::needless_borrow)] #![warn(clippy::nursery)] #![warn(clippy::pedantic)] #![warn(clippy::perf)] #![warn(clippy::suboptimal_flops)] #![warn(clippy::unneeded_field_pattern)] #![warn(macro_use_extern_crate)] #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] #![warn(missing_docs)] #![warn(non_ascii_idents)] #![warn(trivial_casts)] #![warn(trivial_numeric_casts)] #![warn(unreachable_pub)] #![warn(unused_crate_dependencies)] #![warn(unused_extern_crates)] #![warn(unused_import_braces)] #![allow(clippy::module_name_repetitions)] mod date; mod error; pub use date::{ FmtUtc2k, Utc2k, }; pub use error::Utc2kError; /// # Seconds per Minute. pub const MINUTE_IN_SECONDS: u32 = 60; /// # Seconds per Hour. pub const HOUR_IN_SECONDS: u32 = 3600; /// # Seconds per Day. pub const DAY_IN_SECONDS: u32 = 86_400; /// # Seconds per Week. pub const WEEK_IN_SECONDS: u32 = 604_800; /// # Seconds per (Normal) Year. pub const YEAR_IN_SECONDS: u32 = 31_536_000; /// # Seconds Between 1970 and 2000. pub const OLD_SECONDS: u32 = 946_684_800; #[allow(clippy::cast_possible_truncation)] // It fits. #[must_use] /// # Now (Current Unixtime). /// /// This returns the current unix timestamp as a `u32`. /// /// Rather than panic on out-of-range values — in the event the system clock is /// broken or an archaeologist is running this in the distant future — the /// timetsamp will be saturated to [`Utc2k::MIN_UNIXTIME`] or /// [`Utc2k::MAX_UNIXTIME`]. pub fn unixtime() -> u32 { use std::time::SystemTime; SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map_or( Utc2k::MIN_UNIXTIME, |n| { let n = n.as_secs(); if n < 4_102_444_799 { n as u32 } else { Utc2k::MAX_UNIXTIME } } ) }