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
// License: see LICENSE file at root directory of `master` branch

//! # A small time kit
//!
//! # Project
//!
//! - Repository: <https://bitbucket.org/haibison/dia-time>
//! - License: [Free Public License 1.0.0](https://opensource.org/licenses/FPL-1.0.0)
//! - _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

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

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

/// # Crate name
pub const CRATE_NAME: &'static str = "Dia-time";

/// # Crate code name
pub const CRATE_CODE_NAME: &'static str = crate_code_name!();

/// # Crate version
pub const CRATE_VERSION: &'static str = crate_version!();

/// # Crate release date (year/month/day)
pub const CRATE_RELEASE_DATE: (u16, u8, u8) = (2018, 10, 7);

/// # Unique universally identifier of this crate. Its CRC-32 is `f2b3f462`.
pub const UUID: &'static str = "e580a119-6587-4910-9fa4-34947188c486";

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

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

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

use std::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;

/// # 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() as f64;
    let (minutes, seconds) = {
        let minutes = (seconds / 60.0).floor();
        (minutes, seconds - minutes * 60.0)
    };
    let (hours, minutes) = {
        let hours = (minutes / 60.0).floor();
        (hours, minutes - hours * 60.0)
    };
    let (days, hours) = {
        let days = (hours / 24.0).floor();
        (days, hours - days * 24.0)
    };

    (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);
    format!("{:02}:{:02}:{:02}.{:03}", h, m, s, 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_dhmss(&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_dhmss(duration: &Duration) -> String {
    format!("{}d, {}", duration.as_secs() / DAY, format_hmss(duration))
}

/// # If the duration is within a day, forwards to [`::format_hmss()`]; otherwise, forwards to [`::format_dhmss()`].
///
/// ## 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_dhmss(&Duration::from_secs(HOUR * 2 + MINUTE * 53 + 58)),
///     "02:53:58.000"
/// );
/// assert_eq!(dia_time::smart_format_dhmss(&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_dhmss()`]: fn.format_dhmss.html
pub fn smart_format_dhmss(duration: &Duration) -> String {
    match duration.as_secs() / DAY {
        0 => format_hmss(duration),
        _ => format_dhmss(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!("{:02}:{:02}:{:02}", h, m, 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 {
    format!("{}d, {}", duration.as_secs() / DAY, 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),
    }
}