chrono_ext

Crates.iochrono_ext
lib.rschrono_ext
version0.1.1
sourcesrc
created_at2020-03-30 14:36:10.797124
updated_at2020-03-31 14:48:38.623829
descriptionUnofficial extensions for chrono crate (custom week definitions)
homepagehttps://github.com/bcourtine/chrono-ext
repositoryhttps://github.com/bcourtine/chrono-ext
max_upload_size
id224378
size15,491
Benoît Courtine (bcourtine)

documentation

https://docs.rs/chrono_ext/

README

Unofficial extensions for Rust chrono crate.

Overview

The purpose of this crate is to provide various helpers/extensions for the chrono crate.

Why?

The first requirement that decide me to write this crate was to have "custom" week definitions. For example, in France, movies are released on Wednesday.

Features

Calculate week number with custom week definitions.

French theater calendar have a week definition where the first week has at least 4 days in current year, and starts on Wednesday. For example:

  • the first week for 2019 starts on 2019-01-02.
  • the first week for 2016 starts on 2015-12-30.

The API for custom week definition is inspired by Java API WeekFields. Week is defined by:

  • The first day-of-week (Monday, Tuesday, …).
  • The minimal number of days in the first week.

Example:

use chrono::NaiveDate;
use chrono_ext::{WeekSpecification, CustomWeek};

fn use_french_theater_week() {
    let french_theater_week: WeekSpecification = WeekSpecification::french_theater_week();
    let date = NaiveDate::from_ymd(2017, 1, 3);

    let week = french_theater_week.week(date);
    println!("{}", week.format("%Y - W%W")); // 2016 - W53
}

Installation

Add the following to Cargo.toml under [dependencies]:

chrono_ext = "0.1.1"
Commit count: 9

cargo fmt