Crate dtg_lib

source ·
Expand description

About

Date/time library

See also the API documentation and dtg crate.

Examples

use chrono::{TimeZone, Utc};
use dtg_lib::{tz, Dtg, Format};

let epoch = 1658448142;
let nanoseconds = 936196858;
let rfc_3339 = "2022-07-22T00:02:22Z";
let default_utc = "Fri 22 Jul 2022 00:02:22 UTC";
let default_mt = "Thu 21 Jul 2022 18:02:22 MDT";
let x = "Xg6L02M";
let a_utc = format!("{epoch}.000000000\n{rfc_3339}\n{default_utc}\n{default_utc}");
let a_mt = format!("{epoch}.000000000\n{rfc_3339}\n{default_utc}\n{default_mt}");
let day_of_week_utc = "Friday";
let day_of_week_mt = "Thursday";
let tz_utc = tz("UTC").ok();
let tz_mt = tz("MST7MDT").ok();
let default_fmt = Some(Format::default());
let day_of_week_fmt = Some(Format::custom("%A"));

// Create Dtg

let dtg_1_str = format!("{}", epoch);

let dtg_1_ts = Dtg::from(&dtg_1_str).unwrap();
let dtg_1_dt = Dtg::from_dt(&Utc.timestamp(epoch, 0));
let dtg_1_x = Dtg::from_x(x).unwrap();

assert_eq!(dtg_1_ts, dtg_1_dt);
assert_eq!(dtg_1_dt, dtg_1_x);
assert_eq!(dtg_1_x, dtg_1_ts);

// Create Dtg with nanoseconds

let dtg_2_str = format!("{}.{}", epoch, nanoseconds);

let dtg_2_ts = Dtg::from(&dtg_2_str).unwrap();
let dtg_2_dt = Dtg::from_dt(&Utc.timestamp(epoch, nanoseconds));

assert_eq!(dtg_2_ts, dtg_2_dt);

// Default format

assert_eq!(dtg_1_ts.default(&None), default_utc);
assert_eq!(dtg_1_ts.default(&tz_utc), default_utc);
assert_eq!(dtg_1_ts.default(&tz_mt), default_mt);

assert_eq!(dtg_1_ts.format(&default_fmt, &None), default_utc);
assert_eq!(dtg_1_ts.format(&default_fmt, &tz_utc), default_utc);
assert_eq!(dtg_1_ts.format(&default_fmt, &tz_mt), default_mt);

// RFC 3339 format

assert_eq!(dtg_1_ts.rfc_3339(), rfc_3339);
assert_eq!(dtg_1_ts.format(&None, &None), rfc_3339);

// "x" format

assert_eq!(dtg_1_ts.x_format(), x);
assert_eq!(dtg_1_ts.format(&Some(Format::X), &None), x);

// "a" format

assert_eq!(dtg_1_ts.a_format(&None), a_utc);
assert_eq!(dtg_1_ts.a_format(&tz_utc), a_utc);
assert_eq!(dtg_1_ts.a_format(&tz_mt), a_mt);

assert_eq!(dtg_1_ts.format(&Some(Format::A), &None), a_utc);
assert_eq!(dtg_1_ts.format(&Some(Format::A), &tz_utc), a_utc);
assert_eq!(dtg_1_ts.format(&Some(Format::A), &tz_mt), a_mt);

// Custom format

assert_eq!(dtg_1_ts.format(&day_of_week_fmt, &None), day_of_week_utc);
assert_eq!(dtg_1_ts.format(&day_of_week_fmt, &tz_mt), day_of_week_mt);

Formats

The following information originates from the chrono documentation, which dtg and dtg-lib use internally.

Date specifiers

Spec.ExampleDescription
%Y2001The full proleptic Gregorian year, zero-padded to 4 digits.
%C20The proleptic Gregorian year divided by 100, zero-padded to 2 digits.
%y01The proleptic Gregorian year modulo 100, zero-padded to 2 digits.
%m07Month number (01–12), zero-padded to 2 digits.
%bJulAbbreviated month name. Always 3 letters.
%BJulyFull month name. Also accepts corresponding abbreviation in parsing.
%hJulSame as %b.
%d08Day number (01–31), zero-padded to 2 digits.
%e 8Same as %d but space-padded. Same as %_d.
%aSunAbbreviated weekday name. Always 3 letters.
%ASundayFull weekday name. Also accepts corresponding abbreviation in parsing.
%w0Sunday = 0, Monday = 1, …, Saturday = 6.
%u7Monday = 1, Tuesday = 2, …, Sunday = 7. (ISO 8601)
%U28Week number starting with Sunday (00–53), zero-padded to 2 digits.
%W27Same as %U, but week 1 starts with the first Monday in that year instead.
%G2001Same as %Y but uses the year number in ISO 8601 week date.
%g01Same as %y but uses the year number in ISO 8601 week date.
%V27Same as %U but uses the week number in ISO 8601 week date (01–53).
%j189Day of the year (001–366), zero-padded to 3 digits.
%D07/08/01Month-day-year format. Same as %m/%d/%y.
%x07/08/01Locale’s date representation (e.g., 12/31/99).
%F2001-07-08Year-month-day format (ISO 8601). Same as %Y-%m-%d.
%v 8-Jul-2001Day-month-year format. Same as %e-%b-%Y.

Time specifiers

Spec.ExampleDescription
%H00Hour number (00–23), zero-padded to 2 digits.
%k 0Same as %H but space-padded. Same as %_H.
%I12Hour number in 12-hour clocks (01–12), zero-padded to 2 digits.
%l12Same as %I but space-padded. Same as %_I.
%Pamam or pm in 12-hour clocks.
%pAMAM or PM in 12-hour clocks.
%M34Minute number (00–59), zero-padded to 2 digits.
%S60Second number (00–60), zero-padded to 2 digits.
%f026490000The fractional seconds (in nanoseconds) since last whole second.
%.f.026490Similar to .%f but left-aligned. These all consume the leading dot.
%.3f.026Similar to .%f but left-aligned but fixed to a length of 3.
%.6f.026490Similar to .%f but left-aligned but fixed to a length of 6.
%.9f.026490000Similar to .%f but left-aligned but fixed to a length of 9.
%3f026Similar to %.3f but without the leading dot.
%6f026490Similar to %.6f but without the leading dot.
%9f026490000Similar to %.9f but without the leading dot.
%R00:34Hour-minute format. Same as %H:%M.
%T00:34:60Hour-minute-second format. Same as %H:%M:%S.
%X00:34:60Locale’s time representation (e.g., 23:13:48).
%r12:34:60 AMHour-minute-second format in 12-hour clocks. Same as %I:%M:%S %p.

Time zone specifiers

Spec.ExampleDescription
%ZACSTLocal time zone name. Skips all non-whitespace characters during parsing.
%z+0930Offset from the local time to UTC (with UTC being +0000).
%:z+09:30Same as %z but with a colon.
%#z+09Parsing only: Same as %z but allows minutes to be missing or present.

Date & time specifiers

Spec.ExampleDescription
%cSun Jul 8 00:34:60 2001Locale’s date and time (e.g., Thu Mar 3 23:05:25 2005).
%+2001-07-08T00:34:60.026490+09:30ISO 8601 / RFC 3339 date & time format.
%s994518299UNIX timestamp, the number of seconds since 1970-01-01 00:00 UTC.

Special specifiers

Spec.Description
%tLiteral tab (\t).
%nLiteral newline (\n).
%%Literal percent sign.

Structs

Enums

Functions

  • Get a timezone by name