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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
// License: see LICENSE file at root directory of `master` branch //! # A small time kit //! //! ## Project //! //! - Repository: <https://bitbucket.org/haibison/dia-time> //! - License: Nice License 1.0.0 _(see LICENSE file at root directory of `master` branch)_ //! - _This project follows [Semantic Versioning 2.0.0]_ //! //! ## Features //! //! - Constants as seconds: [`MINUTE`][::MINUTE], [`HOUR`][::HOUR], [`DAY`][::DAY], [`WEEK`][::WEEK]. //! - Constants as milliseconds: [`millis::SECOND`], [`millis::MINUTE`], [`millis::HOUR`], [`millis::DAY`], [`millis::WEEK`]. //! - Constants as microseconds: [`micros::SECOND`], [`micros::MINUTE`], [`micros::HOUR`], [`micros::DAY`], [`micros::WEEK`]. //! - Constants as nanoseconds: [`nanos::SECOND`], [`nanos::MINUTE`], [`nanos::HOUR`], [`nanos::DAY`], [`nanos::WEEK`]. //! - Other modules: [`decis`][::decis], [`centis`][::centis], [`picos`][::picos], [`femtos`][::femtos], [`attos`][::attos], //! [`zeptos`][::zeptos], [`yoctos`][::yoctos]. //! - And some helper functions for formatting time... //! //! ## References //! //! - Wikipedia: <https://en.wikipedia.org/wiki/Second#SI_multiples> //! //! [Semantic Versioning 2.0.0]: https://semver.org/spec/v2.0.0.html //! [::MINUTE]: constant.MINUTE.html //! [::HOUR]: constant.HOUR.html //! [::DAY]: constant.DAY.html //! [::WEEK]: constant.WEEK.html //! [::decis]: decis/index.html //! [::centis]: centis/index.html //! [::picos]: picos/index.html //! [::femtos]: femtos/index.html //! [::attos]: attos/index.html //! [::zeptos]: zeptos/index.html //! [::yoctos]: yoctos/index.html //! [`millis::SECOND`]: millis/constant.SECOND.html //! [`millis::MINUTE`]: millis/constant.MINUTE.html //! [`millis::HOUR`]: millis/constant.HOUR.html //! [`millis::DAY`]: millis/constant.DAY.html //! [`millis::WEEK`]: millis/constant.WEEK.html //! [`micros::SECOND`]: micros/constant.SECOND.html //! [`micros::MINUTE`]: micros/constant.MINUTE.html //! [`micros::HOUR`]: micros/constant.HOUR.html //! [`micros::DAY`]: micros/constant.DAY.html //! [`micros::WEEK`]: micros/constant.WEEK.html //! [`nanos::SECOND`]: nanos/constant.SECOND.html //! [`nanos::MINUTE`]: nanos/constant.MINUTE.html //! [`nanos::HOUR`]: nanos/constant.HOUR.html //! [`nanos::DAY`]: nanos/constant.DAY.html //! [`nanos::WEEK`]: nanos/constant.WEEK.html #![warn(missing_docs)] #![no_std] // ╔═════════════════╗ // ║ IDENTIFIERS ║ // ╚═════════════════╝ macro_rules! crate_code_name { () => { "dia-time" }} macro_rules! crate_version { () => { "3.0.0" }} /// # Crate name pub const NAME: &str = "Dia-time"; /// # Crate code name pub const CODE_NAME: &str = crate_code_name!(); /// # ID of this crate pub const ID: &str = concat!( "88754f65-f8ad90df-c6f5d48f-3b7ec1e3-43dd5631-337efed4-cda6b228-5fb13c39-", "c88959ab-59b4eb2d-b6ea09d6-c3fbc084-c3b4f246-ce05808f-91bdb0dc-a06f9b1e", ); /// # Crate version pub const VERSION: &str = crate_version!(); /// # Crate release date (year/month/day) pub const RELEASE_DATE: (u16, u8, u8) = (2019, 8, 19); /// # Tag, which can be used for logging... pub const TAG: &str = concat!(crate_code_name!(), "::88754f65::", crate_version!()); // ╔════════════════════╗ // ║ IMPLEMENTATION ║ // ╚════════════════════╝ extern crate alloc; use alloc::string::String; #[test] fn test_crate_version() { assert_eq!(VERSION, env!("CARGO_PKG_VERSION")); } use core::time::Duration; pub mod decis; pub mod centis; pub mod millis; pub mod micros; pub mod nanos; pub mod picos; pub mod femtos; pub mod attos; pub mod zeptos; pub mod yoctos; pub mod symbols; pub mod version_info; /// # 1 second pub const SECOND: u64 = 1; /// # 1 minute in seconds pub const MINUTE: u64 = 60; /// # 1 hour in seconds pub const HOUR: u64 = 3_600; /// # 1 day in seconds pub const DAY: u64 = 86_400; /// # 1 week in seconds pub const WEEK: u64 = 604_800; /// # 1 decasecond pub const DECASECOND: u64 = 10; /// # 1 hectosecond pub const HECTOSECOND: u64 = 100; /// # 1 kilosecond pub const KILOSECOND: u64 = 1_000; /// # 1 megasecond pub const MEGASECOND: u64 = 1_000_000; /// # 1 gigasecond pub const GIGASECOND: u64 = 1_000_000_000; /// # 1 terasecond pub const TERASECOND: u64 = 1_000_000_000_000; /// # 1 petasecond pub const PETASECOND: u64 = 1_000_000_000_000_000; /// # 1 exasecond pub const EXASECOND: u64 = 1_000_000_000_000_000_000; /// # 1 zettasecond pub const ZETTASECOND: u128 = 1_000_000_000_000_000_000_000; /// # 1 yottasecond pub const YOTTASECOND: u128 = 1_000_000_000_000_000_000_000_000; /// # Gets _local_ time /// /// This ***unsafe-unstable*** macro requires [`libc`][crate:libc] crate. It returns `std::io::Result`. /// /// ## Examples /// /// ``` /// let (year, month, day, hour, min, sec): (i32, i32, i32, i32, i32, i32) = unsafe { /// dia_time::local_time!().unwrap() /// }; /// assert!(month >= 1 && month <= 12); /// assert!(day >= 1 && day <= 31); /// assert!(hour >= 0 && hour <= 23); /// assert!(min >= 0 && min <= 59); /// assert!(sec >= 0 && sec <= 59); /// ``` /// /// [crate:libc]: https://crates.io/crates/libc #[macro_export] macro_rules! local_time { () => {{ use std::{ io::{Error, ErrorKind}, ptr, }; let localtime = libc::localtime(&libc::time(ptr::null_mut())); match localtime.is_null() { true => Err(Error::new(ErrorKind::Other, "libc::localtime() returned null")), false => Ok(( (*localtime).tm_year.saturating_add(1900), (*localtime).tm_mon.saturating_add(1), (*localtime).tm_mday, (*localtime).tm_hour, (*localtime).tm_min, (*localtime).tm_sec, )), } }}} /// # Converts duration to days, hours, minutes, seconds. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use dia_time::{self, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::duration_to_dhms(&Duration::from_secs(HOUR + MINUTE)), /// (0, 1, 1, 0) /// ); /// ``` pub fn duration_to_dhms(duration: &Duration) -> (u64, u64, u64, u64) { let seconds = duration.as_secs(); let (minutes, seconds) = { let minutes = seconds / 60; (minutes, seconds - minutes * 60) }; let (hours, minutes) = { let hours = minutes / 60; (hours, minutes - hours * 60) }; let (days, hours) = { let days = hours / 24; (days, hours - days * 24) }; (days as u64, hours as u64, minutes as u64, seconds as u64) } /// # Examples: `02:53:58.018` /// /// ``` /// use std::time::Duration; /// use dia_time::{self, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::format_hmss(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)), /// "02:53:58.000" /// ); /// ``` pub fn format_hmss(duration: &Duration) -> String { let (_, h, m, s) = duration_to_dhms(duration); alloc::format!("{h:02}:{m:02}:{s:02}.{ms:03}", h=h, m=m, s=s, ms=duration.subsec_millis()) } /// # Examples: `3d, 02:53:58.018` /// /// ## Notes /// /// - [`Duration.subsec_millis()`] is a nightly-only experimental API (issue [#46507]). /// /// ``` /// use std::time::Duration; /// use dia_time::{self, DAY, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::format_dhms_ms(&Duration::from_secs(DAY * 3 + HOUR * 2 + MINUTE * 53 + 58)), /// "3d, 02:53:58.000" /// ); /// ``` /// /// [`Duration.subsec_millis()`]: https://doc.rust-lang.org/std/time/struct.Duration.html#method.subsec_millis /// [#46507]: https://github.com/rust-lang/rust/issues/46507 pub fn format_dhms_ms(duration: &Duration) -> String { alloc::format!("{d}d, {t}", d=duration.as_secs() / DAY, t=format_hmss(duration)) } /// # If the duration is within a day, forwards to [`::format_hmss()`]; otherwise, forwards to [`::format_dhms_ms()`]. /// /// ## Notes /// /// - [`Duration.subsec_millis()`] is a nightly-only experimental API (issue [#46507]). /// /// # Examples /// /// ``` /// use std::time::Duration; /// use dia_time::{self, DAY, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::smart_format_dhms_ms(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)), /// "02:53:58.000" /// ); /// assert_eq!(dia_time::smart_format_dhms_ms(&Duration::from_secs(DAY * 9 + HOUR * 2 + MINUTE * 53 + 58)), "9d, 02:53:58.000"); /// ``` /// /// [`Duration.subsec_millis()`]: https://doc.rust-lang.org/std/time/struct.Duration.html#method.subsec_millis /// [#46507]: https://github.com/rust-lang/rust/issues/46507 /// [`::format_hmss()`]: fn.format_hmss.html /// [`::format_dhms_ms()`]: fn.format_dhms_ms.html pub fn smart_format_dhms_ms(duration: &Duration) -> String { match duration.as_secs() / DAY { 0 => format_hmss(duration), _ => format_dhms_ms(duration), } } /// # Examples: `02:53:58` /// /// ``` /// use std::time::Duration; /// use dia_time::{self, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::format_hms(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)), /// "02:53:58" /// ); /// ``` pub fn format_hms(duration: &Duration) -> String { let (_, h, m, s) = duration_to_dhms(duration); alloc::format!("{h:02}:{m:02}:{s:02}", h=h, m=m, s=s) } /// # Examples: `9d, 02:53:58` /// /// ``` /// use std::time::Duration; /// use dia_time::{self, DAY, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::format_dhms(&Duration::from_secs(DAY * 9 + HOUR * 2 + MINUTE * 53 + 58)), /// "9d, 02:53:58" /// ); /// ``` pub fn format_dhms(duration: &Duration) -> String { alloc::format!("{d}d, {t}", d=duration.as_secs() / DAY, t=format_hms(duration)) } /// # If the duration is within a day, forwards to [`::format_hms()`]; otherwise, forwards to [`::format_dhms()`]. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use dia_time::{self, DAY, HOUR, MINUTE}; /// /// assert_eq!( /// dia_time::smart_format_dhms(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)), /// "02:53:58" /// ); /// assert_eq!( /// dia_time::smart_format_dhms(&Duration::from_secs(DAY * 9 + HOUR * 2 + MINUTE * 53 + 58)), /// "9d, 02:53:58" /// ); /// ``` /// /// [`::format_hms()`]: fn.format_hms.html /// [`::format_dhms()`]: fn.format_dhms.html pub fn smart_format_dhms(duration: &Duration) -> String { match duration.as_secs() / DAY { 0 => format_hms(duration), _ => format_dhms(duration), } }