Type Alias timespan::DateTimeSpan

source ·
pub type DateTimeSpan<T> = Span<DateTime<T>>;
Expand description

The DateTimeSpan alias is a span consisting of chrono::DateTimes.

It can be used to represent datetime spans that depend on a specific time zone.

The DateTimeSpan can be formatted and parsed from a string. It can be used for serialization and deserialization with serde. The deserialization is currently only supported for the Utc, Local and FixedOffset time zones. The time zones provided by chrono-tz do not implement from_str and parse_from_str for chrono::DateTime<Tz> and can therefore not be deserialized.

§Example

use timespan::DateTimeSpan;
use chrono::Utc;

let a: DateTimeSpan<Utc> = "2017-01-01T15:10:00 +0200 - 2017-01-02T09:30:00 +0200"
   .parse().unwrap();

assert!(
    format!("{}", a.format("{start} to {end}", "%c", "%c")) ==
    "Sun Jan  1 13:10:00 2017 to Mon Jan  2 07:30:00 2017"
);

Aliased Type§

struct DateTimeSpan<T> {
    pub start: DateTime<T>,
    pub end: DateTime<T>,
}

Fields§

§start: DateTime<T>

The starting point of the span.

§end: DateTime<T>

The end point of the span.

Implementations§

source§

impl<T: TimeZone> DateTimeSpan<T>

source

pub fn from_local_datetimespan( span: &NaiveDateTimeSpan, tz: &T ) -> Result<Self, Error>

Create a DateTimeSpan from a NaiveDateTimeSpan with the time zone set to the local time zone.

Currently the result handling of the internally used TimeZone::from_local_datetime is not implemented properly. Therefore only date spans with a single local time zone can be created. Ambigious local time zones will lead to Error::LocalAmbigious.

To avoid this from_utc_datetimespan should be prefered.

source

pub fn from_utc_datetimespan(span: &NaiveDateTimeSpan, tz: &T) -> Self

Create a DateTimeSpan from a NaiveDateTimeSpan with the time zone set to UTC.

§Example
use timespan::DateTimeSpan;
use chrono_tz::America::Puerto_Rico;

let a = DateTimeSpan::from_utc_datetimespan(
    &"2017-03-12T12:00:00 - 2017-03-15T14:00:00".parse().unwrap(),
    &Puerto_Rico,
);

assert!(format!("{}", a) == "2017-03-12 08:00:00 AST - 2017-03-15 10:00:00 AST");

Trait Implementations§

source§

impl FromStr for DateTimeSpan<Tz>

Parses a Span from a string in the format {start} - {end}.

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more