utcnow 0.2.0

Get the current unixtime in a no-std context
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2022 René Kijewski <crates.io@k6i.de>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#![allow(unknown_lints)]
#![allow(clippy::doc_markdown)]

//! # utcnow — Get the current unixtime in a no-std context
//!
//! [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Kijewski/utcnow/CI?logo=github)](https://github.com/Kijewski/tzdb/actions/workflows/ci.yml)
//! [![Crates.io](https://img.shields.io/crates/v/utcnow?logo=rust)](https://crates.io/crates/utcnow)
//! ![Minimum supported Rust version](https://img.shields.io/badge/rustc-1.48-important?logo=rust "Minimum Supported Rust Version")
//! [![License](https://img.shields.io/crates/l/utcnow?color=informational&logo=apache)](https://github.com/Kijewski/utcnow/blob/v0.0.0-pre1/LICENSE.md)
//!
//! This library solves one question, and one question only: *What's the time?*
//!
//! In [UTC](https://en.wikipedia.org/w/index.php?title=Coordinated_Universal_Time&oldid=1099753328 "Coordinated Universal Time"), and
//! according to the clock of the PC, tablet, toaster … the library runs on,
//! expressed as seconds + nanoseconds since [`1970-01-01`](https://en.wikipedia.org/w/index.php?title=Unix_time&oldid=1099912565 "Unix time").
//!
//! ```rust
//! # use utcnow::utcnow;
//! let now = utcnow().unwrap();
//! let seconds = now.as_secs();
//! let nanos = now.subsec_nanos();
//! ```
//!
//! For many target platforms this call cannot fail.
//! If this is true for the current target, then the constant [`INFALLIBLE`] will be `true`.
//!
//! If the target platform is not supported, then [`utcnow()`] will always return an error instead of failing to compile.
//! Use the library with `default-features = false` and without the feature `"fallback"` to get a compile-time error instead.
//!
//! The feature `"std"` (enabled by default) is only needed if you need the [`Error`] type to implement [`std::error::Error`].
//!
//! ### Supported platforms
//!
//! If you have successfully tested one of the untested targets, then please [tell me](https://github.com/Kijewski/utcnow/issues).
//! And if not, then even more so!
//!
//! If you know how to implement another target, then please open a [pull request](https://github.com/Kijewski/utcnow/pulls).
//!
//! **Supported and tested:**
//!
//! * Android
//! * Emscripten
//! * FreeBSD
//! * Illumos
//! * Linux
//! * Linux with Musl
//! * MacOS
//! * NetBSD
//! * WASI
//! * wasm32
//! * Windows
//!
//! **(Probably) supported, but not actually tested:**
//!
//! * Darwin
//! * Dragonfly
//! * Fuchsia
//! * iOS
//! * OpenBSD
//! * Redox
//! * Solaris
//!
//! Increasing the <abbr title="Minimum Supported Rust Version">msrv</abbr> for [tier-2](https://doc.rust-lang.org/nightly/rustc/platform-support.html) or
//! lower platforms will not be indicated as a breaking change to the semver version.
//!
//! ### Feature flags
//!
//! `utcnow` has the following optional features:
//!
//! * `serde`, which implements [`serde::Deserialize`] and [`serde::Serialize`] for [`UtcTime`].
//!
//! * `arbitrary`, which implements the [`arbitrary::Arbitrary`] trait for using [`UtcTime`].
//!
//! * `proptest`, which implements the [`proptest::arbitrary::Arbitrary`] trait for using [`UtcTime`].
//!
//! * `quickcheck`, which implements the [`quickcheck::Arbitrary`] trait for using [`UtcTime`].
//!

#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(unused_attributes)]
#![warn(absolute_paths_not_starting_with_crate)]
#![warn(elided_lifetimes_in_paths)]
#![warn(explicit_outlives_requirements)]
#![warn(meta_variable_misuse)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(trivial_casts)]
#![warn(unreachable_pub)]
#![warn(unused_extern_crates)]
#![warn(unused_lifetimes)]
#![warn(unused_results)]

#[cfg(docsrs)]
pub mod changelog;
#[cfg(feature = "arbitrary")]
mod feat_arbitrary;
#[cfg(feature = "proptest")]
mod feat_proptest;
#[cfg(feature = "quickcheck")]
mod feat_quickcheck;
#[cfg(feature = "serde")]
mod feat_serde;
#[cfg_attr(
    any(
        target_os = "dragonfly",
        target_os = "freebsd",
        target_os = "ios",
        target_os = "linux",
        target_os = "macos",
        target_os = "openbsd",
        target_os = "redox",
    ),
    path = "impl_rustix.rs"
)]
#[cfg_attr(
    any(
        target_os = "android",
        target_os = "darwin",
        target_os = "emscripten",
        target_os = "fuchsia",
        target_os = "illumos",
        target_os = "netbsd",
        target_os = "solaris",
    ),
    path = "impl_libc.rs"
)]
#[cfg_attr(target_os = "wasi", path = "impl_wasi.rs")]
#[cfg_attr(target_os = "windows", path = "impl_winapi.rs")]
#[cfg_attr(
    all(target_arch = "wasm32", not(target_os = "wasi")),
    path = "impl_web.rs"
)]
mod platform;
mod u30;

use core::convert::TryFrom;
use core::fmt;
use core::time::Duration;
#[cfg(feature = "std")]
use std::time::SystemTime;

use crate::platform::OsError;
use crate::u30::U30;

/// `true` if getting the time is implemented for the target platform
pub const IMPLEMENTED: bool = platform::IMPLEMENTED;

/// `true` if [`utcnow()`] cannot fail
pub const INFALLIBLE: bool = platform::INFALLIBLE;

/// A Unix time, i.e. seconds since 1970-01-01 in UTC
///
/// Using [`i64`] values as seconds since 1970-01-01, this library will work for the next 292 billion years.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UtcTime {
    /// Seconds since epoch
    secs: i64,
    /// Nanoseconds since epoch
    nanos: U30,
}

impl UtcTime {
    /// Start of the [Unix time](https://en.wikipedia.org/w/index.php?title=Unix_time&oldid=1099912565) epoch, 1970-01-01.
    pub const EPOCH: UtcTime = UtcTime {
        secs: 0,
        nanos: U30::ZERO,
    };

    /// Get the current time
    ///
    /// This method does the same as calling [`utcnow()`].
    ///
    /// # Errors
    ///
    /// See [`utcnow()`] for further information.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let seconds = now.as_secs();
    /// let nanos = now.subsec_nanos();
    /// ```
    #[inline]
    pub fn now() -> Result<Self> {
        utcnow()
    }

    #[inline]
    #[const_fn::const_fn("1.56")]
    unsafe fn create(secs: i64, nanos: u32) -> Self {
        let nanos = U30::new_unchecked(nanos);
        Self { secs, nanos }
    }

    /// Build  a new [`UtcTime`]
    ///
    /// `nanos` will be normalized to a values less than `1_000_000_000`, the number of nanoseconds in a second.
    /// If the resulting number of seconds will exceed [`i64::MAX`], [`None`] is returned.
    ///
    /// # Example
    ///
    /// ```
    /// # use utcnow::UtcTime;
    /// // August 3, 2022, about 19 o'clock in the evening in CEST.
    /// let timestamp = UtcTime::new(1_659_545_693, 895_531_827).unwrap();
    /// ```
    #[must_use]
    #[const_fn::const_fn("1.56")]
    pub fn new(secs: i64, nanos: u32) -> Option<Self> {
        const NANOS_PER_SEC: u32 = 1_000_000_000;

        if nanos < NANOS_PER_SEC {
            return Some(unsafe { Self::create(secs, nanos) });
        }

        let extra_seconds = nanos / NANOS_PER_SEC;
        let nanos = nanos % NANOS_PER_SEC;
        match secs.checked_add(extra_seconds as i64) {
            Some(secs) => Some(unsafe { Self::create(secs, nanos) }),
            None => None,
        }
    }

    /// Convert a [SystemTime]
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "std")] let _: () = {
    /// # use std::time::SystemTime;
    /// # use utcnow::UtcTime;
    /// let system_time = SystemTime::now();
    /// let now = UtcTime::from_system_time(system_time).unwrap();
    /// # };
    /// ```
    #[must_use]
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn from_system_time(value: SystemTime) -> Option<Self> {
        Self::from_duration(value.duration_since(SystemTime::UNIX_EPOCH).ok()?)
    }

    /// Convert a [Duration]
    ///
    /// The duration is interpreted as seconds since epoch (1970-01-01 in UTC).
    ///
    /// # Example
    ///
    /// ```
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let duration = Duration::from_secs(42);
    /// let timestamp = UtcTime::from_duration(duration).unwrap();
    /// assert_eq!(timestamp.as_nanos(), 42_000_000_000);
    /// ```
    #[must_use]
    #[allow(clippy::cast_possible_wrap)]
    #[const_fn::const_fn("1.56")]
    pub fn from_duration(value: Duration) -> Option<Self> {
        const I64_MAX: u64 = i64::MAX as u64;
        let secs = match value.as_secs() {
            secs @ 0..=I64_MAX => secs as i64,
            _ => return None,
        };
        let nanos = value.subsec_nanos();
        Some(unsafe { Self::create(secs, nanos) })
    }

    /// Total number of whole seconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let total_secs = now.as_secs();
    /// assert!(total_secs > 1_658_711_810);
    /// assert!(total_secs < 1_974_324_043); // update before 2032-07-25
    /// ```
    #[must_use]
    #[inline]
    pub const fn as_secs(self) -> i64 {
        self.secs
    }

    /// Total number of whole milliseconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let total_millis = now.as_millis();
    /// assert!(total_millis > 1_658_711_810_802);
    /// assert!(total_millis < 1_974_324_043_000); // update before 2032-07-25
    /// ```
    #[must_use]
    #[const_fn::const_fn("1.56")]
    pub fn as_millis(self) -> i128 {
        (self.secs as i128 * 1_000) + (self.nanos.get() as i128 / 1_000_000)
    }

    /// Total number of whole microseconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let total_micros = now.as_micros();
    /// assert!(total_micros > 1_658_711_810_802_520);
    /// assert!(total_micros < 1_974_324_043_000_000); // update before 2032-07-25
    /// ```
    #[must_use]
    #[const_fn::const_fn("1.56")]
    pub fn as_micros(self) -> i128 {
        (self.secs as i128 * 1_000_000) + (self.nanos.get() as i128 / 1_000)
    }

    /// Total number of whole nanoseconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let total_nanos = now.as_nanos();
    /// assert!(total_nanos > 1_658_711_810_802_520_027);
    /// assert!(total_nanos < 1_974_324_043_000_000_000); // update before 2032-07-25
    /// ```
    #[must_use]
    #[const_fn::const_fn("1.56")]
    pub fn as_nanos(self) -> i128 {
        (self.secs as i128 * 1_000_000_000) + (self.nanos.get() as i128)
    }

    /// Fractional number of milliseconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let millis = now.subsec_millis();
    /// assert!(millis < 1_000);
    /// ```
    #[must_use]
    #[const_fn::const_fn("1.56")]
    pub fn subsec_millis(self) -> u32 {
        self.nanos.get() / 1_000_000
    }

    /// Fractional number of microseconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let micros = now.subsec_micros();
    /// assert!(micros < 1_000_000);
    /// ```
    #[must_use]
    #[const_fn::const_fn("1.56")]
    pub fn subsec_micros(self) -> u32 {
        self.nanos.get() / 1_000
    }

    /// Fractional number of nanoseconds since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let nanos = now.subsec_nanos();
    /// assert!(nanos < 1_000_000_000);
    /// ```
    #[must_use]
    #[inline]
    #[const_fn::const_fn("1.56")]
    pub fn subsec_nanos(self) -> u32 {
        self.nanos.get()
    }

    /// Convert the timestamp to a [Duration] since epoch (1970-01-01 in UTC)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use core::time::Duration;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let duration = now.into_duration().unwrap();
    /// ```
    #[allow(clippy::cast_sign_loss)]
    #[const_fn::const_fn("1.58")]
    pub fn into_duration(self) -> Result<Duration, ConversionError> {
        let secs = match self.secs {
            secs @ 0..=i64::MAX => secs as u64,
            _ => return Err(ConversionError),
        };
        Ok(Duration::new(secs, self.nanos.get()))
    }

    /// Convert the timestamp to a [SystemTime]
    ///
    /// # Errors
    ///
    /// The conversion won't succeed if and only if the stored date is earlier than 1970-01-01.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[cfg(feature = "std")] let _: () = {
    /// # use std::time::SystemTime;
    /// # use utcnow::UtcTime;
    /// let now = UtcTime::now().unwrap();
    /// let system_time = now.into_system_time().unwrap();
    /// # };
    /// ```
    #[inline]
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn into_system_time(self) -> Result<SystemTime, ConversionError> {
        SystemTime::UNIX_EPOCH
            .checked_add(self.into_duration()?)
            .ok_or(ConversionError)
    }
}

/// Get the current unix time, seconds since 1970-01-01 in UTC
///
/// Please see the [module level documentation](crate) for more information.
///
/// # Errors
///
/// For many target platforms this call cannot fail.
/// If this is true for the current target, then the constant [`INFALLIBLE`] will be `true`.
/// Rust will automatically optimize the [`unwrap()`](Result::unwrap) call into a no-op in this case.
/// Independent of the target platform the [`Error`] type will always be [`Send`] + [`Sync`] + [`Copy`].
///
/// # Example
///
/// ```rust
/// let now = utcnow::utcnow().unwrap();
/// let seconds = now.as_secs();
/// let nanos = now.subsec_nanos();
/// ```
#[inline]
pub fn utcnow() -> Result<UtcTime> {
    platform::utcnow()
}

impl TryFrom<UtcTime> for Duration {
    type Error = ConversionError;

    #[inline]
    fn try_from(value: UtcTime) -> Result<Self, ConversionError> {
        value.into_duration()
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl TryFrom<UtcTime> for SystemTime {
    type Error = ConversionError;

    #[inline]
    fn try_from(value: UtcTime) -> Result<Self, ConversionError> {
        value.into_system_time()
    }
}

impl TryFrom<Duration> for UtcTime {
    type Error = ConversionError;

    #[inline]
    fn try_from(value: Duration) -> Result<Self, ConversionError> {
        Self::from_duration(value).ok_or(ConversionError)
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl TryFrom<SystemTime> for UtcTime {
    type Error = ConversionError;

    #[inline]
    fn try_from(value: SystemTime) -> Result<Self, ConversionError> {
        Self::from_system_time(value).ok_or(ConversionError)
    }
}

/// A result type that default to [`Error`] as error type
///
/// For many target platforms [`utcnow()`] cannot fail.
/// If this is true for the current target, then the constant [`INFALLIBLE`] will be `true`.
/// Rust will automatically optimize the [`unwrap()`](Result::unwrap) call into a no-op in this case.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Could not query system time
///
/// For many target platforms [`utcnow()`] cannot fail.
/// If this is true for the current target, then the constant [`INFALLIBLE`] will be `true`.
/// Independent of the target platform the [`Error`] type will always be [`Send`] + [`Sync`] + [`Copy`].
#[derive(Debug, Clone, Copy)]
pub struct Error(OsError);

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<OsError> for Error {
    #[inline]
    fn from(err: OsError) -> Self {
        Self(err)
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error {}

/// Could not convert from or to a [`UtcTime`]
///
/// You cannot convert a negative [`UtcTime`]  (i.e. before 1970-01-01) into a [`SystemTime`] or [`Duration`].
/// You cannot convert a [`SystemTime`] or [`Duration`] later than year 292 billion into a [`UtcTime`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ConversionError;

impl fmt::Display for ConversionError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("cannot convert a negative UtcTime")
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ConversionError {}

#[cfg(test)]
#[test]
fn test_if_can_call() {
    let _ = utcnow().unwrap();
}

#[cfg(test)]
#[test]
fn test_layout() {
    use core::mem;

    assert_eq!(mem::align_of::<u32>(), mem::align_of::<U30>());
    assert_eq!(mem::size_of::<u32>(), mem::size_of::<U30>());
    assert_eq!(mem::size_of::<u32>(), mem::size_of::<Option<U30>>());

    assert_eq!(mem::size_of::<UtcTime>(), mem::size_of::<Option<UtcTime>>());
    assert_eq!(mem::size_of::<UtcTime>(), mem::size_of::<Result<UtcTime>>());
}