#time #date #utc #unixtime #printing

utc2k

A fast and lean UTC date/time library concerned only with happenings in this century (2000-2099)

27 releases

Uses new Rust 2021

new 0.5.5 Aug 11, 2022
0.5.4 Jul 23, 2022
0.5.1 Jun 28, 2022
0.4.2 Mar 28, 2022
0.2.4 Jul 19, 2021

#28 in Date and time

Download history 5/week @ 2022-04-21 217/week @ 2022-04-28 146/week @ 2022-05-05 325/week @ 2022-05-12 118/week @ 2022-05-19 137/week @ 2022-05-26 251/week @ 2022-06-02 249/week @ 2022-06-09 203/week @ 2022-06-16 85/week @ 2022-06-23 118/week @ 2022-06-30 43/week @ 2022-07-07 44/week @ 2022-07-14 157/week @ 2022-07-21 29/week @ 2022-07-28 9/week @ 2022-08-04

266 downloads per month
Used in fyi_msg

WTFPL license

125KB
2.5K SLoC

UTC2K

Documentation crates.io Build Status Dependency Status PRs Welcome

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 and YYYY-MM-DD hh:mm:ss varieties;
  • Perform addition/subtraction (in seconds), checked or saturating;
  • Calculate the date's ordinal;
  • Calculate the number of seconds from midnight;

That's it!

Compared to more robust libraries like chrono and 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 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();

Optional Crate Features

  • local: Enables the LocalOffset struct. Refer to the documentation for important caveats and limitations.
  • serde: Enables serialization/deserialization support.

Installation

Add utc2k to your dependencies in Cargo.toml, like:

[dependencies]
utc2k = "0.5.*"

License

Copyright © 2022 Blobfolio, LLC <hello@blobfolio.com>

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

Dependencies

~130KB