Flattened intermediate implementation modules.

There used to be multiple modules like `chrono::datetime` which only
provide a single type `DateTime`. In retrospect, this module structure
never reflected how people use those types; with the release of 0.3.0
`chrono::prelude` is a preferred way to glob-import types, and due to
reexports `chrono::DateTime` and likes are also common enough.

Therefore this commit removes those implementation modules and
flattens the module structure. Specifically:

    Before                              After
    ----------------------------------  ----------------------------
    chrono:πŸ“…:Date                  chrono::Date
    chrono:πŸ“…:MIN                   chrono::MIN_DATE
    chrono:πŸ“…:MAX                   chrono::MAX_DATE
    chrono::datetime::DateTime          chrono::DateTime
    chrono::datetime::TsSeconds         chrono::TsSeconds
    chrono::datetime::serde::*          chrono::serde::*
    chrono::naive::time::NaiveTime      chrono::naive::NaiveTime
    chrono::naive:πŸ“…:NaiveDate      chrono::naive::NaiveDate
    chrono::naive:πŸ“…:MIN            chrono::naive::MIN_DATE
    chrono::naive:πŸ“…:MAX            chrono::naive::MAX_DATE
    chrono::naive::datetime::NaiveDateTime
                                        chrono::naive::NaiveDateTime
    chrono::naive::datetime::TsSeconds  chrono::naive::TsSeconds
    chrono::naive::datetime::serde::*   chrono::naive::serde::*
    chrono::offset::utc::UTC            chrono::offset::UTC
    chrono::offset::fixed::FixedOffset  chrono::offset::FixedOffset
    chrono::offset::local::Local        chrono::offset::Local
    chrono::format::parsed::Parsed      chrono::format::Parsed

All internal documentation links have been updated (phew!) and
verified with LinkChecker [1]. Probably we can automate this check
in the future.

[1] https://wummel.github.io/linkchecker/

Closes #161. Compared to the original proposal, `chrono::naive` is
retained as we had `TsSeconds` types duplicated for `NaiveDateTime`
and `DateTime` (legitimately).
This commit is contained in:
Kang Seonghoon 2017-06-21 14:03:49 +09:00
parent 4be3962e31
commit c06bc01f0b
No known key found for this signature in database
GPG Key ID: 82440FABA6709020
15 changed files with 461 additions and 489 deletions

View File

@ -7,6 +7,7 @@ Ashley Mannix <ashleymannix@live.com.au>
Ben Boeckel <mathstuf@gmail.com> Ben Boeckel <mathstuf@gmail.com>
Ben Eills <ben@beneills.com> Ben Eills <ben@beneills.com>
Brandon W Maister <bwm@knewton.com> Brandon W Maister <bwm@knewton.com>
Brandon W Maister <quodlibetor@gmail.com>
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>
@ -19,6 +20,7 @@ Eric Findlay <e.findlay@protonmail.ch>
Eunchong Yu <kroisse@gmail.com> Eunchong Yu <kroisse@gmail.com>
Frans Skarman <frans.skarman@gmail.com> Frans Skarman <frans.skarman@gmail.com>
Huon Wilson <dbau.pp+github@gmail.com> Huon Wilson <dbau.pp+github@gmail.com>
Igor Gnatenko <ignatenko@redhat.com>
Jim Turner <jturner314@gmail.com> Jim Turner <jturner314@gmail.com>
Jisoo Park <xxxyel@gmail.com> Jisoo Park <xxxyel@gmail.com>
Joe Wilm <joe@jwilm.com> Joe Wilm <joe@jwilm.com>

View File

@ -90,7 +90,7 @@ methods.
### Date and Time ### Date and Time
Chrono provides a Chrono provides a
[**`DateTime`**](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html) [**`DateTime`**](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html)
type to represent a date and a time in a timezone. type to represent a date and a time in a timezone.
For more abstract moment-in-time tracking such as internal timekeeping For more abstract moment-in-time tracking such as internal timekeeping
@ -105,11 +105,11 @@ the [**`TimeZone`**](https://docs.rs/chrono/0.4.0/chrono/offset/trait.TimeZone.h
which defines how the local date is converted to and back from the UTC date. which defines how the local date is converted to and back from the UTC date.
There are three well-known `TimeZone` implementations: There are three well-known `TimeZone` implementations:
* [**`UTC`**](https://docs.rs/chrono/0.4.0/chrono/offset/utc/struct.UTC.html) specifies the UTC time zone. It is most efficient. * [**`UTC`**](https://docs.rs/chrono/0.4.0/chrono/offset/struct.UTC.html) specifies the UTC time zone. It is most efficient.
* [**`Local`**](https://docs.rs/chrono/0.4.0/chrono/offset/local/struct.Local.html) specifies the system local time zone. * [**`Local`**](https://docs.rs/chrono/0.4.0/chrono/offset/struct.Local.html) specifies the system local time zone.
* [**`FixedOffset`**](https://docs.rs/chrono/0.4.0/chrono/offset/fixed/struct.FixedOffset.html) specifies * [**`FixedOffset`**](https://docs.rs/chrono/0.4.0/chrono/offset/struct.FixedOffset.html) specifies
an arbitrary, fixed time zone such as UTC+09:00 or UTC-10:30. an arbitrary, fixed time zone such as UTC+09:00 or UTC-10:30.
This often results from the parsed textual date and time. This often results from the parsed textual date and time.
Since it stores the most information and does not depend on the system environment, Since it stores the most information and does not depend on the system environment,
@ -117,12 +117,12 @@ There are three well-known `TimeZone` implementations:
`DateTime`s with different `TimeZone` types are distinct and do not mix, `DateTime`s with different `TimeZone` types are distinct and do not mix,
but can be converted to each other using but can be converted to each other using
the [`DateTime::with_timezone`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.with_timezone) method. the [`DateTime::with_timezone`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.with_timezone) method.
You can get the current date and time in the UTC time zone You can get the current date and time in the UTC time zone
([`UTC::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/utc/struct.UTC.html#method.now)) ([`UTC::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/struct.UTC.html#method.now))
or in the local time zone or in the local time zone
([`Local::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/local/struct.Local.html#method.now)). ([`Local::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/struct.Local.html#method.now)).
```rust ```rust
use chrono::prelude::*; use chrono::prelude::*;
@ -205,14 +205,14 @@ assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_00
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`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.format) method, Formatting is done via the [`format`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.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](https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers) (See the [`format::strftime` module documentation](https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers)
for full syntax.) 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_rfc2822`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.to_rfc2822) and Chrono also provides [`to_rfc2822`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.to_rfc2822) and
[`to_rfc3339`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.to_rfc3339) methods [`to_rfc3339`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.to_rfc3339) methods
for well-known formats. for well-known formats.
```rust ```rust
@ -238,13 +238,13 @@ Parsing can be done with three methods:
([`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html)) ([`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html))
format specifier prints, and requires the offset to be present. format specifier prints, and requires the offset to be present.
2. [`DateTime::parse_from_str`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.parse_from_str) parses 2. [`DateTime::parse_from_str`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.parse_from_str) parses
a date and time with offsets and returns `DateTime<FixedOffset>`. a date and time with offsets and 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_rfc2822`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.parse_from_rfc2822) [`DateTime::parse_from_rfc2822`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.parse_from_rfc2822)
and and
[`DateTime::parse_from_rfc3339`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.parse_from_rfc3339) [`DateTime::parse_from_rfc3339`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.parse_from_rfc3339)
are similar but for well-known formats. are similar but for well-known formats.
3. [`Offset::datetime_from_str`](https://docs.rs/chrono/0.4.0/chrono/offset/trait.TimeZone.html#method.datetime_from_str) is 3. [`Offset::datetime_from_str`](https://docs.rs/chrono/0.4.0/chrono/offset/trait.TimeZone.html#method.datetime_from_str) is
@ -288,7 +288,7 @@ assert!(UTC.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_e
### Individual date ### Individual date
Chrono also provides an individual date type ([**`Date`**](https://docs.rs/chrono/0.4.0/chrono/date/struct.Date.html)). Chrono also provides an individual date type ([**`Date`**](https://docs.rs/chrono/0.4.0/chrono/struct.Date.html)).
It also has time zones attached, and have to be constructed via time zones. 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. Most operations available to `DateTime` are also available to `Date` whenever appropriate.
@ -307,26 +307,26 @@ assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_
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`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.date) method `DateTime` has [`date`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.date) method
which returns a `Date` which represents its date component. which returns a `Date` which represents its date component.
There is also a [`time`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.time) method, There is also a [`time`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.time) method,
which simply returns a naive local time described below. 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` Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime`
as [**`NaiveDate`**](https://docs.rs/chrono/0.4.0/chrono/naive/date/struct.NaiveDate.html), as [**`NaiveDate`**](https://docs.rs/chrono/0.4.0/chrono/naive/struct.NaiveDate.html),
[**`NaiveTime`**](https://docs.rs/chrono/0.4.0/chrono/naive/time/struct.NaiveTime.html) and [**`NaiveTime`**](https://docs.rs/chrono/0.4.0/chrono/naive/struct.NaiveTime.html) and
[**`NaiveDateTime`**](https://docs.rs/chrono/0.4.0/chrono/naive/datetime/struct.NaiveDateTime.html) respectively. [**`NaiveDateTime`**](https://docs.rs/chrono/0.4.0/chrono/naive/struct.NaiveDateTime.html) respectively.
They have almost equivalent interfaces as their timezone-aware twins, They have almost equivalent interfaces as their timezone-aware twins,
but are not associated to time zones obviously and can be quite low-level. 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. They are mostly useful for building blocks for higher-level types.
Timezone-aware `DateTime` and `Date` types have two methods returning naive versions: Timezone-aware `DateTime` and `Date` types have two methods returning naive versions:
[`naive_local`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.naive_local) returns [`naive_local`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.naive_local) returns
a view to the naive local time, a view to the naive local time,
and [`naive_utc`](https://docs.rs/chrono/0.4.0/chrono/datetime/struct.DateTime.html#method.naive_utc) returns and [`naive_utc`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.naive_utc) returns
a view to the naive UTC time. a view to the naive UTC time.
## Limitations ## Limitations
@ -338,7 +338,7 @@ Date types are limited in about +/- 262,000 years from the common epoch.
Time types are limited in the nanosecond accuracy. Time types are limited in the nanosecond accuracy.
[Leap seconds are supported in the representation but [Leap seconds are supported in the representation but
Chrono doesn't try to make use of them](https://docs.rs/chrono/0.4.0/chrono/naive/time/index.html#leap-second-handling). Chrono doesn't try to make use of them](https://docs.rs/chrono/0.4.0/chrono/naive/struct.NaiveTime.html#leap-second-handling).
(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 Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale

View File

@ -9,12 +9,9 @@ use std::ops::{Add, Sub};
use oldtime::Duration as OldDuration; use oldtime::Duration as OldDuration;
use {Weekday, Datelike}; use {Weekday, Datelike};
use offset::TimeZone; use offset::{TimeZone, UTC};
use offset::utc::UTC; use naive::{self, NaiveDate, NaiveTime};
use naive; use DateTime;
use naive::date::NaiveDate;
use naive::time::NaiveTime;
use datetime::DateTime;
use format::{Item, DelayedFormat, StrftimeItems}; use format::{Item, DelayedFormat, StrftimeItems};
/// ISO 8601 calendar date with time zone. /// ISO 8601 calendar date with time zone.
@ -48,9 +45,9 @@ pub struct Date<Tz: TimeZone> {
} }
/// The minimum possible `Date`. /// The minimum possible `Date`.
pub const MIN: Date<UTC> = Date { date: naive::date::MIN, offset: UTC }; pub const MIN_DATE: Date<UTC> = Date { date: naive::MIN_DATE, offset: UTC };
/// The maximum possible `Date`. /// The maximum possible `Date`.
pub const MAX: Date<UTC> = Date { date: naive::date::MAX, offset: UTC }; pub const MAX_DATE: Date<UTC> = Date { date: naive::MAX_DATE, offset: UTC };
impl<Tz: TimeZone> Date<Tz> { impl<Tz: TimeZone> Date<Tz> {
/// Makes a new `Date` with given *UTC* date and offset. /// Makes a new `Date` with given *UTC* date and offset.

View File

@ -11,13 +11,9 @@ use std::ops::Deref;
use oldtime::Duration as OldDuration; use oldtime::Duration as OldDuration;
use {Weekday, Timelike, Datelike}; use {Weekday, Timelike, Datelike};
use offset::{TimeZone, Offset}; use offset::{TimeZone, Offset, UTC, Local, FixedOffset};
use offset::utc::UTC; use naive::{NaiveTime, NaiveDateTime};
use offset::local::Local; use Date;
use offset::fixed::FixedOffset;
use naive::time::NaiveTime;
use naive::datetime::NaiveDateTime;
use date::Date;
use format::{Item, Numeric, Pad, Fixed}; use format::{Item, Numeric, Pad, Fixed};
use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
@ -510,10 +506,7 @@ fn test_decodable_json_timestamps<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
mod rustc_serialize { mod rustc_serialize {
use std::fmt; use std::fmt;
use super::{DateTime, TsSeconds}; use super::{DateTime, TsSeconds};
use offset::{TimeZone, LocalResult}; use offset::{TimeZone, LocalResult, UTC, Local, FixedOffset};
use offset::utc::UTC;
use offset::local::Local;
use offset::fixed::FixedOffset;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
impl<Tz: TimeZone> Encodable for DateTime<Tz> { impl<Tz: TimeZone> Encodable for DateTime<Tz> {
@ -609,11 +602,8 @@ mod rustc_serialize {
pub mod serde { pub mod serde {
use std::fmt; use std::fmt;
use super::DateTime; use super::DateTime;
use offset::{TimeZone, LocalResult}; use offset::{TimeZone, LocalResult, UTC, Local, FixedOffset};
use offset::utc::UTC; use serdelib::{ser, de};
use offset::local::Local;
use offset::fixed::FixedOffset;
use serde::{ser, de};
/// Ser/de to/from timestamps in seconds /// Ser/de to/from timestamps in seconds
/// ///
@ -630,7 +620,7 @@ pub mod serde {
/// # #[macro_use] extern crate serde_json; /// # #[macro_use] extern crate serde_json;
/// # extern crate chrono; /// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, UTC}; /// # use chrono::{TimeZone, DateTime, UTC};
/// use chrono::datetime::serde::ts_seconds; /// use chrono::serde::ts_seconds;
/// #[derive(Deserialize, Serialize)] /// #[derive(Deserialize, Serialize)]
/// struct S { /// struct S {
/// #[serde(with = "ts_seconds")] /// #[serde(with = "ts_seconds")]
@ -653,7 +643,7 @@ pub mod serde {
/// ``` /// ```
pub mod ts_seconds { pub mod ts_seconds {
use std::fmt; use std::fmt;
use serde::{ser, de}; use serdelib::{ser, de};
use {DateTime, UTC, FixedOffset}; use {DateTime, UTC, FixedOffset};
use offset::TimeZone; use offset::TimeZone;
@ -674,7 +664,7 @@ pub mod serde {
/// # #[macro_use] extern crate serde_json; /// # #[macro_use] extern crate serde_json;
/// # extern crate chrono; /// # extern crate chrono;
/// # use chrono::{DateTime, UTC}; /// # use chrono::{DateTime, UTC};
/// use chrono::datetime::serde::ts_seconds::deserialize as from_ts; /// use chrono::serde::ts_seconds::deserialize as from_ts;
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct S { /// struct S {
/// #[serde(deserialize_with = "from_ts")] /// #[serde(deserialize_with = "from_ts")]
@ -708,7 +698,7 @@ pub mod serde {
/// # #[macro_use] extern crate serde_json; /// # #[macro_use] extern crate serde_json;
/// # extern crate chrono; /// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, UTC}; /// # use chrono::{TimeZone, DateTime, UTC};
/// use chrono::datetime::serde::ts_seconds::serialize as to_ts; /// use chrono::serde::ts_seconds::serialize as to_ts;
/// #[derive(Serialize)] /// #[derive(Serialize)]
/// struct S { /// struct S {
/// #[serde(serialize_with = "to_ts")] /// #[serde(serialize_with = "to_ts")]
@ -882,12 +872,8 @@ pub mod serde {
mod tests { mod tests {
use super::DateTime; use super::DateTime;
use Datelike; use Datelike;
use naive::time::NaiveTime; use naive::{NaiveTime, NaiveDate};
use naive::date::NaiveDate; use offset::{TimeZone, UTC, Local, FixedOffset};
use offset::TimeZone;
use offset::utc::UTC;
use offset::local::Local;
use offset::fixed::FixedOffset;
use oldtime::Duration; use oldtime::Duration;
#[test] #[test]

View File

@ -9,10 +9,8 @@ use std::error::Error;
use {Datelike, Timelike, Weekday, ParseWeekdayError}; use {Datelike, Timelike, Weekday, ParseWeekdayError};
use div::{div_floor, mod_floor}; use div::{div_floor, mod_floor};
use offset::Offset; use offset::{Offset, FixedOffset};
use offset::fixed::FixedOffset; use naive::{NaiveDate, NaiveTime};
use naive::date::NaiveDate;
use naive::time::NaiveTime;
pub use self::strftime::StrftimeItems; pub use self::strftime::StrftimeItems;
pub use self::parsed::Parsed; pub use self::parsed::Parsed;
@ -175,14 +173,14 @@ pub enum Fixed {
/// ///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespaces. /// In the parser, the colon can be omitted and/or surrounded with any amount of whitespaces.
/// The offset is limited from `-24:00` to `+24:00`, /// The offset is limited from `-24:00` to `+24:00`,
/// which is same to [`FixedOffset`](../offset/fixed/struct.FixedOffset.html)'s range. /// which is same to [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
TimezoneOffsetColon, TimezoneOffsetColon,
/// Offset from the local time to UTC (`+09:00` or `-04:00` or `Z`). /// Offset from the local time to UTC (`+09:00` or `-04:00` or `Z`).
/// ///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespaces, /// In the parser, the colon can be omitted and/or surrounded with any amount of whitespaces,
/// and `Z` can be either in upper case or in lower case. /// and `Z` can be either in upper case or in lower case.
/// The offset is limited from `-24:00` to `+24:00`, /// The offset is limited from `-24:00` to `+24:00`,
/// which is same to [`FixedOffset`](../offset/fixed/struct.FixedOffset.html)'s range. /// which is same to [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
TimezoneOffsetColonZ, TimezoneOffsetColonZ,
/// Same to [`TimezoneOffsetColon`](#variant.TimezoneOffsetColon) but prints no colon. /// Same to [`TimezoneOffsetColon`](#variant.TimezoneOffsetColon) but prints no colon.
/// Parsing allows an optional colon. /// Parsing allows an optional colon.
@ -515,7 +513,7 @@ pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Opt
Ok(()) Ok(())
} }
pub mod parsed; mod parsed;
// due to the size of parsing routines, they are in separate modules. // due to the size of parsing routines, they are in separate modules.
mod scan; mod scan;

View File

@ -603,8 +603,8 @@ fn test_parse() {
#[cfg(test)] #[cfg(test)]
#[test] #[test]
fn test_rfc2822() { fn test_rfc2822() {
use datetime::DateTime; use DateTime;
use offset::fixed::FixedOffset; use offset::FixedOffset;
use super::*; use super::*;
use super::NOT_ENOUGH; use super::NOT_ENOUGH;
@ -687,8 +687,8 @@ fn parse_rfc850() {
#[cfg(test)] #[cfg(test)]
#[test] #[test]
fn test_rfc3339() { fn test_rfc3339() {
use datetime::DateTime; use DateTime;
use offset::fixed::FixedOffset; use offset::FixedOffset;
use super::*; use super::*;
// Test data - (input, Ok(expected result after parse and format) or Err(error code)) // Test data - (input, Ok(expected result after parse and format) or Err(error code))

View File

@ -10,12 +10,9 @@ use oldtime::Duration as OldDuration;
use {Datelike, Timelike}; use {Datelike, Timelike};
use Weekday; use Weekday;
use div::div_rem; use div::div_rem;
use offset::{TimeZone, Offset, LocalResult}; use offset::{TimeZone, Offset, LocalResult, FixedOffset};
use offset::fixed::FixedOffset; use naive::{NaiveDate, NaiveTime, NaiveDateTime};
use naive::date::NaiveDate; use DateTime;
use naive::time::NaiveTime;
use naive::datetime::NaiveDateTime;
use datetime::DateTime;
use super::{ParseResult, OUT_OF_RANGE, IMPOSSIBLE, NOT_ENOUGH}; use super::{ParseResult, OUT_OF_RANGE, IMPOSSIBLE, NOT_ENOUGH};
/// Parsed parts of date and time. There are two classes of methods: /// Parsed parts of date and time. There are two classes of methods:
@ -641,11 +638,8 @@ mod tests {
use super::super::{OUT_OF_RANGE, IMPOSSIBLE, NOT_ENOUGH}; use super::super::{OUT_OF_RANGE, IMPOSSIBLE, NOT_ENOUGH};
use Datelike; use Datelike;
use Weekday::*; use Weekday::*;
use naive::date::{self, NaiveDate}; use naive::{MIN_DATE, MAX_DATE, NaiveDate, NaiveTime};
use naive::time::NaiveTime; use offset::{TimeZone, UTC, FixedOffset};
use offset::TimeZone;
use offset::utc::UTC;
use offset::fixed::FixedOffset;
#[test] #[test]
fn test_parsed_set_fields() { fn test_parsed_set_fields() {
@ -764,7 +758,7 @@ mod tests {
ymd(0, 1, 1)); ymd(0, 1, 1));
assert_eq!(parse!(year_div_100: -1, year_mod_100: 42, month: 1, day: 1), assert_eq!(parse!(year_div_100: -1, year_mod_100: 42, month: 1, day: 1),
Err(OUT_OF_RANGE)); Err(OUT_OF_RANGE));
let max_year = date::MAX.year(); let max_year = MAX_DATE.year();
assert_eq!(parse!(year_div_100: max_year / 100, assert_eq!(parse!(year_div_100: max_year / 100,
year_mod_100: max_year % 100, month: 1, day: 1), year_mod_100: max_year % 100, month: 1, day: 1),
ymd(max_year, 1, 1)); ymd(max_year, 1, 1));
@ -963,17 +957,17 @@ mod tests {
// more timestamps // more timestamps
let max_days_from_year_1970 = let max_days_from_year_1970 =
date::MAX.signed_duration_since(NaiveDate::from_ymd(1970,1,1)); MAX_DATE.signed_duration_since(NaiveDate::from_ymd(1970,1,1));
let year_0_from_year_1970 = let year_0_from_year_1970 =
NaiveDate::from_ymd(0,1,1).signed_duration_since(NaiveDate::from_ymd(1970,1,1)); NaiveDate::from_ymd(0,1,1).signed_duration_since(NaiveDate::from_ymd(1970,1,1));
let min_days_from_year_1970 = let min_days_from_year_1970 =
date::MIN.signed_duration_since(NaiveDate::from_ymd(1970,1,1)); MIN_DATE.signed_duration_since(NaiveDate::from_ymd(1970,1,1));
assert_eq!(parse!(timestamp: min_days_from_year_1970.num_seconds()), assert_eq!(parse!(timestamp: min_days_from_year_1970.num_seconds()),
ymdhms(date::MIN.year(),1,1, 0,0,0)); ymdhms(MIN_DATE.year(),1,1, 0,0,0));
assert_eq!(parse!(timestamp: year_0_from_year_1970.num_seconds()), assert_eq!(parse!(timestamp: year_0_from_year_1970.num_seconds()),
ymdhms(0,1,1, 0,0,0)); ymdhms(0,1,1, 0,0,0));
assert_eq!(parse!(timestamp: max_days_from_year_1970.num_seconds() + 86399), assert_eq!(parse!(timestamp: max_days_from_year_1970.num_seconds() + 86399),
ymdhms(date::MAX.year(),12,31, 23,59,59)); ymdhms(MAX_DATE.year(),12,31, 23,59,59));
// leap seconds #1: partial fields // leap seconds #1: partial fields
assert_eq!(parse!(second: 59, timestamp: 1_341_100_798), Err(IMPOSSIBLE)); assert_eq!(parse!(second: 59, timestamp: 1_341_100_798), Err(IMPOSSIBLE));

View File

@ -76,7 +76,7 @@
//! ### Date and Time //! ### Date and Time
//! //!
//! Chrono provides a //! Chrono provides a
//! [**`DateTime`**](./datetime/struct.DateTime.html) //! [**`DateTime`**](./struct.DateTime.html)
//! type to represent a date and a time in a timezone. //! type to represent a date and a time in a timezone.
//! //!
//! For more abstract moment-in-time tracking such as internal timekeeping //! For more abstract moment-in-time tracking such as internal timekeeping
@ -91,11 +91,11 @@
//! which defines how the local date is converted to and back from the UTC date. //! which defines how the local date is converted to and back from the UTC date.
//! There are three well-known `TimeZone` implementations: //! There are three well-known `TimeZone` implementations:
//! //!
//! * [**`UTC`**](./offset/utc/struct.UTC.html) specifies the UTC time zone. It is most efficient. //! * [**`UTC`**](./offset/struct.UTC.html) specifies the UTC time zone. It is most efficient.
//! //!
//! * [**`Local`**](./offset/local/struct.Local.html) specifies the system local time zone. //! * [**`Local`**](./offset/struct.Local.html) specifies the system local time zone.
//! //!
//! * [**`FixedOffset`**](./offset/fixed/struct.FixedOffset.html) specifies //! * [**`FixedOffset`**](./offset/struct.FixedOffset.html) specifies
//! an arbitrary, fixed time zone such as UTC+09:00 or UTC-10:30. //! an arbitrary, fixed time zone such as UTC+09:00 or UTC-10:30.
//! This often results from the parsed textual date and time. //! This often results from the parsed textual date and time.
//! Since it stores the most information and does not depend on the system environment, //! Since it stores the most information and does not depend on the system environment,
@ -103,12 +103,12 @@
//! //!
//! `DateTime`s with different `TimeZone` types are distinct and do not mix, //! `DateTime`s with different `TimeZone` types are distinct and do not mix,
//! but can be converted to each other using //! but can be converted to each other using
//! the [`DateTime::with_timezone`](./datetime/struct.DateTime.html#method.with_timezone) method. //! the [`DateTime::with_timezone`](./struct.DateTime.html#method.with_timezone) method.
//! //!
//! You can get the current date and time in the UTC time zone //! You can get the current date and time in the UTC time zone
//! ([`UTC::now()`](./offset/utc/struct.UTC.html#method.now)) //! ([`UTC::now()`](./offset/struct.UTC.html#method.now))
//! or in the local time zone //! or in the local time zone
//! ([`Local::now()`](./offset/local/struct.Local.html#method.now)). //! ([`Local::now()`](./offset/struct.Local.html#method.now)).
//! //!
//! ```rust //! ```rust
//! use chrono::prelude::*; //! use chrono::prelude::*;
@ -198,14 +198,14 @@
//! # } //! # }
//! ``` //! ```
//! //!
//! Formatting is done via the [`format`](./datetime/struct.DateTime.html#method.format) method, //! Formatting is done via the [`format`](./struct.DateTime.html#method.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](./format/strftime/index.html#specifiers) //! (See the [`format::strftime` module documentation](./format/strftime/index.html#specifiers)
//! for full syntax.) //! 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_rfc2822`](./datetime/struct.DateTime.html#method.to_rfc2822) and //! Chrono also provides [`to_rfc2822`](./struct.DateTime.html#method.to_rfc2822) and
//! [`to_rfc3339`](./datetime/struct.DateTime.html#method.to_rfc3339) methods //! [`to_rfc3339`](./struct.DateTime.html#method.to_rfc3339) methods
//! for well-known formats. //! for well-known formats.
//! //!
//! ```rust //! ```rust
@ -231,13 +231,13 @@
//! ([`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html)) //! ([`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html))
//! format specifier prints, and requires the offset to be present. //! format specifier prints, and requires the offset to be present.
//! //!
//! 2. [`DateTime::parse_from_str`](./datetime/struct.DateTime.html#method.parse_from_str) parses //! 2. [`DateTime::parse_from_str`](./struct.DateTime.html#method.parse_from_str) parses
//! a date and time with offsets and returns `DateTime<FixedOffset>`. //! a date and time with offsets and 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_rfc2822`](./datetime/struct.DateTime.html#method.parse_from_rfc2822) //! [`DateTime::parse_from_rfc2822`](./struct.DateTime.html#method.parse_from_rfc2822)
//! and //! and
//! [`DateTime::parse_from_rfc3339`](./datetime/struct.DateTime.html#method.parse_from_rfc3339) //! [`DateTime::parse_from_rfc3339`](./struct.DateTime.html#method.parse_from_rfc3339)
//! are similar but for well-known formats. //! are similar but for well-known formats.
//! //!
//! 3. [`Offset::datetime_from_str`](./offset/trait.TimeZone.html#method.datetime_from_str) is //! 3. [`Offset::datetime_from_str`](./offset/trait.TimeZone.html#method.datetime_from_str) is
@ -281,7 +281,7 @@
//! //!
//! ### Individual date //! ### Individual date
//! //!
//! Chrono also provides an individual date type ([**`Date`**](./date/struct.Date.html)). //! Chrono also provides an individual date type ([**`Date`**](./struct.Date.html)).
//! It also has time zones attached, and have to be constructed via time zones. //! 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. //! Most operations available to `DateTime` are also available to `Date` whenever appropriate.
//! //!
@ -301,26 +301,26 @@
//! //!
//! 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`](./datetime/struct.DateTime.html#method.date) method //! `DateTime` has [`date`](./struct.DateTime.html#method.date) method
//! which returns a `Date` which represents its date component. //! which returns a `Date` which represents its date component.
//! There is also a [`time`](./datetime/struct.DateTime.html#method.time) method, //! There is also a [`time`](./struct.DateTime.html#method.time) method,
//! which simply returns a naive local time described below. //! 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` //! Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime`
//! as [**`NaiveDate`**](./naive/date/struct.NaiveDate.html), //! as [**`NaiveDate`**](./naive/struct.NaiveDate.html),
//! [**`NaiveTime`**](./naive/time/struct.NaiveTime.html) and //! [**`NaiveTime`**](./naive/struct.NaiveTime.html) and
//! [**`NaiveDateTime`**](./naive/datetime/struct.NaiveDateTime.html) respectively. //! [**`NaiveDateTime`**](./naive/struct.NaiveDateTime.html) respectively.
//! //!
//! They have almost equivalent interfaces as their timezone-aware twins, //! They have almost equivalent interfaces as their timezone-aware twins,
//! but are not associated to time zones obviously and can be quite low-level. //! 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. //! They are mostly useful for building blocks for higher-level types.
//! //!
//! Timezone-aware `DateTime` and `Date` types have two methods returning naive versions: //! Timezone-aware `DateTime` and `Date` types have two methods returning naive versions:
//! [`naive_local`](./datetime/struct.DateTime.html#method.naive_local) returns //! [`naive_local`](./struct.DateTime.html#method.naive_local) returns
//! a view to the naive local time, //! a view to the naive local time,
//! and [`naive_utc`](./datetime/struct.DateTime.html#method.naive_utc) returns //! and [`naive_utc`](./struct.DateTime.html#method.naive_utc) returns
//! a view to the naive UTC time. //! a view to the naive UTC time.
//! //!
//! ## Limitations //! ## Limitations
@ -332,7 +332,7 @@
//! Time types are limited in the nanosecond accuracy. //! Time types are limited in the nanosecond accuracy.
//! //!
//! [Leap seconds are supported in the representation but //! [Leap seconds are supported in the representation but
//! Chrono doesn't try to make use of them](./naive/time/index.html#leap-second-handling). //! Chrono doesn't try to make use of them](./naive/struct.NaiveTime.html#leap-second-handling).
//! (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 //! Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale
@ -356,34 +356,26 @@ extern crate num;
#[cfg(feature = "rustc-serialize")] #[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize; extern crate rustc_serialize;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
extern crate serde; extern crate serde as serdelib;
// this reexport is to aid the transition and should not be in the prelude! // this reexport is to aid the transition and should not be in the prelude!
pub use oldtime::Duration; pub use oldtime::Duration;
pub use offset::{TimeZone, Offset, LocalResult}; #[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, UTC, FixedOffset, Local};
pub use offset::utc::UTC; #[doc(no_inline)] pub use naive::{NaiveDate, NaiveTime, NaiveDateTime};
pub use offset::fixed::FixedOffset; pub use date_::{Date, MIN_DATE, MAX_DATE};
pub use offset::local::Local; pub use datetime_::DateTime;
pub use naive::date::NaiveDate; #[cfg(feature = "rustc-serialize")] pub use datetime_::TsSeconds;
pub use naive::time::NaiveTime;
pub use naive::datetime::NaiveDateTime;
pub use date::Date;
pub use datetime::DateTime;
pub use format::{ParseError, ParseResult}; pub use format::{ParseError, ParseResult};
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`). /// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
pub mod prelude { pub mod prelude {
pub use {Datelike, Timelike, Weekday}; #[doc(no_inline)] pub use {Datelike, Timelike, Weekday};
pub use offset::{TimeZone, Offset}; #[doc(no_inline)] pub use {TimeZone, Offset};
pub use offset::utc::UTC; #[doc(no_inline)] pub use {UTC, FixedOffset, Local};
pub use offset::fixed::FixedOffset; #[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime};
pub use offset::local::Local; #[doc(no_inline)] pub use Date;
pub use naive::date::NaiveDate; #[doc(no_inline)] pub use DateTime;
pub use naive::time::NaiveTime;
pub use naive::datetime::NaiveDateTime;
pub use date::Date;
pub use datetime::DateTime;
} }
// useful throughout the codebase // useful throughout the codebase
@ -399,14 +391,35 @@ pub mod naive {
//! They are primarily building blocks for other types //! They are primarily building blocks for other types
//! (e.g. [`TimeZone`](../offset/trait.TimeZone.html)), //! (e.g. [`TimeZone`](../offset/trait.TimeZone.html)),
//! but can be also used for the simpler date and time handling. //! but can be also used for the simpler date and time handling.
pub mod date;
pub mod time; // avoid using them directly even in the crate itself
pub mod datetime; #[path = "date.rs"] mod date_;
#[path = "time.rs"] mod time_;
#[path = "datetime.rs"] mod datetime_;
pub use self::date_::{NaiveDate, MIN_DATE, MAX_DATE};
pub use self::time_::NaiveTime;
pub use self::datetime_::{NaiveDateTime, TsSeconds};
/// Tools to help serializing/deserializing naive types.
#[cfg(feature = "serde")]
pub mod serde {
pub use super::datetime_::serde::*;
} }
pub mod date; }
pub mod datetime; #[path = "date.rs"] mod date_;
#[path = "datetime.rs"] mod datetime_;
pub mod format; pub mod format;
/// Ser/de helpers
///
/// The various modules in here are intended to be used with serde's [`with`
/// annotation](https://serde.rs/attributes.html#field-attributes).
#[cfg(feature = "serde")]
pub mod serde {
pub use super::datetime_::serde::*;
}
/// The day of week. /// The day of week.
/// ///
/// The order of the days of week depends on the context. /// The order of the days of week depends on the context.
@ -594,7 +607,7 @@ impl fmt::Debug for ParseWeekdayError {
mod weekday_serde { mod weekday_serde {
use super::Weekday; use super::Weekday;
use std::fmt; use std::fmt;
use serde::{ser, de}; use serdelib::{ser, de};
impl ser::Serialize for Weekday { impl ser::Serialize for Weekday {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@ -698,7 +711,7 @@ mod weekday_serde {
/// The common set of methods for date component. /// The common set of methods for date component.
pub trait Datelike: Sized { pub trait Datelike: Sized {
/// Returns the year number in the [calendar date](./naive/date/index.html#calendar-date). /// Returns the year number in the [calendar date](./naive/struct.NaiveDate.html#calendar-date).
fn year(&self) -> i32; fn year(&self) -> i32;
/// Returns the absolute year number starting from 1 with a boolean flag, /// Returns the absolute year number starting from 1 with a boolean flag,
@ -826,7 +839,7 @@ pub trait Timelike: Sized {
/// Returns the number of nanoseconds since the whole non-leap second. /// Returns the number of nanoseconds since the whole non-leap second.
/// The range from 1,000,000,000 to 1,999,999,999 represents /// The range from 1,000,000,000 to 1,999,999,999 represents
/// the [leap second](./naive/time/index.html#leap-second-handling). /// the [leap second](./naive/struct.NaiveTime.html#leap-second-handling).
fn nanosecond(&self) -> u32; fn nanosecond(&self) -> u32;
/// Makes a new value with the hour number changed. /// Makes a new value with the hour number changed.
@ -864,7 +877,7 @@ pub trait Timelike: Sized {
fn test_readme_doomsday() { fn test_readme_doomsday() {
use num::iter::range_inclusive; use num::iter::range_inclusive;
for y in range_inclusive(naive::date::MIN.year(), naive::date::MAX.year()) { for y in range_inclusive(naive::MIN_DATE.year(), naive::MAX_DATE.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);

View File

@ -2,49 +2,6 @@
// See README.md and LICENSE.txt for details. // See README.md and LICENSE.txt for details.
//! ISO 8601 calendar date without timezone. //! ISO 8601 calendar date without timezone.
//!
//! # Calendar Date
//!
//! The ISO 8601 **calendar date** follows the proleptic Gregorian calendar.
//! It is like a normal civil calendar but note some slight differences:
//!
//! * Dates before the Gregorian calendar's inception in 1582 are defined via the extrapolation.
//! Be careful, as historical dates are often noted in the Julian calendar and others
//! and the transition to Gregorian may differ across countries (as late as early 20C).
//!
//! (Some example: Both Shakespeare from Britain and Cervantes from Spain seemingly died
//! on the same calendar date---April 23, 1616---but in the different calendar.
//! Britain used the Julian calendar at that time, so Shakespeare's death is later.)
//!
//! * ISO 8601 calendars has the year 0, which is 1 BCE (a year before 1 CE).
//! If you need a typical BCE/BC and CE/AD notation for year numbers,
//! use the [`Datelike::year_ce`](../../trait.Datelike.html#method.year_ce) method.
//!
//! # Week Date
//!
//! The ISO 8601 **week date** is a triple of year number, week number
//! and [day of the week](../../enum.Weekday.html) with the following rules:
//!
//! * A week consists of Monday through Sunday, and is always numbered within some year.
//! The week number ranges from 1 to 52 or 53 depending on the year.
//!
//! * The week 1 of given year is defined as the first week containing January 4 of that year,
//! or equivalently, the first week containing four or more days in that year.
//!
//! * The year number in the week date may *not* correspond to the actual Gregorian year.
//! For example, January 3, 2016 (Sunday) was on the last (53rd) week of 2015.
//!
//! Chrono's date types default to the ISO 8601 [calendar date](#calendar-date),
//! but the [`Datelike::isoweekdate`](../../trait.Datelike.html#tymethod.isoweekdate) method
//! can be used to get the corresponding week date.
//!
//! # Ordinal Date
//!
//! The ISO 8601 **ordinal date** is a pair of year number and day of the year ("ordinal").
//! The ordinal number ranges from 1 to 365 or 366 depending on the year.
//! The year number is same to that of the [calendar date](#calendar-date).
//!
//! This is currently the internal format of Chrono's date types.
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
@ -53,8 +10,7 @@ use oldtime::Duration as OldDuration;
use {Weekday, Datelike}; use {Weekday, Datelike};
use div::div_mod_floor; use div::div_mod_floor;
use naive::time::NaiveTime; use naive::{NaiveTime, NaiveDateTime};
use naive::datetime::NaiveDateTime;
use format::{Item, Numeric, Pad}; use format::{Item, Numeric, Pad};
use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
@ -89,32 +45,76 @@ const MIN_DAYS_FROM_YEAR_0: i32 = (MIN_YEAR + 400_000) * 365 +
const MAX_BITS: usize = 44; const MAX_BITS: usize = 44;
/// ISO 8601 calendar date without timezone. /// ISO 8601 calendar date without timezone.
/// Allows for every [proleptic Gregorian date](./index.html#calendar-date) /// Allows for every [proleptic Gregorian date](#calendar-date)
/// from Jan 1, 262145 BCE to Dec 31, 262143 CE. /// from Jan 1, 262145 BCE to Dec 31, 262143 CE.
/// Also supports the conversion from ISO 8601 ordinal and week date. /// Also supports the conversion from ISO 8601 ordinal and week date.
///
/// # Calendar Date
///
/// The ISO 8601 **calendar date** follows the proleptic Gregorian calendar.
/// It is like a normal civil calendar but note some slight differences:
///
/// * Dates before the Gregorian calendar's inception in 1582 are defined via the extrapolation.
/// Be careful, as historical dates are often noted in the Julian calendar and others
/// and the transition to Gregorian may differ across countries (as late as early 20C).
///
/// (Some example: Both Shakespeare from Britain and Cervantes from Spain seemingly died
/// on the same calendar date---April 23, 1616---but in the different calendar.
/// Britain used the Julian calendar at that time, so Shakespeare's death is later.)
///
/// * ISO 8601 calendars has the year 0, which is 1 BCE (a year before 1 CE).
/// If you need a typical BCE/BC and CE/AD notation for year numbers,
/// use the [`Datelike::year_ce`](../../trait.Datelike.html#method.year_ce) method.
///
/// # Week Date
///
/// The ISO 8601 **week date** is a triple of year number, week number
/// and [day of the week](../../enum.Weekday.html) with the following rules:
///
/// * A week consists of Monday through Sunday, and is always numbered within some year.
/// The week number ranges from 1 to 52 or 53 depending on the year.
///
/// * The week 1 of given year is defined as the first week containing January 4 of that year,
/// or equivalently, the first week containing four or more days in that year.
///
/// * The year number in the week date may *not* correspond to the actual Gregorian year.
/// For example, January 3, 2016 (Sunday) was on the last (53rd) week of 2015.
///
/// Chrono's date types default to the ISO 8601 [calendar date](#calendar-date),
/// but the [`Datelike::isoweekdate`](../../trait.Datelike.html#tymethod.isoweekdate) method
/// can be used to get the corresponding week date.
///
/// # Ordinal Date
///
/// The ISO 8601 **ordinal date** is a pair of year number and day of the year ("ordinal").
/// The ordinal number ranges from 1 to 365 or 366 depending on the year.
/// The year number is same to that of the [calendar date](#calendar-date).
///
/// This is currently the internal format of Chrono's date types.
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub struct NaiveDate { pub struct NaiveDate {
ymdf: DateImpl, // (year << 13) | of ymdf: DateImpl, // (year << 13) | of
} }
/// The minimum possible `NaiveDate` (January 1, 262145 BCE). /// The minimum possible `NaiveDate` (January 1, 262145 BCE).
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ }; pub const MIN_DATE: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ };
/// The maximum possible `NaiveDate` (December 31, 262143 CE). /// The maximum possible `NaiveDate` (December 31, 262143 CE).
pub const MAX: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o17 /*F*/ }; pub const MAX_DATE: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o17 /*F*/ };
// as it is hard to verify year flags in `MIN` and `MAX`, we use a separate run-time test. // as it is hard to verify year flags in `MIN_DATE` and `MAX_DATE`,
// we use a separate run-time test.
#[test] #[test]
fn test_date_bounds() { fn test_date_bounds() {
let calculated_min = NaiveDate::from_ymd(MIN_YEAR, 1, 1); let calculated_min = NaiveDate::from_ymd(MIN_YEAR, 1, 1);
let calculated_max = NaiveDate::from_ymd(MAX_YEAR, 12, 31); let calculated_max = NaiveDate::from_ymd(MAX_YEAR, 12, 31);
assert!(MIN == calculated_min, assert!(MIN_DATE == calculated_min,
"`MIN` should have a year flag {:?}", calculated_min.of().flags()); "`MIN_DATE` should have a year flag {:?}", calculated_min.of().flags());
assert!(MAX == calculated_max, assert!(MAX_DATE == calculated_max,
"`MAX` should have a year flag {:?}", calculated_max.of().flags()); "`MAX_DATE` should have a year flag {:?}", calculated_max.of().flags());
// let's also check that the entire range do not exceed 2^44 seconds // let's also check that the entire range do not exceed 2^44 seconds
// (sometimes used for bounding `Duration` against overflow) // (sometimes used for bounding `Duration` against overflow)
let maxsecs = MAX.signed_duration_since(MIN).num_seconds(); let maxsecs = MAX_DATE.signed_duration_since(MIN_DATE).num_seconds();
let maxsecs = maxsecs + 86401; // also take care of DateTime let maxsecs = maxsecs + 86401; // also take care of DateTime
assert!(maxsecs < (1 << MAX_BITS), assert!(maxsecs < (1 << MAX_BITS),
"The entire `NaiveDate` range somehow exceeds 2^{} seconds", MAX_BITS); "The entire `NaiveDate` range somehow exceeds 2^{} seconds", MAX_BITS);
@ -136,7 +136,7 @@ impl NaiveDate {
NaiveDate::from_of(year, mdf.to_of()) NaiveDate::from_of(year, mdf.to_of())
} }
/// Makes a new `NaiveDate` from the [calendar date](./index.html#calendar-date) /// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
/// (year, month and day). /// (year, month and day).
/// ///
/// Panics on the out-of-range date, invalid month and/or day. /// Panics on the out-of-range date, invalid month and/or day.
@ -158,7 +158,7 @@ impl NaiveDate {
NaiveDate::from_ymd_opt(year, month, day).expect("invalid or out-of-range date") NaiveDate::from_ymd_opt(year, month, day).expect("invalid or out-of-range date")
} }
/// Makes a new `NaiveDate` from the [calendar date](./index.html#calendar-date) /// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
/// (year, month and day). /// (year, month and day).
/// ///
/// Returns `None` on the out-of-range date, invalid month and/or day. /// Returns `None` on the out-of-range date, invalid month and/or day.
@ -182,7 +182,7 @@ impl NaiveDate {
NaiveDate::from_mdf(year, Mdf::new(month, day, flags)) NaiveDate::from_mdf(year, Mdf::new(month, day, flags))
} }
/// Makes a new `NaiveDate` from the [ordinal date](./index.html#ordinal-date) /// Makes a new `NaiveDate` from the [ordinal date](#ordinal-date)
/// (year and day of the year). /// (year and day of the year).
/// ///
/// Panics on the out-of-range date and/or invalid day of year. /// Panics on the out-of-range date and/or invalid day of year.
@ -204,7 +204,7 @@ impl NaiveDate {
NaiveDate::from_yo_opt(year, ordinal).expect("invalid or out-of-range date") NaiveDate::from_yo_opt(year, ordinal).expect("invalid or out-of-range date")
} }
/// Makes a new `NaiveDate` from the [ordinal date](./index.html#ordinal-date) /// Makes a new `NaiveDate` from the [ordinal date](#ordinal-date)
/// (year and day of the year). /// (year and day of the year).
/// ///
/// Returns `None` on the out-of-range date and/or invalid day of year. /// Returns `None` on the out-of-range date and/or invalid day of year.
@ -229,7 +229,7 @@ impl NaiveDate {
NaiveDate::from_of(year, Of::new(ordinal, flags)) NaiveDate::from_of(year, Of::new(ordinal, flags))
} }
/// Makes a new `NaiveDate` from the [ISO week date](./index.html#week-date) /// Makes a new `NaiveDate` from the [ISO week date](#week-date)
/// (year, week number and day of the week). /// (year, week number and day of the week).
/// The resulting `NaiveDate` may have a different year from the input year. /// The resulting `NaiveDate` may have a different year from the input year.
/// ///
@ -252,7 +252,7 @@ impl NaiveDate {
NaiveDate::from_isoywd_opt(year, week, weekday).expect("invalid or out-of-range date") NaiveDate::from_isoywd_opt(year, week, weekday).expect("invalid or out-of-range date")
} }
/// Makes a new `NaiveDate` from the [ISO week date](./index.html#week-date) /// Makes a new `NaiveDate` from the [ISO week date](#week-date)
/// (year, week number and day of the week). /// (year, week number and day of the week).
/// The resulting `NaiveDate` may have a different year from the input year. /// The resulting `NaiveDate` may have a different year from the input year.
/// ///
@ -466,7 +466,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute and second. /// Makes a new `NaiveDateTime` from the current date, hour, minute and second.
/// ///
/// No [leap second](../time/index.html#leap-second-handling) is allowed here; /// No [leap second](./struct.NaiveTime.html#leap-second-handling) is allowed here;
/// use `NaiveDate::and_hms_*` methods with a subsecond parameter instead. /// use `NaiveDate::and_hms_*` methods with a subsecond parameter instead.
/// ///
/// Panics on invalid hour, minute and/or second. /// Panics on invalid hour, minute and/or second.
@ -490,7 +490,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute and second. /// Makes a new `NaiveDateTime` from the current date, hour, minute and second.
/// ///
/// No [leap second](../time/index.html#leap-second-handling) is allowed here; /// No [leap second](./struct.NaiveTime.html#leap-second-handling) is allowed here;
/// use `NaiveDate::and_hms_*_opt` methods with a subsecond parameter instead. /// use `NaiveDate::and_hms_*_opt` methods with a subsecond parameter instead.
/// ///
/// Returns `None` on invalid hour, minute and/or second. /// Returns `None` on invalid hour, minute and/or second.
@ -514,7 +514,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond. /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
/// ///
/// The millisecond part can exceed 1,000 /// The millisecond part can exceed 1,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// Panics on invalid hour, minute, second and/or millisecond. /// Panics on invalid hour, minute, second and/or millisecond.
/// ///
@ -539,7 +539,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond. /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
/// ///
/// The millisecond part can exceed 1,000 /// The millisecond part can exceed 1,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// Returns `None` on invalid hour, minute, second and/or millisecond. /// Returns `None` on invalid hour, minute, second and/or millisecond.
/// ///
@ -565,7 +565,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond. /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
/// ///
/// The microsecond part can exceed 1,000,000 /// The microsecond part can exceed 1,000,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// Panics on invalid hour, minute, second and/or microsecond. /// Panics on invalid hour, minute, second and/or microsecond.
/// ///
@ -590,7 +590,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond. /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
/// ///
/// The microsecond part can exceed 1,000,000 /// The microsecond part can exceed 1,000,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// Returns `None` on invalid hour, minute, second and/or microsecond. /// Returns `None` on invalid hour, minute, second and/or microsecond.
/// ///
@ -616,7 +616,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond. /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
/// ///
/// The nanosecond part can exceed 1,000,000,000 /// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// Panics on invalid hour, minute, second and/or nanosecond. /// Panics on invalid hour, minute, second and/or nanosecond.
/// ///
@ -641,7 +641,7 @@ impl NaiveDate {
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond. /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
/// ///
/// The nanosecond part can exceed 1,000,000,000 /// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// Returns `None` on invalid hour, minute, second and/or nanosecond. /// Returns `None` on invalid hour, minute, second and/or nanosecond.
/// ///
@ -723,11 +723,11 @@ impl NaiveDate {
/// ///
/// ~~~~ /// ~~~~
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use chrono::naive::date::MAX; /// use chrono::naive::MAX_DATE;
/// ///
/// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).succ_opt(), /// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).succ_opt(),
/// Some(NaiveDate::from_ymd(2015, 6, 4))); /// Some(NaiveDate::from_ymd(2015, 6, 4)));
/// assert_eq!(MAX.succ_opt(), None); /// assert_eq!(MAX_DATE.succ_opt(), None);
/// ~~~~ /// ~~~~
#[inline] #[inline]
pub fn succ_opt(&self) -> Option<NaiveDate> { pub fn succ_opt(&self) -> Option<NaiveDate> {
@ -760,11 +760,11 @@ impl NaiveDate {
/// ///
/// ~~~~ /// ~~~~
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use chrono::naive::date::MIN; /// use chrono::naive::MIN_DATE;
/// ///
/// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred_opt(), /// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred_opt(),
/// Some(NaiveDate::from_ymd(2015, 6, 2))); /// Some(NaiveDate::from_ymd(2015, 6, 2)));
/// assert_eq!(MIN.pred_opt(), None); /// assert_eq!(MIN_DATE.pred_opt(), None);
/// ~~~~ /// ~~~~
#[inline] #[inline]
pub fn pred_opt(&self) -> Option<NaiveDate> { pub fn pred_opt(&self) -> Option<NaiveDate> {
@ -780,7 +780,7 @@ impl NaiveDate {
/// ~~~~ /// ~~~~
/// # extern crate chrono; extern crate time; fn main() { /// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use chrono::naive::date::MAX; /// use chrono::naive::MAX_DATE;
/// use time::Duration; /// use time::Duration;
/// ///
/// let d = NaiveDate::from_ymd(2015, 9, 5); /// let d = NaiveDate::from_ymd(2015, 9, 5);
@ -790,7 +790,7 @@ impl NaiveDate {
/// Some(NaiveDate::from_ymd(2015, 7, 27))); /// Some(NaiveDate::from_ymd(2015, 7, 27)));
/// assert_eq!(d.checked_add_signed(Duration::days(1_000_000_000)), None); /// assert_eq!(d.checked_add_signed(Duration::days(1_000_000_000)), None);
/// assert_eq!(d.checked_add_signed(Duration::days(-1_000_000_000)), None); /// assert_eq!(d.checked_add_signed(Duration::days(-1_000_000_000)), None);
/// assert_eq!(MAX.checked_add_signed(Duration::days(1)), None); /// assert_eq!(MAX_DATE.checked_add_signed(Duration::days(1)), None);
/// # } /// # }
/// ~~~~ /// ~~~~
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<NaiveDate> { pub fn checked_add_signed(self, rhs: OldDuration) -> Option<NaiveDate> {
@ -816,7 +816,7 @@ impl NaiveDate {
/// ~~~~ /// ~~~~
/// # extern crate chrono; extern crate time; fn main() { /// # extern crate chrono; extern crate time; fn main() {
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use chrono::naive::date::MIN; /// use chrono::naive::MIN_DATE;
/// use time::Duration; /// use time::Duration;
/// ///
/// let d = NaiveDate::from_ymd(2015, 9, 5); /// let d = NaiveDate::from_ymd(2015, 9, 5);
@ -826,7 +826,7 @@ impl NaiveDate {
/// Some(NaiveDate::from_ymd(2015, 10, 15))); /// Some(NaiveDate::from_ymd(2015, 10, 15)));
/// assert_eq!(d.checked_sub_signed(Duration::days(1_000_000_000)), None); /// assert_eq!(d.checked_sub_signed(Duration::days(1_000_000_000)), None);
/// assert_eq!(d.checked_sub_signed(Duration::days(-1_000_000_000)), None); /// assert_eq!(d.checked_sub_signed(Duration::days(-1_000_000_000)), None);
/// assert_eq!(MIN.checked_sub_signed(Duration::days(1)), None); /// assert_eq!(MIN_DATE.checked_sub_signed(Duration::days(1)), None);
/// # } /// # }
/// ~~~~ /// ~~~~
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<NaiveDate> { pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<NaiveDate> {
@ -951,7 +951,7 @@ impl NaiveDate {
} }
impl Datelike for NaiveDate { impl Datelike for NaiveDate {
/// Returns the year number in the [calendar date](./index.html#calendar-date). /// Returns the year number in the [calendar date](#calendar-date).
/// ///
/// # Example /// # Example
/// ///
@ -1013,7 +1013,7 @@ impl Datelike for NaiveDate {
/// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day(), 14); /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day(), 14);
/// ~~~~ /// ~~~~
/// ///
/// Combined with [`NaiveDate::pred`](./struct.NaiveDate.html#method.pred), /// Combined with [`NaiveDate::pred`](#method.pred),
/// one can determine the number of days in a particular month. /// one can determine the number of days in a particular month.
/// (Note that this panics when `year` is out of range.) /// (Note that this panics when `year` is out of range.)
/// ///
@ -1070,7 +1070,7 @@ impl Datelike for NaiveDate {
/// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal(), 74); /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal(), 74);
/// ~~~~ /// ~~~~
/// ///
/// Combined with [`NaiveDate::pred`](./struct.NaiveDate.html#method.pred), /// Combined with [`NaiveDate::pred`](#method.pred),
/// one can determine the number of days in a particular year. /// one can determine the number of days in a particular year.
/// (Note that this panics when `year` is out of range.) /// (Note that this panics when `year` is out of range.)
/// ///
@ -1487,9 +1487,9 @@ fn test_encodable_json<F, E>(to_string: F)
Some(r#""0000-01-01""#.into())); Some(r#""0000-01-01""#.into()));
assert_eq!(to_string(&NaiveDate::from_ymd(-1, 12, 31)).ok(), assert_eq!(to_string(&NaiveDate::from_ymd(-1, 12, 31)).ok(),
Some(r#""-0001-12-31""#.into())); Some(r#""-0001-12-31""#.into()));
assert_eq!(to_string(&MIN).ok(), assert_eq!(to_string(&MIN_DATE).ok(),
Some(r#""-262144-01-01""#.into())); Some(r#""-262144-01-01""#.into()));
assert_eq!(to_string(&MAX).ok(), assert_eq!(to_string(&MAX_DATE).ok(),
Some(r#""+262143-12-31""#.into())); Some(r#""+262143-12-31""#.into()));
} }
@ -1505,8 +1505,8 @@ fn test_decodable_json<F, E>(from_str: F)
assert_eq!(from_str(r#""0000-01-01""#).ok(), Some(NaiveDate::from_ymd(0, 1, 1))); assert_eq!(from_str(r#""0000-01-01""#).ok(), Some(NaiveDate::from_ymd(0, 1, 1)));
assert_eq!(from_str(r#""0-1-1""#).ok(), Some(NaiveDate::from_ymd(0, 1, 1))); assert_eq!(from_str(r#""0-1-1""#).ok(), Some(NaiveDate::from_ymd(0, 1, 1)));
assert_eq!(from_str(r#""-0001-12-31""#).ok(), Some(NaiveDate::from_ymd(-1, 12, 31))); assert_eq!(from_str(r#""-0001-12-31""#).ok(), Some(NaiveDate::from_ymd(-1, 12, 31)));
assert_eq!(from_str(r#""-262144-01-01""#).ok(), Some(MIN)); assert_eq!(from_str(r#""-262144-01-01""#).ok(), Some(MIN_DATE));
assert_eq!(from_str(r#""+262143-12-31""#).ok(), Some(MAX)); assert_eq!(from_str(r#""+262143-12-31""#).ok(), Some(MAX_DATE));
// bad formats // bad formats
assert!(from_str(r#""""#).is_err()); assert!(from_str(r#""""#).is_err());
@ -1562,7 +1562,7 @@ mod rustc_serialize {
mod serde { mod serde {
use std::fmt; use std::fmt;
use super::NaiveDate; use super::NaiveDate;
use serde::{ser, de}; use serdelib::{ser, de};
// TODO not very optimized for space (binary formats would want something better) // TODO not very optimized for space (binary formats would want something better)
@ -1638,8 +1638,8 @@ mod serde {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::NaiveDate; use super::NaiveDate;
use super::{MIN, MIN_YEAR, MIN_DAYS_FROM_YEAR_0}; use super::{MIN_DATE, MIN_YEAR, MIN_DAYS_FROM_YEAR_0};
use super::{MAX, MAX_YEAR, MAX_DAYS_FROM_YEAR_0}; use super::{MAX_DATE, MAX_YEAR, MAX_DAYS_FROM_YEAR_0};
use {Datelike, Weekday}; use {Datelike, Weekday};
use std::{i32, u32}; use std::{i32, u32};
use oldtime::Duration; use oldtime::Duration;
@ -1778,10 +1778,10 @@ mod tests {
assert_eq!(from_ndays_from_ce(days).map(|d| d.num_days_from_ce()), Some(days)); assert_eq!(from_ndays_from_ce(days).map(|d| d.num_days_from_ce()), Some(days));
} }
assert_eq!(from_ndays_from_ce(MIN.num_days_from_ce()), Some(MIN)); assert_eq!(from_ndays_from_ce(MIN_DATE.num_days_from_ce()), Some(MIN_DATE));
assert_eq!(from_ndays_from_ce(MIN.num_days_from_ce() - 1), None); assert_eq!(from_ndays_from_ce(MIN_DATE.num_days_from_ce() - 1), None);
assert_eq!(from_ndays_from_ce(MAX.num_days_from_ce()), Some(MAX)); assert_eq!(from_ndays_from_ce(MAX_DATE.num_days_from_ce()), Some(MAX_DATE));
assert_eq!(from_ndays_from_ce(MAX.num_days_from_ce() + 1), None); assert_eq!(from_ndays_from_ce(MAX_DATE.num_days_from_ce() + 1), None);
} }
#[test] #[test]
@ -1887,7 +1887,7 @@ mod tests {
assert_eq!(ymd(2014, 5, 31).succ_opt(), Some(ymd(2014, 6, 1))); assert_eq!(ymd(2014, 5, 31).succ_opt(), Some(ymd(2014, 6, 1)));
assert_eq!(ymd(2014, 12, 31).succ_opt(), Some(ymd(2015, 1, 1))); assert_eq!(ymd(2014, 12, 31).succ_opt(), Some(ymd(2015, 1, 1)));
assert_eq!(ymd(2016, 2, 28).succ_opt(), Some(ymd(2016, 2, 29))); assert_eq!(ymd(2016, 2, 28).succ_opt(), Some(ymd(2016, 2, 29)));
assert_eq!(ymd(MAX.year(), 12, 31).succ_opt(), None); assert_eq!(ymd(MAX_DATE.year(), 12, 31).succ_opt(), None);
} }
#[test] #[test]
@ -1897,7 +1897,7 @@ mod tests {
assert_eq!(ymd(2015, 1, 1).pred_opt(), Some(ymd(2014, 12, 31))); assert_eq!(ymd(2015, 1, 1).pred_opt(), Some(ymd(2014, 12, 31)));
assert_eq!(ymd(2014, 6, 1).pred_opt(), Some(ymd(2014, 5, 31))); assert_eq!(ymd(2014, 6, 1).pred_opt(), Some(ymd(2014, 5, 31)));
assert_eq!(ymd(2014, 5, 7).pred_opt(), Some(ymd(2014, 5, 6))); assert_eq!(ymd(2014, 5, 7).pred_opt(), Some(ymd(2014, 5, 6)));
assert_eq!(ymd(MIN.year(), 1, 1).pred_opt(), None); assert_eq!(ymd(MIN_DATE.year(), 1, 1).pred_opt(), None);
} }
#[test] #[test]

View File

@ -10,8 +10,7 @@ use oldtime::Duration as OldDuration;
use {Weekday, Timelike, Datelike}; use {Weekday, Timelike, Datelike};
use div::div_mod_floor; use div::div_mod_floor;
use naive::time::NaiveTime; use naive::{NaiveTime, NaiveDate};
use naive::date::NaiveDate;
use format::{Item, Numeric, Pad, Fixed}; use format::{Item, Numeric, Pad, Fixed};
use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
@ -27,7 +26,7 @@ const MAX_SECS_BITS: usize = 44;
/// ///
/// # Example /// # Example
/// ///
/// `NaiveDateTime` is commonly created from [`NaiveDate`](../date/struct.NaiveDate.html). /// `NaiveDateTime` is commonly created from [`NaiveDate`](./struct.NaiveDate.html).
/// ///
/// ~~~~ /// ~~~~
/// use chrono::{NaiveDate, NaiveDateTime}; /// use chrono::{NaiveDate, NaiveDateTime};
@ -74,7 +73,7 @@ impl Deref for TsSeconds {
impl NaiveDateTime { impl NaiveDateTime {
/// Makes a new `NaiveDateTime` from date and time components. /// Makes a new `NaiveDateTime` from date and time components.
/// Equivalent to [`date.and_time(time)`](../date/struct.NaiveDate.html#method.and_time) /// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
/// and many other helper constructors on `NaiveDate`. /// and many other helper constructors on `NaiveDate`.
/// ///
/// # Example /// # Example
@ -103,7 +102,7 @@ impl NaiveDateTime {
/// [`TimeZone::timestamp`](../../offset/trait.TimeZone.html#method.timestamp). /// [`TimeZone::timestamp`](../../offset/trait.TimeZone.html#method.timestamp).
/// ///
/// The nanosecond part can exceed 1,000,000,000 in order to represent the /// The nanosecond part can exceed 1,000,000,000 in order to represent the
/// [leap second](../time/index.html#leap-second-handling). (The true "UNIX /// [leap second](./struct.NaiveTime.html#leap-second-handling). (The true "UNIX
/// timestamp" cannot represent a leap second unambiguously.) /// timestamp" cannot represent a leap second unambiguously.)
/// ///
/// Panics on the out-of-range number of seconds and/or invalid nanosecond. /// Panics on the out-of-range number of seconds and/or invalid nanosecond.
@ -131,7 +130,7 @@ impl NaiveDateTime {
/// and the number of nanoseconds since the last whole non-leap second. /// and the number of nanoseconds since the last whole non-leap second.
/// ///
/// The nanosecond part can exceed 1,000,000,000 /// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](../time/index.html#leap-second-handling). /// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// (The true "UNIX timestamp" cannot represent a leap second unambiguously.) /// (The true "UNIX timestamp" cannot represent a leap second unambiguously.)
/// ///
/// Returns `None` on the out-of-range number of seconds and/or invalid nanosecond. /// Returns `None` on the out-of-range number of seconds and/or invalid nanosecond.
@ -285,7 +284,7 @@ impl NaiveDateTime {
/// Returns the number of milliseconds since the last whole non-leap second. /// Returns the number of milliseconds since the last whole non-leap second.
/// ///
/// The return value ranges from 0 to 999, /// The return value ranges from 0 to 999,
/// or for [leap seconds](../time/index.html#leap-second-handling), to 1,999. /// or for [leap seconds](./struct.NaiveTime.html#leap-second-handling), to 1,999.
/// ///
/// # Example /// # Example
/// ///
@ -306,7 +305,7 @@ impl NaiveDateTime {
/// Returns the number of microseconds since the last whole non-leap second. /// Returns the number of microseconds since the last whole non-leap second.
/// ///
/// The return value ranges from 0 to 999,999, /// The return value ranges from 0 to 999,999,
/// or for [leap seconds](../time/index.html#leap-second-handling), to 1,999,999. /// or for [leap seconds](./struct.NaiveTime.html#leap-second-handling), to 1,999,999.
/// ///
/// # Example /// # Example
/// ///
@ -327,7 +326,7 @@ impl NaiveDateTime {
/// Returns the number of nanoseconds since the last whole non-leap second. /// Returns the number of nanoseconds since the last whole non-leap second.
/// ///
/// The return value ranges from 0 to 999,999,999, /// The return value ranges from 0 to 999,999,999,
/// or for [leap seconds](../time/index.html#leap-second-handling), to 1,999,999,999. /// or for [leap seconds](./struct.NaiveTime.html#leap-second-handling), to 1,999,999,999.
/// ///
/// # Example /// # Example
/// ///
@ -347,7 +346,7 @@ impl NaiveDateTime {
/// Adds given `Duration` to the current date and time. /// Adds given `Duration` to the current date and time.
/// ///
/// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling), /// As a part of Chrono's [leap second handling](./struct.NaiveTime.html#leap-second-handling),
/// the addition assumes that **there is no leap second ever**, /// the addition assumes that **there is no leap second ever**,
/// except when the `NaiveDateTime` itself represents a leap second /// except when the `NaiveDateTime` itself represents a leap second
/// in which case the assumption becomes that **there is exactly a single leap second ever**. /// in which case the assumption becomes that **there is exactly a single leap second ever**.
@ -433,7 +432,7 @@ impl NaiveDateTime {
/// Subtracts given `Duration` from the current date and time. /// Subtracts given `Duration` from the current date and time.
/// ///
/// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling), /// As a part of Chrono's [leap second handling](./struct.NaiveTime.html#leap-second-handling),
/// the subtraction assumes that **there is no leap second ever**, /// the subtraction assumes that **there is no leap second ever**,
/// except when the `NaiveDateTime` itself represents a leap second /// except when the `NaiveDateTime` itself represents a leap second
/// in which case the assumption becomes that **there is exactly a single leap second ever**. /// in which case the assumption becomes that **there is exactly a single leap second ever**.
@ -516,7 +515,7 @@ impl NaiveDateTime {
/// Subtracts another `NaiveDateTime` from the current date and time. /// Subtracts another `NaiveDateTime` from the current date and time.
/// This does not overflow or underflow at all. /// This does not overflow or underflow at all.
/// ///
/// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling), /// As a part of Chrono's [leap second handling](./struct.NaiveTime.html#leap-second-handling),
/// the subtraction assumes that **there is no leap second ever**, /// the subtraction assumes that **there is no leap second ever**,
/// except when any of the `NaiveDateTime`s themselves represents a leap second /// except when any of the `NaiveDateTime`s themselves represents a leap second
/// in which case the assumption becomes that /// in which case the assumption becomes that
@ -635,7 +634,7 @@ impl NaiveDateTime {
impl Datelike for NaiveDateTime { impl Datelike for NaiveDateTime {
/// Returns the year number in the [calendar date](./index.html#calendar-date). /// Returns the year number in the [calendar date](./index.html#calendar-date).
/// ///
/// See also the [`NaiveDate::year`](../date/struct.NaiveDate.html#method.year) method. /// See also the [`NaiveDate::year`](./struct.NaiveDate.html#method.year) method.
/// ///
/// # Example /// # Example
/// ///
@ -654,7 +653,7 @@ impl Datelike for NaiveDateTime {
/// ///
/// The return value ranges from 1 to 12. /// The return value ranges from 1 to 12.
/// ///
/// See also the [`NaiveDate::month`](../date/struct.NaiveDate.html#method.month) method. /// See also the [`NaiveDate::month`](./struct.NaiveDate.html#method.month) method.
/// ///
/// # Example /// # Example
/// ///
@ -673,7 +672,7 @@ impl Datelike for NaiveDateTime {
/// ///
/// The return value ranges from 0 to 11. /// The return value ranges from 0 to 11.
/// ///
/// See also the [`NaiveDate::month0`](../date/struct.NaiveDate.html#method.month0) method. /// See also the [`NaiveDate::month0`](./struct.NaiveDate.html#method.month0) method.
/// ///
/// # Example /// # Example
/// ///
@ -692,7 +691,7 @@ impl Datelike for NaiveDateTime {
/// ///
/// The return value ranges from 1 to 31. (The last day of month differs by months.) /// The return value ranges from 1 to 31. (The last day of month differs by months.)
/// ///
/// See also the [`NaiveDate::day`](../date/struct.NaiveDate.html#method.day) method. /// See also the [`NaiveDate::day`](./struct.NaiveDate.html#method.day) method.
/// ///
/// # Example /// # Example
/// ///
@ -711,7 +710,7 @@ impl Datelike for NaiveDateTime {
/// ///
/// The return value ranges from 0 to 30. (The last day of month differs by months.) /// The return value ranges from 0 to 30. (The last day of month differs by months.)
/// ///
/// See also the [`NaiveDate::day0`](../date/struct.NaiveDate.html#method.day0) method. /// See also the [`NaiveDate::day0`](./struct.NaiveDate.html#method.day0) method.
/// ///
/// # Example /// # Example
/// ///
@ -730,7 +729,7 @@ impl Datelike for NaiveDateTime {
/// ///
/// The return value ranges from 1 to 366. (The last day of year differs by years.) /// The return value ranges from 1 to 366. (The last day of year differs by years.)
/// ///
/// See also the [`NaiveDate::ordinal`](../date/struct.NaiveDate.html#method.ordinal) method. /// See also the [`NaiveDate::ordinal`](./struct.NaiveDate.html#method.ordinal) method.
/// ///
/// # Example /// # Example
/// ///
@ -749,7 +748,7 @@ impl Datelike for NaiveDateTime {
/// ///
/// The return value ranges from 0 to 365. (The last day of year differs by years.) /// The return value ranges from 0 to 365. (The last day of year differs by years.)
/// ///
/// See also the [`NaiveDate::ordinal0`](../date/struct.NaiveDate.html#method.ordinal0) method. /// See also the [`NaiveDate::ordinal0`](./struct.NaiveDate.html#method.ordinal0) method.
/// ///
/// # Example /// # Example
/// ///
@ -766,7 +765,7 @@ impl Datelike for NaiveDateTime {
/// Returns the day of week. /// Returns the day of week.
/// ///
/// See also the [`NaiveDate::weekday`](../date/struct.NaiveDate.html#method.weekday) method. /// See also the [`NaiveDate::weekday`](./struct.NaiveDate.html#method.weekday) method.
/// ///
/// # Example /// # Example
/// ///
@ -791,7 +790,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_year`](../date/struct.NaiveDate.html#method.with_year) method. /// [`NaiveDate::with_year`](./struct.NaiveDate.html#method.with_year) method.
/// ///
/// # Example /// # Example
/// ///
@ -812,7 +811,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_month`](../date/struct.NaiveDate.html#method.with_month) method. /// [`NaiveDate::with_month`](./struct.NaiveDate.html#method.with_month) method.
/// ///
/// # Example /// # Example
/// ///
@ -834,7 +833,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_month0`](../date/struct.NaiveDate.html#method.with_month0) method. /// [`NaiveDate::with_month0`](./struct.NaiveDate.html#method.with_month0) method.
/// ///
/// # Example /// # Example
/// ///
@ -856,7 +855,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_day`](../date/struct.NaiveDate.html#method.with_day) method. /// [`NaiveDate::with_day`](./struct.NaiveDate.html#method.with_day) method.
/// ///
/// # Example /// # Example
/// ///
@ -877,7 +876,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_day0`](../date/struct.NaiveDate.html#method.with_day0) method. /// [`NaiveDate::with_day0`](./struct.NaiveDate.html#method.with_day0) method.
/// ///
/// # Example /// # Example
/// ///
@ -898,7 +897,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_ordinal`](../date/struct.NaiveDate.html#method.with_ordinal) method. /// [`NaiveDate::with_ordinal`](./struct.NaiveDate.html#method.with_ordinal) method.
/// ///
/// # Example /// # Example
/// ///
@ -926,7 +925,7 @@ impl Datelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveDate::with_ordinal0`](../date/struct.NaiveDate.html#method.with_ordinal0) method. /// [`NaiveDate::with_ordinal0`](./struct.NaiveDate.html#method.with_ordinal0) method.
/// ///
/// # Example /// # Example
/// ///
@ -953,7 +952,7 @@ impl Datelike for NaiveDateTime {
impl Timelike for NaiveDateTime { impl Timelike for NaiveDateTime {
/// Returns the hour number from 0 to 23. /// Returns the hour number from 0 to 23.
/// ///
/// See also the [`NaiveTime::hour`](../time/struct.NaiveTime.html#method.hour) method. /// See also the [`NaiveTime::hour`](./struct.NaiveTime.html#method.hour) method.
/// ///
/// # Example /// # Example
/// ///
@ -970,7 +969,7 @@ impl Timelike for NaiveDateTime {
/// Returns the minute number from 0 to 59. /// Returns the minute number from 0 to 59.
/// ///
/// See also the [`NaiveTime::minute`](../time/struct.NaiveTime.html#method.minute) method. /// See also the [`NaiveTime::minute`](./struct.NaiveTime.html#method.minute) method.
/// ///
/// # Example /// # Example
/// ///
@ -987,7 +986,7 @@ impl Timelike for NaiveDateTime {
/// Returns the second number from 0 to 59. /// Returns the second number from 0 to 59.
/// ///
/// See also the [`NaiveTime::second`](../time/struct.NaiveTime.html#method.second) method. /// See also the [`NaiveTime::second`](./struct.NaiveTime.html#method.second) method.
/// ///
/// # Example /// # Example
/// ///
@ -1004,10 +1003,10 @@ impl Timelike for NaiveDateTime {
/// Returns the number of nanoseconds since the whole non-leap second. /// Returns the number of nanoseconds since the whole non-leap second.
/// The range from 1,000,000,000 to 1,999,999,999 represents /// The range from 1,000,000,000 to 1,999,999,999 represents
/// the [leap second](./naive/time/index.html#leap-second-handling). /// the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// ///
/// See also the /// See also the
/// [`NaiveTime::nanosecond`](../time/struct.NaiveTime.html#method.nanosecond) method. /// [`NaiveTime::nanosecond`](./struct.NaiveTime.html#method.nanosecond) method.
/// ///
/// # Example /// # Example
/// ///
@ -1027,7 +1026,7 @@ impl Timelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveTime::with_hour`](../time/struct.NaiveTime.html#method.with_hour) method. /// [`NaiveTime::with_hour`](./struct.NaiveTime.html#method.with_hour) method.
/// ///
/// # Example /// # Example
/// ///
@ -1049,7 +1048,7 @@ impl Timelike for NaiveDateTime {
/// Returns `None` when the resulting `NaiveDateTime` would be invalid. /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// ///
/// See also the /// See also the
/// [`NaiveTime::with_minute`](../time/struct.NaiveTime.html#method.with_minute) method. /// [`NaiveTime::with_minute`](./struct.NaiveTime.html#method.with_minute) method.
/// ///
/// # Example /// # Example
/// ///
@ -1073,7 +1072,7 @@ impl Timelike for NaiveDateTime {
/// the input range is restricted to 0 through 59. /// the input range is restricted to 0 through 59.
/// ///
/// See also the /// See also the
/// [`NaiveTime::with_second`](../time/struct.NaiveTime.html#method.with_second) method. /// [`NaiveTime::with_second`](./struct.NaiveTime.html#method.with_second) method.
/// ///
/// # Example /// # Example
/// ///
@ -1097,7 +1096,7 @@ impl Timelike for NaiveDateTime {
/// the input range can exceed 1,000,000,000 for leap seconds. /// the input range can exceed 1,000,000,000 for leap seconds.
/// ///
/// See also the /// See also the
/// [`NaiveTime::with_nanosecond`](../time/struct.NaiveTime.html#method.with_nanosecond) /// [`NaiveTime::with_nanosecond`](./struct.NaiveTime.html#method.with_nanosecond)
/// method. /// method.
/// ///
/// # Example /// # Example
@ -1131,7 +1130,7 @@ impl hash::Hash for NaiveDateTime {
/// An addition of `Duration` to `NaiveDateTime` yields another `NaiveDateTime`. /// An addition of `Duration` to `NaiveDateTime` yields another `NaiveDateTime`.
/// ///
/// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling), /// As a part of Chrono's [leap second handling](./struct.NaiveTime.html#leap-second-handling),
/// the addition assumes that **there is no leap second ever**, /// the addition assumes that **there is no leap second ever**,
/// except when the `NaiveDateTime` itself represents a leap second /// except when the `NaiveDateTime` itself represents a leap second
/// in which case the assumption becomes that **there is exactly a single leap second ever**. /// in which case the assumption becomes that **there is exactly a single leap second ever**.
@ -1196,7 +1195,7 @@ impl Add<OldDuration> for NaiveDateTime {
/// A subtraction of `Duration` from `NaiveDateTime` yields another `NaiveDateTime`. /// A subtraction of `Duration` from `NaiveDateTime` yields another `NaiveDateTime`.
/// It is same to the addition with a negated `Duration`. /// It is same to the addition with a negated `Duration`.
/// ///
/// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling), /// As a part of Chrono's [leap second handling](./struct.NaiveTime.html#leap-second-handling),
/// the addition assumes that **there is no leap second ever**, /// the addition assumes that **there is no leap second ever**,
/// except when the `NaiveDateTime` itself represents a leap second /// except when the `NaiveDateTime` itself represents a leap second
/// in which case the assumption becomes that **there is exactly a single leap second ever**. /// in which case the assumption becomes that **there is exactly a single leap second ever**.
@ -1365,7 +1364,7 @@ impl str::FromStr for NaiveDateTime {
fn test_encodable_json<F, E>(to_string: F) fn test_encodable_json<F, E>(to_string: F)
where F: Fn(&NaiveDateTime) -> Result<String, E>, E: ::std::fmt::Debug where F: Fn(&NaiveDateTime) -> Result<String, E>, E: ::std::fmt::Debug
{ {
use naive::date; use naive::{MIN_DATE, MAX_DATE};
assert_eq!( assert_eq!(
to_string(&NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90)).ok(), to_string(&NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90)).ok(),
@ -1380,10 +1379,10 @@ fn test_encodable_json<F, E>(to_string: F)
to_string(&NaiveDate::from_ymd(-1, 12, 31).and_hms_nano(23, 59, 59, 7)).ok(), to_string(&NaiveDate::from_ymd(-1, 12, 31).and_hms_nano(23, 59, 59, 7)).ok(),
Some(r#""-0001-12-31T23:59:59.000000007""#.into())); Some(r#""-0001-12-31T23:59:59.000000007""#.into()));
assert_eq!( assert_eq!(
to_string(&date::MIN.and_hms(0, 0, 0)).ok(), to_string(&MIN_DATE.and_hms(0, 0, 0)).ok(),
Some(r#""-262144-01-01T00:00:00""#.into())); Some(r#""-262144-01-01T00:00:00""#.into()));
assert_eq!( assert_eq!(
to_string(&date::MAX.and_hms_nano(23, 59, 59, 1_999_999_999)).ok(), to_string(&MAX_DATE.and_hms_nano(23, 59, 59, 1_999_999_999)).ok(),
Some(r#""+262143-12-31T23:59:60.999999999""#.into())); Some(r#""+262143-12-31T23:59:60.999999999""#.into()));
} }
@ -1391,7 +1390,7 @@ fn test_encodable_json<F, E>(to_string: F)
fn test_decodable_json<F, E>(from_str: F) fn test_decodable_json<F, E>(from_str: F)
where F: Fn(&str) -> Result<NaiveDateTime, E>, E: ::std::fmt::Debug where F: Fn(&str) -> Result<NaiveDateTime, E>, E: ::std::fmt::Debug
{ {
use naive::date; use naive::{MIN_DATE, MAX_DATE};
assert_eq!( assert_eq!(
from_str(r#""2016-07-08T09:10:48.090""#).ok(), from_str(r#""2016-07-08T09:10:48.090""#).ok(),
@ -1413,13 +1412,13 @@ fn test_decodable_json<F, E>(from_str: F)
Some(NaiveDate::from_ymd(-1, 12, 31).and_hms_nano(23, 59, 59, 7))); Some(NaiveDate::from_ymd(-1, 12, 31).and_hms_nano(23, 59, 59, 7)));
assert_eq!( assert_eq!(
from_str(r#""-262144-01-01T00:00:00""#).ok(), from_str(r#""-262144-01-01T00:00:00""#).ok(),
Some(date::MIN.and_hms(0, 0, 0))); Some(MIN_DATE.and_hms(0, 0, 0)));
assert_eq!( assert_eq!(
from_str(r#""+262143-12-31T23:59:60.999999999""#).ok(), from_str(r#""+262143-12-31T23:59:60.999999999""#).ok(),
Some(date::MAX.and_hms_nano(23, 59, 59, 1_999_999_999))); Some(MAX_DATE.and_hms_nano(23, 59, 59, 1_999_999_999)));
assert_eq!( assert_eq!(
from_str(r#""+262143-12-31T23:59:60.9999999999997""#).ok(), // excess digits are ignored from_str(r#""+262143-12-31T23:59:60.9999999999997""#).ok(), // excess digits are ignored
Some(date::MAX.and_hms_nano(23, 59, 59, 1_999_999_999))); Some(MAX_DATE.and_hms_nano(23, 59, 59, 1_999_999_999)));
// bad formats // bad formats
assert!(from_str(r#""""#).is_err()); assert!(from_str(r#""""#).is_err());
@ -1510,11 +1509,11 @@ mod rustc_serialize {
pub mod serde { pub mod serde {
use std::fmt; use std::fmt;
use super::{NaiveDateTime}; use super::{NaiveDateTime};
use serde::{ser, de}; use serdelib::{ser, de};
/// Serialize a NaiveDateTime as a string /// Serialize a NaiveDateTime as a string
/// ///
/// See the [`ts_seconds`](./ts_seconds/index.html) module to serialize as /// See the [`ts_seconds`](./serde/ts_seconds/index.html) module to serialize as
/// a timestamp. /// a timestamp.
impl ser::Serialize for NaiveDateTime { impl ser::Serialize for NaiveDateTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@ -1573,7 +1572,7 @@ pub mod serde {
/// # extern crate serde; /// # extern crate serde;
/// # extern crate chrono; /// # extern crate chrono;
/// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, UTC}; /// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, UTC};
/// use chrono::naive::datetime::serde::ts_seconds; /// use chrono::naive::serde::ts_seconds;
/// #[derive(Deserialize, Serialize)] /// #[derive(Deserialize, Serialize)]
/// struct S { /// struct S {
/// #[serde(with = "ts_seconds")] /// #[serde(with = "ts_seconds")]
@ -1596,7 +1595,7 @@ pub mod serde {
/// ``` /// ```
pub mod ts_seconds { pub mod ts_seconds {
use std::fmt; use std::fmt;
use serde::{ser, de}; use serdelib::{ser, de};
use NaiveDateTime; use NaiveDateTime;
@ -1617,7 +1616,7 @@ pub mod serde {
/// # extern crate chrono; /// # extern crate chrono;
/// # use chrono::{NaiveDateTime, UTC}; /// # use chrono::{NaiveDateTime, UTC};
/// # use serde::Deserialize; /// # use serde::Deserialize;
/// use chrono::naive::datetime::serde::ts_seconds::deserialize as from_ts; /// use chrono::naive::serde::ts_seconds::deserialize as from_ts;
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct S { /// struct S {
/// #[serde(deserialize_with = "from_ts")] /// #[serde(deserialize_with = "from_ts")]
@ -1653,7 +1652,7 @@ pub mod serde {
/// # extern crate chrono; /// # extern crate chrono;
/// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, UTC}; /// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, UTC};
/// # use serde::Serialize; /// # use serde::Serialize;
/// use chrono::naive::datetime::serde::ts_seconds::serialize as to_ts; /// use chrono::naive::serde::ts_seconds::serialize as to_ts;
/// #[derive(Serialize)] /// #[derive(Serialize)]
/// struct S { /// struct S {
/// #[serde(serialize_with = "to_ts")] /// #[serde(serialize_with = "to_ts")]
@ -1720,7 +1719,7 @@ pub mod serde {
fn test_serde_bincode() { fn test_serde_bincode() {
// Bincode is relevant to test separately from JSON because // Bincode is relevant to test separately from JSON because
// it is not self-describing. // it is not self-describing.
use naive::date::NaiveDate; use naive::NaiveDate;
use self::bincode::{Infinite, serialize, deserialize}; use self::bincode::{Infinite, serialize, deserialize};
let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90); let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90);
@ -1734,8 +1733,7 @@ pub mod serde {
mod tests { mod tests {
use super::NaiveDateTime; use super::NaiveDateTime;
use Datelike; use Datelike;
use naive::date as naive_date; use naive::{NaiveDate, MIN_DATE, MAX_DATE};
use naive::date::NaiveDate;
use std::i64; use std::i64;
use oldtime::Duration; use oldtime::Duration;
@ -1772,17 +1770,15 @@ mod tests {
// overflow check // overflow check
// assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`. // assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`.
// (they are private constants, but the equivalence is tested in that module.) // (they are private constants, but the equivalence is tested in that module.)
let max_days_from_year_0 = let max_days_from_year_0 = MAX_DATE.signed_duration_since(NaiveDate::from_ymd(0,1,1));
naive_date::MAX.signed_duration_since(NaiveDate::from_ymd(0,1,1)); check((0,1,1, 0,0,0), max_days_from_year_0, Some((MAX_DATE.year(),12,31, 0,0,0)));
check((0,1,1, 0,0,0), max_days_from_year_0, Some((naive_date::MAX.year(),12,31, 0,0,0)));
check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86399), check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86399),
Some((naive_date::MAX.year(),12,31, 23,59,59))); Some((MAX_DATE.year(),12,31, 23,59,59)));
check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86400), None); check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86400), None);
check((0,1,1, 0,0,0), Duration::max_value(), None); check((0,1,1, 0,0,0), Duration::max_value(), None);
let min_days_from_year_0 = let min_days_from_year_0 = MIN_DATE.signed_duration_since(NaiveDate::from_ymd(0,1,1));
naive_date::MIN.signed_duration_since(NaiveDate::from_ymd(0,1,1)); check((0,1,1, 0,0,0), min_days_from_year_0, Some((MIN_DATE.year(),1,1, 0,0,0)));
check((0,1,1, 0,0,0), min_days_from_year_0, Some((naive_date::MIN.year(),1,1, 0,0,0)));
check((0,1,1, 0,0,0), min_days_from_year_0 - Duration::seconds(1), None); check((0,1,1, 0,0,0), min_days_from_year_0 - Duration::seconds(1), None);
check((0,1,1, 0,0,0), Duration::min_value(), None); check((0,1,1, 0,0,0), Duration::min_value(), None);
} }

View File

@ -2,159 +2,6 @@
// See README.md and LICENSE.txt for details. // See README.md and LICENSE.txt for details.
//! ISO 8601 time without timezone. //! ISO 8601 time without timezone.
//!
//! # Leap Second Handling
//!
//! Since 1960s, the manmade atomic clock has been so accurate that
//! it is much more accurate than Earth's own motion.
//! It became desirable to define the civil time in terms of the atomic clock,
//! but that risks the desynchronization of the civil time from Earth.
//! To account for this, the designers of the Coordinated Universal Time (UTC)
//! made that the UTC should be kept within 0.9 seconds of the observed Earth-bound time.
//! When the mean solar day is longer than the ideal (86,400 seconds),
//! the error slowly accumulates and it is necessary to add a **leap second**
//! to slow the UTC down a bit.
//! (We may also remove a second to speed the UTC up a bit, but it never happened.)
//! The leap second, if any, follows 23:59:59 of June 30 or December 31 in the UTC.
//!
//! Fast forward to the 21st century,
//! we have seen 26 leap seconds from January 1972 to December 2015.
//! Yes, 26 seconds. Probably you can read this paragraph within 26 seconds.
//! But those 26 seconds, and possibly more in the future, are never predictable,
//! and whether to add a leap second or not is known only before 6 months.
//! Internet-based clocks (via NTP) do account for known leap seconds,
//! but the system API normally doesn't (and often can't, with no network connection)
//! and there is no reliable way to retrieve leap second information.
//!
//! Chrono does not try to accurately implement leap seconds; it is impossible.
//! Rather, **it allows for leap seconds but behaves as if there are *no other* leap seconds.**
//! Various operations will ignore any possible leap second(s)
//! except when any of the operands were actually leap seconds.
//!
//! If you cannot tolerate this behavior,
//! you must use a separate `TimeZone` for the International Atomic Time (TAI).
//! TAI is like UTC but has no leap seconds, and thus slightly differs from UTC.
//! Chrono 0.3 does not provide such implementation, but it is planned for 0.4.
//!
//! ## Representing Leap Seconds
//!
//! The leap second is indicated via fractional seconds more than 1 second.
//! This makes possible to treat a leap second as the prior non-leap second
//! if you don't care about sub-second accuracy.
//! You should use the proper formatting to get the raw leap second.
//!
//! All methods accepting fractional seconds will accept such values.
//!
//! ~~~~
//! use chrono::{NaiveDate, NaiveTime, UTC, TimeZone};
//!
//! let t = NaiveTime::from_hms_milli(8, 59, 59, 1_000);
//!
//! let dt1 = NaiveDate::from_ymd(2015, 7, 1).and_hms_micro(8, 59, 59, 1_000_000);
//!
//! let dt2 = UTC.ymd(2015, 6, 30).and_hms_nano(23, 59, 59, 1_000_000_000);
//! # let _ = (t, dt1, dt2);
//! ~~~~
//!
//! Note that the leap second can happen anytime given an appropriate time zone;
//! 2015-07-01 01:23:60 would be a proper leap second if UTC+01:24 had existed.
//! Practically speaking, though, by the time of the first leap second on 1972-06-30,
//! every time zone offset around the world has standardized to the 5-minute alignment.
//!
//! ## Date And Time Arithmetics
//!
//! As a concrete example, let's assume that `03:00:60` and `04:00:60` are leap seconds.
//! (In reality, of course, leap seconds are separated by at least 6 months.)
//!
//! `Time + Duration`:
//!
//! - `03:00:00 + 1s = 03:00:01`.
//! - `03:00:59 + 60s = 03:02:00`.
//! - `03:00:59 + 1s = 03:01:00`.
//! - `03:00:60 + 1s = 03:01:00`.
//! Note that the sum is identical to the previous.
//! - `03:00:60 + 60s = 03:01:59`.
//! - `03:00:60 + 61s = 03:02:00`.
//! - `03:00:60.1 + 0.8s = 03:00:60.9`.
//!
//! `Time - Duration`:
//!
//! - `03:00:00 - 1s = 02:59:59`.
//! - `03:01:00 - 1s = 03:00:59`.
//! - `03:01:00 - 60s = 03:00:00`.
//! - `03:00:60 - 60s = 03:00:00`.
//! Note that the result is identical to the previous.
//! - `03:00:60.7 - 0.4s = 03:00:60.3`.
//! - `03:00:60.7 - 0.9s = 03:00:59.8`.
//!
//! `Time - Time`:
//!
//! - `04:00:00 - 03:00:00 = 3600s`.
//! - `03:01:00 - 03:00:00 = 60s`.
//! - `03:00:60 - 03:00:00 = 60s`.
//! Note that the difference is identical to the previous.
//! - `03:00:60.6 - 03:00:59.4 = 1.2s`.
//! - `03:01:00 - 03:00:59.8 = 0.2s`.
//! - `03:01:00 - 03:00:60.5 = 0.5s`.
//! Note that the difference is larger than the previous,
//! even though the leap second clearly follows the previous whole second.
//! - `04:00:60.9 - 03:00:60.1 =
//! (04:00:60.9 - 04:00:00) + (04:00:00 - 03:01:00) + (03:01:00 - 03:00:60.1) =
//! 60.9s + 3540s + 0.9s = 3601.8s`.
//!
//! In general,
//!
//! - `Time + Duration` unconditionally equals to `Duration + Time`.
//!
//! - `Time - Duration` unconditionally equals to `Time + (-Duration)`.
//!
//! - `Time1 - Time2` unconditionally equals to `-(Time2 - Time1)`.
//!
//! - Associativity does not generally hold, because
//! `(Time + Duration1) - Duration2` no longer equals to `Time + (Duration1 - Duration2)`
//! for two positive durations.
//!
//! - As a special case, `(Time + Duration) - Duration` also does not equal to `Time`.
//!
//! - If you can assume that all durations have the same sign, however,
//! then the associativity holds:
//! `(Time + Duration1) + Duration2` equals to `Time + (Duration1 + Duration2)`
//! for two positive durations.
//!
//! ## Reading And Writing Leap Seconds
//!
//! The "typical" leap seconds on the minute boundary are
//! correctly handled both in the formatting and parsing.
//! The leap second in the human-readable representation
//! will be represented as the second part being 60, as required by ISO 8601.
//!
//! ~~~~
//! use chrono::{UTC, TimeZone};
//!
//! let dt = UTC.ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_000);
//! assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");
//! ~~~~
//!
//! There are hypothetical leap seconds not on the minute boundary
//! nevertheless supported by Chrono.
//! They are allowed for the sake of completeness and consistency;
//! there were several "exotic" time zone offsets with fractional minutes prior to UTC after all.
//! For such cases the human-readable representation is ambiguous
//! and would be read back to the next non-leap second.
//!
//! ~~~~
//! use chrono::{DateTime, UTC, TimeZone};
//!
//! let dt = UTC.ymd(2015, 6, 30).and_hms_milli(23, 56, 4, 1_000);
//! assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
//!
//! let dt = UTC.ymd(2015, 6, 30).and_hms(23, 56, 5);
//! assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
//! assert_eq!(DateTime::parse_from_rfc3339("2015-06-30T23:56:05Z").unwrap(), dt);
//! ~~~~
//!
//! Since Chrono alone cannot determine any existence of leap seconds,
//! **there is absolutely no guarantee that the leap second read has actually happened**.
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
@ -168,9 +15,158 @@ use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItem
/// ISO 8601 time without timezone. /// ISO 8601 time without timezone.
/// Allows for the nanosecond precision and optional leap second representation. /// Allows for the nanosecond precision and optional leap second representation.
/// ///
/// <a name="leap-second-what?"></a> /// # Leap Second Handling
/// Chrono has a notable policy on the [leap second handling](./index.html#leap-second-handling), ///
/// designed to be maximally useful for typical users. /// Since 1960s, the manmade atomic clock has been so accurate that
/// it is much more accurate than Earth's own motion.
/// It became desirable to define the civil time in terms of the atomic clock,
/// but that risks the desynchronization of the civil time from Earth.
/// To account for this, the designers of the Coordinated Universal Time (UTC)
/// made that the UTC should be kept within 0.9 seconds of the observed Earth-bound time.
/// When the mean solar day is longer than the ideal (86,400 seconds),
/// the error slowly accumulates and it is necessary to add a **leap second**
/// to slow the UTC down a bit.
/// (We may also remove a second to speed the UTC up a bit, but it never happened.)
/// The leap second, if any, follows 23:59:59 of June 30 or December 31 in the UTC.
///
/// Fast forward to the 21st century,
/// we have seen 26 leap seconds from January 1972 to December 2015.
/// Yes, 26 seconds. Probably you can read this paragraph within 26 seconds.
/// But those 26 seconds, and possibly more in the future, are never predictable,
/// and whether to add a leap second or not is known only before 6 months.
/// Internet-based clocks (via NTP) do account for known leap seconds,
/// but the system API normally doesn't (and often can't, with no network connection)
/// and there is no reliable way to retrieve leap second information.
///
/// Chrono does not try to accurately implement leap seconds; it is impossible.
/// Rather, **it allows for leap seconds but behaves as if there are *no other* leap seconds.**
/// Various operations will ignore any possible leap second(s)
/// except when any of the operands were actually leap seconds.
///
/// If you cannot tolerate this behavior,
/// you must use a separate `TimeZone` for the International Atomic Time (TAI).
/// TAI is like UTC but has no leap seconds, and thus slightly differs from UTC.
/// Chrono 0.3 does not provide such implementation, but it is planned for 0.4.
///
/// ## Representing Leap Seconds
///
/// The leap second is indicated via fractional seconds more than 1 second.
/// This makes possible to treat a leap second as the prior non-leap second
/// if you don't care about sub-second accuracy.
/// You should use the proper formatting to get the raw leap second.
///
/// All methods accepting fractional seconds will accept such values.
///
/// ~~~~
/// use chrono::{NaiveDate, NaiveTime, UTC, TimeZone};
///
/// let t = NaiveTime::from_hms_milli(8, 59, 59, 1_000);
///
/// let dt1 = NaiveDate::from_ymd(2015, 7, 1).and_hms_micro(8, 59, 59, 1_000_000);
///
/// let dt2 = UTC.ymd(2015, 6, 30).and_hms_nano(23, 59, 59, 1_000_000_000);
/// # let _ = (t, dt1, dt2);
/// ~~~~
///
/// Note that the leap second can happen anytime given an appropriate time zone;
/// 2015-07-01 01:23:60 would be a proper leap second if UTC+01:24 had existed.
/// Practically speaking, though, by the time of the first leap second on 1972-06-30,
/// every time zone offset around the world has standardized to the 5-minute alignment.
///
/// ## Date And Time Arithmetics
///
/// As a concrete example, let's assume that `03:00:60` and `04:00:60` are leap seconds.
/// (In reality, of course, leap seconds are separated by at least 6 months.)
///
/// `Time + Duration`:
///
/// - `03:00:00 + 1s = 03:00:01`.
/// - `03:00:59 + 60s = 03:02:00`.
/// - `03:00:59 + 1s = 03:01:00`.
/// - `03:00:60 + 1s = 03:01:00`.
/// Note that the sum is identical to the previous.
/// - `03:00:60 + 60s = 03:01:59`.
/// - `03:00:60 + 61s = 03:02:00`.
/// - `03:00:60.1 + 0.8s = 03:00:60.9`.
///
/// `Time - Duration`:
///
/// - `03:00:00 - 1s = 02:59:59`.
/// - `03:01:00 - 1s = 03:00:59`.
/// - `03:01:00 - 60s = 03:00:00`.
/// - `03:00:60 - 60s = 03:00:00`.
/// Note that the result is identical to the previous.
/// - `03:00:60.7 - 0.4s = 03:00:60.3`.
/// - `03:00:60.7 - 0.9s = 03:00:59.8`.
///
/// `Time - Time`:
///
/// - `04:00:00 - 03:00:00 = 3600s`.
/// - `03:01:00 - 03:00:00 = 60s`.
/// - `03:00:60 - 03:00:00 = 60s`.
/// Note that the difference is identical to the previous.
/// - `03:00:60.6 - 03:00:59.4 = 1.2s`.
/// - `03:01:00 - 03:00:59.8 = 0.2s`.
/// - `03:01:00 - 03:00:60.5 = 0.5s`.
/// Note that the difference is larger than the previous,
/// even though the leap second clearly follows the previous whole second.
/// - `04:00:60.9 - 03:00:60.1 =
/// (04:00:60.9 - 04:00:00) + (04:00:00 - 03:01:00) + (03:01:00 - 03:00:60.1) =
/// 60.9s + 3540s + 0.9s = 3601.8s`.
///
/// In general,
///
/// - `Time + Duration` unconditionally equals to `Duration + Time`.
///
/// - `Time - Duration` unconditionally equals to `Time + (-Duration)`.
///
/// - `Time1 - Time2` unconditionally equals to `-(Time2 - Time1)`.
///
/// - Associativity does not generally hold, because
/// `(Time + Duration1) - Duration2` no longer equals to `Time + (Duration1 - Duration2)`
/// for two positive durations.
///
/// - As a special case, `(Time + Duration) - Duration` also does not equal to `Time`.
///
/// - If you can assume that all durations have the same sign, however,
/// then the associativity holds:
/// `(Time + Duration1) + Duration2` equals to `Time + (Duration1 + Duration2)`
/// for two positive durations.
///
/// ## Reading And Writing Leap Seconds
///
/// The "typical" leap seconds on the minute boundary are
/// correctly handled both in the formatting and parsing.
/// The leap second in the human-readable representation
/// will be represented as the second part being 60, as required by ISO 8601.
///
/// ~~~~
/// use chrono::{UTC, TimeZone};
///
/// let dt = UTC.ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_000);
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");
/// ~~~~
///
/// There are hypothetical leap seconds not on the minute boundary
/// nevertheless supported by Chrono.
/// They are allowed for the sake of completeness and consistency;
/// there were several "exotic" time zone offsets with fractional minutes prior to UTC after all.
/// For such cases the human-readable representation is ambiguous
/// and would be read back to the next non-leap second.
///
/// ~~~~
/// use chrono::{DateTime, UTC, TimeZone};
///
/// let dt = UTC.ymd(2015, 6, 30).and_hms_milli(23, 56, 4, 1_000);
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
///
/// let dt = UTC.ymd(2015, 6, 30).and_hms(23, 56, 5);
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
/// assert_eq!(DateTime::parse_from_rfc3339("2015-06-30T23:56:05Z").unwrap(), dt);
/// ~~~~
///
/// Since Chrono alone cannot determine any existence of leap seconds,
/// **there is absolutely no guarantee that the leap second read has actually happened**.
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub struct NaiveTime { pub struct NaiveTime {
secs: u32, secs: u32,
@ -834,7 +830,7 @@ impl Timelike for NaiveTime {
/// Returns the number of nanoseconds since the whole non-leap second. /// Returns the number of nanoseconds since the whole non-leap second.
/// The range from 1,000,000,000 to 1,999,999,999 represents /// The range from 1,000,000,000 to 1,999,999,999 represents
/// the [leap second](./naive/time/index.html#leap-second-handling). /// the [leap second](#leap-second-handling).
/// ///
/// # Example /// # Example
/// ///
@ -1337,7 +1333,7 @@ mod rustc_serialize {
mod serde { mod serde {
use std::fmt; use std::fmt;
use super::NaiveTime; use super::NaiveTime;
use serde::{ser, de}; use serdelib::{ser, de};
// TODO not very optimized for space (binary formats would want something better) // TODO not very optimized for space (binary formats would want something better)
// TODO round-trip for general leap seconds (not just those with second = 60) // TODO round-trip for general leap seconds (not just those with second = 60)

View File

@ -9,10 +9,8 @@ use oldtime::Duration as OldDuration;
use Timelike; use Timelike;
use div::div_mod_floor; use div::div_mod_floor;
use naive::time::NaiveTime; use naive::{NaiveTime, NaiveDate, NaiveDateTime};
use naive::date::NaiveDate; use DateTime;
use naive::datetime::NaiveDateTime;
use datetime::DateTime;
use super::{TimeZone, Offset, LocalResult}; use super::{TimeZone, Offset, LocalResult};
/// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59.

View File

@ -6,11 +6,8 @@
use oldtime; use oldtime;
use {Datelike, Timelike}; use {Datelike, Timelike};
use naive::date::NaiveDate; use naive::{NaiveDate, NaiveTime, NaiveDateTime};
use naive::time::NaiveTime; use {Date, DateTime};
use naive::datetime::NaiveDateTime;
use date::Date;
use datetime::DateTime;
use super::{TimeZone, LocalResult}; use super::{TimeZone, LocalResult};
use super::fixed::FixedOffset; use super::fixed::FixedOffset;

View File

@ -1,35 +1,29 @@
// This is a part of Chrono. // This is a part of Chrono.
// See README.md and LICENSE.txt for details. // See README.md and LICENSE.txt for details.
/*! //! The time zone, which calculates offsets from the local time to UTC.
* The time zone, which calculates offsets from the local time to UTC. //!
* //! There are four operations provided by the `TimeZone` trait:
* There are four operations provided by the `TimeZone` trait: //!
* //! 1. Converting the local `NaiveDateTime` to `DateTime<Tz>`
* 1. Converting the local `NaiveDateTime` to `DateTime<Tz>` //! 2. Converting the UTC `NaiveDateTime` to `DateTime<Tz>`
* 2. Converting the UTC `NaiveDateTime` to `DateTime<Tz>` //! 3. Converting `DateTime<Tz>` to the local `NaiveDateTime`
* 3. Converting `DateTime<Tz>` to the local `NaiveDateTime` //! 4. Constructing `DateTime<Tz>` objects from various offsets
* 4. Constructing `DateTime<Tz>` objects from various offsets //!
* //! 1 is used for constructors. 2 is used for the `with_timezone` method of date and time types.
* 1 is used for constructors. 2 is used for the `with_timezone` method of date and time types. //! 3 is used for other methods, e.g. `year()` or `format()`, and provided by an associated type
* 3 is used for other methods, e.g. `year()` or `format()`, and provided by an associated type //! which implements `Offset` (which then passed to `TimeZone` for actual implementations).
* which implements `Offset` (which then passed to `TimeZone` for actual implementations). //! Technically speaking `TimeZone` has a total knowledge about given timescale,
* Technically speaking `TimeZone` has a total knowledge about given timescale, //! but `Offset` is used as a cache to avoid the repeated conversion
* but `Offset` is used as a cache to avoid the repeated conversion //! and provides implementations for 1 and 3.
* and provides implementations for 1 and 3. //! An `TimeZone` instance can be reconstructed from the corresponding `Offset` instance.
* An `TimeZone` instance can be reconstructed from the corresponding `Offset` instance.
*/
use std::fmt; use std::fmt;
use Weekday; use Weekday;
use naive::date::NaiveDate; use naive::{NaiveDate, NaiveTime, NaiveDateTime};
use naive::time::NaiveTime; use {Date, DateTime};
use naive::datetime::NaiveDateTime;
use date::Date;
use datetime::DateTime;
use format::{parse, Parsed, ParseResult, StrftimeItems}; use format::{parse, Parsed, ParseResult, StrftimeItems};
use self::fixed::FixedOffset;
/// The conversion result from the local time to the timezone-aware datetime types. /// The conversion result from the local time to the timezone-aware datetime types.
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
@ -165,8 +159,8 @@ pub trait Offset: Sized + Clone + fmt::Debug {
/// The time zone. /// The time zone.
/// ///
/// The methods here are the primarily constructors for [`Date`](../date/struct.Date.html) and /// The methods here are the primarily constructors for [`Date`](../struct.Date.html) and
/// [`DateTime`](../datetime/struct.DateTime.html) types. /// [`DateTime`](../struct.DateTime.html) types.
pub trait TimeZone: Sized + Clone { pub trait TimeZone: Sized + Clone {
/// An associated offset type. /// An associated offset type.
/// This type is used to store the actual offset in date and time types. /// This type is used to store the actual offset in date and time types.
@ -373,7 +367,11 @@ pub trait TimeZone: Sized + Clone {
} }
} }
pub mod utc; mod utc;
pub mod fixed; mod fixed;
pub mod local; mod local;
pub use self::utc::UTC;
pub use self::fixed::FixedOffset;
pub use self::local::Local;

View File

@ -6,12 +6,9 @@
use std::fmt; use std::fmt;
use oldtime; use oldtime;
use naive::date::NaiveDate; use naive::{NaiveDate, NaiveDateTime};
use naive::datetime::NaiveDateTime; use {Date, DateTime};
use date::Date; use super::{TimeZone, Offset, LocalResult, FixedOffset};
use datetime::DateTime;
use super::{TimeZone, Offset, LocalResult};
use super::fixed::FixedOffset;
/// The UTC time zone. This is the most efficient time zone when you don't need the local time. /// The UTC time zone. This is the most efficient time zone when you don't need the local time.
/// It is also used as an offset (which is also a dummy type). /// It is also used as an offset (which is also a dummy type).