#calendar #date #time

chrono_ext

Unofficial extensions for chrono crate (custom week definitions)

2 releases

0.1.1 Mar 31, 2020
0.1.0 Mar 30, 2020

#371 in Date and time

MIT license

16KB
172 lines

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"

Dependencies

~1.5MB
~19K SLoC