Struct dtg_lib::Dtg

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

Date time group

Implementations§

source§

impl Dtg

source

pub fn now() -> Dtg

Create a current Dtg

source

pub fn from(s: &str) -> Result<Dtg, DtgError>

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_opt(1658448142, 0).unwrap()),
);
assert_eq!(
    Dtg::from("1658448142.936196858").unwrap(),
    Dtg::from_dt(&Utc.timestamp_opt(1658448142, 936196858).unwrap()),
);
source

pub fn from_x(s: &str) -> Result<Dtg, DtgError>

Create a Dtg from an “x” format timestamp

use dtg_lib::{Dtg, Format};

let dtg = Dtg::from_x("Xg6L02M").unwrap();

assert_eq!(dtg.format(&Some(Format::custom("%s")), &None), "1658448142");
assert_eq!(dtg.rfc_3339(), "2022-07-22T00:02:22Z");
source

pub fn from_dt(dt: &DateTime<Utc>) -> Dtg

Create a Dtg from a DateTime<Utc>

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

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

pub fn default(&self, tz: &Option<Tz>) -> String

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);
source

pub fn rfc_3339(&self) -> String

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");
source

pub fn x_format(&self) -> String

Format as “x” format

use dtg_lib::Dtg;

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

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

pub fn a_format(&self, tz: &Option<Tz>) -> String

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);
source

pub fn bcd_format(&self) -> String

Format like a binary clock using the Braille Patterns Unicode Block and | separators

use dtg_lib::Dtg;

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

assert_eq!(dtg.bcd_format(), "⠄⠤|⢰|⠤|⠀|⠠|⠤"); // 2022|07|22|00|02|22
source

pub fn format(&self, fmt: &Option<Format>, tz: &Option<Tz>) -> String

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§

source§

impl Debug for Dtg

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for Dtg

source§

fn eq(&self, other: &Dtg) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl RefUnwindSafe for Dtg

§

impl Send for Dtg

§

impl Sync for Dtg

§

impl Unpin for Dtg

§

impl UnwindSafe for Dtg

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.