Code Monkey home page Code Monkey logo

rust-timespan's Introduction

Timespan Library for Rust ๐Ÿฆ€

Build Status Coverage Status crates.io Docs.rs Homepage GitHub PRs Welcome
A simple timespan for chrono times


Usage

Put this in your Cargo.toml:

[dependencies]
timespan = "^0"

Or, if you want Serde support, include it like this:

[dependencies]
timespan = { version = "^0", features = ["with-serde"] }

Overview

Date and Time Spans

Timespan can be used to create a time zone aware span consisting of chrono::DateTimes.

Currently the DateTimeSpan supports serialization and deserialization for the chrono::Utc, chrono::Local and chrono::FixedOffset time zones. For support of other time zone types please refer to the documentation.

When the with-serde feature is enabled DateTimeSpan has support for serde serialization and deserialization.

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

Individual Date Spans

A DateSpan can be used to create a time zone aware span consisting of chrono::Dates.

Currently the DateSpan does not support serialization and deserialization from strings.

use timespan::DateSpan;
use chrono_tz::Europe::Paris;

let a = DateSpan::from_utc_datespan(
    &"1789-06-17 - 1799-11-10".parse().unwrap(),
    &Paris,
);

let f = a.format(
    "The french revolution lasted from the {start} to the {end}.",
    "%eth of %B %Y",
    "%eth of %B %Y",
);
assert!(
    format!("{}", f) ==
    "The french revolution lasted from the 17th of June 1789 to the 10th of November 1799."
);

Naive Date and Time Spans

The NaiveDateSpan, NaiveTimeSpan and NaiveDateTimeSpan are all not aware of time zones and can be used for simple time spans.

All naive spans have full support for serialization and deserialization from strings.

When the with-serde feature is enabled all naive spans have support for serde serialization and deserialization.

use timespan::NaiveDateSpan;

let a: NaiveDateSpan = "2017-04-15 - 2017-08-15".parse().unwrap();
let b = NaiveDateSpan::parse_from_str(
    "15.04.17 - 15.08.17",
    "{start} - {end}",
    "%d.%m.%y", "%d.%m.%y"
).unwrap();

let f = a.format("from {start} to {end}", "%m/%d", "%m/%d");
assert!(format!("{}", f) == "from 04/15 to 08/15");
assert!(a == b);
use timespan::NaiveTimeSpan;

let a: NaiveTimeSpan = "17:30:00 - 19:15:00".parse().unwrap();
let b = NaiveTimeSpan::parse_from_str(
    "05.30 PM - 07.15 PM",
    "{start} - {end}",
    "%I.%M %P", "%I.%M %P"
).unwrap();

let f = a.format("from {start} to {end}", "%R", "%R");
assert!(format!("{}", f) == "from 17:30 to 19:15");
assert!(a == b);
use timespan::NaiveDateTimeSpan;

let a: NaiveDateTimeSpan = "2017-02-20T11:30:00 - 2017-02-23T18:00:00".parse().unwrap();
let b = NaiveDateTimeSpan::parse_from_str(
    "02/20/17 11.30 am - 02/23/17 06.00 pm",
    "{start} - {end}",
    "%D %I.%M %p", "%D %I.%M %p"
).unwrap();

let f = a.format("from {start} to {end}", "%R on %A", "%R on %A");
assert!(format!("{}", f) == "from 11:30 on Monday to 18:00 on Thursday");
assert!(a == b);

How to Run the Examples

In order to run an example from the example folder issue the following command.

$ cargo run --example <name>

The convert Example

Convert from 10.30 to 14.00 to 10:30 - 14:00:

$ cargo run --example convert -- "from 10.30 to 14.00" \
    "from {start} to {end}" "%H.%M" "%H.%M" \
    "{start} - {end}" "%R" "%R"

The duration Example

Get the duration of the time span from 10.30 to 14.00:

$ cargo run --example duration -- "from 10.30 to 14.00" \
    "from {start} to {end}" "%H.%M" "%H.%M"

The contains Example

Get whether 11.20 is contained in the time span from 10.30 to 14.00:

$ cargo run --example contains -- "from 10.30 to 14.00" "11.20" \
    "from {start} to {end}" "%H.%M" "%H.%M" "%H.%M"

License

This project is licensed under the GPL-v3 license - see the LICENSE file for details.

rust-timespan's People

Contributors

fin-ger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

rust-timespan's Issues

handle upper_inc = false ?

Hi thanks for this awesome library.

I am porting part of a python project in rust, which uses the python spans library.

Is there plans to support the equivalent of upper_inc=false (i.e don't include the upper limit)

e.g:

let slot: DateTimeSpan<Utc> = "2019-01-01T15:00:00 +0000 - 2019-01-01T16:00:00 +0000"
    .parse()
    .unwrap();

let t = "2019-01-01T16:00:00 +0000".parse().unwrap();
assert!(!slot.contains(&t));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.