Struct ::exo::DateTime

Overview

DateTime is a specific date and time, which is aligned towards the UTC timezone.

Methods

fn new() -> DateTime

Creates a new DateTime with the actual local time.

Creates a new DateTime with epoch time. Epoch time is Midnight, 1 January, 1970 (UTC).

Return the date as iso8601 encoded string.

Example: 1997-11-21T09:55:06.000000000-06:00

fn parse_str(inp: String) -> Result

This function tries to parse the DateTime from a string. Multiple formats are tried to be parsed based on the following order:

  • json formatted DateTime string
  • psql formatted string
  • iso8601 formatted string
  • local time formatted string

Return the DateTime as local user readable string.

Format: "[year]-[month]-[day], [hour]:[minute]:[second]"

Example: "2023-10-19, 11:35:29"

Tries to parse DateTime from a local user readable string.

Format: "[year]-[month]-[day], [hour]:[minute]:[second]"

Example: "2023-10-19, 11:35:29"

let dt = exo::DateTime::from_local_string("2023-10-19, 11:35:29")?;
assert_eq!(dt.year(), 2023);
fn year(self) -> i64

Returns the year of the DateTime as number.

fn set_year(self, year: i64) -> Result

Set the year of the DateTime to the given number. When the number is unrealistic, this function returns an error.

fn month(self) -> i64

Returns the month of the DateTime as number. Janurary is a 0 and December is a 12

fn set_month(self, month: i64) -> Result

Set the month of the DateTime to the given number. Janurary is a 0 and December is a 12

fn day(self) -> i64

Returns the day of the DateTime as number. The returned value will always be in the range 1..=31.

fn set_day(self, day: i64) -> Result

Set the month of the DateTime to the given number. The number need to be in the range of 1..=31.

fn hour(self) -> i64

Returns the hour of the DateTime as number.

fn set_hour(self, hour: i64) -> Result

Set the hour of the DateTime to the given number.

fn minute(self) -> i64

Returns the minute of the DateTime as number.

fn set_minute(self, minute: i64) -> Result

Set the minute of the DateTime to the given number.

fn second(self) -> i64

Returns the second of the DateTime as number.

fn set_second(self, second: i64) -> Result

Set the second of the DateTime to the given number.

fn diff_whole_days(self, other: DateTime) -> i64

Returns the difference of full days between two DateTimes

fn is_epoch(self) -> bool

Returns true if the date is set to epoch.

Clones the DateTime and returns a new independet value.

Protocols

protocol partial_eq
if value == b { }

Allows for partial equality operations to work.

protocol string_debug
println("{:?}", value)

Allows the value to be debug printed.

protocol string_display
println("{}", value)

Allows the value to be display printed.

protocol add
let output = value + b

The add operation for adding a Duration to a DateTime.

Example

let dt = exo::DateTime::new_epoch();
let one = exo::Duration::from_days(1);
let res = dt + one;
assert_eq!(res.day(), 2);
protocol add_assign
value += b

The add assign operation for Duration to a Datetime.

Example

let dt = exo::DateTime::new_epoch();
dt += exo::Duration::from_days(1);
assert_eq!(dt.day(), 2);
protocol sub
let output = value - b

The sub operation for Duration from a Datetime.

Example subtract two DateTimes

let dt1 = exo::DateTime::from_local_string("2000-01-01, 00:00:00")?;
let dt2 = exo::DateTime::from_local_string("2000-01-02, 00:00:00")?;
let res = dt2 - dt1;
assert_eq!(res.whole_days(), 1);

Example subtract a Duration

let dt = exo::DateTime::from_local_string("2002-02-02, 02:02:02")?;
let one = exo::Duration::from_days(1);
let res = dt - one;
assert_eq!(res.day(), 1);
protocol sub_assign
value -= b

The sub assign operation for Duration from a Datetime.

Example

let dt = exo::DateTime::from_local_string("2002-02-02, 02:02:02")?;
dt -= exo::Duration::from_days(1);
assert_eq!(dt.day(), 1);