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
// 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`][const:MINUTE], [`HOUR`][const:HOUR], [`DAY`][const:DAY], [`WEEK`][const:WEEK].
//! - Constants as milliseconds: [`millis::SECOND`][const:millis/SECOND], [`millis::MINUTE`][const:millis/MINUTE],
//!   [`millis::HOUR`][const:millis/HOUR], [`millis::DAY`][const:millis/DAY], [`millis::WEEK`][const:millis/WEEK].
//! - Other modules: [`micros`][mod:micros], [`nanos`][mod:nanos], [`decis`][mod:decis], [`centis`][mod:centis], [`picos`][mod:picos],
//!   [`femtos`][mod:femtos], [`attos`][mod:attos], [`zeptos`][mod:zeptos], [`yoctos`][mod:yoctos].
//! - And some helper functions for formatting time...
//!
//! Some components require `unix` feature. If you can't access them, you should enable that feature. For example:
//!
//! ```toml
//! [dependencies]
//! dia-time = { version='...', features=['unix'] }
//! ```
//!
//! ## Notes
//!
//! The crate uses `#![no_std]` by default. Documentation is built with all features, which include `std`. If you see some components from
//! [`std`][crate:std] crate, you have to use that feature.
//!
//! ## References
//!
//! - Wikipedia: <https://en.wikipedia.org/wiki/Second#SI_multiples>
//!
//! [Semantic Versioning 2.0.0]: https://semver.org/spec/v2.0.0.html
//! [crate:std]: https://doc.rust-lang.org/std/
//! [const:MINUTE]: constant.MINUTE.html
//! [const:HOUR]: constant.HOUR.html
//! [const:DAY]: constant.DAY.html
//! [const:WEEK]: constant.WEEK.html
//! [mod:micros]: micros/index.html
//! [mod:nanos]: nanos/index.html
//! [mod:decis]: decis/index.html
//! [mod:centis]: centis/index.html
//! [mod:picos]: picos/index.html
//! [mod:femtos]: femtos/index.html
//! [mod:attos]: attos/index.html
//! [mod:zeptos]: zeptos/index.html
//! [mod:yoctos]: yoctos/index.html
//! [const:millis/SECOND]: millis/constant.SECOND.html
//! [const:millis/MINUTE]: millis/constant.MINUTE.html
//! [const:millis/HOUR]: millis/constant.HOUR.html
//! [const:millis/DAY]: millis/constant.DAY.html
//! [const:millis/WEEK]: millis/constant.WEEK.html

#![warn(missing_docs)]
#![no_std]

// ╔═════════════════╗
// ║   IDENTIFIERS   ║
// ╚═════════════════╝

macro_rules! crate_code_name    { () => { "dia-time" }}
macro_rules! crate_version      { () => { "4.2.2" }}

/// # 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) = (2020, 4, 6);

/// # Tag, which can be used for logging...
pub const TAG: &str = concat!(crate_code_name!(), "::88754f65::", crate_version!());

// ╔════════════════════╗
// ║   IMPLEMENTATION   ║
// ╚════════════════════╝

#[macro_use]
extern crate alloc;

#[cfg(any(feature="std", feature="unix"))]
extern crate std;

use alloc::string::String;

#[test]
fn test_crate_version() {
    assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
}

use core::time::Duration;

/// # Wrapper for format!(), which prefixes your message with: ::TAG, module_path!(), line!()
macro_rules! __ { ($($arg: tt)+) => {
    format!("[{tag}][{module_path}-{line}] {format}", tag=crate::TAG, module_path=module_path!(), line=line!(), format=format!($($arg)+))
};}

mod error;
mod time;

pub use self::{
    error::*,
    time::*,
};

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;

/// # Result type used in this crate
pub type Result<T> = core::result::Result<T, Error>;

/// # 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;

/// # 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_hms_ms(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)),
///     "02:53:58.000"
/// );
/// ```
pub fn format_hms_ms(duration: &Duration) -> String {
    let (_, h, m, s) = duration_to_dhms(duration);
    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`
///
/// ```
/// use std::time::Duration;
/// use dia_time::{self, DAY, HOUR, MINUTE};
///
/// assert_eq!(
///     dia_time::format_day_hms_ms(&Duration::from_secs(DAY * 3 + HOUR * 2 + MINUTE * 53 + 58)),
///     "3d, 02:53:58.000"
/// );
/// ```
pub fn format_day_hms_ms(duration: &Duration) -> String {
    format!("{d}d, {t}", d=duration.as_secs() / DAY, t=format_hms_ms(duration))
}

/// # If the duration is within a day, forwards to [`::format_hms_ms()`]; otherwise, forwards to [`::format_day_hms_ms()`].
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use dia_time::{self, DAY, HOUR, MINUTE};
///
/// assert_eq!(
///     dia_time::smart_format_day_hms_ms(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)),
///     "02:53:58.000"
/// );
/// assert_eq!(dia_time::smart_format_day_hms_ms(&Duration::from_secs(DAY * 9 + HOUR * 2 + MINUTE * 53 + 58)), "9d, 02:53:58.000");
/// ```
///
/// [`::format_hms_ms()`]: fn.format_hms_ms.html
/// [`::format_day_hms_ms()`]: fn.format_day_hms_ms.html
pub fn smart_format_day_hms_ms(duration: &Duration) -> String {
    match duration.as_secs() / DAY {
        0 => format_hms_ms(duration),
        _ => format_day_hms_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);
    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_day_hms(&Duration::from_secs(DAY * 9 + HOUR * 2 + MINUTE * 53 + 58)),
///     "9d, 02:53:58"
/// );
/// ```
pub fn format_day_hms(duration: &Duration) -> String {
    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_day_hms()`].
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use dia_time::{self, DAY, HOUR, MINUTE};
///
/// assert_eq!(
///     dia_time::smart_format_day_hms(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)),
///     "02:53:58"
/// );
/// assert_eq!(
///     dia_time::smart_format_day_hms(&Duration::from_secs(DAY * 9 + HOUR * 2 + MINUTE * 53 + 58)),
///     "9d, 02:53:58"
/// );
/// ```
///
/// [`::format_hms()`]: fn.format_hms.html
/// [`::format_day_hms()`]: fn.format_day_hms.html
pub fn smart_format_day_hms(duration: &Duration) -> String {
    match duration.as_secs() / DAY {
        0 => format_hms(duration),
        _ => format_day_hms(duration),
    }
}