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 an RFC 3339 string

use dtg_lib::Dtg;

assert_eq!(
    Dtg::from("1658448142").unwrap().rfc_3339(),
    "2022-07-22T00:02:22Z",
);

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(String::from("%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.