Struct dtg_lib::Dtg

source · []
pub struct Dtg { /* private fields */ }
Expand description

Date time group

Implementations

Create a current Dtg

Create a Dtg from a string timestamp

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

assert_eq!(
    Dtg::from("1658448142").unwrap(),
    Dtg::from_dt(&Utc.timestamp(1658448142, 0)),
);
assert_eq!(
    Dtg::from("1658448142.936196858").unwrap(),
    Dtg::from_dt(&Utc.timestamp(1658448142, 936196858)),
);

Create a Dtg from an “x” format timestamp

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

assert_eq!(
    Dtg::from_x("Xg6L02M").unwrap(),
    Dtg::from_dt(&Utc.timestamp(1658448142, 0)),
);

Create a Dtg from a DateTime

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

assert_eq!(
    Dtg::from_dt(&Utc.timestamp(1658448142, 0)),
    Dtg::from("1658448142").unwrap(),
);

Format as a string

use dtg_lib::{tz, Dtg};

let dtg = Dtg::from("1658448142").unwrap();
let default_utc = "Fri 22 Jul 2022 00:02:22 UTC";
let default_mt = "Thu 21 Jul 2022 18:02:22 MDT";

assert_eq!(dtg.default(&None), default_utc);
assert_eq!(dtg.default(&tz("UTC").ok()), default_utc);
assert_eq!(dtg.default(&tz("MST7MDT").ok()), default_mt);

Format as an RFC 3339 string

use dtg_lib::Dtg;

let dtg = Dtg::from("1658448142").unwrap();

assert_eq!(dtg.rfc_3339(), "2022-07-22T00:02:22Z");

Format as “x” format

use dtg_lib::Dtg;

let dtg = Dtg::from("1658448142").unwrap();

assert_eq!(dtg.x_format(), "Xg6L02M");

Format as “a” format

use dtg_lib::{tz, Dtg};

let dtg = Dtg::from("1658448142").unwrap();
let a_utc = "\
1658448142.000000000
2022-07-22T00:02:22Z
Fri 22 Jul 2022 00:02:22 UTC
Fri 22 Jul 2022 00:02:22 UTC";
let a_mt = "\
1658448142.000000000
2022-07-22T00:02:22Z
Fri 22 Jul 2022 00:02:22 UTC
Thu 21 Jul 2022 18:02:22 MDT";

assert_eq!(dtg.a_format(&None), a_utc);
assert_eq!(dtg.a_format(&tz("UTC").ok()), a_utc);
assert_eq!(dtg.a_format(&tz("MST7MDT").ok()), a_mt);

Format as a string with format and timezone

use dtg_lib::{tz, Dtg, Format};

let dtg = Dtg::from("1658448142").unwrap();

assert_eq!(
    dtg.format(&None, &None),
    "2022-07-22T00:02:22Z",
);
assert_eq!(
    dtg.format(&Some(Format::X), &None),
    "Xg6L02M",
);

let a_fmt = Some(Format::custom("%A"));

assert_eq!(
    dtg.format(&a_fmt, &None),
    "Friday",
);
assert_eq!(
    dtg.format(&a_fmt, &tz("MST7MDT").ok()),
    "Thursday",
);

Trait Implementations

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.