rustfmt'ed src/lib.rs (only).

This commit is contained in:
Kang Seonghoon 2016-01-23 14:33:20 +09:00
parent 1f7fd95020
commit d3384780d0
4 changed files with 289 additions and 284 deletions

View File

@ -4,6 +4,7 @@ and also the following people (in ascending order):
Colin Ray <r.colinray@gmail.com> Colin Ray <r.colinray@gmail.com>
Corey Farwell <coreyf@rwell.org> Corey Farwell <coreyf@rwell.org>
Dan <dan@ebip.co.uk> Dan <dan@ebip.co.uk>
David Hewson <dev@daveid.co.uk>
David Ross <daboross@daboross.net> David Ross <daboross@daboross.net>
Eunchong Yu <kroisse@gmail.com> Eunchong Yu <kroisse@gmail.com>
Huon Wilson <dbau.pp+github@gmail.com> Huon Wilson <dbau.pp+github@gmail.com>

View File

@ -14,18 +14,17 @@ readme: README.md
README.md: src/lib.rs README.md: src/lib.rs
# really, really sorry for this mess. # really, really sorry for this mess.
awk '/^# Chrono /{print "[Chrono][doc]",$$3}' $< > $@ awk '/^\/\/! # Chrono /{print "[Chrono][doc]",$$4}' $< > $@
awk '/^# Chrono /{print "[Chrono][doc]",$$3}' $< | sed 's/./=/g' >> $@ awk '/^\/\/! # Chrono /{print "[Chrono][doc]",$$4}' $< | sed 's/./=/g' >> $@
echo >> $@ echo >> $@
echo '[![Chrono on Travis CI][travis-image]][travis]' >> $@ echo '[![Chrono on Travis CI][travis-image]][travis]' >> $@
echo >> $@ echo >> $@
echo '[travis-image]: https://travis-ci.org/lifthrasiir/rust-chrono.png' >> $@ echo '[travis-image]: https://travis-ci.org/lifthrasiir/rust-chrono.png' >> $@
echo '[travis]: https://travis-ci.org/lifthrasiir/rust-chrono' >> $@ echo '[travis]: https://travis-ci.org/lifthrasiir/rust-chrono' >> $@
awk '/^# Chrono /,/^## /' $< | tail -n +2 | sed '$$d' | sed '$$d' >> $@ awk '/^\/\/! # Chrono /,/^\/\/! ## /' $< | cut -b 5- | grep -v '^#' >> $@
echo >> $@
echo '[Complete Documentation][doc]' >> $@ echo '[Complete Documentation][doc]' >> $@
echo >> $@ echo >> $@
echo '[doc]: https://lifthrasiir.github.io/rust-chrono/' >> $@ echo '[doc]: https://lifthrasiir.github.io/rust-chrono/' >> $@
echo >> $@ echo >> $@
awk '/^## /,/^\*\/$$/' $< | grep -v '^# ' | sed '$$d' >> $@ awk '/^\/\/! ## /,!/^\/\/!/' $< | cut -b 5- | grep -v '^# ' >> $@

View File

@ -7,7 +7,8 @@
[travis]: https://travis-ci.org/lifthrasiir/rust-chrono [travis]: https://travis-ci.org/lifthrasiir/rust-chrono
Date and time handling for Rust. (also known as `rust-chrono`) Date and time handling for Rust. (also known as `rust-chrono`)
It aims to be a feature-complete superset of the [time](https://github.com/rust-lang/time) library. It aims to be a feature-complete superset of
the [time](https://github.com/rust-lang/time) library.
In particular, In particular,
* Chrono strictly adheres to ISO 8601. * Chrono strictly adheres to ISO 8601.
@ -173,7 +174,8 @@ Parsing can be done with three methods:
3. `Offset::datetime_from_str` is similar but returns `DateTime` of given offset. 3. `Offset::datetime_from_str` is similar but returns `DateTime` of given offset.
When the explicit offset is missing from the input, it simply uses given offset. When the explicit offset is missing from the input, it simply uses given offset.
It issues an error when the input contains an explicit offset different from the current offset. It issues an error when the input contains an explicit offset different
from the current offset.
More detailed control over the parsing process is available via `format` module. More detailed control over the parsing process is available via `format` module.
@ -191,7 +193,8 @@ assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<FixedOffset>>(), Ok(fixe
// method 2 // method 2
assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"), assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"),
Ok(fixed_dt.clone())); Ok(fixed_dt.clone()));
assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"), Ok(fixed_dt.clone())); assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
Ok(fixed_dt.clone()));
assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone())); assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone()));
// method 3 // method 3
@ -253,7 +256,8 @@ Time types are limited in the nanosecond accuracy.
Leap seconds are supported in the representation but Chrono doesn't try to make use of them. Leap seconds are supported in the representation but Chrono doesn't try to make use of them.
(The main reason is that leap seconds are not really predictable.) (The main reason is that leap seconds are not really predictable.)
Almost *every* operation over the possible leap seconds will ignore them. Almost *every* operation over the possible leap seconds will ignore them.
Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale if you want. Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale
if you want.
Chrono inherently does not support an inaccurate or partial date and time representation. Chrono inherently does not support an inaccurate or partial date and time representation.
Any operation that can be ambiguous will return `None` in such cases. Any operation that can be ambiguous will return `None` in such cases.

View File

@ -2,269 +2,269 @@
// Copyright (c) 2014-2015, Kang Seonghoon. // Copyright (c) 2014-2015, Kang Seonghoon.
// See README.md and LICENSE.txt for details. // See README.md and LICENSE.txt for details.
/*! //! # Chrono 0.2.17
//!
# Chrono 0.2.17 //! Date and time handling for Rust. (also known as `rust-chrono`)
//! It aims to be a feature-complete superset of
Date and time handling for Rust. (also known as `rust-chrono`) //! the [time](https://github.com/rust-lang/time) library.
It aims to be a feature-complete superset of the [time](https://github.com/rust-lang/time) library. //! In particular,
In particular, //!
//! * Chrono strictly adheres to ISO 8601.
* Chrono strictly adheres to ISO 8601. //! * Chrono is timezone-aware by default, with separate timezone-naive types.
* Chrono is timezone-aware by default, with separate timezone-naive types. //! * Chrono is space-optimal and (while not being the primary goal) reasonably efficient.
* Chrono is space-optimal and (while not being the primary goal) reasonably efficient. //!
//! There were several previous attempts to bring a good date and time library to Rust,
There were several previous attempts to bring a good date and time library to Rust, //! which Chrono builts upon and should acknowledge:
which Chrono builts upon and should acknowledge: //!
//! * [Initial research on
* [Initial research on //! the wiki](https://github.com/rust-lang/rust-wiki-backup/blob/master/Lib-datetime.md)
the wiki](https://github.com/rust-lang/rust-wiki-backup/blob/master/Lib-datetime.md) //! * Dietrich Epp's [datetime-rs](https://github.com/depp/datetime-rs)
* Dietrich Epp's [datetime-rs](https://github.com/depp/datetime-rs) //! * Luis de Bethencourt's [rust-datetime](https://github.com/luisbg/rust-datetime)
* Luis de Bethencourt's [rust-datetime](https://github.com/luisbg/rust-datetime) //!
//! ## Usage
## Usage //!
//! Put this in your `Cargo.toml`:
Put this in your `Cargo.toml`: //!
//! ```toml
```toml //! [dependencies]
[dependencies] //! chrono = "0.2"
chrono = "0.2" //! ```
``` //!
//! And put this in your crate root:
And put this in your crate root: //!
//! ```rust
```rust //! extern crate chrono;
extern crate chrono; //! ```
``` //!
//! ## Overview
## Overview //!
//! ### Duration
### Duration //!
//! Chrono used to have a `Duration` type, which represents the time span.
Chrono used to have a `Duration` type, which represents the time span. //! This is a simple reexport of
This is a simple reexport of //! [`time::Duration`](http://doc.rust-lang.org/time/time/struct.Duration.html) type
[`time::Duration`](http://doc.rust-lang.org/time/time/struct.Duration.html) type //! provided by crates.io `time` crate (which originally comes from Chrono).
provided by crates.io `time` crate (which originally comes from Chrono). //!
//! ### Date and Time
### Date and Time //!
//! Chrono provides a `DateTime` type for the combined date and time.
Chrono provides a `DateTime` type for the combined date and time. //!
//! `DateTime`, among others, is timezone-aware and
`DateTime`, among others, is timezone-aware and //! must be constructed from the `TimeZone` object.
must be constructed from the `TimeZone` object. //! `DateTime`s with different time zones do not mix, but can be converted to each other.
`DateTime`s with different time zones do not mix, but can be converted to each other. //!
//! You can get the current date and time in the UTC time zone (`UTC::now()`)
You can get the current date and time in the UTC time zone (`UTC::now()`) //! or in the local time zone (`Local::now()`).
or in the local time zone (`Local::now()`). //!
//! ~~~~ {.rust}
~~~~ {.rust} //! use chrono::*;
use chrono::*; //!
//! let utc: DateTime<UTC> = UTC::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
let utc: DateTime<UTC> = UTC::now(); // e.g. `2014-11-28T12:45:59.324310806Z` //! let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00` //! # let _ = utc; let _ = local;
# let _ = utc; let _ = local; //! ~~~~
~~~~ //!
//! Alternatively, you can create your own date and time.
Alternatively, you can create your own date and time. //! This is a bit verbose due to Rust's lack of function and method overloading,
This is a bit verbose due to Rust's lack of function and method overloading, //! but in turn we get a rich combination of initialization methods.
but in turn we get a rich combination of initialization methods. //!
//! ~~~~ {.rust}
~~~~ {.rust} //! use chrono::*;
use chrono::*; //!
//! let dt = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
let dt = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z` //! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
// July 8 is 188th day of the year 2014 (`o` for "ordinal") //! assert_eq!(dt, UTC.yo(2014, 189).and_hms(9, 10, 11));
assert_eq!(dt, UTC.yo(2014, 189).and_hms(9, 10, 11)); //! // July 8 is Tuesday in ISO week 28 of the year 2014.
// July 8 is Tuesday in ISO week 28 of the year 2014. //! assert_eq!(dt, UTC.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
assert_eq!(dt, UTC.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11)); //!
//! let dt = UTC.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); // `2014-07-08T09:10:11.012Z`
let dt = UTC.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); // `2014-07-08T09:10:11.012Z` //! assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_micro(9, 10, 11, 12_000));
assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_micro(9, 10, 11, 12_000)); //! assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_nano(9, 10, 11, 12_000_000));
assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_nano(9, 10, 11, 12_000_000)); //!
//! // dynamic verification
// dynamic verification //! assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(21, 15, 33),
assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(21, 15, 33), //! LocalResult::Single(UTC.ymd(2014, 7, 8).and_hms(21, 15, 33)));
LocalResult::Single(UTC.ymd(2014, 7, 8).and_hms(21, 15, 33))); //! assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(80, 15, 33), LocalResult::None);
assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(80, 15, 33), LocalResult::None); //! assert_eq!(UTC.ymd_opt(2014, 7, 38).and_hms_opt(21, 15, 33), LocalResult::None);
assert_eq!(UTC.ymd_opt(2014, 7, 38).and_hms_opt(21, 15, 33), LocalResult::None); //!
//! // other time zone objects can be used to construct a local datetime.
// other time zone objects can be used to construct a local datetime. //! // obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical.
// obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical. //! let local_dt = Local.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12);
let local_dt = Local.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); //! let fixed_dt = FixedOffset::east(9 * 3600).ymd(2014, 7, 8).and_hms_milli(18, 10, 11, 12);
let fixed_dt = FixedOffset::east(9 * 3600).ymd(2014, 7, 8).and_hms_milli(18, 10, 11, 12); //! assert_eq!(dt, fixed_dt);
assert_eq!(dt, fixed_dt); //! # let _ = local_dt;
# let _ = local_dt; //! ~~~~
~~~~ //!
//! Various properties are available to the date and time, and can be altered individually.
Various properties are available to the date and time, and can be altered individually. //! Most of them are defined in the traits `Datelike` and `Timelike` which you should `use` before.
Most of them are defined in the traits `Datelike` and `Timelike` which you should `use` before. //! Addition and subtraction is also supported.
Addition and subtraction is also supported. //! The following illustrates most supported operations to the date and time:
The following illustrates most supported operations to the date and time: //!
//! ~~~~ {.rust}
~~~~ {.rust} //! use chrono::*;
use chrono::*; //!
//! # /* we intentionally fake the datetime...
# /* we intentionally fake the datetime... //! // assume this returned `2014-11-28T21:45:59.324310806+09:00`:
// assume this returned `2014-11-28T21:45:59.324310806+09:00`: //! let dt = Local::now();
let dt = Local::now(); //! # */ // up to here. we now define a fixed datetime for the illustrative purpose.
# */ // up to here. we now define a fixed datetime for the illustrative purpose. //! # let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806);
# let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806); //!
//! // property accessors
// property accessors //! assert_eq!((dt.year(), dt.month(), dt.day()), (2014, 11, 28));
assert_eq!((dt.year(), dt.month(), dt.day()), (2014, 11, 28)); //! assert_eq!((dt.month0(), dt.day0()), (10, 27)); // for unfortunate souls
assert_eq!((dt.month0(), dt.day0()), (10, 27)); // for unfortunate souls //! assert_eq!((dt.hour(), dt.minute(), dt.second()), (21, 45, 59));
assert_eq!((dt.hour(), dt.minute(), dt.second()), (21, 45, 59)); //! assert_eq!(dt.weekday(), Weekday::Fri);
assert_eq!(dt.weekday(), Weekday::Fri); //! assert_eq!(dt.weekday().number_from_monday(), 5); // Mon=1, ..., Sat=7
assert_eq!(dt.weekday().number_from_monday(), 5); // Mon=1, ..., Sat=7 //! assert_eq!(dt.ordinal(), 332); // the day of year
assert_eq!(dt.ordinal(), 332); // the day of year //! assert_eq!(dt.num_days_from_ce(), 735565); // the number of days from and including Jan 1, 1
assert_eq!(dt.num_days_from_ce(), 735565); // the number of days from and including Jan 1, 1 //!
//! // time zone accessor and manipulation
// time zone accessor and manipulation //! assert_eq!(dt.offset().local_minus_utc(), Duration::hours(9));
assert_eq!(dt.offset().local_minus_utc(), Duration::hours(9)); //! assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600));
assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600)); //! assert_eq!(dt.with_timezone(&UTC), UTC.ymd(2014, 11, 28).and_hms_nano(12, 45, 59, 324310806));
assert_eq!(dt.with_timezone(&UTC), UTC.ymd(2014, 11, 28).and_hms_nano(12, 45, 59, 324310806)); //!
//! // a sample of property manipulations (validates dynamically)
// a sample of property manipulations (validates dynamically) //! assert_eq!(dt.with_day(29).unwrap().weekday(), Weekday::Sat); // 2014-11-29 is Saturday
assert_eq!(dt.with_day(29).unwrap().weekday(), Weekday::Sat); // 2014-11-29 is Saturday //! assert_eq!(dt.with_day(32), None);
assert_eq!(dt.with_day(32), None); //! assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November 29, 301 BCE
assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November 29, 301 BCE //!
//! // arithmetic operations
// arithmetic operations //! assert_eq!(UTC.ymd(2014, 11, 14).and_hms(8, 9, 10) - UTC.ymd(2014, 11, 14).and_hms(10, 9, 8),
assert_eq!(UTC.ymd(2014, 11, 14).and_hms(8, 9, 10) - UTC.ymd(2014, 11, 14).and_hms(10, 9, 8), //! Duration::seconds(-2 * 3600 + 2));
Duration::seconds(-2 * 3600 + 2)); //! assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) + Duration::seconds(1_000_000_000),
assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) + Duration::seconds(1_000_000_000), //! UTC.ymd(2001, 9, 9).and_hms(1, 46, 40));
UTC.ymd(2001, 9, 9).and_hms(1, 46, 40)); //! assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_000),
assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_000), //! UTC.ymd(1938, 4, 24).and_hms(22, 13, 20));
UTC.ymd(1938, 4, 24).and_hms(22, 13, 20)); //! ~~~~
~~~~ //!
//! Formatting is done via the `format` method,
Formatting is done via the `format` method, //! which format is equivalent to the familiar `strftime` format.
which format is equivalent to the familiar `strftime` format. //! (See the `format::strftime` module documentation for full syntax.)
(See the `format::strftime` module documentation for full syntax.) //!
//! The default `to_string` method and `{:?}` specifier also give a reasonable representation.
The default `to_string` method and `{:?}` specifier also give a reasonable representation. //! Chrono also provides `to_rfc{2822,3339}` methods for well-known formats.
Chrono also provides `to_rfc{2822,3339}` methods for well-known formats. //!
//! ~~~~ {.rust}
~~~~ {.rust} //! use chrono::*;
use chrono::*; //!
//! let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9); //! assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09");
assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09"); //! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), "Fri Nov 28 12:00:09 2014");
assert_eq!(dt.format("%a %b %e %T %Y").to_string(), "Fri Nov 28 12:00:09 2014"); //! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string());
assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string()); //!
//! assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC");
assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC"); //! assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000");
assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000"); //! assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00");
assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00"); //! assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z"); //! ~~~~
~~~~ //!
//! Parsing can be done with three methods:
Parsing can be done with three methods: //!
//! 1. The standard `FromStr` trait (and `parse` method on a string) can be used for
1. The standard `FromStr` trait (and `parse` method on a string) can be used for //! parsing `DateTime<FixedOffset>`, `DateTime<UTC>` and `DateTime<Local>` values.
parsing `DateTime<FixedOffset>`, `DateTime<UTC>` and `DateTime<Local>` values. //! This parses what the `{:?}` (`std::fmt::Debug`) format specifier prints,
This parses what the `{:?}` (`std::fmt::Debug`) format specifier prints, //! and requires the offset to be present.
and requires the offset to be present. //!
//! 2. `DateTime::parse_from_str` parses a date and time with offsets and
2. `DateTime::parse_from_str` parses a date and time with offsets and //! returns `DateTime<FixedOffset>`.
returns `DateTime<FixedOffset>`. //! This should be used when the offset is a part of input and the caller cannot guess that.
This should be used when the offset is a part of input and the caller cannot guess that. //! It *cannot* be used when the offset can be missing.
It *cannot* be used when the offset can be missing. //! `DateTime::parse_from_rfc{2822,3339}` are similar but for well-known formats.
`DateTime::parse_from_rfc{2822,3339}` are similar but for well-known formats. //!
//! 3. `Offset::datetime_from_str` is similar but returns `DateTime` of given offset.
3. `Offset::datetime_from_str` is similar but returns `DateTime` of given offset. //! When the explicit offset is missing from the input, it simply uses given offset.
When the explicit offset is missing from the input, it simply uses given offset. //! It issues an error when the input contains an explicit offset different
It issues an error when the input contains an explicit offset different from the current offset. //! from the current offset.
//!
More detailed control over the parsing process is available via `format` module. //! More detailed control over the parsing process is available via `format` module.
//!
~~~~ {.rust} //! ~~~~ {.rust}
use chrono::*; //! use chrono::*;
//!
let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9); //! let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600)); //! let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600));
//!
// method 1 //! // method 1
assert_eq!("2014-11-28T12:00:09Z".parse::<DateTime<UTC>>(), Ok(dt.clone())); //! assert_eq!("2014-11-28T12:00:09Z".parse::<DateTime<UTC>>(), Ok(dt.clone()));
assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<UTC>>(), Ok(dt.clone())); //! assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<UTC>>(), Ok(dt.clone()));
assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<FixedOffset>>(), Ok(fixed_dt.clone())); //! assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<FixedOffset>>(), Ok(fixed_dt.clone()));
//!
// method 2 //! // method 2
assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"), //! assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"),
Ok(fixed_dt.clone())); //! Ok(fixed_dt.clone()));
assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"), Ok(fixed_dt.clone())); //! assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone())); //! Ok(fixed_dt.clone()));
//! assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone()));
// method 3 //!
assert_eq!(UTC.datetime_from_str("2014-11-28 12:00:09", "%Y-%m-%d %H:%M:%S"), Ok(dt.clone())); //! // method 3
assert_eq!(UTC.datetime_from_str("Fri Nov 28 12:00:09 2014", "%a %b %e %T %Y"), Ok(dt.clone())); //! assert_eq!(UTC.datetime_from_str("2014-11-28 12:00:09", "%Y-%m-%d %H:%M:%S"), Ok(dt.clone()));
//! assert_eq!(UTC.datetime_from_str("Fri Nov 28 12:00:09 2014", "%a %b %e %T %Y"), Ok(dt.clone()));
// oops, the year is missing! //!
assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T %Y").is_err()); //! // oops, the year is missing!
// oops, the format string does not include the year at all! //! assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T %Y").is_err());
assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T").is_err()); //! // oops, the format string does not include the year at all!
// oops, the weekday is incorrect! //! assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T").is_err());
assert!(UTC.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_err()); //! // oops, the weekday is incorrect!
~~~~ //! assert!(UTC.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_err());
//! ~~~~
### Individual date //!
//! ### Individual date
Chrono also provides an individual date type (`Date`). //!
It also has time zones attached, and have to be constructed via time zones. //! Chrono also provides an individual date type (`Date`).
Most operations available to `DateTime` are also available to `Date` whenever appropriate. //! It also has time zones attached, and have to be constructed via time zones.
//! Most operations available to `DateTime` are also available to `Date` whenever appropriate.
~~~~ {.rust} //!
use chrono::*; //! ~~~~ {.rust}
//! use chrono::*;
# // these *may* fail, but only very rarely. just rerun the test if you were that unfortunate ;) //!
assert_eq!(UTC::today(), UTC::now().date()); //! # // these *may* fail, but only very rarely. just rerun the test if you were that unfortunate ;)
assert_eq!(Local::today(), Local::now().date()); //! assert_eq!(UTC::today(), UTC::now().date());
//! assert_eq!(Local::today(), Local::now().date());
assert_eq!(UTC.ymd(2014, 11, 28).weekday(), Weekday::Fri); //!
assert_eq!(UTC.ymd_opt(2014, 11, 31), LocalResult::None); //! assert_eq!(UTC.ymd(2014, 11, 28).weekday(), Weekday::Fri);
assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(), //! assert_eq!(UTC.ymd_opt(2014, 11, 31), LocalResult::None);
"070809"); //! assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(),
~~~~ //! "070809");
//! ~~~~
There is no timezone-aware `Time` due to the lack of usefulness and also the complexity. //!
//! There is no timezone-aware `Time` due to the lack of usefulness and also the complexity.
`DateTime` has `date` method which returns a `Date` which represents its date component. //!
There is also a `time` method, which simply returns a naive local time described below. //! `DateTime` has `date` method which returns a `Date` which represents its date component.
//! There is also a `time` method, which simply returns a naive local time described below.
### Naive date and time //!
//! ### Naive date and time
Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime` //!
as `NaiveDate`, `NaiveTime` and `NaiveDateTime` respectively. //! Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime`
//! as `NaiveDate`, `NaiveTime` and `NaiveDateTime` respectively.
They have almost equivalent interfaces as their timezone-aware twins, //!
but are not associated to time zones obviously and can be quite low-level. //! They have almost equivalent interfaces as their timezone-aware twins,
They are mostly useful for building blocks for higher-level types. //! but are not associated to time zones obviously and can be quite low-level.
//! They are mostly useful for building blocks for higher-level types.
Timezone-aware `DateTime` and `Date` types have two methods returning naive versions: //!
`naive_local` returns a view to the naive local time, //! Timezone-aware `DateTime` and `Date` types have two methods returning naive versions:
and `naive_utc` returns a view to the naive UTC time. //! `naive_local` returns a view to the naive local time,
//! and `naive_utc` returns a view to the naive UTC time.
## Limitations //!
//! ## Limitations
Only proleptic Gregorian calendar (i.e. extended to support older dates) is supported. //!
Be very careful if you really have to deal with pre-20C dates, they can be in Julian or others. //! Only proleptic Gregorian calendar (i.e. extended to support older dates) is supported.
//! Be very careful if you really have to deal with pre-20C dates, they can be in Julian or others.
Date types are limited in about +/- 262,000 years from the common epoch. //!
Time types are limited in the nanosecond accuracy. //! Date types are limited in about +/- 262,000 years from the common epoch.
//! Time types are limited in the nanosecond accuracy.
Leap seconds are supported in the representation but Chrono doesn't try to make use of them. //!
(The main reason is that leap seconds are not really predictable.) //! Leap seconds are supported in the representation but Chrono doesn't try to make use of them.
Almost *every* operation over the possible leap seconds will ignore them. //! (The main reason is that leap seconds are not really predictable.)
Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale if you want. //! Almost *every* operation over the possible leap seconds will ignore them.
//! Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale
Chrono inherently does not support an inaccurate or partial date and time representation. //! if you want.
Any operation that can be ambiguous will return `None` in such cases. //!
For example, "a month later" of 2014-01-30 is not well-defined //! Chrono inherently does not support an inaccurate or partial date and time representation.
and consequently `UTC.ymd(2014, 1, 30).with_month(2)` returns `None`. //! Any operation that can be ambiguous will return `None` in such cases.
//! For example, "a month later" of 2014-01-30 is not well-defined
Advanced time zone handling is not yet supported (but is planned in 0.3). //! and consequently `UTC.ymd(2014, 1, 30).with_month(2)` returns `None`.
//!
*/ //! Advanced time zone handling is not yet supported (but is planned in 0.3).
#![doc(html_root_url = "https://lifthrasiir.github.io/rust-chrono/")] #![doc(html_root_url = "https://lifthrasiir.github.io/rust-chrono/")]
@ -430,7 +430,6 @@ impl Weekday {
/// which equals to `Weekday::num_days_from_monday` in this implementation. /// which equals to `Weekday::num_days_from_monday` in this implementation.
/// Do not heavily depend on this though; use explicit methods whenever possible. /// Do not heavily depend on this though; use explicit methods whenever possible.
impl num::traits::FromPrimitive for Weekday { impl num::traits::FromPrimitive for Weekday {
#[inline] #[inline]
fn from_i64(n: i64) -> Option<Weekday> { fn from_i64(n: i64) -> Option<Weekday> {
match n { match n {
@ -565,7 +564,9 @@ pub trait Timelike: Sized {
fn hour12(&self) -> (bool, u32) { fn hour12(&self) -> (bool, u32) {
let hour = self.hour(); let hour = self.hour();
let mut hour12 = hour % 12; let mut hour12 = hour % 12;
if hour12 == 0 { hour12 = 12; } if hour12 == 0 {
hour12 = 12;
}
(hour >= 12, hour12) (hour >= 12, hour12)
} }
@ -612,20 +613,20 @@ fn test_readme_doomsday() {
for y in range_inclusive(naive::date::MIN.year(), naive::date::MAX.year()) { for y in range_inclusive(naive::date::MIN.year(), naive::date::MAX.year()) {
// even months // even months
let d4 = NaiveDate::from_ymd(y, 4, 4); let d4 = NaiveDate::from_ymd(y, 4, 4);
let d6 = NaiveDate::from_ymd(y, 6, 6); let d6 = NaiveDate::from_ymd(y, 6, 6);
let d8 = NaiveDate::from_ymd(y, 8, 8); let d8 = NaiveDate::from_ymd(y, 8, 8);
let d10 = NaiveDate::from_ymd(y, 10, 10); let d10 = NaiveDate::from_ymd(y, 10, 10);
let d12 = NaiveDate::from_ymd(y, 12, 12); let d12 = NaiveDate::from_ymd(y, 12, 12);
// nine to five, seven-eleven // nine to five, seven-eleven
let d59 = NaiveDate::from_ymd(y, 5, 9); let d59 = NaiveDate::from_ymd(y, 5, 9);
let d95 = NaiveDate::from_ymd(y, 9, 5); let d95 = NaiveDate::from_ymd(y, 9, 5);
let d711 = NaiveDate::from_ymd(y, 7, 11); let d711 = NaiveDate::from_ymd(y, 7, 11);
let d117 = NaiveDate::from_ymd(y, 11, 7); let d117 = NaiveDate::from_ymd(y, 11, 7);
// "March 0" // "March 0"
let d30 = NaiveDate::from_ymd(y, 3, 1).pred(); let d30 = NaiveDate::from_ymd(y, 3, 1).pred();
let weekday = d30.weekday(); let weekday = d30.weekday();
let other_dates = [d4, d6, d8, d10, d12, d59, d95, d711, d117]; let other_dates = [d4, d6, d8, d10, d12, d59, d95, d711, d117];