Struct timespan::Span [] [src]

pub struct Span<T> {
    pub start: T,
    pub end: T,
}

This describes a span of something that is Spanable by providing a start and end point.

When the provided Spanable type T is Formatable the span can be serialized to a string. For deserialization from a string the Parsable trait must be implemented by T. Support for serde is available when the timespan crate is configured with the with-serde feature.

This type implements operations known from the set theory. However, there are only operations allowed that produce a single span (e.g. the resulting span is continuous) that must not be empty. When an operation would violate these restrictions an error is emitted.

Developer note: A Span may accept all possible input values without leading to errors in the future by producing an iterator over the results allowing an arbitrary amount of resulting spans.

Example

use timespan::Span;
use chrono::NaiveTime;

let start = "12:30:00".parse().unwrap();
let end = "14:45:00".parse().unwrap();
let span: Span<NaiveTime> = Span::new(start, end).unwrap();

assert!(format!("{}", span) == "12:30:00 - 14:45:00");

Fields

The starting point of the span.

The end point of the span.

Methods

impl<T> Span<T> where
    T: Spanable
[src]

Create a new span with a given starting point and a given end point.

This method emits an Error::Ordering error when the end point lies before the start point.

Get the total duration of the span as a chrono::Duration.

Calculate the mathematical difference of two spans with the same Spanable type.

The difference of span self and other includes the parts of span self that are not included in span other.

This method produces an error when

  • the resulting difference would produce an empty span (Error::Empty)
  • the resulting difference is not continuous (e.g. splitted) (Error::NotContinuous)

Calculate the mathematical symmetric difference of two spans with the same Spanable type.

The symmetric difference of span self and other includes the parts of span self that are not included in span other and the parts of span other that are not included in span self.

This method produces an error when the resulting symmetric difference is not continuous (e.g. splitted) (Error::NotContinuous). As this is only not the case when the two spans are adjacent this method will most likely produce an error.

Calculate the mathematical intersection of two spans with the same Spanable type.

The intersection of span self and other includes the parts that are included in span self and span other.

This method produces an Error::Empty error when there is no intersection between the two spans.

Calculate the mathematical union of two spans with the same Spanable type.

The union of span self and other includes the parts that are included in span self or span other.

This method produces an Error::NotContinuous error when the two spans are not intersecting or adjacent to each other.

Returns true when a given Spanable is included in self. Otherwise returns false.

Returns true when self has no parts in common with other. Otherwise returns false.

Returns true when self is completely included in other. Otherwise returns false.

Returns true when other is completely included in self. Otherwise returns false.

Split self at a given time point at into two spans of the same Spanable type.

This emits an Error::OutOfRange error when at is not included in self.

Move the end point forward in time by a given duration.

This emits an Error::Empty error when the operation would produce an empty span (e.g. the duration is negative).

Move the start point backward in time by a given duration.

This emits an Error::Empty error when the operation would produce an empty span. (e.g. the duration is negative).

Move the end point backward in time by a given duration.

This emits an Error::Empty error when the operation would produce an empty span.

Move the start point forward in time by a given duration.

This emits an Error::Empty error when the operation would produce an empty span.

impl<T> Span<T> where
    T: Spanable + Formatable
[src]

Formats the span with the specified format strings.

For the start and end format strings see the chrono::format::strftime module.

The fmt string is used to format the span to a string. It must contain the following substrings:

  • {start} to match the start point of the span
  • {end} to match the end point of the span

Example

use timespan::NaiveTimeSpan;

let span: NaiveTimeSpan = "12:30:00 - 14:45:00".parse().unwrap();

let f = span.format("from {start} to {end}", "%H.%M", "%H.%M");
assert!(f.to_string() == "from 12.30 to 14.45");
assert!(format!("{}", f) == "from 12.30 to 14.45");

impl<T> Span<T> where
    T: Spanable + Parsable
[src]

Parses the span with the specified format strings from a given string s.

For the start and end format strings see the chrono::format::strftime module.

The fmt string is used to parse a span from a string. It must contain the following substrings:

  • {start} to match the start point of the span
  • {end} to match the end point of the span

Example

use timespan::NaiveTimeSpan;

let span = NaiveTimeSpan::parse_from_str(
    "from 12.30 to 14.45",
    "from {start} to {end}",
    "%H.%M",
    "%H.%M",
).unwrap();

assert!(format!("{}", span) == "12:30:00 - 14:45:00");

Trait Implementations

impl<T> Serialize for Span<T> where
    T: Spanable + Formatable
[src]

Serialize this value into the given Serde serializer. Read more

impl<'de, T> Deserialize<'de> for Span<T> where
    T: Spanable + Parsable
[src]

Deserialize this value from the given Serde deserializer. Read more

impl<T: PartialEq> PartialEq for Span<T>
[src]

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

This method tests for !=.

impl<T: Clone> Clone for Span<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T> FromStr for Span<T> where
    T: Spanable + Parsable
[src]

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

The associated error which can be returned from parsing.

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

impl<T> Debug for Span<T> where
    T: Spanable + Formatable
[src]

Formats a Span in the format {start} - {end}.

Formats the value using the given formatter.

impl<T> Display for Span<T> where
    T: Spanable + Formatable
[src]

Formats a Span in the format {start} - {end}.

Formats the value using the given formatter. Read more