remove chrono-0.3 from crosvm
`grep`ing around, it doesn't seem crosvm requires chrono 0.3 anymore.
Remove it.
BUG=b:239449434
TEST=CQ
Change-Id: Ib246d0a96e2147f56e1cc4bc0b138679a51d07c9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/rust_crates/+/4149609
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
Commit-Queue: George Burgess <gbiv@chromium.org>
diff --git a/projects/Cargo.lock b/projects/Cargo.lock
index cf6ef7a..f971d61 100644
--- a/projects/Cargo.lock
+++ b/projects/Cargo.lock
@@ -435,7 +435,7 @@
checksum = "4f346b6890a0dfa7266974910e7df2d5088120dd54721b9b0e5aae1ae5e05715"
dependencies = [
"cargo-lock",
- "chrono 0.4.19",
+ "chrono",
]
[[package]]
@@ -556,16 +556,6 @@
[[package]]
name = "chrono"
-version = "0.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "158b0bd7d75cbb6bf9c25967a48a2e9f77da95876b858eadfabaa99cd069de6e"
-dependencies = [
- "num 0.1.42",
- "time 0.1.43",
-]
-
-[[package]]
-name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
@@ -963,7 +953,7 @@
version = "0.24.52"
dependencies = [
"bitflags",
- "chrono 0.4.19",
+ "chrono",
"dbus 0.9.5",
"getopts",
"lazy_static",
@@ -1047,8 +1037,7 @@
"cbindgen",
"cc",
"cfg-if",
- "chrono 0.3.0",
- "chrono 0.4.19",
+ "chrono",
"com_logger",
"crc32fast",
"crossbeam-utils",
@@ -1075,7 +1064,7 @@
"merge",
"mio 0.7.14",
"nom 7.1.1",
- "num 0.2.1",
+ "num",
"num-traits",
"once_cell",
"paste 1.0.4",
@@ -1685,7 +1674,7 @@
version = "1.6.0"
dependencies = [
"built",
- "chrono 0.4.19",
+ "chrono",
"clap 2.33.3",
"libc",
"log",
@@ -1755,7 +1744,7 @@
checksum = "6bb65943183b6b3cbf00f64c181e8178217e30194381b150e4f87ec59864c803"
dependencies = [
"lazy_static",
- "num 0.2.1",
+ "num",
]
[[package]]
@@ -2561,7 +2550,7 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4e00ad2de771fc4988af88b02cbd618c08c17920208c35c4bbfe67ccfab31eb"
dependencies = [
- "chrono 0.4.19",
+ "chrono",
"derive-getters",
"thiserror",
"xml-rs",
@@ -2585,7 +2574,7 @@
dependencies = [
"anyhow",
"bindgen 0.59.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "chrono 0.4.19",
+ "chrono",
"clap 3.2.22",
"env_logger 0.9.0",
"glob",
@@ -2701,7 +2690,7 @@
"anyhow",
"assert_matches",
"base64 0.13.0",
- "chrono 0.4.19",
+ "chrono",
"flexbuffers",
"getopts",
"libc",
@@ -2871,7 +2860,7 @@
name = "memd"
version = "0.1.0"
dependencies = [
- "chrono 0.4.19",
+ "chrono",
"dbus 0.6.5",
"env_logger 0.9.0",
"libc",
@@ -3150,17 +3139,6 @@
[[package]]
name = "num"
-version = "0.1.42"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
-dependencies = [
- "num-integer",
- "num-iter",
- "num-traits",
-]
-
-[[package]]
-name = "num"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"
@@ -3380,7 +3358,7 @@
version = "1.0.0"
dependencies = [
"anyhow",
- "chrono 0.4.19",
+ "chrono",
"crossbeam-channel",
"dbus 0.8.4",
"lazy_static",
@@ -3555,7 +3533,7 @@
name = "pinned_crates"
version = "0.0.1"
dependencies = [
- "chrono 0.4.19",
+ "chrono",
"miniz_oxide 0.4.4",
]
@@ -4897,7 +4875,7 @@
checksum = "af95cb8a5f79db5b2af2a46f44da7594b5adbcbb65cbf87b8da0959bfdd82460"
dependencies = [
"atty",
- "chrono 0.4.19",
+ "chrono",
"log",
"termcolor",
"thread_local",
@@ -5275,7 +5253,7 @@
checksum = "9ce51b50006056f590c9b7c3808c3bd70f0d1101666629713866c227d6e58d39"
dependencies = [
"ascii",
- "chrono 0.4.19",
+ "chrono",
"chunked_transfer",
"log",
"url",
@@ -6105,7 +6083,7 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "757a458f9bfab0542c11feed99bd492cbe23add50515bd8eecf8c6973673d32d"
dependencies = [
- "chrono 0.4.19",
+ "chrono",
"log",
"serde",
"thiserror",
diff --git a/projects/platform/crosvm/Cargo.toml b/projects/platform/crosvm/Cargo.toml
index 631927f..1ea150c 100644
--- a/projects/platform/crosvm/Cargo.toml
+++ b/projects/platform/crosvm/Cargo.toml
@@ -15,8 +15,7 @@
cbindgen = "0.20"
cc = "1.0.25"
cfg-if = "1"
-chrono-03 = { package = "chrono", version = "0.3" }
-chrono-04 = { package = "chrono", version = "0.4" }
+chrono = "0.4"
com_logger = "0.1"
crc32fast = "1.2"
crossbeam-utils = "0.8"
diff --git a/vendor/chrono-0.3.0/.cargo-checksum.json b/vendor/chrono-0.3.0/.cargo-checksum.json
deleted file mode 100644
index 28418fd..0000000
--- a/vendor/chrono-0.3.0/.cargo-checksum.json
+++ /dev/null
@@ -1 +0,0 @@
-{"files":{"AUTHORS.txt":"7a29a4f839fbb1db41134986c68188c28b833ee7778e9fbabdb86597b0ec60ba","CHANGELOG.md":"66a38d0d0af1b8e35c51f83d5276696eae17d56870cbba08c85ee2fd2c69ebcc","Cargo.toml":"2ba9838c9dd9b5ad554b9a782dc37c5676eff1f69e57704f3454a73805871436","LICENSE.txt":"46610329ff0b38effb9cb05979ff1ef761e465fed96b2eaca39e439d00129fd7","Makefile":"cac82a0720a931ce1bcff5fa14750745e56bd3d0b32887a977d8bc6396eed974","README.md":"13672d1f65291224b884a2790abc4371b31e5521808266e745c98ee143ffe45d","appveyor.yml":"e0d9d8df22e519bd822102149c03f293ab03b07198157df45935fcbb04f210d4","src/date.rs":"c45c7810c4c2186989b0252c9f361b496894edb78db52a0fb308d8158de3e347","src/datetime.rs":"48c326a60cf86de2652aa7360b814d25a7d1db7faaf5e93f9d13618a84d6c590","src/div.rs":"6ee3e5355209bfe45a4f65e8227adec26dc708b60a18e88085f2d9d19223bc27","src/format/mod.rs":"470008d8752bcfd1aa1189ba525fe643db5f9cb613226b5f2d9d3bd5a2e226f3","src/format/parse.rs":"fdea197dc8fca006210b82f3f9c9cea8d11b7a08a2022e1e0b9b319aadd6c8d4","src/format/parsed.rs":"ceeaaeab272b39f55ebe115b06372544d74d0729f33eda69f5472b917f492b17","src/format/scan.rs":"cf09c813f0aa98f51f6d2ec948ef8a6196c6eaa50801dcd104fef13a9e24165a","src/format/strftime.rs":"c4928b9604e81797f7109bcaceb9be4247ca98514c7b75d30938bbdab2e5c990","src/lib.rs":"85a4ee63720d52fc942b119eaf96fb6eb8f63d3650975309bca523d3a96f8d6a","src/naive/date.rs":"949cafd14db01c7f56e19228c56c718432757a3452de3cd8d64ea1cbb3afd23f","src/naive/datetime.rs":"3daa9cf4a98c24350e7102045dfbb83a6282a74815ae0c2f4003e13094d6724a","src/naive/time.rs":"9a2a45656124919ea447289e73f5c6596022b7e3bb3df8a38d5984ce29fa236a","src/offset/fixed.rs":"27f7fcfa4f3ce92b94304c16b0cc9375bbe6886bebe6adf01e1fff4dbf2d996d","src/offset/local.rs":"7a1e70a74d72793a1e6c6a42a2616b46ca607510460aee6c0b64a98099adcc2e","src/offset/mod.rs":"16a8a825015c93be3076ea9c2bca11fe9149621d01541e2887baaeb4b8428382","src/offset/utc.rs":"19f2dd86c0ca5fb86d8f0238e3cf6097de002d65ba9679adb5740da4feba9e28"},"package":"158b0bd7d75cbb6bf9c25967a48a2e9f77da95876b858eadfabaa99cd069de6e"}
\ No newline at end of file
diff --git a/vendor/chrono-0.3.0/AUTHORS.txt b/vendor/chrono-0.3.0/AUTHORS.txt
deleted file mode 100644
index f23d3eb..0000000
--- a/vendor/chrono-0.3.0/AUTHORS.txt
+++ /dev/null
@@ -1,34 +0,0 @@
-Chrono is mainly written by Kang Seonghoon <public+rust@mearie.org>,
-and also the following people (in ascending order):
-
-Alex Mikhalev <alexmikhalevalex@gmail.com>
-Alexander Bulaev <alexbool@yandex-team.ru>
-Ashley Mannix <ashleymannix@live.com.au>
-Ben Eills <ben@beneills.com>
-Brandon W Maister <bwm@knewton.com>
-Colin Ray <r.colinray@gmail.com>
-Corey Farwell <coreyf@rwell.org>
-Dan <dan@ebip.co.uk>
-Danilo Bargen <mail@dbrgn.ch>
-David Hewson <dev@daveid.co.uk>
-David Ross <daboross@daboross.net>
-David Tolnay <dtolnay@gmail.com>
-David Willie <david.willie.1@gmail.com>
-Eunchong Yu <kroisse@gmail.com>
-Huon Wilson <dbau.pp+github@gmail.com>
-Jim Turner <jturner314@gmail.com>
-Jisoo Park <xxxyel@gmail.com>
-Joe Wilm <joe@jwilm.com>
-John Heitmann <jheitmann@gmail.com>
-John Nagle <nagle@sitetruth.com>
-Jonas mg <jonasmg@yepmail.net>
-János Illés <ijanos@gmail.com>
-Ken Tossell <ken@tossell.net>
-Martin Risell Lilja <martin.risell.lilja@gmail.com>
-Richard Petrie <rap1011@ksu.edu>
-Ryan Lewis <ryansname@gmail.com>
-Sergey V. Galtsev <sergey.v.galtsev@github.com>
-Steve Klabnik <steve@steveklabnik.com>
-Tom Gallacher <tomgallacher23@gmail.com>
-klutzy <klutzytheklutzy@gmail.com>
-kud1ing <github@kudling.de>
diff --git a/vendor/chrono-0.3.0/CHANGELOG.md b/vendor/chrono-0.3.0/CHANGELOG.md
deleted file mode 100644
index 1915bf5..0000000
--- a/vendor/chrono-0.3.0/CHANGELOG.md
+++ /dev/null
@@ -1,384 +0,0 @@
-ChangeLog for Chrono
-====================
-
-This documents all notable changes to [Chrono](https://github.com/chronotope/chrono).
-
-Chrono obeys the principle of [Semantic Versioning](http://semver.org/).
-
-There were/are numerous minor versions before 1.0 due to the language changes.
-Versions with only mechnical changes will be omitted from the following list.
-
-## 0.3.0 (2017-02-07)
-
-The project has moved to the [Chronotope](https://github.com/chronotope/) organization.
-
-### Added
-
-- `chrono::prelude` module has been added. All other glob imports are now discouraged.
-
-- `FixedOffset` can be added to or subtracted from any timelike types.
-
- - `FixedOffset::local_minus_utc` and `FixedOffset::utc_minus_local` methods have been added.
- Note that the old `Offset::local_minus_utc` method is gone; see below.
-
-- Serde support for non-self-describing formats like Bincode is added. (#89)
-
-- Added `Item::Owned{Literal,Space}` variants for owned formatting items. (#76)
-
-- Formatting items and the `Parsed` type have been slightly adjusted so that
- they can be internally extended without breaking any compatibility.
-
-- `Weekday` is now `Hash`able. (#109)
-
-- `ParseError` now implements `Eq` as well as `PartialEq`. (#114)
-
-- More documentation improvements. (#101, #108, #112)
-
-### Changed
-
-- Chrono now only supports Rust 1.13.0 or later (previously: Rust 1.8.0 or later).
-
-- Serde 0.9 is now supported.
- Due to the API difference, support for 0.8 or older is discontinued. (#122)
-
-- Rustc-serialize implementations are now on par with corresponding Serde implementations.
- They both standardize on the `std::fmt::Debug` textual output.
-
- **This is a silent breaking change (hopefully the last though).**
- You should be prepared for the format change if you depended on rustc-serialize.
-
-- `Offset::local_minus_utc` is now `Offset::fix`, and returns `FixedOffset` instead of a duration.
-
- This makes every time zone operation operate within a bias less than one day,
- and vastly simplifies many logics.
-
-- `chrono::format::format` now receives `FixedOffset` instead of `time::Duration`.
-
-- The following methods and implementations have been renamed and older names have been *removed*.
- The older names will be reused for the same methods with `std::time::Duration` in the future.
-
- - `checked_*` → `checked_*_signed` in `Date`, `DateTime`, `NaiveDate` and `NaiveDateTime` types
-
- - `overflowing_*` → `overflowing_*_signed` in the `NaiveTime` type
-
- - All subtraction implementations between two time instants have been moved to
- `signed_duration_since`, following the naming in `std::time`.
-
-### Fixed
-
-- Fixed a panic when the `Local` offset receives a leap second. (#123)
-
-### Removed
-
-- Rustc-serialize support for `Date<Tz>` types and all offset types has been dropped.
-
- These implementations were automatically derived and never had been in a good shape.
- Moreover there are no corresponding Serde implementations, limiting their usefulness.
- In the future they may be revived with more complete implementations.
-
-- The following method aliases deprecated in the 0.2 branch have been removed.
-
- - `DateTime::num_seconds_from_unix_epoch` (→ `DateTime::timestamp`)
- - `NaiveDateTime::from_num_seconds_from_unix_epoch` (→ `NaiveDateTime::from_timestamp`)
- - `NaiveDateTime::from_num_seconds_from_unix_epoch_opt` (→ `NaiveDateTime::from_timestamp_opt`)
- - `NaiveDateTime::num_seconds_unix_epoch` (→ `NaiveDateTime::timestamp`)
-
-- Formatting items are no longer `Copy`, except for `chrono::format::Pad`.
-
-- `chrono::offset::add_with_leapsecond` has been removed.
- Use a direct addition with `FixedOffset` instead.
-
-## 0.2.25 (2016-08-04)
-
-This is the last version officially supports Rust 1.12.0 or older.
-
-(0.2.24 was accidentally uploaded without a proper check for warnings in the default state,
-and replaced by 0.2.25 very shortly. Duh.)
-
-### Added
-
-- Serde 0.8 is now supported. 0.7 also remains supported. (#86)
-
-### Fixed
-
-- The deserialization implementation for rustc-serialize now properly verifies the input.
- All serialization codes are also now thoroughly tested. (#42)
-
-## 0.2.23 (2016-08-03)
-
-### Added
-
-- The documentation was greatly improved for several types,
- and tons of cross-references have been added. (#77, #78, #80, #82)
-
-- `DateTime::timestamp_subsec_{millis,micros,nanos}` methods have been added. (#81)
-
-### Fixed
-
-- When the system time records a leap second,
- the nanosecond component was mistakenly reset to zero. (#84)
-
-- `Local` offset misbehaves in Windows for August and later,
- due to the long-standing libtime bug (dates back to mid-2015).
- Workaround has been implemented. (#85)
-
-## 0.2.22 (2016-04-22)
-
-### Fixed
-
-- `%.6f` and `%.9f` used to print only three digits when the nanosecond part is zero. (#71)
-- The documentation for `%+` has been updated to reflect the current status. (#71)
-
-## 0.2.21 (2016-03-29)
-
-### Fixed
-
-- `Fixed::LongWeekdayName` was unable to recognize `"sunday"` (whoops). (#66)
-
-## 0.2.20 (2016-03-06)
-
-### Changed
-
-- `serde` dependency has been updated to 0.7. (#63, #64)
-
-## 0.2.19 (2016-02-05)
-
-### Added
-
-- The documentation for `Date` is made clear about its ambiguity and guarantees.
-
-### Fixed
-
-- `DateTime::date` had been wrong when the local date and the UTC date is in disagreement. (#61)
-
-## 0.2.18 (2016-01-23)
-
-### Fixed
-
-- Chrono no longer pulls a superfluous `rand` dependency. (#57)
-
-## 0.2.17 (2015-11-22)
-
-### Added
-
-- Naive date and time types and `DateTime` now have a `serde` support.
- They serialize as an ISO 8601 / RFC 3339 string just like `Debug`. (#51)
-
-## 0.2.16 (2015-09-06)
-
-### Added
-
-- Added `%.3f`, `%.6f` and `%.9f` specifier for formatting fractional seconds
- up to 3, 6 or 9 decimal digits. This is a natural extension to the existing `%f`.
- Note that this is (not yet) generic, no other value of precision is supported. (#45)
-
-### Changed
-
-- Forbade unsized types from implementing `Datelike` and `Timelike`.
- This does not make a big harm as any type implementing them should be already sized
- to be practical, but this change still can break highly generic codes. (#46)
-
-### Fixed
-
-- Fixed a broken link in the `README.md`. (#41)
-
-## 0.2.15 (2015-07-05)
-
-### Added
-
-- Padding modifiers `%_?`, `%-?` and `%0?` are implemented.
- They are glibc extensions which seem to be reasonably widespread (e.g. Ruby).
-
-- Added `%:z` specifier and corresponding formatting items
- which is essentially same to `%z` but with a colon.
-
-- Added a new specifier `%.f` which precision adapts from the input.
- This was added as a response to the UX problems in the original nanosecond specifier `%f`.
-
-### Fixed
-
-- `Numeric::Timestamp` specifier (`%s`) was ignoring the time zone offset when provided.
-
-- Improved the documentation and associated tests for `strftime`.
-
-## 0.2.14 (2015-05-15)
-
-### Fixed
-
-- `NaiveDateTime +/- Duration` or `NaiveTime +/- Duration` could have gone wrong
- when the `Duration` to be added is negative and has a fractional second part.
- This was caused by an underflow in the conversion from `Duration` to the parts;
- the lack of tests for this case allowed a bug. (#37)
-
-## 0.2.13 (2015-04-29)
-
-### Added
-
-- The optional dependency on `rustc_serialize` and
- relevant `Rustc{En,De}codable` implementations for supported types has been added.
- This is enabled by the `rustc-serialize` Cargo feature. (#34)
-
-### Changed
-
-- `chrono::Duration` reexport is changed to that of crates.io `time` crate.
- This enables Rust 1.0 beta compatibility.
-
-## 0.2.4 (2015-03-03)
-
-### Fixed
-
-- Clarified the meaning of `Date<Tz>` and fixed unwanted conversion problem
- that only occurs with positive UTC offsets. (#27)
-
-## 0.2.3 (2015-02-27)
-
-### Added
-
-- `DateTime<Tz>` and `Date<Tz>` is now `Copy`/`Send` when `Tz::Offset` is `Copy`/`Send`.
- The implementations for them were mistakenly omitted. (#25)
-
-### Fixed
-
-- `Local::from_utc_datetime` didn't set a correct offset. (#26)
-
-## 0.2.1 (2015-02-21)
-
-### Changed
-
-- `DelayedFormat` no longer conveys a redundant lifetime.
-
-## 0.2.0 (2015-02-19)
-
-### Added
-
-- `Offset` is splitted into `TimeZone` (constructor) and `Offset` (storage) types.
- You would normally see only the former, as the latter is mostly an implementation detail.
- Most importantly, `Local` now can be used to directly construct timezone-aware values.
-
- Some types (currently, `UTC` and `FixedOffset`) are both `TimeZone` and `Offset`,
- but others aren't (e.g. `Local` is not what is being stored to each `DateTime` values).
-
-- `LocalResult::map` convenience method has been added.
-
-- `TimeZone` now allows a construction of `DateTime` values from UNIX timestamp,
- via `timestamp` and `timestamp_opt` methods.
-
-- `TimeZone` now also has a method for parsing `DateTime`, namely `datetime_from_str`.
-
-- The following methods have been added to all date and time types:
-
- - `checked_add`
- - `checked_sub`
- - `format_with_items`
-
-- The following methods have been added to all timezone-aware types:
-
- - `timezone`
- - `with_timezone`
- - `naive_utc`
- - `naive_local`
-
-- `parse_from_str` method has been added to all naive types and `DateTime<FixedOffset>`.
-
-- All naive types and instances of `DateTime` with time zones `UTC`, `Local` and `FixedOffset`
- implement the `FromStr` trait. They parse what `std::fmt::Debug` would print.
-
-- `chrono::format` has been greatly rewritten.
-
- - The formatting syntax parser is modular now, available at `chrono::format::strftime`.
-
- - The parser and resolution algorithm is also modular, the former is available at
- `chrono::format::parse` while the latter is available at `chrono::format::parsed`.
-
- - Explicit support for RFC 2822 and 3339 syntaxes is landed.
-
- - There is a minor formatting difference with atypical values,
- e.g. for years not between 1 BCE and 9999 CE.
-
-### Changed
-
-- Most uses of `Offset` are converted to `TimeZone`.
- In fact, *all* user-facing code is expected to be `Offset`-free.
-
-- `[Naive]DateTime::*num_seconds_from_unix_epoch*` methods have been renamed to
- simply `timestamp` or `from_timestamp*`. The original names have been deprecated.
-
-### Removed
-
-- `Time` has been removed. This also prompts a related set of methods in `TimeZone`.
-
- This is in principle possible, but in practice has seen a little use
- because it can only be meaningfully constructed via an existing `DateTime` value.
- This made many operations to `Time` unintuitive or ambiguous,
- so we simply let it go.
-
- In the case that `Time` is really required, one can use a simpler `NaiveTime`.
- `NaiveTime` and `NaiveDate` can be freely combined and splitted,
- and `TimeZone::from_{local,utc}_datetime` can be used to convert from/to the local time.
-
-- `with_offset` method has been removed. Use `with_timezone` method instead.
- (This is not deprecated since it is an integral part of offset reform.)
-
-## 0.1.14 (2015-01-10)
-
-### Added
-
-- Added a missing `std::fmt::String` impl for `Local`.
-
-## 0.1.13 (2015-01-10)
-
-### Changed
-
-- Most types now implement both `std::fmt::Show` and `std::fmt::String`,
- with the former used for the stricter output and the latter used for more casual output.
-
-### Removed
-
-- `Offset::name` has been replaced by a `std::fmt::String` implementation to `Offset`.
-
-## 0.1.12 (2015-01-08)
-
-### Removed
-
-- `Duration + T` no longer works due to the updated impl reachability rules.
- Use `T + Duration` as a workaround.
-
-## 0.1.4 (2014-12-13)
-
-### Fixed
-
-- Fixed a bug that `Date::and_*` methods with an offset that can change the date are
- off by one day.
-
-## 0.1.3 (2014-11-28)
-
-### Added
-
-- `{Date,Time,DateTime}::with_offset` methods have been added.
-
-- `LocalResult` now implements a common set of traits.
-
-- `LocalResult::and_*` methods have been added.
- They are useful for safely chaining `LocalResult<Date<Off>>` methods
- to make `LocalResult<DateTime<Off>>`.
-
-### Changed
-
-- `Offset::name` now returns `SendStr`.
-
-- `{Date,Time} - Duration` overloadings are now allowed.
-
-## 0.1.2 (2014-11-24)
-
-### Added
-
-- `Duration + Date` overloading is now allowed.
-
-### Changed
-
-- Chrono no longer needs `num` dependency.
-
-## 0.1.0 (2014-11-20)
-
-The initial version that was available to `crates.io`.
-
diff --git a/vendor/chrono-0.3.0/Cargo.toml b/vendor/chrono-0.3.0/Cargo.toml
deleted file mode 100644
index 51d71c3..0000000
--- a/vendor/chrono-0.3.0/Cargo.toml
+++ /dev/null
@@ -1,30 +0,0 @@
-[package]
-name = "chrono"
-version = "0.3.0"
-authors = ["Kang Seonghoon <public+rust@mearie.org>"]
-
-description = "Date and time library for Rust"
-homepage = "https://github.com/chronotope/chrono"
-documentation = "https://docs.rs/chrono/"
-repository = "https://github.com/chronotope/chrono"
-keywords = ["date", "time", "calendar"]
-categories = ["date-and-time"]
-readme = "README.md"
-license = "MIT/Apache-2.0"
-
-[badges]
-travis-ci = { repository = "chronotope/chrono" }
-appveyor = { repository = "chronotope/chrono" }
-
-[lib]
-name = "chrono"
-
-[dependencies]
-time = "^0.1.36"
-num = { version = "0.1", default-features = false }
-rustc-serialize = { version = "0.3", optional = true }
-serde = { version = "0.9", optional = true }
-
-[dev-dependencies]
-serde_json = { version = ">=0.9.0" }
-bincode = { version = "1.0.0-alpha1", features = ["serde"], default-features = false }
diff --git a/vendor/chrono-0.3.0/LICENSE.txt b/vendor/chrono-0.3.0/LICENSE.txt
deleted file mode 100644
index 924ff57..0000000
--- a/vendor/chrono-0.3.0/LICENSE.txt
+++ /dev/null
@@ -1,240 +0,0 @@
-Rust-chrono is dual-licensed under The MIT License [1] and
-Apache 2.0 License [2]. Copyright (c) 2014--2017, Kang Seonghoon and
-contributors.
-
-Nota Bene: This is same as the Rust Project's own license.
-
-
-[1]: <http://opensource.org/licenses/MIT>, which is reproduced below:
-
-~~~~
-The MIT License (MIT)
-
-Copyright (c) 2014, Kang Seonghoon.
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-~~~~
-
-
-[2]: <http://www.apache.org/licenses/LICENSE-2.0>, which is reproduced below:
-
-~~~~
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
-TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
-1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
-2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
-3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
-4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
-5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
-6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
-7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
-8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
-9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
-END OF TERMS AND CONDITIONS
-
-APPENDIX: How to apply the Apache License to your work.
-
- To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "[]"
- replaced with your own identifying information. (Don't include
- the brackets!) The text should be enclosed in the appropriate
- comment syntax for the file format. We also recommend that a
- file or class name and description of purpose be included on the
- same "printed page" as the copyright notice for easier
- identification within third-party archives.
-
-Copyright [yyyy] [name of copyright owner]
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-~~~~
-
diff --git a/vendor/chrono-0.3.0/Makefile b/vendor/chrono-0.3.0/Makefile
deleted file mode 100644
index e39ffcf..0000000
--- a/vendor/chrono-0.3.0/Makefile
+++ /dev/null
@@ -1,53 +0,0 @@
-# this Makefile is mostly for the packaging convenience.
-# casual users should use `cargo` to retrieve the appropriate version of Chrono.
-
-.PHONY: all
-all:
- @echo 'Try `cargo build` instead.'
-
-.PHONY: authors
-authors:
- echo 'Chrono is mainly written by Kang Seonghoon <public+rust@mearie.org>,' > AUTHORS.txt
- echo 'and also the following people (in ascending order):' >> AUTHORS.txt
- echo >> AUTHORS.txt
- git log --format='%aN <%aE>' | grep -v 'Kang Seonghoon' | sort -u >> AUTHORS.txt
-
-.PHONY: readme
-readme: README.md
-
-README.md: src/lib.rs
- # really, really sorry for this mess.
- ( \
- VERSION="$$(cargo pkgid | cut -d: -f3)"; \
- awk '/^\/\/! # Chrono /{print "[Chrono][docsrs]",$$4}' $<; \
- awk '/^\/\/! # Chrono /{print "[Chrono][docsrs]",$$4}' $< | sed 's/./=/g'; \
- echo; \
- echo '[![Chrono on Travis CI][travis-image]][travis]'; \
- echo '[![Chrono on Appveyor][appveyor-image]][appveyor]'; \
- echo '[![Chrono on crates.io][cratesio-image]][cratesio]'; \
- echo '[![Chrono on docs.rs][docsrs-image]][docsrs]'; \
- echo; \
- echo '[travis-image]: https://travis-ci.org/chronotope/chrono.svg?branch=master'; \
- echo '[travis]: https://travis-ci.org/chronotope/chrono'; \
- echo '[appveyor-image]: https://ci.appveyor.com/api/projects/status/2ia91ofww4w31m2w/branch/master?svg=true'; \
- echo '[appveyor]: https://ci.appveyor.com/project/chronotope/chrono'; \
- echo '[cratesio-image]: https://img.shields.io/crates/v/chrono.svg'; \
- echo '[cratesio]: https://crates.io/crates/chrono'; \
- echo '[docsrs-image]: https://docs.rs/chrono/badge.svg?version='$$VERSION; \
- echo '[docsrs]: https://docs.rs/chrono/'$$VERSION'/'; \
- echo; \
- awk '/^\/\/! # Chrono /,/^\/\/! ## /' $< | cut -b 5- | grep -v '^#' | \
- sed 's/](\.\//](https:\/\/docs.rs\/chrono\/'$$VERSION'\/chrono\//g'; \
- echo; \
- awk '/^\/\/! ## /,!/^\/\/!/' $< | cut -b 5- | grep -v '^# ' | \
- sed 's/](\.\//](https:\/\/docs.rs\/chrono\/'$$VERSION'\/chrono\//g' \
- ) > $@
-
-.PHONY: test
-test:
- cargo test --features 'serde rustc-serialize bincode'
-
-.PHONY: doc
-doc: authors readme
- cargo doc --features 'serde rustc-serialize bincode'
-
diff --git a/vendor/chrono-0.3.0/README.md b/vendor/chrono-0.3.0/README.md
deleted file mode 100644
index 2519149..0000000
--- a/vendor/chrono-0.3.0/README.md
+++ /dev/null
@@ -1,353 +0,0 @@
-[Chrono][docsrs] 0.3.0
-======================
-
-[![Chrono on Travis CI][travis-image]][travis]
-[![Chrono on Appveyor][appveyor-image]][appveyor]
-[![Chrono on crates.io][cratesio-image]][cratesio]
-[![Chrono on docs.rs][docsrs-image]][docsrs]
-
-[travis-image]: https://travis-ci.org/chronotope/chrono.svg?branch=master
-[travis]: https://travis-ci.org/chronotope/chrono
-[appveyor-image]: https://ci.appveyor.com/api/projects/status/2ia91ofww4w31m2w/branch/master?svg=true
-[appveyor]: https://ci.appveyor.com/project/chronotope/chrono
-[cratesio-image]: https://img.shields.io/crates/v/chrono.svg
-[cratesio]: https://crates.io/crates/chrono
-[docsrs-image]: https://docs.rs/chrono/badge.svg?version=0.3.0
-[docsrs]: https://docs.rs/chrono/0.3.0/
-
-
-Date and time handling for Rust.
-It aims to be a feature-complete superset of
-the [time](https://github.com/rust-lang-deprecated/time) library.
-In particular,
-
-* Chrono strictly adheres to ISO 8601.
-* Chrono is timezone-aware by default, with separate timezone-naive types.
-* Chrono is space-optimal and (while not being the primary goal) reasonably efficient.
-
-There were several previous attempts to bring a good date and time library to Rust,
-which Chrono builts upon and should acknowledge:
-
-* [Initial research on
- the wiki](https://github.com/rust-lang/rust-wiki-backup/blob/master/Lib-datetime.md)
-* Dietrich Epp's [datetime-rs](https://github.com/depp/datetime-rs)
-* Luis de Bethencourt's [rust-datetime](https://github.com/luisbg/rust-datetime)
-
-
-## Usage
-
-Put this in your `Cargo.toml`:
-
-```toml
-[dependencies]
-chrono = "0.3"
-```
-
-Or, if you want [Serde](https://github.com/serde-rs/serde) or
-[rustc-serialize](https://github.com/rust-lang-nursery/rustc-serialize) support,
-include the features like this:
-
-```toml
-[dependencies]
-chrono = { version = "0.3", features = ["serde", "rustc-serialize"] }
-```
-
-Then put this in your crate root:
-
-```rust
-extern crate chrono;
-```
-
-Avoid using `use chrono::*;` as Chrono exports several modules other than types.
-If you prefer the glob imports, use the following instead:
-
-```rust
-use chrono::prelude::*;
-```
-
-## Overview
-
-### Duration
-
-Chrono currently uses
-the [`time::Duration`](https://doc.rust-lang.org/time/time/struct.Duration.html) type
-from the `time` crate to represent the magnitude of a time span.
-Since this has the same name to the newer, standard type for duration,
-the reference will refer this type as `OldDuration`.
-Note that this is an "accurate" duration represented as seconds and
-nanoseconds and does not represent "nominal" components such as days or
-months.
-
-Chrono does not yet natively support
-the standard [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) type,
-but it will be supported in the future.
-Meanwhile you can convert between two types with
-[`Duration::from_std`](https://doc.rust-lang.org/time/time/struct.Duration.html#method.from_std)
-and
-[`Duration::to_std`](https://doc.rust-lang.org/time/time/struct.Duration.html#method.to_std)
-methods.
-
-### Date and Time
-
-Chrono provides a
-[**`DateTime`**](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html)
-type to represent a date and a time in a timezone.
-
-For more abstract moment-in-time tracking such as internal timekeeping
-that is unconcerned with timezones, consider
-[`time::SystemTime`](https://doc.rust-lang.org/std/time/struct.SystemTime.html),
-which tracks your system clock, or
-[`time::Instant`](https://doc.rust-lang.org/std/time/struct.Instant.html), which
-is an opaque but monotonically-increasing representation of a moment in time.
-
-`DateTime` is timezone-aware and must be constructed from
-the [**`TimeZone`**](https://docs.rs/chrono/0.3.0/chrono/offset/trait.TimeZone.html) object,
-which defines how the local date is converted to and back from the UTC date.
-There are three well-known `TimeZone` implementations:
-
-* [**`UTC`**](https://docs.rs/chrono/0.3.0/chrono/offset/utc/struct.UTC.html) specifies the UTC time zone. It is most efficient.
-
-* [**`Local`**](https://docs.rs/chrono/0.3.0/chrono/offset/local/struct.Local.html) specifies the system local time zone.
-
-* [**`FixedOffset`**](https://docs.rs/chrono/0.3.0/chrono/offset/fixed/struct.FixedOffset.html) specifies
- an arbitrary, fixed time zone such as UTC+09:00 or UTC-10:30.
- This often results from the parsed textual date and time.
- Since it stores the most information and does not depend on the system environment,
- you would want to normalize other `TimeZone`s into this type.
-
-`DateTime`s with different `TimeZone` types are distinct and do not mix,
-but can be converted to each other using
-the [`DateTime::with_timezone`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.with_timezone) method.
-
-You can get the current date and time in the UTC time zone
-([`UTC::now()`](https://docs.rs/chrono/0.3.0/chrono/offset/utc/struct.UTC.html#method.now))
-or in the local time zone
-([`Local::now()`](https://docs.rs/chrono/0.3.0/chrono/offset/local/struct.Local.html#method.now)).
-
-~~~~ {.rust}
-use chrono::prelude::*;
-
-let utc: DateTime<UTC> = UTC::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
-let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
-~~~~
-
-Alternatively, you can create your own date and time.
-This is a bit verbose due to Rust's lack of function and method overloading,
-but in turn we get a rich combination of initialization methods.
-
-~~~~ {.rust}
-use chrono::prelude::*;
-use chrono::offset::LocalResult;
-
-let dt = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
-// July 8 is 188th day of the year 2014 (`o` for "ordinal")
-assert_eq!(dt, UTC.yo(2014, 189).and_hms(9, 10, 11));
-// July 8 is Tuesday in ISO week 28 of the year 2014.
-assert_eq!(dt, UTC.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
-
-let dt = UTC.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); // `2014-07-08T09:10:11.012Z`
-assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_micro(9, 10, 11, 12_000));
-assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_nano(9, 10, 11, 12_000_000));
-
-// dynamic verification
-assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(21, 15, 33),
- LocalResult::Single(UTC.ymd(2014, 7, 8).and_hms(21, 15, 33)));
-assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(80, 15, 33), LocalResult::None);
-assert_eq!(UTC.ymd_opt(2014, 7, 38).and_hms_opt(21, 15, 33), LocalResult::None);
-
-// other time zone objects can be used to construct a local datetime.
-// obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical.
-let local_dt = Local.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12);
-let fixed_dt = FixedOffset::east(9 * 3600).ymd(2014, 7, 8).and_hms_milli(18, 10, 11, 12);
-assert_eq!(dt, fixed_dt);
-~~~~
-
-Various properties are available to the date and time, and can be altered individually.
-Most of them are defined in the traits [`Datelike`](https://docs.rs/chrono/0.3.0/chrono/trait.Datelike.html) and
-[`Timelike`](https://docs.rs/chrono/0.3.0/chrono/trait.Timelike.html) which you should `use` before.
-Addition and subtraction is also supported.
-The following illustrates most supported operations to the date and time:
-
-~~~~ {.rust}
-use chrono::prelude::*;
-use time::Duration;
-
-// assume this returned `2014-11-28T21:45:59.324310806+09:00`:
-let dt = Local::now();
-
-// property accessors
-assert_eq!((dt.year(), dt.month(), dt.day()), (2014, 11, 28));
-assert_eq!((dt.month0(), dt.day0()), (10, 27)); // for unfortunate souls
-assert_eq!((dt.hour(), dt.minute(), dt.second()), (21, 45, 59));
-assert_eq!(dt.weekday(), Weekday::Fri);
-assert_eq!(dt.weekday().number_from_monday(), 5); // Mon=1, ..., Sat=7
-assert_eq!(dt.ordinal(), 332); // the day of year
-assert_eq!(dt.num_days_from_ce(), 735565); // the number of days from and including Jan 1, 1
-
-// time zone accessor and manipulation
-assert_eq!(dt.offset().fix().local_minus_utc(), 9 * 3600);
-assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600));
-assert_eq!(dt.with_timezone(&UTC), UTC.ymd(2014, 11, 28).and_hms_nano(12, 45, 59, 324310806));
-
-// a sample of property manipulations (validates dynamically)
-assert_eq!(dt.with_day(29).unwrap().weekday(), Weekday::Sat); // 2014-11-29 is Saturday
-assert_eq!(dt.with_day(32), None);
-assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November 29, 301 BCE
-
-// arithmetic operations
-let dt1 = UTC.ymd(2014, 11, 14).and_hms(8, 9, 10);
-let dt2 = UTC.ymd(2014, 11, 14).and_hms(10, 9, 8);
-assert_eq!(dt1.signed_duration_since(dt2), Duration::seconds(-2 * 3600 + 2));
-assert_eq!(dt2.signed_duration_since(dt1), Duration::seconds(2 * 3600 - 2));
-assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) + Duration::seconds(1_000_000_000),
- UTC.ymd(2001, 9, 9).and_hms(1, 46, 40));
-assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_000),
- UTC.ymd(1938, 4, 24).and_hms(22, 13, 20));
-~~~~
-
-Formatting is done via the [`format`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.format) method,
-which format is equivalent to the familiar `strftime` format.
-(See the [`format::strftime` module documentation](https://docs.rs/chrono/0.3.0/chrono/format/strftime/index.html#specifiers)
-for full syntax.)
-
-The default `to_string` method and `{:?}` specifier also give a reasonable representation.
-Chrono also provides [`to_rfc2822`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.to_rfc2822) and
-[`to_rfc3339`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.to_rfc3339) methods
-for well-known formats.
-
-~~~~ {.rust}
-use chrono::prelude::*;
-
-let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
-assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09");
-assert_eq!(dt.format("%a %b %e %T %Y").to_string(), "Fri Nov 28 12:00:09 2014");
-assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string());
-
-assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC");
-assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000");
-assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00");
-assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
-~~~~
-
-Parsing can be done with three methods:
-
-1. The standard [`FromStr`](https://doc.rust-lang.org/std/str/trait.FromStr.html) trait
- (and [`parse`](https://doc.rust-lang.org/std/primitive.str.html#method.parse) method
- on a string) can be used for parsing `DateTime<FixedOffset>`, `DateTime<UTC>` and
- `DateTime<Local>` values. This parses what the `{:?}`
- ([`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html))
- format specifier prints, and requires the offset to be present.
-
-2. [`DateTime::parse_from_str`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.parse_from_str) parses
- 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.
- It *cannot* be used when the offset can be missing.
- [`DateTime::parse_from_rfc2822`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.parse_from_rfc2822)
- and
- [`DateTime::parse_from_rfc3339`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.parse_from_rfc3339)
- are similar but for well-known formats.
-
-3. [`Offset::datetime_from_str`](https://docs.rs/chrono/0.3.0/chrono/offset/trait.TimeZone.html#method.datetime_from_str) is
- similar but returns `DateTime` of given offset.
- When the explicit offset is missing from the input, it simply uses given offset.
- It issues an error when the input contains an explicit offset different
- from the current offset.
-
-More detailed control over the parsing process is available via
-[`format`](https://docs.rs/chrono/0.3.0/chrono/format/index.html) module.
-
-~~~~ {.rust}
-use chrono::prelude::*;
-
-let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
-let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600));
-
-// method 1
-assert_eq!("2014-11-28T12:00:09Z".parse::<DateTime<UTC>>(), Ok(dt.clone()));
-assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<UTC>>(), Ok(dt.clone()));
-assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<FixedOffset>>(), Ok(fixed_dt.clone()));
-
-// method 2
-assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"),
- Ok(fixed_dt.clone()));
-assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
- Ok(fixed_dt.clone()));
-assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone()));
-
-// method 3
-assert_eq!(UTC.datetime_from_str("2014-11-28 12:00:09", "%Y-%m-%d %H:%M:%S"), Ok(dt.clone()));
-assert_eq!(UTC.datetime_from_str("Fri Nov 28 12:00:09 2014", "%a %b %e %T %Y"), Ok(dt.clone()));
-
-// oops, the year is missing!
-assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T %Y").is_err());
-// oops, the format string does not include the year at all!
-assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T").is_err());
-// oops, the weekday is incorrect!
-assert!(UTC.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_err());
-~~~~
-
-### Individual date
-
-Chrono also provides an individual date type ([**`Date`**](https://docs.rs/chrono/0.3.0/chrono/date/struct.Date.html)).
-It also has time zones attached, and have to be constructed via time zones.
-Most operations available to `DateTime` are also available to `Date` whenever appropriate.
-
-~~~~ {.rust}
-use chrono::prelude::*;
-use chrono::offset::LocalResult;
-
-assert_eq!(UTC::today(), UTC::now().date());
-assert_eq!(Local::today(), Local::now().date());
-
-assert_eq!(UTC.ymd(2014, 11, 28).weekday(), Weekday::Fri);
-assert_eq!(UTC.ymd_opt(2014, 11, 31), LocalResult::None);
-assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(),
- "070809");
-~~~~
-
-There is no timezone-aware `Time` due to the lack of usefulness and also the complexity.
-
-`DateTime` has [`date`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.date) method
-which returns a `Date` which represents its date component.
-There is also a [`time`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.time) method,
-which simply returns a naive local time described below.
-
-### Naive date and time
-
-Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime`
-as [**`NaiveDate`**](https://docs.rs/chrono/0.3.0/chrono/naive/date/struct.NaiveDate.html),
-[**`NaiveTime`**](https://docs.rs/chrono/0.3.0/chrono/naive/time/struct.NaiveTime.html) and
-[**`NaiveDateTime`**](https://docs.rs/chrono/0.3.0/chrono/naive/datetime/struct.NaiveDateTime.html) respectively.
-
-They have almost equivalent interfaces as their timezone-aware twins,
-but are not associated to time zones obviously and can be quite low-level.
-They are mostly useful for building blocks for higher-level types.
-
-Timezone-aware `DateTime` and `Date` types have two methods returning naive versions:
-[`naive_local`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.naive_local) returns
-a view to the naive local time,
-and [`naive_utc`](https://docs.rs/chrono/0.3.0/chrono/datetime/struct.DateTime.html#method.naive_utc) returns
-a view to the naive UTC time.
-
-## Limitations
-
-Only proleptic Gregorian calendar (i.e. extended to support older dates) is supported.
-Be very careful if you really have to deal with pre-20C dates, they can be in Julian or others.
-
-Date types are limited in about +/- 262,000 years from the common epoch.
-Time types are limited in the nanosecond accuracy.
-
-[Leap seconds are supported in the representation but
-Chrono doesn't try to make use of them](https://docs.rs/chrono/0.3.0/chrono/naive/time/index.html#leap-second-handling).
-(The main reason is that leap seconds are not really predictable.)
-Almost *every* operation over the possible leap seconds will ignore them.
-Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale
-if you want.
-
-Chrono inherently does not support an inaccurate or partial date and time representation.
-Any operation that can be ambiguous will return `None` in such cases.
-For example, "a month later" of 2014-01-30 is not well-defined
-and consequently `UTC.ymd(2014, 1, 30).with_month(2)` returns `None`.
-
-Advanced time zone handling is not yet supported (but is planned in 0.4).
-
diff --git a/vendor/chrono-0.3.0/appveyor.yml b/vendor/chrono-0.3.0/appveyor.yml
deleted file mode 100644
index 241d049..0000000
--- a/vendor/chrono-0.3.0/appveyor.yml
+++ /dev/null
@@ -1,21 +0,0 @@
-environment:
- matrix:
- - TARGET: 1.13.0-x86_64-pc-windows-gnu
- - TARGET: nightly-x86_64-pc-windows-msvc
- - TARGET: nightly-i686-pc-windows-msvc
- - TARGET: nightly-x86_64-pc-windows-gnu
- - TARGET: nightly-i686-pc-windows-gnu
-matrix:
- allow_failures:
- - channel: nightly
-install:
- - ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:TARGET}.exe" -FileName "rust-install.exe"
- - ps: .\rust-install.exe /VERYSILENT /NORESTART /DIR="C:\rust" | Out-Null
- - ps: $env:PATH="$env:PATH;C:\rust\bin"
- - rustc -vV
- - cargo -vV
-build_script:
- # do not test all combinations, Travis will handle that
- - cargo build -v --features "serde rustc-serialize"
-test_script:
- - cargo test -v --features "serde rustc-serialize"
diff --git a/vendor/chrono-0.3.0/src/date.rs b/vendor/chrono-0.3.0/src/date.rs
deleted file mode 100644
index a88ac18..0000000
--- a/vendor/chrono-0.3.0/src/date.rs
+++ /dev/null
@@ -1,375 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! ISO 8601 calendar date with time zone.
-
-use std::{fmt, hash};
-use std::cmp::Ordering;
-use std::ops::{Add, Sub};
-use oldtime::Duration as OldDuration;
-
-use {Weekday, Datelike};
-use offset::TimeZone;
-use offset::utc::UTC;
-use naive;
-use naive::date::NaiveDate;
-use naive::time::NaiveTime;
-use datetime::DateTime;
-use format::{Item, DelayedFormat, StrftimeItems};
-
-/// ISO 8601 calendar date with time zone.
-///
-/// This type should be considered ambiguous at best,
-/// due to the inherent lack of precision required for the time zone resolution.
-/// There are some guarantees on the usage of `Date<Tz>`:
-///
-/// - If properly constructed via `TimeZone::ymd` and others without an error,
-/// the corresponding local date should exist for at least a moment.
-/// (It may still have a gap from the offset changes.)
-///
-/// - The `TimeZone` is free to assign *any* `Offset` to the local date,
-/// as long as that offset did occur in given day.
-/// For example, if `2015-03-08T01:59-08:00` is followed by `2015-03-08T03:00-07:00`,
-/// it may produce either `2015-03-08-08:00` or `2015-03-08-07:00`
-/// but *not* `2015-03-08+00:00` and others.
-///
-/// - Once constructed as a full `DateTime`,
-/// `DateTime::date` and other associated methods should return those for the original `Date`.
-/// For example, if `dt = tz.ymd(y,m,d).hms(h,n,s)` were valid, `dt.date() == tz.ymd(y,m,d)`.
-///
-/// - The date is timezone-agnostic up to one day (i.e. practically always),
-/// so the local date and UTC date should be equal for most cases
-/// even though the raw calculation between `NaiveDate` and `Duration` may not.
-#[derive(Clone)]
-pub struct Date<Tz: TimeZone> {
- date: NaiveDate,
- offset: Tz::Offset,
-}
-
-/// The minimum possible `Date`.
-pub const MIN: Date<UTC> = Date { date: naive::date::MIN, offset: UTC };
-/// The maximum possible `Date`.
-pub const MAX: Date<UTC> = Date { date: naive::date::MAX, offset: UTC };
-
-impl<Tz: TimeZone> Date<Tz> {
- /// Makes a new `Date` with given *UTC* date and offset.
- /// The local date should be constructed via the `TimeZone` trait.
- //
- // note: this constructor is purposedly not named to `new` to discourage the direct usage.
- #[inline]
- pub fn from_utc(date: NaiveDate, offset: Tz::Offset) -> Date<Tz> {
- Date { date: date, offset: offset }
- }
-
- /// Makes a new `DateTime` from the current date and given `NaiveTime`.
- /// The offset in the current date is preserved.
- ///
- /// Panics on invalid datetime.
- #[inline]
- pub fn and_time(&self, time: NaiveTime) -> Option<DateTime<Tz>> {
- let localdt = self.naive_local().and_time(time);
- self.timezone().from_local_datetime(&localdt).single()
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute and second.
- /// The offset in the current date is preserved.
- ///
- /// Panics on invalid hour, minute and/or second.
- #[inline]
- pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> DateTime<Tz> {
- self.and_hms_opt(hour, min, sec).expect("invalid time")
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute and second.
- /// The offset in the current date is preserved.
- ///
- /// Returns `None` on invalid hour, minute and/or second.
- #[inline]
- pub fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<DateTime<Tz>> {
- NaiveTime::from_hms_opt(hour, min, sec).and_then(|time| self.and_time(time))
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
- /// The millisecond part can exceed 1,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Panics on invalid hour, minute, second and/or millisecond.
- #[inline]
- pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> DateTime<Tz> {
- self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time")
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
- /// The millisecond part can exceed 1,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Returns `None` on invalid hour, minute, second and/or millisecond.
- #[inline]
- pub fn and_hms_milli_opt(&self, hour: u32, min: u32, sec: u32,
- milli: u32) -> Option<DateTime<Tz>> {
- NaiveTime::from_hms_milli_opt(hour, min, sec, milli).and_then(|time| self.and_time(time))
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
- /// The microsecond part can exceed 1,000,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Panics on invalid hour, minute, second and/or microsecond.
- #[inline]
- pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> DateTime<Tz> {
- self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time")
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
- /// The microsecond part can exceed 1,000,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Returns `None` on invalid hour, minute, second and/or microsecond.
- #[inline]
- pub fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32,
- micro: u32) -> Option<DateTime<Tz>> {
- NaiveTime::from_hms_micro_opt(hour, min, sec, micro).and_then(|time| self.and_time(time))
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
- /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Panics on invalid hour, minute, second and/or nanosecond.
- #[inline]
- pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> DateTime<Tz> {
- self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time")
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
- /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Returns `None` on invalid hour, minute, second and/or nanosecond.
- #[inline]
- pub fn and_hms_nano_opt(&self, hour: u32, min: u32, sec: u32,
- nano: u32) -> Option<DateTime<Tz>> {
- NaiveTime::from_hms_nano_opt(hour, min, sec, nano).and_then(|time| self.and_time(time))
- }
-
- /// Makes a new `Date` for the next date.
- ///
- /// Panics when `self` is the last representable date.
- #[inline]
- pub fn succ(&self) -> Date<Tz> {
- self.succ_opt().expect("out of bound")
- }
-
- /// Makes a new `Date` for the next date.
- ///
- /// Returns `None` when `self` is the last representable date.
- #[inline]
- pub fn succ_opt(&self) -> Option<Date<Tz>> {
- self.date.succ_opt().map(|date| Date::from_utc(date, self.offset.clone()))
- }
-
- /// Makes a new `Date` for the prior date.
- ///
- /// Panics when `self` is the first representable date.
- #[inline]
- pub fn pred(&self) -> Date<Tz> {
- self.pred_opt().expect("out of bound")
- }
-
- /// Makes a new `Date` for the prior date.
- ///
- /// Returns `None` when `self` is the first representable date.
- #[inline]
- pub fn pred_opt(&self) -> Option<Date<Tz>> {
- self.date.pred_opt().map(|date| Date::from_utc(date, self.offset.clone()))
- }
-
- /// Retrieves an associated offset from UTC.
- #[inline]
- pub fn offset<'a>(&'a self) -> &'a Tz::Offset {
- &self.offset
- }
-
- /// Retrieves an associated time zone.
- #[inline]
- pub fn timezone(&self) -> Tz {
- TimeZone::from_offset(&self.offset)
- }
-
- /// Changes the associated time zone.
- /// This does not change the actual `Date` (but will change the string representation).
- #[inline]
- pub fn with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> Date<Tz2> {
- tz.from_utc_date(&self.date)
- }
-
- /// Adds given `Duration` to the current date.
- ///
- /// Returns `None` when it will result in overflow.
- #[inline]
- pub fn checked_add_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
- let date = try_opt!(self.date.checked_add_signed(rhs));
- Some(Date { date: date, offset: self.offset })
- }
-
- /// Subtracts given `Duration` from the current date.
- ///
- /// Returns `None` when it will result in overflow.
- #[inline]
- pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
- let date = try_opt!(self.date.checked_sub_signed(rhs));
- Some(Date { date: date, offset: self.offset })
- }
-
- /// Subtracts another `Date` from the current date.
- /// Returns a `Duration` of integral numbers.
- ///
- /// This does not overflow or underflow at all,
- /// as all possible output fits in the range of `Duration`.
- #[inline]
- pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: Date<Tz2>) -> OldDuration {
- self.date.signed_duration_since(rhs.date)
- }
-
- /// Returns a view to the naive UTC date.
- #[inline]
- pub fn naive_utc(&self) -> NaiveDate {
- self.date
- }
-
- /// Returns a view to the naive local date.
- ///
- /// This is technically same to [`naive_utc`](#method.naive_utc)
- /// because the offset is restricted to never exceed one day,
- /// but provided for the consistency.
- #[inline]
- pub fn naive_local(&self) -> NaiveDate {
- self.date
- }
-}
-
-/// Maps the local date to other date with given conversion function.
-fn map_local<Tz: TimeZone, F>(d: &Date<Tz>, mut f: F) -> Option<Date<Tz>>
- where F: FnMut(NaiveDate) -> Option<NaiveDate> {
- f(d.naive_local()).and_then(|date| d.timezone().from_local_date(&date).single())
-}
-
-impl<Tz: TimeZone> Date<Tz> where Tz::Offset: fmt::Display {
- /// Formats the date with the specified formatting items.
- #[inline]
- pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
- where I: Iterator<Item=Item<'a>> + Clone {
- DelayedFormat::new_with_offset(Some(self.naive_local()), None, &self.offset, items)
- }
-
- /// Formats the date with the specified format string.
- /// See the [`format::strftime` module](../format/strftime/index.html)
- /// on the supported escape sequences.
- #[inline]
- pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
- self.format_with_items(StrftimeItems::new(fmt))
- }
-}
-
-impl<Tz: TimeZone> Datelike for Date<Tz> {
- #[inline] fn year(&self) -> i32 { self.naive_local().year() }
- #[inline] fn month(&self) -> u32 { self.naive_local().month() }
- #[inline] fn month0(&self) -> u32 { self.naive_local().month0() }
- #[inline] fn day(&self) -> u32 { self.naive_local().day() }
- #[inline] fn day0(&self) -> u32 { self.naive_local().day0() }
- #[inline] fn ordinal(&self) -> u32 { self.naive_local().ordinal() }
- #[inline] fn ordinal0(&self) -> u32 { self.naive_local().ordinal0() }
- #[inline] fn weekday(&self) -> Weekday { self.naive_local().weekday() }
- #[inline] fn isoweekdate(&self) -> (i32, u32, Weekday) { self.naive_local().isoweekdate() }
-
- #[inline]
- fn with_year(&self, year: i32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_year(year))
- }
-
- #[inline]
- fn with_month(&self, month: u32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_month(month))
- }
-
- #[inline]
- fn with_month0(&self, month0: u32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_month0(month0))
- }
-
- #[inline]
- fn with_day(&self, day: u32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_day(day))
- }
-
- #[inline]
- fn with_day0(&self, day0: u32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_day0(day0))
- }
-
- #[inline]
- fn with_ordinal(&self, ordinal: u32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_ordinal(ordinal))
- }
-
- #[inline]
- fn with_ordinal0(&self, ordinal0: u32) -> Option<Date<Tz>> {
- map_local(self, |date| date.with_ordinal0(ordinal0))
- }
-}
-
-// we need them as automatic impls cannot handle associated types
-impl<Tz: TimeZone> Copy for Date<Tz> where <Tz as TimeZone>::Offset: Copy {}
-unsafe impl<Tz: TimeZone> Send for Date<Tz> where <Tz as TimeZone>::Offset: Send {}
-
-impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<Date<Tz2>> for Date<Tz> {
- fn eq(&self, other: &Date<Tz2>) -> bool { self.date == other.date }
-}
-
-impl<Tz: TimeZone> Eq for Date<Tz> {
-}
-
-impl<Tz: TimeZone> PartialOrd for Date<Tz> {
- fn partial_cmp(&self, other: &Date<Tz>) -> Option<Ordering> {
- self.date.partial_cmp(&other.date)
- }
-}
-
-impl<Tz: TimeZone> Ord for Date<Tz> {
- fn cmp(&self, other: &Date<Tz>) -> Ordering { self.date.cmp(&other.date) }
-}
-
-impl<Tz: TimeZone> hash::Hash for Date<Tz> {
- fn hash<H: hash::Hasher>(&self, state: &mut H) { self.date.hash(state) }
-}
-
-impl<Tz: TimeZone> Add<OldDuration> for Date<Tz> {
- type Output = Date<Tz>;
-
- #[inline]
- fn add(self, rhs: OldDuration) -> Date<Tz> {
- self.checked_add_signed(rhs).expect("`Date + Duration` overflowed")
- }
-}
-
-impl<Tz: TimeZone> Sub<OldDuration> for Date<Tz> {
- type Output = Date<Tz>;
-
- #[inline]
- fn sub(self, rhs: OldDuration) -> Date<Tz> {
- self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed")
- }
-}
-
-impl<Tz: TimeZone> fmt::Debug for Date<Tz> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{:?}{:?}", self.naive_local(), self.offset)
- }
-}
-
-impl<Tz: TimeZone> fmt::Display for Date<Tz> where Tz::Offset: fmt::Display {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}{}", self.naive_local(), self.offset)
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/datetime.rs b/vendor/chrono-0.3.0/src/datetime.rs
deleted file mode 100644
index c88d4c9..0000000
--- a/vendor/chrono-0.3.0/src/datetime.rs
+++ /dev/null
@@ -1,779 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! ISO 8601 date and time with time zone.
-
-use std::{str, fmt, hash};
-use std::cmp::Ordering;
-use std::ops::{Add, Sub};
-use oldtime::Duration as OldDuration;
-
-use {Weekday, Timelike, Datelike};
-use offset::{TimeZone, Offset};
-use offset::utc::UTC;
-use offset::local::Local;
-use offset::fixed::FixedOffset;
-use naive::time::NaiveTime;
-use naive::datetime::NaiveDateTime;
-use date::Date;
-use format::{Item, Numeric, Pad, Fixed};
-use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
-
-/// ISO 8601 combined date and time with time zone.
-///
-/// There are some constructors implemented here (the `from_*` methods), but
-/// the general-purpose constructors are all via the methods on the
-/// [`TimeZone`](../trait.TimeZone.html) implementations.
-#[derive(Clone)]
-pub struct DateTime<Tz: TimeZone> {
- datetime: NaiveDateTime,
- offset: Tz::Offset,
-}
-
-impl<Tz: TimeZone> DateTime<Tz> {
- /// Makes a new `DateTime` with given *UTC* datetime and offset.
- /// The local datetime should be constructed via the `TimeZone` trait.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{DateTime, TimeZone, NaiveDateTime, UTC};
- ///
- /// let dt = DateTime::<UTC>::from_utc(NaiveDateTime::from_timestamp(61, 0), UTC);
- /// assert_eq!(UTC.timestamp(61, 0), dt);
- /// ~~~~
- //
- // note: this constructor is purposedly not named to `new` to discourage the direct usage.
- #[inline]
- pub fn from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz> {
- DateTime { datetime: datetime, offset: offset }
- }
-
- /// Retrieves a date component.
- #[inline]
- pub fn date(&self) -> Date<Tz> {
- Date::from_utc(self.naive_local().date(), self.offset.clone())
- }
-
- /// Retrieves a time component.
- /// Unlike `date`, this is not associated to the time zone.
- #[inline]
- pub fn time(&self) -> NaiveTime {
- self.datetime.time() + self.offset.fix()
- }
-
- /// Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC
- /// (aka "UNIX timestamp").
- #[inline]
- pub fn timestamp(&self) -> i64 {
- self.datetime.timestamp()
- }
-
- /// Returns the number of milliseconds since the last second boundary
- ///
- /// warning: in event of a leap second, this may exceed 999
- ///
- /// note: this is not the number of milliseconds since January 1, 1970 0:00:00 UTC
- #[inline]
- pub fn timestamp_subsec_millis(&self) -> u32 {
- self.datetime.timestamp_subsec_millis()
- }
-
- /// Returns the number of microseconds since the last second boundary
- ///
- /// warning: in event of a leap second, this may exceed 999_999
- ///
- /// note: this is not the number of microseconds since January 1, 1970 0:00:00 UTC
- #[inline]
- pub fn timestamp_subsec_micros(&self) -> u32 {
- self.datetime.timestamp_subsec_micros()
- }
-
- /// Returns the number of nanoseconds since the last second boundary
- ///
- /// warning: in event of a leap second, this may exceed 999_999_999
- ///
- /// note: this is not the number of nanoseconds since January 1, 1970 0:00:00 UTC
- #[inline]
- pub fn timestamp_subsec_nanos(&self) -> u32 {
- self.datetime.timestamp_subsec_nanos()
- }
-
- /// Retrieves an associated offset from UTC.
- #[inline]
- pub fn offset<'a>(&'a self) -> &'a Tz::Offset {
- &self.offset
- }
-
- /// Retrieves an associated time zone.
- #[inline]
- pub fn timezone(&self) -> Tz {
- TimeZone::from_offset(&self.offset)
- }
-
- /// Changes the associated time zone.
- /// This does not change the actual `DateTime` (but will change the string representation).
- #[inline]
- pub fn with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> DateTime<Tz2> {
- tz.from_utc_datetime(&self.datetime)
- }
-
- /// Adds given `Duration` to the current date and time.
- ///
- /// Returns `None` when it will result in overflow.
- #[inline]
- pub fn checked_add_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
- let datetime = try_opt!(self.datetime.checked_add_signed(rhs));
- Some(DateTime { datetime: datetime, offset: self.offset })
- }
-
- /// Subtracts given `Duration` from the current date and time.
- ///
- /// Returns `None` when it will result in overflow.
- #[inline]
- pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
- let datetime = try_opt!(self.datetime.checked_sub_signed(rhs));
- Some(DateTime { datetime: datetime, offset: self.offset })
- }
-
- /// Subtracts another `DateTime` from the current date and time.
- /// This does not overflow or underflow at all.
- #[inline]
- pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: DateTime<Tz2>) -> OldDuration {
- self.datetime.signed_duration_since(rhs.datetime)
- }
-
- /// Returns a view to the naive UTC datetime.
- #[inline]
- pub fn naive_utc(&self) -> NaiveDateTime {
- self.datetime
- }
-
- /// Returns a view to the naive local datetime.
- #[inline]
- pub fn naive_local(&self) -> NaiveDateTime {
- self.datetime + self.offset.fix()
- }
-}
-
-/// Maps the local datetime to other datetime with given conversion function.
-fn map_local<Tz: TimeZone, F>(dt: &DateTime<Tz>, mut f: F) -> Option<DateTime<Tz>>
- where F: FnMut(NaiveDateTime) -> Option<NaiveDateTime> {
- f(dt.naive_local()).and_then(|datetime| dt.timezone().from_local_datetime(&datetime).single())
-}
-
-impl DateTime<FixedOffset> {
- /// Parses an RFC 2822 date and time string such as `Tue, 1 Jul 2003 10:52:37 +0200`,
- /// then returns a new `DateTime` with a parsed `FixedOffset`.
- pub fn parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>> {
- const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC2822)];
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, ITEMS.iter().cloned()));
- parsed.to_datetime()
- }
-
- /// Parses an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`,
- /// then returns a new `DateTime` with a parsed `FixedOffset`.
- ///
- /// Why isn't this named `parse_from_iso8601`? That's because ISO 8601 allows some freedom
- /// over the syntax and RFC 3339 exercises that freedom to rigidly define a fixed format.
- pub fn parse_from_rfc3339(s: &str) -> ParseResult<DateTime<FixedOffset>> {
- const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC3339)];
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, ITEMS.iter().cloned()));
- parsed.to_datetime()
- }
-
- /// Parses a string with the specified format string and
- /// returns a new `DateTime` with a parsed `FixedOffset`.
- /// See the [`format::strftime` module](../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// See also `Offset::datetime_from_str` which gives a local `DateTime` on specific time zone.
- pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, StrftimeItems::new(fmt)));
- parsed.to_datetime()
- }
-}
-
-impl<Tz: TimeZone> DateTime<Tz> where Tz::Offset: fmt::Display {
- /// Returns an RFC 2822 date and time string such as `Tue, 1 Jul 2003 10:52:37 +0200`.
- pub fn to_rfc2822(&self) -> String {
- const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC2822)];
- self.format_with_items(ITEMS.iter().cloned()).to_string()
- }
-
- /// Returns an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`.
- pub fn to_rfc3339(&self) -> String {
- const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC3339)];
- self.format_with_items(ITEMS.iter().cloned()).to_string()
- }
-
- /// Formats the combined date and time with the specified formatting items.
- #[inline]
- pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
- where I: Iterator<Item=Item<'a>> + Clone {
- let local = self.naive_local();
- DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
- }
-
- /// Formats the combined date and time with the specified format string.
- /// See the [`format::strftime` module](../format/strftime/index.html)
- /// on the supported escape sequences.
- #[inline]
- pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
- self.format_with_items(StrftimeItems::new(fmt))
- }
-}
-
-impl<Tz: TimeZone> Datelike for DateTime<Tz> {
- #[inline] fn year(&self) -> i32 { self.naive_local().year() }
- #[inline] fn month(&self) -> u32 { self.naive_local().month() }
- #[inline] fn month0(&self) -> u32 { self.naive_local().month0() }
- #[inline] fn day(&self) -> u32 { self.naive_local().day() }
- #[inline] fn day0(&self) -> u32 { self.naive_local().day0() }
- #[inline] fn ordinal(&self) -> u32 { self.naive_local().ordinal() }
- #[inline] fn ordinal0(&self) -> u32 { self.naive_local().ordinal0() }
- #[inline] fn weekday(&self) -> Weekday { self.naive_local().weekday() }
- #[inline] fn isoweekdate(&self) -> (i32, u32, Weekday) { self.naive_local().isoweekdate() }
-
- #[inline]
- fn with_year(&self, year: i32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_year(year))
- }
-
- #[inline]
- fn with_month(&self, month: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_month(month))
- }
-
- #[inline]
- fn with_month0(&self, month0: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_month0(month0))
- }
-
- #[inline]
- fn with_day(&self, day: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_day(day))
- }
-
- #[inline]
- fn with_day0(&self, day0: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_day0(day0))
- }
-
- #[inline]
- fn with_ordinal(&self, ordinal: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_ordinal(ordinal))
- }
-
- #[inline]
- fn with_ordinal0(&self, ordinal0: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_ordinal0(ordinal0))
- }
-}
-
-impl<Tz: TimeZone> Timelike for DateTime<Tz> {
- #[inline] fn hour(&self) -> u32 { self.naive_local().hour() }
- #[inline] fn minute(&self) -> u32 { self.naive_local().minute() }
- #[inline] fn second(&self) -> u32 { self.naive_local().second() }
- #[inline] fn nanosecond(&self) -> u32 { self.naive_local().nanosecond() }
-
- #[inline]
- fn with_hour(&self, hour: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_hour(hour))
- }
-
- #[inline]
- fn with_minute(&self, min: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_minute(min))
- }
-
- #[inline]
- fn with_second(&self, sec: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_second(sec))
- }
-
- #[inline]
- fn with_nanosecond(&self, nano: u32) -> Option<DateTime<Tz>> {
- map_local(self, |datetime| datetime.with_nanosecond(nano))
- }
-}
-
-// we need them as automatic impls cannot handle associated types
-impl<Tz: TimeZone> Copy for DateTime<Tz> where <Tz as TimeZone>::Offset: Copy {}
-unsafe impl<Tz: TimeZone> Send for DateTime<Tz> where <Tz as TimeZone>::Offset: Send {}
-
-impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateTime<Tz2>> for DateTime<Tz> {
- fn eq(&self, other: &DateTime<Tz2>) -> bool { self.datetime == other.datetime }
-}
-
-impl<Tz: TimeZone> Eq for DateTime<Tz> {
-}
-
-impl<Tz: TimeZone> PartialOrd for DateTime<Tz> {
- fn partial_cmp(&self, other: &DateTime<Tz>) -> Option<Ordering> {
- self.datetime.partial_cmp(&other.datetime)
- }
-}
-
-impl<Tz: TimeZone> Ord for DateTime<Tz> {
- fn cmp(&self, other: &DateTime<Tz>) -> Ordering { self.datetime.cmp(&other.datetime) }
-}
-
-impl<Tz: TimeZone> hash::Hash for DateTime<Tz> {
- fn hash<H: hash::Hasher>(&self, state: &mut H) { self.datetime.hash(state) }
-}
-
-impl<Tz: TimeZone> Add<OldDuration> for DateTime<Tz> {
- type Output = DateTime<Tz>;
-
- #[inline]
- fn add(self, rhs: OldDuration) -> DateTime<Tz> {
- self.checked_add_signed(rhs).expect("`DateTime + Duration` overflowed")
- }
-}
-
-impl<Tz: TimeZone> Sub<OldDuration> for DateTime<Tz> {
- type Output = DateTime<Tz>;
-
- #[inline]
- fn sub(self, rhs: OldDuration) -> DateTime<Tz> {
- self.checked_sub_signed(rhs).expect("`DateTime - Duration` overflowed")
- }
-}
-
-impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{:?}{:?}", self.naive_local(), self.offset)
- }
-}
-
-impl<Tz: TimeZone> fmt::Display for DateTime<Tz> where Tz::Offset: fmt::Display {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{} {}", self.naive_local(), self.offset)
- }
-}
-
-impl str::FromStr for DateTime<FixedOffset> {
- type Err = ParseError;
-
- fn from_str(s: &str) -> ParseResult<DateTime<FixedOffset>> {
- const ITEMS: &'static [Item<'static>] = &[
- Item::Space(""), Item::Numeric(Numeric::Year, Pad::Zero),
- Item::Space(""), Item::Literal("-"),
- Item::Space(""), Item::Numeric(Numeric::Month, Pad::Zero),
- Item::Space(""), Item::Literal("-"),
- Item::Space(""), Item::Numeric(Numeric::Day, Pad::Zero),
- Item::Space(""), Item::Literal("T"), // XXX shouldn't this be case-insensitive?
- Item::Space(""), Item::Numeric(Numeric::Hour, Pad::Zero),
- Item::Space(""), Item::Literal(":"),
- Item::Space(""), Item::Numeric(Numeric::Minute, Pad::Zero),
- Item::Space(""), Item::Literal(":"),
- Item::Space(""), Item::Numeric(Numeric::Second, Pad::Zero),
- Item::Fixed(Fixed::Nanosecond),
- Item::Space(""), Item::Fixed(Fixed::TimezoneOffsetZ),
- Item::Space(""),
- ];
-
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, ITEMS.iter().cloned()));
- parsed.to_datetime()
- }
-}
-
-impl str::FromStr for DateTime<UTC> {
- type Err = ParseError;
-
- fn from_str(s: &str) -> ParseResult<DateTime<UTC>> {
- s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&UTC))
- }
-}
-
-impl str::FromStr for DateTime<Local> {
- type Err = ParseError;
-
- fn from_str(s: &str) -> ParseResult<DateTime<Local>> {
- s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&Local))
- }
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_encodable_json<FUTC, FFixed, E>(to_string_utc: FUTC, to_string_fixed: FFixed)
- where FUTC: Fn(&DateTime<UTC>) -> Result<String, E>,
- FFixed: Fn(&DateTime<FixedOffset>) -> Result<String, E>,
- E: ::std::fmt::Debug
-{
- assert_eq!(to_string_utc(&UTC.ymd(2014, 7, 24).and_hms(12, 34, 6)).ok(),
- Some(r#""2014-07-24T12:34:06Z""#.into()));
-
- assert_eq!(to_string_fixed(&FixedOffset::east(3660).ymd(2014, 7, 24).and_hms(12, 34, 6)).ok(),
- Some(r#""2014-07-24T12:34:06+01:01""#.into()));
- assert_eq!(to_string_fixed(&FixedOffset::east(3650).ymd(2014, 7, 24).and_hms(12, 34, 6)).ok(),
- Some(r#""2014-07-24T12:34:06+01:00:50""#.into()));
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_decodable_json<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
- fixed_from_str: FFixed,
- local_from_str: FLocal)
- where FUTC: Fn(&str) -> Result<DateTime<UTC>, E>,
- FFixed: Fn(&str) -> Result<DateTime<FixedOffset>, E>,
- FLocal: Fn(&str) -> Result<DateTime<Local>, E>,
- E: ::std::fmt::Debug
-{
- // should check against the offset as well (the normal DateTime comparison will ignore them)
- fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
- dt.as_ref().map(|dt| (dt, dt.offset()))
- }
-
- assert_eq!(norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
- norm(&Some(UTC.ymd(2014, 7, 24).and_hms(12, 34, 6))));
- assert_eq!(norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
- norm(&Some(UTC.ymd(2014, 7, 24).and_hms(12, 34, 6))));
-
- assert_eq!(norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
- norm(&Some(FixedOffset::east(0).ymd(2014, 7, 24).and_hms(12, 34, 6))));
- assert_eq!(norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
- norm(&Some(FixedOffset::east(60*60 + 23*60).ymd(2014, 7, 24).and_hms(13, 57, 6))));
-
- // we don't know the exact local offset but we can check that
- // the conversion didn't change the instant itself
- assert_eq!(local_from_str(r#""2014-07-24T12:34:06Z""#).unwrap(),
- UTC.ymd(2014, 7, 24).and_hms(12, 34, 6));
- assert_eq!(local_from_str(r#""2014-07-24T13:57:06+01:23""#).unwrap(),
- UTC.ymd(2014, 7, 24).and_hms(12, 34, 6));
-
- assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
- assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
-}
-
-#[cfg(feature = "rustc-serialize")]
-mod rustc_serialize {
- use super::DateTime;
- use offset::TimeZone;
- use offset::utc::UTC;
- use offset::local::Local;
- use offset::fixed::FixedOffset;
- use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
-
- impl<Tz: TimeZone> Encodable for DateTime<Tz> {
- fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
- format!("{:?}", self).encode(s)
- }
- }
-
- impl Decodable for DateTime<FixedOffset> {
- fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<FixedOffset>, D::Error> {
- match d.read_str()?.parse::<DateTime<FixedOffset>>() {
- Ok(dt) => Ok(dt),
- Err(_) => Err(d.error("invalid date and time")),
- }
- }
- }
-
- impl Decodable for DateTime<UTC> {
- fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<UTC>, D::Error> {
- match d.read_str()?.parse::<DateTime<FixedOffset>>() {
- Ok(dt) => Ok(dt.with_timezone(&UTC)),
- Err(_) => Err(d.error("invalid date and time")),
- }
- }
- }
-
- impl Decodable for DateTime<Local> {
- fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Local>, D::Error> {
- match d.read_str()?.parse::<DateTime<FixedOffset>>() {
- Ok(dt) => Ok(dt.with_timezone(&Local)),
- Err(_) => Err(d.error("invalid date and time")),
- }
- }
- }
-
- #[cfg(test)] use rustc_serialize::json;
-
- #[test]
- fn test_encodable() {
- super::test_encodable_json(json::encode, json::encode);
- }
-
- #[test]
- fn test_decodable() {
- super::test_decodable_json(json::decode, json::decode, json::decode);
- }
-}
-
-#[cfg(feature = "serde")]
-mod serde {
- use std::fmt;
- use super::DateTime;
- use offset::TimeZone;
- use offset::utc::UTC;
- use offset::local::Local;
- use offset::fixed::FixedOffset;
- use serde::{ser, de};
-
- // TODO not very optimized for space (binary formats would want something better)
-
- impl<Tz: TimeZone> ser::Serialize for DateTime<Tz> {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where S: ser::Serializer
- {
- // Debug formatting is correct RFC3339, and it allows Zulu.
- serializer.serialize_str(&format!("{:?}", self))
- }
- }
-
- struct DateTimeVisitor;
-
- impl de::Visitor for DateTimeVisitor {
- type Value = DateTime<FixedOffset>;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result
- {
- write!(formatter, "a formatted date and time string")
- }
-
- fn visit_str<E>(self, value: &str) -> Result<DateTime<FixedOffset>, E>
- where E: de::Error
- {
- value.parse().map_err(|err| E::custom(format!("{}", err)))
- }
- }
-
- impl de::Deserialize for DateTime<FixedOffset> {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where D: de::Deserializer
- {
- deserializer.deserialize_str(DateTimeVisitor)
- }
- }
-
- impl de::Deserialize for DateTime<UTC> {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where D: de::Deserializer
- {
- deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC))
- }
- }
-
- impl de::Deserialize for DateTime<Local> {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where D: de::Deserializer
- {
- deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Local))
- }
- }
-
- #[cfg(test)] extern crate serde_json;
- #[cfg(test)] extern crate bincode;
-
- #[test]
- fn test_serde_serialize() {
- super::test_encodable_json(self::serde_json::to_string, self::serde_json::to_string);
- }
-
- #[test]
- fn test_serde_deserialize() {
- super::test_decodable_json(self::serde_json::from_str, self::serde_json::from_str,
- self::serde_json::from_str);
- }
-
- #[test]
- fn test_serde_bincode() {
- // Bincode is relevant to test separately from JSON because
- // it is not self-describing.
- use self::bincode::SizeLimit;
- use self::bincode::serde::{serialize, deserialize};
-
- let dt = UTC.ymd(2014, 7, 24).and_hms(12, 34, 6);
- let encoded = serialize(&dt, SizeLimit::Infinite).unwrap();
- let decoded: DateTime<UTC> = deserialize(&encoded).unwrap();
- assert_eq!(dt, decoded);
- assert_eq!(dt.offset(), decoded.offset());
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::DateTime;
- use Datelike;
- use naive::time::NaiveTime;
- use naive::date::NaiveDate;
- use offset::TimeZone;
- use offset::utc::UTC;
- use offset::local::Local;
- use offset::fixed::FixedOffset;
- use oldtime::Duration;
-
- #[test]
- #[allow(non_snake_case)]
- fn test_datetime_offset() {
- let EST = FixedOffset::west(5*60*60);
- let EDT = FixedOffset::west(4*60*60);
- let KST = FixedOffset::east(9*60*60);
-
- assert_eq!(format!("{}", UTC.ymd(2014, 5, 6).and_hms(7, 8, 9)),
- "2014-05-06 07:08:09 UTC");
- assert_eq!(format!("{}", EDT.ymd(2014, 5, 6).and_hms(7, 8, 9)),
- "2014-05-06 07:08:09 -04:00");
- assert_eq!(format!("{}", KST.ymd(2014, 5, 6).and_hms(7, 8, 9)),
- "2014-05-06 07:08:09 +09:00");
- assert_eq!(format!("{:?}", UTC.ymd(2014, 5, 6).and_hms(7, 8, 9)),
- "2014-05-06T07:08:09Z");
- assert_eq!(format!("{:?}", EDT.ymd(2014, 5, 6).and_hms(7, 8, 9)),
- "2014-05-06T07:08:09-04:00");
- assert_eq!(format!("{:?}", KST.ymd(2014, 5, 6).and_hms(7, 8, 9)),
- "2014-05-06T07:08:09+09:00");
-
- // edge cases
- assert_eq!(format!("{:?}", UTC.ymd(2014, 5, 6).and_hms(0, 0, 0)),
- "2014-05-06T00:00:00Z");
- assert_eq!(format!("{:?}", EDT.ymd(2014, 5, 6).and_hms(0, 0, 0)),
- "2014-05-06T00:00:00-04:00");
- assert_eq!(format!("{:?}", KST.ymd(2014, 5, 6).and_hms(0, 0, 0)),
- "2014-05-06T00:00:00+09:00");
- assert_eq!(format!("{:?}", UTC.ymd(2014, 5, 6).and_hms(23, 59, 59)),
- "2014-05-06T23:59:59Z");
- assert_eq!(format!("{:?}", EDT.ymd(2014, 5, 6).and_hms(23, 59, 59)),
- "2014-05-06T23:59:59-04:00");
- assert_eq!(format!("{:?}", KST.ymd(2014, 5, 6).and_hms(23, 59, 59)),
- "2014-05-06T23:59:59+09:00");
-
- let dt = UTC.ymd(2014, 5, 6).and_hms(7, 8, 9);
- assert_eq!(dt, EDT.ymd(2014, 5, 6).and_hms(3, 8, 9));
- assert_eq!(dt + Duration::seconds(3600 + 60 + 1), UTC.ymd(2014, 5, 6).and_hms(8, 9, 10));
- assert_eq!(dt.signed_duration_since(EDT.ymd(2014, 5, 6).and_hms(10, 11, 12)),
- Duration::seconds(-7*3600 - 3*60 - 3));
-
- assert_eq!(*UTC.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), UTC);
- assert_eq!(*EDT.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), EDT);
- assert!(*EDT.ymd(2014, 5, 6).and_hms(7, 8, 9).offset() != EST);
- }
-
- #[test]
- fn test_datetime_date_and_time() {
- let tz = FixedOffset::east(5*60*60);
- let d = tz.ymd(2014, 5, 6).and_hms(7, 8, 9);
- assert_eq!(d.time(), NaiveTime::from_hms(7, 8, 9));
- assert_eq!(d.date(), tz.ymd(2014, 5, 6));
- assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2014, 5, 6));
- assert_eq!(d.date().and_time(d.time()), Some(d));
-
- let tz = FixedOffset::east(4*60*60);
- let d = tz.ymd(2016, 5, 4).and_hms(3, 2, 1);
- assert_eq!(d.time(), NaiveTime::from_hms(3, 2, 1));
- assert_eq!(d.date(), tz.ymd(2016, 5, 4));
- assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2016, 5, 4));
- assert_eq!(d.date().and_time(d.time()), Some(d));
-
- let tz = FixedOffset::west(13*60*60);
- let d = tz.ymd(2017, 8, 9).and_hms(12, 34, 56);
- assert_eq!(d.time(), NaiveTime::from_hms(12, 34, 56));
- assert_eq!(d.date(), tz.ymd(2017, 8, 9));
- assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2017, 8, 9));
- assert_eq!(d.date().and_time(d.time()), Some(d));
- }
-
- #[test]
- fn test_datetime_with_timezone() {
- let local_now = Local::now();
- let utc_now = local_now.with_timezone(&UTC);
- let local_now2 = utc_now.with_timezone(&Local);
- assert_eq!(local_now, local_now2);
- }
-
- #[test]
- #[allow(non_snake_case)]
- fn test_datetime_rfc2822_and_rfc3339() {
- let EDT = FixedOffset::east(5*60*60);
- assert_eq!(UTC.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc2822(),
- "Wed, 18 Feb 2015 23:16:09 +0000");
- assert_eq!(UTC.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc3339(),
- "2015-02-18T23:16:09+00:00");
- assert_eq!(EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc2822(),
- "Wed, 18 Feb 2015 23:16:09 +0500");
- assert_eq!(EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc3339(),
- "2015-02-18T23:16:09.150+05:00");
- assert_eq!(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc2822(),
- "Wed, 18 Feb 2015 23:59:60 +0500");
- assert_eq!(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc3339(),
- "2015-02-18T23:59:60.234567+05:00");
-
- assert_eq!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000"),
- Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms(23, 16, 9)));
- assert_eq!(DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"),
- Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms(23, 16, 9)));
- assert_eq!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"),
- Ok(EDT.ymd(2015, 2, 18).and_hms_milli(23, 59, 59, 1_000)));
- assert_eq!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"),
- Ok(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567)));
- }
-
- #[test]
- fn test_datetime_from_str() {
- assert_eq!("2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
- Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150)));
- assert_eq!("2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
- Ok(FixedOffset::west(10 * 3600).ymd(2015, 2, 18).and_hms_milli(13, 16, 9, 150)));
- assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());
-
- assert_eq!("2015-2-18T23:16:9.15Z".parse::<DateTime<UTC>>(),
- Ok(UTC.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150)));
- assert_eq!("2015-2-18T13:16:9.15-10:00".parse::<DateTime<UTC>>(),
- Ok(UTC.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150)));
- assert!("2015-2-18T23:16:9.15".parse::<DateTime<UTC>>().is_err());
-
- // no test for `DateTime<Local>`, we cannot verify that much.
- }
-
- #[test]
- fn test_datetime_parse_from_str() {
- let ymdhms = |y,m,d,h,n,s,off| FixedOffset::east(off).ymd(y,m,d).and_hms(h,n,s);
- assert_eq!(DateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- Ok(ymdhms(2014, 5, 7, 12, 34, 56, 570*60))); // ignore offset
- assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset
- assert!(DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT",
- "%a, %d %b %Y %H:%M:%S GMT").is_err());
- assert_eq!(UTC.datetime_from_str("Fri, 09 Aug 2013 23:54:35 GMT",
- "%a, %d %b %Y %H:%M:%S GMT"),
- Ok(UTC.ymd(2013, 8, 9).and_hms(23, 54, 35)));
- }
-
- #[test]
- fn test_datetime_format_with_local() {
- // if we are not around the year boundary, local and UTC date should have the same year
- let dt = Local::now().with_month(5).unwrap();
- assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&UTC).format("%Y").to_string());
- }
-
- #[test]
- fn test_datetime_is_copy() {
- // UTC is known to be `Copy`.
- let a = UTC::now();
- let b = a;
- assert_eq!(a, b);
- }
-
- #[test]
- fn test_datetime_is_send() {
- use std::thread;
-
- // UTC is known to be `Send`.
- let a = UTC::now();
- thread::spawn(move || {
- let _ = a;
- }).join().unwrap();
- }
-
- #[test]
- fn test_subsecond_part() {
- let datetime = UTC.ymd(2014, 7, 8).and_hms_nano(9, 10, 11, 1234567);
-
- assert_eq!(1, datetime.timestamp_subsec_millis());
- assert_eq!(1234, datetime.timestamp_subsec_micros());
- assert_eq!(1234567, datetime.timestamp_subsec_nanos());
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/div.rs b/vendor/chrono-0.3.0/src/div.rs
deleted file mode 100644
index f21cd66..0000000
--- a/vendor/chrono-0.3.0/src/div.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-// This is a part of Chrono.
-// Portions Copyright 2013-2014 The Rust Project Developers.
-// See README.md and LICENSE.txt for details.
-
-//! Integer division utilities. (Shamelessly copied from [num](https://github.com/rust-lang/num/))
-
-// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
-// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
-
-pub use num::integer::{div_rem, div_floor, mod_floor, div_mod_floor};
-
-#[cfg(test)]
-mod tests {
- use super::{mod_floor, div_mod_floor};
-
- #[test]
- fn test_mod_floor() {
- assert_eq!(mod_floor( 8, 3), 2);
- assert_eq!(mod_floor( 8, -3), -1);
- assert_eq!(mod_floor(-8, 3), 1);
- assert_eq!(mod_floor(-8, -3), -2);
-
- assert_eq!(mod_floor( 1, 2), 1);
- assert_eq!(mod_floor( 1, -2), -1);
- assert_eq!(mod_floor(-1, 2), 1);
- assert_eq!(mod_floor(-1, -2), -1);
- }
-
- #[test]
- fn test_div_mod_floor() {
- assert_eq!(div_mod_floor( 8, 3), ( 2, 2));
- assert_eq!(div_mod_floor( 8, -3), (-3, -1));
- assert_eq!(div_mod_floor(-8, 3), (-3, 1));
- assert_eq!(div_mod_floor(-8, -3), ( 2, -2));
-
- assert_eq!(div_mod_floor( 1, 2), ( 0, 1));
- assert_eq!(div_mod_floor( 1, -2), (-1, -1));
- assert_eq!(div_mod_floor(-1, 2), (-1, 1));
- assert_eq!(div_mod_floor(-1, -2), ( 0, -1));
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/format/mod.rs b/vendor/chrono-0.3.0/src/format/mod.rs
deleted file mode 100644
index efd6f76..0000000
--- a/vendor/chrono-0.3.0/src/format/mod.rs
+++ /dev/null
@@ -1,559 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! Formatting utilities for date and time.
-
-use std::fmt;
-use std::error::Error;
-
-use {Datelike, Timelike};
-use div::{div_floor, mod_floor};
-use offset::Offset;
-use offset::fixed::FixedOffset;
-use naive::date::NaiveDate;
-use naive::time::NaiveTime;
-
-pub use self::strftime::StrftimeItems;
-pub use self::parsed::Parsed;
-pub use self::parse::parse;
-
-/// An unhabitated type used for `InternalNumeric` and `InternalFixed` below.
-#[derive(Clone, PartialEq, Eq)]
-enum Void {}
-
-/// Padding characters for numeric items.
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
-pub enum Pad {
- /// No padding.
- None,
- /// Zero (`0`) padding.
- Zero,
- /// Space padding.
- Space,
-}
-
-/// Numeric item types.
-/// They have associated formatting width (FW) and parsing width (PW).
-///
-/// The **formatting width** is the minimal width to be formatted.
-/// If the number is too short, and the padding is not [`Pad::None`](./enum.Pad.html#variant.None),
-/// then it is left-padded.
-/// If the number is too long or (in some cases) negative, it is printed as is.
-///
-/// The **parsing width** is the maximal width to be scanned.
-/// The parser only tries to consume from one to given number of digits (greedily).
-/// It also trims the preceding whitespaces if any.
-/// It cannot parse the negative number, so some date and time cannot be formatted then
-/// parsed with the same formatting items.
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub enum Numeric {
- /// Full Gregorian year (FW=4, PW=∞).
- /// May accept years before 1 BCE or after 9999 CE, given an initial sign.
- Year,
- /// Gregorian year divided by 100 (century number; FW=PW=2). Implies the non-negative year.
- YearDiv100,
- /// Gregorian year modulo 100 (FW=PW=2). Cannot be negative.
- YearMod100,
- /// Year in the ISO week date (FW=4, PW=∞).
- /// May accept years before 1 BCE or after 9999 CE, given an initial sign.
- IsoYear,
- /// Year in the ISO week date, divided by 100 (FW=PW=2). Implies the non-negative year.
- IsoYearDiv100,
- /// Year in the ISO week date, modulo 100 (FW=PW=2). Cannot be negative.
- IsoYearMod100,
- /// Month (FW=PW=2).
- Month,
- /// Day of the month (FW=PW=2).
- Day,
- /// Week number, where the week 1 starts at the first Sunday of January (FW=PW=2).
- WeekFromSun,
- /// Week number, where the week 1 starts at the first Monday of January (FW=PW=2).
- WeekFromMon,
- /// Week number in the ISO week date (FW=PW=2).
- IsoWeek,
- /// Day of the week, where Sunday = 0 and Saturday = 6 (FW=PW=1).
- NumDaysFromSun,
- /// Day of the week, where Monday = 1 and Sunday = 7 (FW=PW=1).
- WeekdayFromMon,
- /// Day of the year (FW=PW=3).
- Ordinal,
- /// Hour number in the 24-hour clocks (FW=PW=2).
- Hour,
- /// Hour number in the 12-hour clocks (FW=PW=2).
- Hour12,
- /// The number of minutes since the last whole hour (FW=PW=2).
- Minute,
- /// The number of seconds since the last whole minute (FW=PW=2).
- Second,
- /// The number of nanoseconds since the last whole second (FW=PW=9).
- /// Note that this is *not* left-aligned;
- /// see also [`Fixed::Nanosecond`](./enum.Fixed.html#variant.Nanosecond).
- Nanosecond,
- /// The number of non-leap seconds since the midnight UTC on January 1, 1970 (FW=1, PW=∞).
- /// For formatting, it assumes UTC upon the absence of time zone offset.
- Timestamp,
-
- /// Internal uses only.
- ///
- /// This item exists so that one can add additional internal-only formatting
- /// without breaking major compatibility (as enum variants cannot be selectively private).
- Internal(InternalNumeric),
-}
-
-/// An opaque type representing numeric item types for internal uses only.
-pub struct InternalNumeric {
- _dummy: Void,
-}
-
-impl Clone for InternalNumeric {
- fn clone(&self) -> Self {
- match self._dummy {}
- }
-}
-
-impl PartialEq for InternalNumeric {
- fn eq(&self, _other: &InternalNumeric) -> bool {
- match self._dummy {}
- }
-}
-
-impl Eq for InternalNumeric {
-}
-
-impl fmt::Debug for InternalNumeric {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "<InternalNumeric>")
- }
-}
-
-/// Fixed-format item types.
-///
-/// They have their own rules of formatting and parsing.
-/// Otherwise noted, they print in the specified cases but parse case-insensitively.
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub enum Fixed {
- /// Abbreviated month names.
- ///
- /// Prints a three-letter-long name in the title case, reads the same name in any case.
- ShortMonthName,
- /// Full month names.
- ///
- /// Prints a full name in the title case, reads either a short or full name in any case.
- LongMonthName,
- /// Abbreviated day of the week names.
- ///
- /// Prints a three-letter-long name in the title case, reads the same name in any case.
- ShortWeekdayName,
- /// Full day of the week names.
- ///
- /// Prints a full name in the title case, reads either a short or full name in any case.
- LongWeekdayName,
- /// AM/PM.
- ///
- /// Prints in lower case, reads in any case.
- LowerAmPm,
- /// AM/PM.
- ///
- /// Prints in upper case, reads in any case.
- UpperAmPm,
- /// An optional dot plus one or more digits for left-aligned nanoseconds.
- /// May print nothing, 3, 6 or 9 digits according to the available accuracy.
- /// See also [`Numeric::Nanosecond`](./enum.Numeric.html#variant.Nanosecond).
- Nanosecond,
- /// Same to [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 3.
- Nanosecond3,
- /// Same to [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 6.
- Nanosecond6,
- /// Same to [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 9.
- Nanosecond9,
- /// Timezone name.
- ///
- /// It does not support parsing, its use in the parser is an immediate failure.
- TimezoneName,
- /// Offset from the local time to UTC (`+09:00` or `-04:00` or `+00:00`).
- ///
- /// 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`,
- /// which is same to [`FixedOffset`](../offset/fixed/struct.FixedOffset.html)'s range.
- TimezoneOffsetColon,
- /// 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,
- /// and `Z` can be either in upper case or in lower case.
- /// The offset is limited from `-24:00` to `+24:00`,
- /// which is same to [`FixedOffset`](../offset/fixed/struct.FixedOffset.html)'s range.
- TimezoneOffsetColonZ,
- /// Same to [`TimezoneOffsetColon`](#variant.TimezoneOffsetColon) but prints no colon.
- /// Parsing allows an optional colon.
- TimezoneOffset,
- /// Same to [`TimezoneOffsetColonZ`](#variant.TimezoneOffsetColonZ) but prints no colon.
- /// Parsing allows an optional colon.
- TimezoneOffsetZ,
- /// RFC 2822 date and time syntax. Commonly used for email and MIME date and time.
- RFC2822,
- /// RFC 3339 & ISO 8601 date and time syntax.
- RFC3339,
-
- /// Internal uses only.
- ///
- /// This item exists so that one can add additional internal-only formatting
- /// without breaking major compatibility (as enum variants cannot be selectively private).
- Internal(InternalFixed),
-}
-
-/// An opaque type representing fixed-format item types for internal uses only.
-pub struct InternalFixed {
- _dummy: Void,
-}
-
-impl Clone for InternalFixed {
- fn clone(&self) -> Self {
- match self._dummy {}
- }
-}
-
-impl PartialEq for InternalFixed {
- fn eq(&self, _other: &InternalFixed) -> bool {
- match self._dummy {}
- }
-}
-
-impl Eq for InternalFixed {
-}
-
-impl fmt::Debug for InternalFixed {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "<InternalFixed>")
- }
-}
-
-/// A single formatting item. This is used for both formatting and parsing.
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub enum Item<'a> {
- /// A literally printed and parsed text.
- Literal(&'a str),
- /// Same to `Literal` but with the string owned by the item.
- OwnedLiteral(Box<str>),
- /// Whitespace. Prints literally but reads zero or more whitespace.
- Space(&'a str),
- /// Same to `Space` but with the string owned by the item.
- OwnedSpace(Box<str>),
- /// Numeric item. Can be optionally padded to the maximal length (if any) when formatting;
- /// the parser simply ignores any padded whitespace and zeroes.
- Numeric(Numeric, Pad),
- /// Fixed-format item.
- Fixed(Fixed),
- /// Issues a formatting error. Used to signal an invalid format string.
- Error,
-}
-
-macro_rules! lit { ($x:expr) => (Item::Literal($x)) }
-macro_rules! sp { ($x:expr) => (Item::Space($x)) }
-macro_rules! num { ($x:ident) => (Item::Numeric(Numeric::$x, Pad::None)) }
-macro_rules! num0 { ($x:ident) => (Item::Numeric(Numeric::$x, Pad::Zero)) }
-macro_rules! nums { ($x:ident) => (Item::Numeric(Numeric::$x, Pad::Space)) }
-macro_rules! fix { ($x:ident) => (Item::Fixed(Fixed::$x)) }
-
-/// An error from the `parse` function.
-#[derive(Debug, Clone, PartialEq, Eq, Copy)]
-pub struct ParseError(ParseErrorKind);
-
-#[derive(Debug, Clone, PartialEq, Eq, Copy)]
-enum ParseErrorKind {
- /// Given field is out of permitted range.
- OutOfRange,
-
- /// There is no possible date and time value with given set of fields.
- ///
- /// This does not include the out-of-range conditions, which are trivially invalid.
- /// It includes the case that there are one or more fields that are inconsistent to each other.
- Impossible,
-
- /// Given set of fields is not enough to make a requested date and time value.
- ///
- /// Note that there *may* be a case that given fields constrain the possible values so much
- /// that there is a unique possible value. Chrono only tries to be correct for
- /// most useful sets of fields however, as such constraint solving can be expensive.
- NotEnough,
-
- /// The input string has some invalid character sequence for given formatting items.
- Invalid,
-
- /// The input string has been prematurely ended.
- TooShort,
-
- /// All formatting items have been read but there is a remaining input.
- TooLong,
-
- /// There was an error on the formatting string, or there were non-supported formating items.
- BadFormat,
-}
-
-/// Same to `Result<T, ParseError>`.
-pub type ParseResult<T> = Result<T, ParseError>;
-
-impl fmt::Display for ParseError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- self.description().fmt(f)
- }
-}
-
-impl Error for ParseError {
- fn description(&self) -> &str {
- match self.0 {
- ParseErrorKind::OutOfRange => "input is out of range",
- ParseErrorKind::Impossible => "no possible date and time matching input",
- ParseErrorKind::NotEnough => "input is not enough for unique date and time",
- ParseErrorKind::Invalid => "input contains invalid characters",
- ParseErrorKind::TooShort => "premature end of input",
- ParseErrorKind::TooLong => "trailing input",
- ParseErrorKind::BadFormat => "bad or unsupported format string",
- }
- }
-}
-
-// to be used in this module and submodules
-const OUT_OF_RANGE: ParseError = ParseError(ParseErrorKind::OutOfRange);
-const IMPOSSIBLE: ParseError = ParseError(ParseErrorKind::Impossible);
-const NOT_ENOUGH: ParseError = ParseError(ParseErrorKind::NotEnough);
-const INVALID: ParseError = ParseError(ParseErrorKind::Invalid);
-const TOO_SHORT: ParseError = ParseError(ParseErrorKind::TooShort);
-const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
-const BAD_FORMAT: ParseError = ParseError(ParseErrorKind::BadFormat);
-
-/// Tries to format given arguments with given formatting items.
-/// Internally used by `DelayedFormat`.
-pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Option<&NaiveTime>,
- off: Option<&(String, FixedOffset)>, items: I) -> fmt::Result
- where I: Iterator<Item=Item<'a>> {
- // full and abbreviated month and weekday names
- static SHORT_MONTHS: [&'static str; 12] =
- ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
- static LONG_MONTHS: [&'static str; 12] =
- ["January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"];
- static SHORT_WEEKDAYS: [&'static str; 7] =
- ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
- static LONG_WEEKDAYS: [&'static str; 7] =
- ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
-
- for item in items {
- match item {
- Item::Literal(s) | Item::Space(s) => try!(write!(w, "{}", s)),
- Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => try!(write!(w, "{}", s)),
-
- Item::Numeric(spec, pad) => {
- use self::Numeric::*;
-
- let week_from_sun = |d: &NaiveDate|
- (d.ordinal() as i32 - d.weekday().num_days_from_sunday() as i32 + 7) / 7;
- let week_from_mon = |d: &NaiveDate|
- (d.ordinal() as i32 - d.weekday().num_days_from_monday() as i32 + 7) / 7;
-
- let (width, v) = match spec {
- Year => (4, date.map(|d| d.year() as i64)),
- YearDiv100 => (2, date.map(|d| div_floor(d.year() as i64, 100))),
- YearMod100 => (2, date.map(|d| mod_floor(d.year() as i64, 100))),
- IsoYear => (4, date.map(|d| d.isoweekdate().0 as i64)),
- IsoYearDiv100 => (2, date.map(|d| div_floor(d.isoweekdate().0 as i64, 100))),
- IsoYearMod100 => (2, date.map(|d| mod_floor(d.isoweekdate().0 as i64, 100))),
- Month => (2, date.map(|d| d.month() as i64)),
- Day => (2, date.map(|d| d.day() as i64)),
- WeekFromSun => (2, date.map(|d| week_from_sun(d) as i64)),
- WeekFromMon => (2, date.map(|d| week_from_mon(d) as i64)),
- IsoWeek => (2, date.map(|d| d.isoweekdate().1 as i64)),
- NumDaysFromSun => (1, date.map(|d| d.weekday().num_days_from_sunday() as i64)),
- WeekdayFromMon => (1, date.map(|d| d.weekday().number_from_monday() as i64)),
- Ordinal => (3, date.map(|d| d.ordinal() as i64)),
- Hour => (2, time.map(|t| t.hour() as i64)),
- Hour12 => (2, time.map(|t| t.hour12().1 as i64)),
- Minute => (2, time.map(|t| t.minute() as i64)),
- Second => (2, time.map(|t| (t.second() +
- t.nanosecond() / 1_000_000_000) as i64)),
- Nanosecond => (9, time.map(|t| (t.nanosecond() % 1_000_000_000) as i64)),
- Timestamp => (1, match (date, time, off) {
- (Some(d), Some(t), None) =>
- Some(d.and_time(*t).timestamp()),
- (Some(d), Some(t), Some(&(_, off))) =>
- Some((d.and_time(*t) - off).timestamp()),
- (_, _, _) => None
- }),
-
- // for the future expansion
- Internal(ref int) => match int._dummy {},
- };
-
- if let Some(v) = v {
- if (spec == Year || spec == IsoYear) && !(0 <= v && v < 10000) {
- // non-four-digit years require an explicit sign as per ISO 8601
- match pad {
- Pad::None => try!(write!(w, "{:+}", v)),
- Pad::Zero => try!(write!(w, "{:+01$}", v, width + 1)),
- Pad::Space => try!(write!(w, "{:+1$}", v, width + 1)),
- }
- } else {
- match pad {
- Pad::None => try!(write!(w, "{}", v)),
- Pad::Zero => try!(write!(w, "{:01$}", v, width)),
- Pad::Space => try!(write!(w, "{:1$}", v, width)),
- }
- }
- } else {
- return Err(fmt::Error); // insufficient arguments for given format
- }
- },
-
- Item::Fixed(spec) => {
- use self::Fixed::*;
-
- /// Prints an offset from UTC in the format of `+HHMM` or `+HH:MM`.
- /// `Z` instead of `+00[:]00` is allowed when `allow_zulu` is true.
- fn write_local_minus_utc(w: &mut fmt::Formatter, off: FixedOffset,
- allow_zulu: bool, use_colon: bool) -> fmt::Result {
- let off = off.local_minus_utc();
- if !allow_zulu || off != 0 {
- let (sign, off) = if off < 0 {('-', -off)} else {('+', off)};
- if use_colon {
- write!(w, "{}{:02}:{:02}", sign, off / 3600, off / 60 % 60)
- } else {
- write!(w, "{}{:02}{:02}", sign, off / 3600, off / 60 % 60)
- }
- } else {
- write!(w, "Z")
- }
- }
-
- let ret = match spec {
- ShortMonthName =>
- date.map(|d| write!(w, "{}", SHORT_MONTHS[d.month0() as usize])),
- LongMonthName =>
- date.map(|d| write!(w, "{}", LONG_MONTHS[d.month0() as usize])),
- ShortWeekdayName =>
- date.map(|d| write!(w, "{}",
- SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize])),
- LongWeekdayName =>
- date.map(|d| write!(w, "{}",
- LONG_WEEKDAYS[d.weekday().num_days_from_monday() as usize])),
- LowerAmPm =>
- time.map(|t| write!(w, "{}", if t.hour12().0 {"pm"} else {"am"})),
- UpperAmPm =>
- time.map(|t| write!(w, "{}", if t.hour12().0 {"PM"} else {"AM"})),
- Nanosecond =>
- time.map(|t| {
- let nano = t.nanosecond() % 1_000_000_000;
- if nano == 0 {
- Ok(())
- } else if nano % 1_000_000 == 0 {
- write!(w, ".{:03}", nano / 1_000_000)
- } else if nano % 1_000 == 0 {
- write!(w, ".{:06}", nano / 1_000)
- } else {
- write!(w, ".{:09}", nano)
- }
- }),
- Nanosecond3 =>
- time.map(|t| {
- let nano = t.nanosecond() % 1_000_000_000;
- write!(w, ".{:03}", nano / 1_000_000)
- }),
- Nanosecond6 =>
- time.map(|t| {
- let nano = t.nanosecond() % 1_000_000_000;
- write!(w, ".{:06}", nano / 1_000)
- }),
- Nanosecond9 =>
- time.map(|t| {
- let nano = t.nanosecond() % 1_000_000_000;
- write!(w, ".{:09}", nano)
- }),
- TimezoneName =>
- off.map(|&(ref name, _)| write!(w, "{}", *name)),
- TimezoneOffsetColon =>
- off.map(|&(_, off)| write_local_minus_utc(w, off, false, true)),
- TimezoneOffsetColonZ =>
- off.map(|&(_, off)| write_local_minus_utc(w, off, true, true)),
- TimezoneOffset =>
- off.map(|&(_, off)| write_local_minus_utc(w, off, false, false)),
- TimezoneOffsetZ =>
- off.map(|&(_, off)| write_local_minus_utc(w, off, true, false)),
- RFC2822 => // same to `%a, %e %b %Y %H:%M:%S %z`
- if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) {
- let sec = t.second() + t.nanosecond() / 1_000_000_000;
- try!(write!(w, "{}, {:2} {} {:04} {:02}:{:02}:{:02} ",
- SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize],
- d.day(), SHORT_MONTHS[d.month0() as usize], d.year(),
- t.hour(), t.minute(), sec));
- Some(write_local_minus_utc(w, off, false, false))
- } else {
- None
- },
- RFC3339 => // same to `%Y-%m-%dT%H:%M:%S%.f%:z`
- if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) {
- // reuse `Debug` impls which already print ISO 8601 format.
- // this is faster in this way.
- try!(write!(w, "{:?}T{:?}", d, t));
- Some(write_local_minus_utc(w, off, false, true))
- } else {
- None
- },
-
- // for the future expansion
- Internal(ref int) => match int._dummy {},
- };
-
- match ret {
- Some(ret) => try!(ret),
- None => return Err(fmt::Error), // insufficient arguments for given format
- }
- },
-
- Item::Error => return Err(fmt::Error),
- }
- }
-
- Ok(())
-}
-
-pub mod parsed;
-
-// due to the size of parsing routines, they are in separate modules.
-mod scan;
-mod parse;
-
-pub mod strftime;
-
-/// A *temporary* object which can be used as an argument to `format!` or others.
-/// This is normally constructed via `format` methods of each date and time type.
-#[derive(Debug)]
-pub struct DelayedFormat<I> {
- /// The date view, if any.
- date: Option<NaiveDate>,
- /// The time view, if any.
- time: Option<NaiveTime>,
- /// The name and local-to-UTC difference for the offset (timezone), if any.
- off: Option<(String, FixedOffset)>,
- /// An iterator returning formatting items.
- items: I,
-}
-
-impl<'a, I: Iterator<Item=Item<'a>> + Clone> DelayedFormat<I> {
- /// Makes a new `DelayedFormat` value out of local date and time.
- pub fn new(date: Option<NaiveDate>, time: Option<NaiveTime>, items: I) -> DelayedFormat<I> {
- DelayedFormat { date: date, time: time, off: None, items: items }
- }
-
- /// Makes a new `DelayedFormat` value out of local date and time and UTC offset.
- pub fn new_with_offset<Off>(date: Option<NaiveDate>, time: Option<NaiveTime>,
- offset: &Off, items: I) -> DelayedFormat<I>
- where Off: Offset + fmt::Display {
- let name_and_diff = (offset.to_string(), offset.fix());
- DelayedFormat { date: date, time: time, off: Some(name_and_diff), items: items }
- }
-}
-
-impl<'a, I: Iterator<Item=Item<'a>> + Clone> fmt::Display for DelayedFormat<I> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- format(f, self.date.as_ref(), self.time.as_ref(), self.off.as_ref(), self.items.clone())
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/format/parse.rs b/vendor/chrono-0.3.0/src/format/parse.rs
deleted file mode 100644
index 3b58335..0000000
--- a/vendor/chrono-0.3.0/src/format/parse.rs
+++ /dev/null
@@ -1,733 +0,0 @@
-// This is a part of Chrono.
-// Portions copyright (c) 2015, John Nagle.
-// See README.md and LICENSE.txt for details.
-
-//! Date and time parsing routines.
-
-use std::usize;
-
-use Weekday;
-
-use super::scan;
-use super::{Parsed, ParseResult, Item};
-use super::{OUT_OF_RANGE, INVALID, TOO_SHORT, TOO_LONG, BAD_FORMAT};
-
-fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult<()> {
- p.set_weekday(match v {
- 0 => Weekday::Sun, 1 => Weekday::Mon, 2 => Weekday::Tue,
- 3 => Weekday::Wed, 4 => Weekday::Thu, 5 => Weekday::Fri,
- 6 => Weekday::Sat, _ => return Err(OUT_OF_RANGE)
- })
-}
-
-fn set_weekday_with_number_from_monday(p: &mut Parsed, v: i64) -> ParseResult<()> {
- p.set_weekday(match v {
- 1 => Weekday::Mon, 2 => Weekday::Tue, 3 => Weekday::Wed,
- 4 => Weekday::Thu, 5 => Weekday::Fri, 6 => Weekday::Sat,
- 7 => Weekday::Sun, _ => return Err(OUT_OF_RANGE)
- })
-}
-
-fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a str, ())> {
- macro_rules! try_consume {
- ($e:expr) => ({ let (s_, v) = try!($e); s = s_; v })
- }
-
- // an adapted RFC 2822 syntax from Section 3.3 and 4.3:
- //
- // date-time = [ day-of-week "," ] date 1*S time *S
- // day-of-week = *S day-name *S
- // day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
- // date = day month year
- // day = *S 1*2DIGIT *S
- // month = 1*S month-name 1*S
- // month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
- // "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
- // year = *S 2*DIGIT *S
- // time = time-of-day 1*S zone
- // time-of-day = hour ":" minute [ ":" second ]
- // hour = *S 2DIGIT *S
- // minute = *S 2DIGIT *S
- // second = *S 2DIGIT *S
- // zone = ( "+" / "-" ) 4DIGIT /
- // "UT" / "GMT" / ; same to +0000
- // "EST" / "CST" / "MST" / "PST" / ; same to -0500 to -0800
- // "EDT" / "CDT" / "MDT" / "PDT" / ; same to -0400 to -0700
- // 1*(%d65-90 / %d97-122) ; same to -0000
- //
- // some notes:
- //
- // - quoted characters can be in any mixture of lower and upper cases.
- //
- // - we do not recognize a folding white space (FWS) or comment (CFWS).
- // for our purposes, instead, we accept any sequence of Unicode
- // white space characters (denoted here to `S`). any actual RFC 2822
- // parser is expected to parse FWS and/or CFWS themselves and replace
- // it with a single SP (`%x20`); this is legitimate.
- //
- // - two-digit year < 50 should be interpreted by adding 2000.
- // two-digit year >= 50 or three-digit year should be interpreted
- // by adding 1900. note that four-or-more-digit years less than 1000
- // are *never* affected by this rule.
- //
- // - zone of `-0000` and any unrecognized legacy time zones (including
- // *every* one-letter military time zones) are considered "missing",
- // in such that we don't actually know what time zone is being used.
- //
- // - mismatching day-of-week is always an error, which is consistent to
- // Chrono's own rules.
- //
- // - zones can range from `-9959` to `+9959`, but `FixedOffset` does not
- // support offsets larger than 24 hours. this is not *that* problematic
- // since we do not directly go to a `DateTime` so one can recover
- // the offset information from `Parsed` anyway.
-
- s = s.trim_left();
-
- if let Ok((s_, weekday)) = scan::short_weekday(s) {
- if !s_.starts_with(',') { return Err(INVALID); }
- s = &s_[1..];
- try!(parsed.set_weekday(weekday));
- }
-
- s = s.trim_left();
- try!(parsed.set_day(try_consume!(scan::number(s, 1, 2))));
- s = try!(scan::space(s)); // mandatory
- try!(parsed.set_month(1 + try_consume!(scan::short_month0(s)) as i64));
- s = try!(scan::space(s)); // mandatory
-
- // distinguish two- and three-digit years from four-digit years
- let prevlen = s.len();
- let mut year = try_consume!(scan::number(s, 2, usize::MAX));
- let yearlen = prevlen - s.len();
- match (yearlen, year) {
- (2, 0...49) => { year += 2000; } // 47 -> 2047, 05 -> 2005
- (2, 50...99) => { year += 1900; } // 79 -> 1979
- (3, _) => { year += 1900; } // 112 -> 2012, 009 -> 1909
- (_, _) => {} // 1987 -> 1987, 0654 -> 0654
- }
- try!(parsed.set_year(year));
-
- s = try!(scan::space(s)); // mandatory
- try!(parsed.set_hour(try_consume!(scan::number(s, 2, 2))));
- s = try!(scan::char(s.trim_left(), b':')).trim_left(); // *S ":" *S
- try!(parsed.set_minute(try_consume!(scan::number(s, 2, 2))));
- s = s.trim_left();
- if !s.is_empty() { // [ ":" *S 2DIGIT ]
- s = try!(scan::char(s, b':')).trim_left();
- try!(parsed.set_second(try_consume!(scan::number(s, 2, 2))));
- }
-
- s = try!(scan::space(s)); // mandatory
- if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) {
- // only set the offset when it is definitely known (i.e. not `-0000`)
- try!(parsed.set_offset(offset as i64));
- }
-
- Ok((s, ()))
-}
-
-fn parse_rfc3339<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a str, ())> {
- macro_rules! try_consume {
- ($e:expr) => ({ let (s_, v) = try!($e); s = s_; v })
- }
-
- // an adapted RFC 3339 syntax from Section 5.6:
- //
- // date-fullyear = 4DIGIT
- // date-month = 2DIGIT ; 01-12
- // date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
- // time-hour = 2DIGIT ; 00-23
- // time-minute = 2DIGIT ; 00-59
- // time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
- // time-secfrac = "." 1*DIGIT
- // time-numoffset = ("+" / "-") time-hour ":" time-minute
- // time-offset = "Z" / time-numoffset
- // partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
- // full-date = date-fullyear "-" date-month "-" date-mday
- // full-time = partial-time time-offset
- // date-time = full-date "T" full-time
- //
- // some notes:
- //
- // - quoted characters can be in any mixture of lower and upper cases.
- //
- // - it may accept any number of fractional digits for seconds.
- // for Chrono, this means that we should skip digits past first 9 digits.
- //
- // - unlike RFC 2822, the valid offset ranges from -23:59 to +23:59.
- // note that this restriction is unique to RFC 3339 and not ISO 8601.
- // since this is not a typical Chrono behavior, we check it earlier.
-
- try!(parsed.set_year(try_consume!(scan::number(s, 4, 4))));
- s = try!(scan::char(s, b'-'));
- try!(parsed.set_month(try_consume!(scan::number(s, 2, 2))));
- s = try!(scan::char(s, b'-'));
- try!(parsed.set_day(try_consume!(scan::number(s, 2, 2))));
-
- s = match s.as_bytes().first() {
- Some(&b't') | Some(&b'T') => &s[1..],
- Some(_) => return Err(INVALID),
- None => return Err(TOO_SHORT),
- };
-
- try!(parsed.set_hour(try_consume!(scan::number(s, 2, 2))));
- s = try!(scan::char(s, b':'));
- try!(parsed.set_minute(try_consume!(scan::number(s, 2, 2))));
- s = try!(scan::char(s, b':'));
- try!(parsed.set_second(try_consume!(scan::number(s, 2, 2))));
- if s.starts_with('.') {
- let nanosecond = try_consume!(scan::nanosecond(&s[1..]));
- try!(parsed.set_nanosecond(nanosecond));
- }
-
- let offset = try_consume!(scan::timezone_offset_zulu(s, |s| scan::char(s, b':')));
- if offset <= -86400 || offset >= 86400 { return Err(OUT_OF_RANGE); }
- try!(parsed.set_offset(offset as i64));
-
- Ok((s, ()))
-}
-
-/// Tries to parse given string into `parsed` with given formatting items.
-/// Returns `Ok` when the entire string has been parsed (otherwise `parsed` should not be used).
-/// There should be no trailing string after parsing;
-/// use a stray [`Item::Space`](./enum.Item.html#variant.Space) to trim whitespaces.
-///
-/// This particular date and time parser is:
-///
-/// - Greedy. It will consume the longest possible prefix.
-/// For example, `April` is always consumed entirely when the long month name is requested;
-/// it equally accepts `Apr`, but prefers the longer prefix in this case.
-///
-/// - Padding-agnostic (for numeric items).
-/// The [`Pad`](./enum.Pad.html) field is completely ignored,
-/// so one can prepend any number of whitespace then any number of zeroes before numbers.
-///
-/// - (Still) obeying the intrinsic parsing width. This allows, for example, parsing `HHMMSS`.
-pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<()>
- where I: Iterator<Item=Item<'a>> {
- macro_rules! try_consume {
- ($e:expr) => ({ let (s_, v) = try!($e); s = s_; v })
- }
-
- for item in items {
- match item {
- Item::Literal(prefix) => {
- if s.len() < prefix.len() { return Err(TOO_SHORT); }
- if !s.starts_with(prefix) { return Err(INVALID); }
- s = &s[prefix.len()..];
- }
-
- Item::OwnedLiteral(ref prefix) => {
- if s.len() < prefix.len() { return Err(TOO_SHORT); }
- if !s.starts_with(&prefix[..]) { return Err(INVALID); }
- s = &s[prefix.len()..];
- }
-
- Item::Space(_) | Item::OwnedSpace(_) => {
- s = s.trim_left();
- }
-
- Item::Numeric(spec, _pad) => {
- use super::Numeric::*;
-
- let (width, signed, set): (usize, bool,
- fn(&mut Parsed, i64) -> ParseResult<()>) = match spec {
- Year => (4, true, Parsed::set_year),
- YearDiv100 => (2, false, Parsed::set_year_div_100),
- YearMod100 => (2, false, Parsed::set_year_mod_100),
- IsoYear => (4, true, Parsed::set_isoyear),
- IsoYearDiv100 => (2, false, Parsed::set_isoyear_div_100),
- IsoYearMod100 => (2, false, Parsed::set_isoyear_mod_100),
- Month => (2, false, Parsed::set_month),
- Day => (2, false, Parsed::set_day),
- WeekFromSun => (2, false, Parsed::set_week_from_sun),
- WeekFromMon => (2, false, Parsed::set_week_from_mon),
- IsoWeek => (2, false, Parsed::set_isoweek),
- NumDaysFromSun => (1, false, set_weekday_with_num_days_from_sunday),
- WeekdayFromMon => (1, false, set_weekday_with_number_from_monday),
- Ordinal => (3, false, Parsed::set_ordinal),
- Hour => (2, false, Parsed::set_hour),
- Hour12 => (2, false, Parsed::set_hour12),
- Minute => (2, false, Parsed::set_minute),
- Second => (2, false, Parsed::set_second),
- Nanosecond => (9, false, Parsed::set_nanosecond),
- Timestamp => (usize::MAX, false, Parsed::set_timestamp),
-
- // for the future expansion
- Internal(ref int) => match int._dummy {},
- };
-
- s = s.trim_left();
- let v = if signed {
- if s.starts_with('-') {
- let v = try_consume!(scan::number(&s[1..], 1, usize::MAX));
- try!(0i64.checked_sub(v).ok_or(OUT_OF_RANGE))
- } else if s.starts_with('+') {
- try_consume!(scan::number(&s[1..], 1, usize::MAX))
- } else {
- // if there is no explicit sign, we respect the original `width`
- try_consume!(scan::number(s, 1, width))
- }
- } else {
- try_consume!(scan::number(s, 1, width))
- };
- try!(set(parsed, v));
- }
-
- Item::Fixed(spec) => {
- use super::Fixed::*;
-
- match spec {
- ShortMonthName => {
- let month0 = try_consume!(scan::short_month0(s));
- try!(parsed.set_month(month0 as i64 + 1));
- }
-
- LongMonthName => {
- let month0 = try_consume!(scan::short_or_long_month0(s));
- try!(parsed.set_month(month0 as i64 + 1));
- }
-
- ShortWeekdayName => {
- let weekday = try_consume!(scan::short_weekday(s));
- try!(parsed.set_weekday(weekday));
- }
-
- LongWeekdayName => {
- let weekday = try_consume!(scan::short_or_long_weekday(s));
- try!(parsed.set_weekday(weekday));
- }
-
- LowerAmPm | UpperAmPm => {
- if s.len() < 2 { return Err(TOO_SHORT); }
- let ampm = match (s.as_bytes()[0] | 32, s.as_bytes()[1] | 32) {
- (b'a',b'm') => false,
- (b'p',b'm') => true,
- _ => return Err(INVALID)
- };
- try!(parsed.set_ampm(ampm));
- s = &s[2..];
- }
-
- Nanosecond | Nanosecond3 | Nanosecond6 | Nanosecond9=> {
- if s.starts_with('.') {
- let nano = try_consume!(scan::nanosecond(&s[1..]));
- try!(parsed.set_nanosecond(nano));
- }
- }
-
- TimezoneName => return Err(BAD_FORMAT),
-
- TimezoneOffsetColon | TimezoneOffset => {
- let offset = try_consume!(scan::timezone_offset(s.trim_left(),
- scan::colon_or_space));
- try!(parsed.set_offset(offset as i64));
- }
-
- TimezoneOffsetColonZ | TimezoneOffsetZ => {
- let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(),
- scan::colon_or_space));
- try!(parsed.set_offset(offset as i64));
- }
-
- RFC2822 => try_consume!(parse_rfc2822(parsed, s)),
- RFC3339 => try_consume!(parse_rfc3339(parsed, s)),
-
- // for the future expansion
- Internal(ref int) => match int._dummy {},
- }
- }
-
- Item::Error => {
- return Err(BAD_FORMAT);
- }
- }
- }
-
- // if there are trailling chars, it is an error
- if !s.is_empty() {
- Err(TOO_LONG)
- } else {
- Ok(())
- }
-}
-
-#[cfg(test)]
-#[test]
-fn test_parse() {
- use super::*;
- use super::IMPOSSIBLE;
-
- // workaround for Rust issue #22255
- fn parse_all(s: &str, items: &[Item]) -> ParseResult<Parsed> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, items.iter().cloned()));
- Ok(parsed)
- }
-
- macro_rules! check {
- ($fmt:expr, $items:expr; $err:tt) => (
- assert_eq!(parse_all($fmt, &$items), Err($err))
- );
- ($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (#[allow(unused_mut)] {
- let mut expected = Parsed::new();
- $(expected.$k = Some($v);)*
- assert_eq!(parse_all($fmt, &$items), Ok(expected))
- });
- }
-
- // empty string
- check!("", []; );
- check!(" ", []; TOO_LONG);
- check!("a", []; TOO_LONG);
-
- // whitespaces
- check!("", [sp!("")]; );
- check!(" ", [sp!("")]; );
- check!("\t", [sp!("")]; );
- check!(" \n\r \n", [sp!("")]; );
- check!("a", [sp!("")]; TOO_LONG);
-
- // literal
- check!("", [lit!("a")]; TOO_SHORT);
- check!(" ", [lit!("a")]; INVALID);
- check!("a", [lit!("a")]; );
- check!("aa", [lit!("a")]; TOO_LONG);
- check!("A", [lit!("a")]; INVALID);
- check!("xy", [lit!("xy")]; );
- check!("xy", [lit!("x"), lit!("y")]; );
- check!("x y", [lit!("x"), lit!("y")]; INVALID);
- check!("xy", [lit!("x"), sp!(""), lit!("y")]; );
- check!("x y", [lit!("x"), sp!(""), lit!("y")]; );
-
- // numeric
- check!("1987", [num!(Year)]; year: 1987);
- check!("1987 ", [num!(Year)]; TOO_LONG);
- check!("0x12", [num!(Year)]; TOO_LONG); // `0` is parsed
- check!("x123", [num!(Year)]; INVALID);
- check!("2015", [num!(Year)]; year: 2015);
- check!("0000", [num!(Year)]; year: 0);
- check!("9999", [num!(Year)]; year: 9999);
- check!(" \t987", [num!(Year)]; year: 987);
- check!("5", [num!(Year)]; year: 5);
- check!("5\0", [num!(Year)]; TOO_LONG);
- check!("\05", [num!(Year)]; INVALID);
- check!("", [num!(Year)]; TOO_SHORT);
- check!("12345", [num!(Year), lit!("5")]; year: 1234);
- check!("12345", [nums!(Year), lit!("5")]; year: 1234);
- check!("12345", [num0!(Year), lit!("5")]; year: 1234);
- check!("12341234", [num!(Year), num!(Year)]; year: 1234);
- check!("1234 1234", [num!(Year), num!(Year)]; year: 1234);
- check!("1234 1235", [num!(Year), num!(Year)]; IMPOSSIBLE);
- check!("1234 1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
- check!("1234x1234", [num!(Year), lit!("x"), num!(Year)]; year: 1234);
- check!("1234xx1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
- check!("1234 x 1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
-
- // signed numeric
- check!("-42", [num!(Year)]; year: -42);
- check!("+42", [num!(Year)]; year: 42);
- check!("-0042", [num!(Year)]; year: -42);
- check!("+0042", [num!(Year)]; year: 42);
- check!("-42195", [num!(Year)]; year: -42195);
- check!("+42195", [num!(Year)]; year: 42195);
- check!(" -42195", [num!(Year)]; year: -42195);
- check!(" +42195", [num!(Year)]; year: 42195);
- check!(" - 42", [num!(Year)]; INVALID);
- check!(" + 42", [num!(Year)]; INVALID);
- check!("-", [num!(Year)]; TOO_SHORT);
- check!("+", [num!(Year)]; TOO_SHORT);
-
- // unsigned numeric
- check!("345", [num!(Ordinal)]; ordinal: 345);
- check!("+345", [num!(Ordinal)]; INVALID);
- check!("-345", [num!(Ordinal)]; INVALID);
- check!(" 345", [num!(Ordinal)]; ordinal: 345);
- check!(" +345", [num!(Ordinal)]; INVALID);
- check!(" -345", [num!(Ordinal)]; INVALID);
-
- // various numeric fields
- check!("1234 5678",
- [num!(Year), num!(IsoYear)];
- year: 1234, isoyear: 5678);
- check!("12 34 56 78",
- [num!(YearDiv100), num!(YearMod100), num!(IsoYearDiv100), num!(IsoYearMod100)];
- year_div_100: 12, year_mod_100: 34, isoyear_div_100: 56, isoyear_mod_100: 78);
- check!("1 2 3 4 5 6",
- [num!(Month), num!(Day), num!(WeekFromSun), num!(WeekFromMon), num!(IsoWeek),
- num!(NumDaysFromSun)];
- month: 1, day: 2, week_from_sun: 3, week_from_mon: 4, isoweek: 5, weekday: Weekday::Sat);
- check!("7 89 01",
- [num!(WeekdayFromMon), num!(Ordinal), num!(Hour12)];
- weekday: Weekday::Sun, ordinal: 89, hour_mod_12: 1);
- check!("23 45 6 78901234 567890123",
- [num!(Hour), num!(Minute), num!(Second), num!(Nanosecond), num!(Timestamp)];
- hour_div_12: 1, hour_mod_12: 11, minute: 45, second: 6, nanosecond: 78_901_234,
- timestamp: 567_890_123);
-
- // fixed: month and weekday names
- check!("apr", [fix!(ShortMonthName)]; month: 4);
- check!("Apr", [fix!(ShortMonthName)]; month: 4);
- check!("APR", [fix!(ShortMonthName)]; month: 4);
- check!("ApR", [fix!(ShortMonthName)]; month: 4);
- check!("April", [fix!(ShortMonthName)]; TOO_LONG); // `Apr` is parsed
- check!("A", [fix!(ShortMonthName)]; TOO_SHORT);
- check!("Sol", [fix!(ShortMonthName)]; INVALID);
- check!("Apr", [fix!(LongMonthName)]; month: 4);
- check!("Apri", [fix!(LongMonthName)]; TOO_LONG); // `Apr` is parsed
- check!("April", [fix!(LongMonthName)]; month: 4);
- check!("Aprill", [fix!(LongMonthName)]; TOO_LONG);
- check!("Aprill", [fix!(LongMonthName), lit!("l")]; month: 4);
- check!("Aprl", [fix!(LongMonthName), lit!("l")]; month: 4);
- check!("April", [fix!(LongMonthName), lit!("il")]; TOO_SHORT); // do not backtrack
- check!("thu", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
- check!("Thu", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
- check!("THU", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
- check!("tHu", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
- check!("Thursday", [fix!(ShortWeekdayName)]; TOO_LONG); // `Thu` is parsed
- check!("T", [fix!(ShortWeekdayName)]; TOO_SHORT);
- check!("The", [fix!(ShortWeekdayName)]; INVALID);
- check!("Nop", [fix!(ShortWeekdayName)]; INVALID);
- check!("Thu", [fix!(LongWeekdayName)]; weekday: Weekday::Thu);
- check!("Thur", [fix!(LongWeekdayName)]; TOO_LONG); // `Thu` is parsed
- check!("Thurs", [fix!(LongWeekdayName)]; TOO_LONG); // ditto
- check!("Thursday", [fix!(LongWeekdayName)]; weekday: Weekday::Thu);
- check!("Thursdays", [fix!(LongWeekdayName)]; TOO_LONG);
- check!("Thursdays", [fix!(LongWeekdayName), lit!("s")]; weekday: Weekday::Thu);
- check!("Thus", [fix!(LongWeekdayName), lit!("s")]; weekday: Weekday::Thu);
- check!("Thursday", [fix!(LongWeekdayName), lit!("rsday")]; TOO_SHORT); // do not backtrack
-
- // fixed: am/pm
- check!("am", [fix!(LowerAmPm)]; hour_div_12: 0);
- check!("pm", [fix!(LowerAmPm)]; hour_div_12: 1);
- check!("AM", [fix!(LowerAmPm)]; hour_div_12: 0);
- check!("PM", [fix!(LowerAmPm)]; hour_div_12: 1);
- check!("am", [fix!(UpperAmPm)]; hour_div_12: 0);
- check!("pm", [fix!(UpperAmPm)]; hour_div_12: 1);
- check!("AM", [fix!(UpperAmPm)]; hour_div_12: 0);
- check!("PM", [fix!(UpperAmPm)]; hour_div_12: 1);
- check!("Am", [fix!(LowerAmPm)]; hour_div_12: 0);
- check!(" Am", [fix!(LowerAmPm)]; INVALID);
- check!("ame", [fix!(LowerAmPm)]; TOO_LONG); // `am` is parsed
- check!("a", [fix!(LowerAmPm)]; TOO_SHORT);
- check!("p", [fix!(LowerAmPm)]; TOO_SHORT);
- check!("x", [fix!(LowerAmPm)]; TOO_SHORT);
- check!("xx", [fix!(LowerAmPm)]; INVALID);
- check!("", [fix!(LowerAmPm)]; TOO_SHORT);
-
- // fixed: dot plus nanoseconds
- check!("", [fix!(Nanosecond)]; ); // no field set, but not an error
- check!("4", [fix!(Nanosecond)]; TOO_LONG); // never consumes `4`
- check!("4", [fix!(Nanosecond), num!(Second)]; second: 4);
- check!(".0", [fix!(Nanosecond)]; nanosecond: 0);
- check!(".4", [fix!(Nanosecond)]; nanosecond: 400_000_000);
- check!(".42", [fix!(Nanosecond)]; nanosecond: 420_000_000);
- check!(".421", [fix!(Nanosecond)]; nanosecond: 421_000_000);
- check!(".42195", [fix!(Nanosecond)]; nanosecond: 421_950_000);
- check!(".421950803", [fix!(Nanosecond)]; nanosecond: 421_950_803);
- check!(".421950803547", [fix!(Nanosecond)]; nanosecond: 421_950_803);
- check!(".000000003547", [fix!(Nanosecond)]; nanosecond: 3);
- check!(".000000000547", [fix!(Nanosecond)]; nanosecond: 0);
- check!(".", [fix!(Nanosecond)]; TOO_SHORT);
- check!(".4x", [fix!(Nanosecond)]; TOO_LONG);
- check!(". 4", [fix!(Nanosecond)]; INVALID);
- check!(" .4", [fix!(Nanosecond)]; TOO_LONG); // no automatic trimming
-
- // fixed: timezone offsets
- check!("+00:00", [fix!(TimezoneOffset)]; offset: 0);
- check!("-00:00", [fix!(TimezoneOffset)]; offset: 0);
- check!("+00:01", [fix!(TimezoneOffset)]; offset: 60);
- check!("-00:01", [fix!(TimezoneOffset)]; offset: -60);
- check!("+00:30", [fix!(TimezoneOffset)]; offset: 30 * 60);
- check!("-00:30", [fix!(TimezoneOffset)]; offset: -30 * 60);
- check!("+04:56", [fix!(TimezoneOffset)]; offset: 296 * 60);
- check!("-04:56", [fix!(TimezoneOffset)]; offset: -296 * 60);
- check!("+24:00", [fix!(TimezoneOffset)]; offset: 24 * 60 * 60);
- check!("-24:00", [fix!(TimezoneOffset)]; offset: -24 * 60 * 60);
- check!("+99:59", [fix!(TimezoneOffset)]; offset: (100 * 60 - 1) * 60);
- check!("-99:59", [fix!(TimezoneOffset)]; offset: -(100 * 60 - 1) * 60);
- check!("+00:59", [fix!(TimezoneOffset)]; offset: 59 * 60);
- check!("+00:60", [fix!(TimezoneOffset)]; OUT_OF_RANGE);
- check!("+00:99", [fix!(TimezoneOffset)]; OUT_OF_RANGE);
- check!("#12:34", [fix!(TimezoneOffset)]; INVALID);
- check!("12:34", [fix!(TimezoneOffset)]; INVALID);
- check!("+12:34 ", [fix!(TimezoneOffset)]; TOO_LONG);
- check!(" +12:34", [fix!(TimezoneOffset)]; offset: 754 * 60);
- check!("\t -12:34", [fix!(TimezoneOffset)]; offset: -754 * 60);
- check!("", [fix!(TimezoneOffset)]; TOO_SHORT);
- check!("+", [fix!(TimezoneOffset)]; TOO_SHORT);
- check!("+1", [fix!(TimezoneOffset)]; TOO_SHORT);
- check!("+12", [fix!(TimezoneOffset)]; TOO_SHORT);
- check!("+123", [fix!(TimezoneOffset)]; TOO_SHORT);
- check!("+1234", [fix!(TimezoneOffset)]; offset: 754 * 60);
- check!("+12345", [fix!(TimezoneOffset)]; TOO_LONG);
- check!("+12345", [fix!(TimezoneOffset), num!(Day)]; offset: 754 * 60, day: 5);
- check!("Z", [fix!(TimezoneOffset)]; INVALID);
- check!("z", [fix!(TimezoneOffset)]; INVALID);
- check!("Z", [fix!(TimezoneOffsetZ)]; offset: 0);
- check!("z", [fix!(TimezoneOffsetZ)]; offset: 0);
- check!("Y", [fix!(TimezoneOffsetZ)]; INVALID);
- check!("Zulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 0);
- check!("zulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 0);
- check!("+1234ulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 754 * 60);
- check!("+12:34ulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 754 * 60);
- check!("???", [fix!(TimezoneName)]; BAD_FORMAT); // not allowed
-
- // some practical examples
- check!("2015-02-04T14:37:05+09:00",
- [num!(Year), lit!("-"), num!(Month), lit!("-"), num!(Day), lit!("T"),
- num!(Hour), lit!(":"), num!(Minute), lit!(":"), num!(Second), fix!(TimezoneOffset)];
- year: 2015, month: 2, day: 4, hour_div_12: 1, hour_mod_12: 2,
- minute: 37, second: 5, offset: 32400);
- check!("Mon, 10 Jun 2013 09:32:37 GMT",
- [fix!(ShortWeekdayName), lit!(","), sp!(" "), num!(Day), sp!(" "),
- fix!(ShortMonthName), sp!(" "), num!(Year), sp!(" "), num!(Hour), lit!(":"),
- num!(Minute), lit!(":"), num!(Second), sp!(" "), lit!("GMT")];
- year: 2013, month: 6, day: 10, weekday: Weekday::Mon,
- hour_div_12: 0, hour_mod_12: 9, minute: 32, second: 37);
- check!("20060102150405",
- [num!(Year), num!(Month), num!(Day), num!(Hour), num!(Minute), num!(Second)];
- year: 2006, month: 1, day: 2, hour_div_12: 1, hour_mod_12: 3, minute: 4, second: 5);
- check!("3:14PM",
- [num!(Hour12), lit!(":"), num!(Minute), fix!(LowerAmPm)];
- hour_div_12: 1, hour_mod_12: 3, minute: 14);
- check!("12345678901234.56789",
- [num!(Timestamp), lit!("."), num!(Nanosecond)];
- nanosecond: 56_789, timestamp: 12_345_678_901_234);
- check!("12345678901234.56789",
- [num!(Timestamp), fix!(Nanosecond)];
- nanosecond: 567_890_000, timestamp: 12_345_678_901_234);
-}
-
-#[cfg(test)]
-#[test]
-fn test_rfc2822() {
- use datetime::DateTime;
- use offset::fixed::FixedOffset;
- use super::*;
- use super::NOT_ENOUGH;
-
- // Test data - (input, Ok(expected result after parse and format) or Err(error code))
- let testdates = [
- ("Tue, 20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // normal case
- ("20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // no day of week
- ("20 JAN 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // upper case month
- ("11 Sep 2001 09:45:00 EST", Ok("Tue, 11 Sep 2001 09:45:00 -0500")),
- ("30 Feb 2015 17:35:20 -0800", Err(OUT_OF_RANGE)), // bad day of month
- ("Tue, 20 Jan 2015", Err(TOO_SHORT)), // omitted fields
- ("Tue, 20 Avr 2015 17:35:20 -0800", Err(INVALID)), // bad month name
- ("Tue, 20 Jan 2015 25:35:20 -0800", Err(OUT_OF_RANGE)), // bad hour
- ("Tue, 20 Jan 2015 7:35:20 -0800", Err(INVALID)), // bad # of digits in hour
- ("Tue, 20 Jan 2015 17:65:20 -0800", Err(OUT_OF_RANGE)), // bad minute
- ("Tue, 20 Jan 2015 17:35:90 -0800", Err(OUT_OF_RANGE)), // bad second
- ("Tue, 20 Jan 2015 17:35:20 -0890", Err(OUT_OF_RANGE)), // bad offset
- ("6 Jun 1944 04:00:00Z", Err(INVALID)), // bad offset (zulu not allowed)
- ("Tue, 20 Jan 2015 17:35:20 HAS", Err(NOT_ENOUGH)) // bad named time zone
- ];
-
- fn rfc2822_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, date, [Item::Fixed(Fixed::RFC2822)].iter().cloned()));
- parsed.to_datetime()
- }
-
- fn fmt_rfc2822_datetime(dt: DateTime<FixedOffset>) -> String {
- dt.format_with_items([Item::Fixed(Fixed::RFC2822)].iter().cloned()).to_string()
- }
-
- // Test against test data above
- for &(date, checkdate) in testdates.iter() {
- let d = rfc2822_to_datetime(date); // parse a date
- let dt = match d { // did we get a value?
- Ok(dt) => Ok(fmt_rfc2822_datetime(dt)), // yes, go on
- Err(e) => Err(e), // otherwise keep an error for the comparison
- };
- if dt != checkdate.map(|s| s.to_string()) { // check for expected result
- panic!("Date conversion failed for {}\nReceived: {:?}\nExpected: {:?}",
- date, dt, checkdate);
- }
- };
-}
-
-
-
-#[cfg(test)]
-#[test]
-fn parse_rfc850() {
- use ::{UTC, TimeZone};
-
- static RFC850_FMT: &'static str = "%A, %d-%b-%y %T GMT";
-
- let dt_str = "Sunday, 06-Nov-94 08:49:37 GMT";
- let dt = UTC.ymd(1994, 11, 6).and_hms(8, 49, 37);
-
- // Check that the format is what we expect
- assert_eq!(dt.format(RFC850_FMT).to_string(), dt_str);
-
- // Check that it parses correctly
- assert_eq!(Ok(dt), UTC.datetime_from_str("Sunday, 06-Nov-94 08:49:37 GMT", RFC850_FMT));
-
- // Check that the rest of the weekdays parse correctly (this test originally failed because
- // Sunday parsed incorrectly).
- let testdates = [
- (UTC.ymd(1994, 11, 7).and_hms(8, 49, 37), "Monday, 07-Nov-94 08:49:37 GMT"),
- (UTC.ymd(1994, 11, 8).and_hms(8, 49, 37), "Tuesday, 08-Nov-94 08:49:37 GMT"),
- (UTC.ymd(1994, 11, 9).and_hms(8, 49, 37), "Wednesday, 09-Nov-94 08:49:37 GMT"),
- (UTC.ymd(1994, 11, 10).and_hms(8, 49, 37), "Thursday, 10-Nov-94 08:49:37 GMT"),
- (UTC.ymd(1994, 11, 11).and_hms(8, 49, 37), "Friday, 11-Nov-94 08:49:37 GMT"),
- (UTC.ymd(1994, 11, 12).and_hms(8, 49, 37), "Saturday, 12-Nov-94 08:49:37 GMT"),
- ];
-
- for val in &testdates {
- assert_eq!(Ok(val.0), UTC.datetime_from_str(val.1, RFC850_FMT));
- }
-}
-
-#[cfg(test)]
-#[test]
-fn test_rfc3339() {
- use datetime::DateTime;
- use offset::fixed::FixedOffset;
- use super::*;
-
- // Test data - (input, Ok(expected result after parse and format) or Err(error code))
- let testdates = [
- ("2015-01-20T17:35:20-08:00", Ok("2015-01-20T17:35:20-08:00")), // normal case
- ("1944-06-06T04:04:00Z", Ok("1944-06-06T04:04:00+00:00")), // D-day
- ("2001-09-11T09:45:00-08:00", Ok("2001-09-11T09:45:00-08:00")),
- ("2015-01-20T17:35:20.001-08:00", Ok("2015-01-20T17:35:20.001-08:00")),
- ("2015-01-20T17:35:20.000031-08:00", Ok("2015-01-20T17:35:20.000031-08:00")),
- ("2015-01-20T17:35:20.000000004-08:00", Ok("2015-01-20T17:35:20.000000004-08:00")),
- ("2015-01-20T17:35:20.000000000452-08:00", Ok("2015-01-20T17:35:20-08:00")), // too small
- ("2015-02-30T17:35:20-08:00", Err(OUT_OF_RANGE)), // bad day of month
- ("2015-01-20T25:35:20-08:00", Err(OUT_OF_RANGE)), // bad hour
- ("2015-01-20T17:65:20-08:00", Err(OUT_OF_RANGE)), // bad minute
- ("2015-01-20T17:35:90-08:00", Err(OUT_OF_RANGE)), // bad second
- ("2015-01-20T17:35:20-24:00", Err(OUT_OF_RANGE)), // bad offset
- ];
-
- fn rfc3339_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, date, [Item::Fixed(Fixed::RFC3339)].iter().cloned()));
- parsed.to_datetime()
- }
-
- fn fmt_rfc3339_datetime(dt: DateTime<FixedOffset>) -> String {
- dt.format_with_items([Item::Fixed(Fixed::RFC3339)].iter().cloned()).to_string()
- }
-
- // Test against test data above
- for &(date, checkdate) in testdates.iter() {
- let d = rfc3339_to_datetime(date); // parse a date
- let dt = match d { // did we get a value?
- Ok(dt) => Ok(fmt_rfc3339_datetime(dt)), // yes, go on
- Err(e) => Err(e), // otherwise keep an error for the comparison
- };
- if dt != checkdate.map(|s| s.to_string()) { // check for expected result
- panic!("Date conversion failed for {}\nReceived: {:?}\nExpected: {:?}",
- date, dt, checkdate);
- }
- };
-}
-
diff --git a/vendor/chrono-0.3.0/src/format/parsed.rs b/vendor/chrono-0.3.0/src/format/parsed.rs
deleted file mode 100644
index 7eb3a3e..0000000
--- a/vendor/chrono-0.3.0/src/format/parsed.rs
+++ /dev/null
@@ -1,1092 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! A collection of parsed date and time items.
-//! They can be constructed incrementally while being checked for consistency.
-
-use num::traits::ToPrimitive;
-use oldtime::Duration as OldDuration;
-
-use {Datelike, Timelike};
-use Weekday;
-use div::div_rem;
-use offset::{TimeZone, Offset, LocalResult};
-use offset::fixed::FixedOffset;
-use naive::date::NaiveDate;
-use naive::time::NaiveTime;
-use naive::datetime::NaiveDateTime;
-use datetime::DateTime;
-use super::{ParseResult, OUT_OF_RANGE, IMPOSSIBLE, NOT_ENOUGH};
-
-/// Parsed parts of date and time. There are two classes of methods:
-///
-/// - `set_*` methods try to set given field(s) while checking for the consistency.
-/// It may or may not check for the range constraint immediately (for efficiency reasons).
-///
-/// - `to_*` methods try to make a concrete date and time value out of set fields.
-/// It fully checks any remaining out-of-range conditions and inconsistent/impossible fields.
-#[allow(missing_copy_implementations)]
-#[derive(Clone, PartialEq, Debug)]
-pub struct Parsed {
- /// Year.
- ///
- /// This can be negative unlike [`year_div_100`](#structfield.year_div_100)
- /// and [`year_mod_100`](#structfield.year_mod_100) fields.
- pub year: Option<i32>,
-
- /// Year divided by 100. Implies that the year is >= 1 BCE when set.
- ///
- /// Due to the common usage, if this field is missing but
- /// [`year_mod_100`](#structfield.year_mod_100) is present,
- /// it is inferred to 19 when `year_mod_100 >= 70` and 20 otherwise.
- pub year_div_100: Option<i32>,
-
- /// Year modulo 100. Implies that the year is >= 1 BCE when set.
- pub year_mod_100: Option<i32>,
-
- /// Year in the [ISO week date](../../naive/date/index.html#week-date).
- ///
- /// This can be negative unlike [`isoyear_div_100`](#structfield.isoyear_div_100) and
- /// [`isoyear_mod_100`](#structfield.isoyear_mod_100) fields.
- pub isoyear: Option<i32>,
-
- /// Year in the [ISO week date](../../naive/date/index.html#week-date), divided by 100.
- /// Implies that the year is >= 1 BCE when set.
- ///
- /// Due to the common usage, if this field is missing but
- /// [`isoyear_mod_100`](#structfield.isoyear_mod_100) is present,
- /// it is inferred to 19 when `isoyear_mod_100 >= 70` and 20 otherwise.
- pub isoyear_div_100: Option<i32>,
-
- /// Year in the [ISO week date](../../naive/date/index.html#week-date), modulo 100.
- /// Implies that the year is >= 1 BCE when set.
- pub isoyear_mod_100: Option<i32>,
-
- /// Month (1--12).
- pub month: Option<u32>,
-
- /// Week number, where the week 1 starts at the first Sunday of January
- /// (0--53, 1--53 or 1--52 depending on the year).
- pub week_from_sun: Option<u32>,
-
- /// Week number, where the week 1 starts at the first Monday of January
- /// (0--53, 1--53 or 1--52 depending on the year).
- pub week_from_mon: Option<u32>,
-
- /// [ISO week number](../../naive/date/index.html#week-date)
- /// (1--52 or 1--53 depending on the year).
- pub isoweek: Option<u32>,
-
- /// Day of the week.
- pub weekday: Option<Weekday>,
-
- /// Day of the year (1--365 or 1--366 depending on the year).
- pub ordinal: Option<u32>,
-
- /// Day of the month (1--28, 1--29, 1--30 or 1--31 depending on the month).
- pub day: Option<u32>,
-
- /// Hour number divided by 12 (0--1). 0 indicates AM and 1 indicates PM.
- pub hour_div_12: Option<u32>,
-
- /// Hour number modulo 12 (0--11).
- pub hour_mod_12: Option<u32>,
-
- /// Minute number (0--59).
- pub minute: Option<u32>,
-
- /// Second number (0--60, accounting for leap seconds).
- pub second: Option<u32>,
-
- /// The number of nanoseconds since the whole second (0--999,999,999).
- pub nanosecond: Option<u32>,
-
- /// The number of non-leap seconds since the midnight UTC on January 1, 1970.
- ///
- /// This can be off by one if [`second`](#structfield.second) is 60 (a leap second).
- pub timestamp: Option<i64>,
-
- /// Offset from the local time to UTC, in seconds.
- pub offset: Option<i32>,
-
- /// A dummy field to make this type not fully destructible (required for API stability).
- _dummy: (),
-}
-
-/// Checks if `old` is either empty or has the same value to `new` (i.e. "consistent"),
-/// and if it is empty, set `old` to `new` as well.
-fn set_if_consistent<T: PartialEq>(old: &mut Option<T>, new: T) -> ParseResult<()> {
- if let Some(ref old) = *old {
- if *old == new {Ok(())} else {Err(IMPOSSIBLE)}
- } else {
- *old = Some(new);
- Ok(())
- }
-}
-
-impl Default for Parsed {
- fn default() -> Parsed {
- Parsed {
- year: None, year_div_100: None, year_mod_100: None, isoyear: None,
- isoyear_div_100: None, isoyear_mod_100: None, month: None,
- week_from_sun: None, week_from_mon: None, isoweek: None, weekday: None,
- ordinal: None, day: None, hour_div_12: None, hour_mod_12: None, minute: None,
- second: None, nanosecond: None, timestamp: None, offset: None,
- _dummy: (),
- }
- }
-}
-
-impl Parsed {
- /// Returns the initial value of parsed parts.
- pub fn new() -> Parsed {
- Parsed::default()
- }
-
- /// Tries to set the [`year`](#structfield.year) field from given value.
- pub fn set_year(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.year, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`year_div_100`](#structfield.year_div_100) field from given value.
- pub fn set_year_div_100(&mut self, value: i64) -> ParseResult<()> {
- if value < 0 { return Err(OUT_OF_RANGE); }
- set_if_consistent(&mut self.year_div_100, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`year_mod_100`](#structfield.year_mod_100) field from given value.
- pub fn set_year_mod_100(&mut self, value: i64) -> ParseResult<()> {
- if value < 0 { return Err(OUT_OF_RANGE); }
- set_if_consistent(&mut self.year_mod_100, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`isoyear`](#structfield.isoyear) field from given value.
- pub fn set_isoyear(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.isoyear, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`isoyear_div_100`](#structfield.isoyear_div_100) field from given value.
- pub fn set_isoyear_div_100(&mut self, value: i64) -> ParseResult<()> {
- if value < 0 { return Err(OUT_OF_RANGE); }
- set_if_consistent(&mut self.isoyear_div_100, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`isoyear_mod_100`](#structfield.isoyear_mod_100) field from given value.
- pub fn set_isoyear_mod_100(&mut self, value: i64) -> ParseResult<()> {
- if value < 0 { return Err(OUT_OF_RANGE); }
- set_if_consistent(&mut self.isoyear_mod_100, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`month`](#structfield.month) field from given value.
- pub fn set_month(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.month, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`week_from_sun`](#structfield.week_from_sun) field from given value.
- pub fn set_week_from_sun(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.week_from_sun, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`week_from_mon`](#structfield.week_from_mon) field from given value.
- pub fn set_week_from_mon(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.week_from_mon, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`isoweek`](#structfield.isoweek) field from given value.
- pub fn set_isoweek(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.isoweek, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`weekday`](#structfield.weekday) field from given value.
- pub fn set_weekday(&mut self, value: Weekday) -> ParseResult<()> {
- set_if_consistent(&mut self.weekday, value)
- }
-
- /// Tries to set the [`ordinal`](#structfield.ordinal) field from given value.
- pub fn set_ordinal(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.ordinal, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`day`](#structfield.day) field from given value.
- pub fn set_day(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.day, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`hour_div_12`](#structfield.hour_div_12) field from given value.
- /// (`false` for AM, `true` for PM)
- pub fn set_ampm(&mut self, value: bool) -> ParseResult<()> {
- set_if_consistent(&mut self.hour_div_12, if value {1} else {0})
- }
-
- /// Tries to set the [`hour_mod_12`](#structfield.hour_mod_12) field from
- /// given hour number in 12-hour clocks.
- pub fn set_hour12(&mut self, value: i64) -> ParseResult<()> {
- if value < 1 || value > 12 { return Err(OUT_OF_RANGE); }
- set_if_consistent(&mut self.hour_mod_12, value as u32 % 12)
- }
-
- /// Tries to set both [`hour_div_12`](#structfield.hour_div_12) and
- /// [`hour_mod_12`](#structfield.hour_mod_12) fields from given value.
- pub fn set_hour(&mut self, value: i64) -> ParseResult<()> {
- let v = try!(value.to_u32().ok_or(OUT_OF_RANGE));
- try!(set_if_consistent(&mut self.hour_div_12, v / 12));
- try!(set_if_consistent(&mut self.hour_mod_12, v % 12));
- Ok(())
- }
-
- /// Tries to set the [`minute`](#structfield.minute) field from given value.
- pub fn set_minute(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.minute, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`second`](#structfield.second) field from given value.
- pub fn set_second(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.second, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`nanosecond`](#structfield.nanosecond) field from given value.
- pub fn set_nanosecond(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.nanosecond, try!(value.to_u32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Tries to set the [`timestamp`](#structfield.timestamp) field from given value.
- pub fn set_timestamp(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.timestamp, value)
- }
-
- /// Tries to set the [`offset`](#structfield.offset) field from given value.
- pub fn set_offset(&mut self, value: i64) -> ParseResult<()> {
- set_if_consistent(&mut self.offset, try!(value.to_i32().ok_or(OUT_OF_RANGE)))
- }
-
- /// Returns a parsed naive date out of given fields.
- ///
- /// This method is able to determine the date from given subset of fields:
- ///
- /// - Year, month, day.
- /// - Year, day of the year (ordinal).
- /// - Year, week number counted from Sunday or Monday, day of the week.
- /// - ISO week date.
- ///
- /// Gregorian year and ISO week date year can have their century number (`*_div_100`) omitted,
- /// the two-digit year is used to guess the century number then.
- pub fn to_naive_date(&self) -> ParseResult<NaiveDate> {
- fn resolve_year(y: Option<i32>, q: Option<i32>,
- r: Option<i32>) -> ParseResult<Option<i32>> {
- match (y, q, r) {
- // if there is no further information, simply return the given full year.
- // this is a common case, so let's avoid division here.
- (y, None, None) => Ok(y),
-
- // if there is a full year *and* also quotient and/or modulo,
- // check if present quotient and/or modulo is consistent to the full year.
- // since the presence of those fields means a positive full year,
- // we should filter a negative full year first.
- (Some(y), q, r @ Some(0...99)) | (Some(y), q, r @ None) => {
- if y < 0 { return Err(OUT_OF_RANGE); }
- let (q_, r_) = div_rem(y, 100);
- if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ {
- Ok(Some(y))
- } else {
- Err(IMPOSSIBLE)
- }
- },
-
- // the full year is missing but we have quotient and modulo.
- // reconstruct the full year. make sure that the result is always positive.
- (None, Some(q), Some(r @ 0...99)) => {
- if q < 0 { return Err(OUT_OF_RANGE); }
- let y = q.checked_mul(100).and_then(|v| v.checked_add(r));
- Ok(Some(try!(y.ok_or(OUT_OF_RANGE))))
- },
-
- // we only have modulo. try to interpret a modulo as a conventional two-digit year.
- // note: we are affected by Rust issue #18060. avoid multiple range patterns.
- (None, None, Some(r @ 0...99)) => Ok(Some(r + if r < 70 {2000} else {1900})),
-
- // otherwise it is an out-of-bound or insufficient condition.
- (None, Some(_), None) => Err(NOT_ENOUGH),
- (_, _, Some(_)) => Err(OUT_OF_RANGE),
- }
- }
-
- let given_year =
- try!(resolve_year(self.year, self.year_div_100, self.year_mod_100));
- let given_isoyear =
- try!(resolve_year(self.isoyear, self.isoyear_div_100, self.isoyear_mod_100));
-
- // verify the normal year-month-day date.
- let verify_ymd = |date: NaiveDate| {
- let year = date.year();
- let (year_div_100, year_mod_100) = if year >= 0 {
- let (q, r) = div_rem(year, 100);
- (Some(q), Some(r))
- } else {
- (None, None) // they should be empty to be consistent
- };
- let month = date.month();
- let day = date.day();
- (self.year.unwrap_or(year) == year &&
- self.year_div_100.or(year_div_100) == year_div_100 &&
- self.year_mod_100.or(year_mod_100) == year_mod_100 &&
- self.month.unwrap_or(month) == month &&
- self.day.unwrap_or(day) == day)
- };
-
- // verify the ISO week date.
- let verify_isoweekdate = |date: NaiveDate| {
- let (isoyear, isoweek, weekday) = date.isoweekdate();
- let (isoyear_div_100, isoyear_mod_100) = if isoyear >= 0 {
- let (q, r) = div_rem(isoyear, 100);
- (Some(q), Some(r))
- } else {
- (None, None) // they should be empty to be consistent
- };
- (self.isoyear.unwrap_or(isoyear) == isoyear &&
- self.isoyear_div_100.or(isoyear_div_100) == isoyear_div_100 &&
- self.isoyear_mod_100.or(isoyear_mod_100) == isoyear_mod_100 &&
- self.isoweek.unwrap_or(isoweek) == isoweek &&
- self.weekday.unwrap_or(weekday) == weekday)
- };
-
- // verify the ordinal and other (non-ISO) week dates.
- let verify_ordinal = |date: NaiveDate| {
- let ordinal = date.ordinal();
- let weekday = date.weekday();
- let week_from_sun = (ordinal as i32 - weekday.num_days_from_sunday() as i32 + 7) / 7;
- let week_from_mon = (ordinal as i32 - weekday.num_days_from_monday() as i32 + 7) / 7;
- (self.ordinal.unwrap_or(ordinal) == ordinal &&
- self.week_from_sun.map_or(week_from_sun, |v| v as i32) == week_from_sun &&
- self.week_from_mon.map_or(week_from_mon, |v| v as i32) == week_from_mon)
- };
-
- // test several possibilities.
- // tries to construct a full `NaiveDate` as much as possible, then verifies that
- // it is consistent with other given fields.
- let (verified, parsed_date) = match (given_year, given_isoyear, self) {
- (Some(year), _, &Parsed { month: Some(month), day: Some(day), .. }) => {
- // year, month, day
- let date = try!(NaiveDate::from_ymd_opt(year, month, day).ok_or(OUT_OF_RANGE));
- (verify_isoweekdate(date) && verify_ordinal(date), date)
- },
-
- (Some(year), _, &Parsed { ordinal: Some(ordinal), .. }) => {
- // year, day of the year
- let date = try!(NaiveDate::from_yo_opt(year, ordinal).ok_or(OUT_OF_RANGE));
- (verify_ymd(date) && verify_isoweekdate(date) && verify_ordinal(date), date)
- },
-
- (Some(year), _, &Parsed { week_from_sun: Some(week_from_sun),
- weekday: Some(weekday), .. }) => {
- // year, week (starting at 1st Sunday), day of the week
- let newyear = try!(NaiveDate::from_yo_opt(year, 1).ok_or(OUT_OF_RANGE));
- let firstweek = match newyear.weekday() {
- Weekday::Sun => 0,
- Weekday::Mon => 6,
- Weekday::Tue => 5,
- Weekday::Wed => 4,
- Weekday::Thu => 3,
- Weekday::Fri => 2,
- Weekday::Sat => 1,
- };
-
- // `firstweek+1`-th day of January is the beginning of the week 1.
- if week_from_sun > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
- let ndays = firstweek + (week_from_sun as i32 - 1) * 7 +
- weekday.num_days_from_sunday() as i32;
- let date = try!(newyear.checked_add_signed(OldDuration::days(ndays as i64))
- .ok_or(OUT_OF_RANGE));
- if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error
-
- (verify_ymd(date) && verify_isoweekdate(date) && verify_ordinal(date), date)
- },
-
- (Some(year), _, &Parsed { week_from_mon: Some(week_from_mon),
- weekday: Some(weekday), .. }) => {
- // year, week (starting at 1st Monday), day of the week
- let newyear = try!(NaiveDate::from_yo_opt(year, 1).ok_or(OUT_OF_RANGE));
- let firstweek = match newyear.weekday() {
- Weekday::Sun => 1,
- Weekday::Mon => 0,
- Weekday::Tue => 6,
- Weekday::Wed => 5,
- Weekday::Thu => 4,
- Weekday::Fri => 3,
- Weekday::Sat => 2,
- };
-
- // `firstweek+1`-th day of January is the beginning of the week 1.
- if week_from_mon > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
- let ndays = firstweek + (week_from_mon as i32 - 1) * 7 +
- weekday.num_days_from_monday() as i32;
- let date = try!(newyear.checked_add_signed(OldDuration::days(ndays as i64))
- .ok_or(OUT_OF_RANGE));
- if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error
-
- (verify_ymd(date) && verify_isoweekdate(date) && verify_ordinal(date), date)
- },
-
- (_, Some(isoyear), &Parsed { isoweek: Some(isoweek), weekday: Some(weekday), .. }) => {
- // ISO year, week, day of the week
- let date = NaiveDate::from_isoywd_opt(isoyear, isoweek, weekday);
- let date = try!(date.ok_or(OUT_OF_RANGE));
- (verify_ymd(date) && verify_ordinal(date), date)
- },
-
- (_, _, _) => return Err(NOT_ENOUGH)
- };
-
- if verified {
- Ok(parsed_date)
- } else {
- Err(IMPOSSIBLE)
- }
- }
-
- /// Returns a parsed naive time out of given fields.
- ///
- /// This method is able to determine the time from given subset of fields:
- ///
- /// - Hour, minute. (second and nanosecond assumed to be 0)
- /// - Hour, minute, second. (nanosecond assumed to be 0)
- /// - Hour, minute, second, nanosecond.
- ///
- /// It is able to handle leap seconds when given second is 60.
- pub fn to_naive_time(&self) -> ParseResult<NaiveTime> {
- let hour_div_12 = match self.hour_div_12 {
- Some(v @ 0...1) => v,
- Some(_) => return Err(OUT_OF_RANGE),
- None => return Err(NOT_ENOUGH),
- };
- let hour_mod_12 = match self.hour_mod_12 {
- Some(v @ 0...11) => v,
- Some(_) => return Err(OUT_OF_RANGE),
- None => return Err(NOT_ENOUGH),
- };
- let hour = hour_div_12 * 12 + hour_mod_12;
-
- let minute = match self.minute {
- Some(v @ 0...59) => v,
- Some(_) => return Err(OUT_OF_RANGE),
- None => return Err(NOT_ENOUGH),
- };
-
- // we allow omitting seconds or nanoseconds, but they should be in the range.
- let (second, mut nano) = match self.second.unwrap_or(0) {
- v @ 0...59 => (v, 0),
- 60 => (59, 1_000_000_000),
- _ => return Err(OUT_OF_RANGE),
- };
- nano += match self.nanosecond {
- Some(v @ 0...999_999_999) if self.second.is_some() => v,
- Some(0...999_999_999) => return Err(NOT_ENOUGH), // second is missing
- Some(_) => return Err(OUT_OF_RANGE),
- None => 0,
- };
-
- NaiveTime::from_hms_nano_opt(hour, minute, second, nano).ok_or(OUT_OF_RANGE)
- }
-
- /// Returns a parsed naive date and time out of given fields,
- /// except for the [`offset`](#structfield.offset) field (assumed to have a given value).
- /// This is required for parsing a local time or other known-timezone inputs.
- ///
- /// This method is able to determine the combined date and time
- /// from date and time fields or a single [`timestamp`](#structfield.timestamp) field.
- /// Either way those fields have to be consistent to each other.
- pub fn to_naive_datetime_with_offset(&self, offset: i32) -> ParseResult<NaiveDateTime> {
- let date = self.to_naive_date();
- let time = self.to_naive_time();
- if let (Ok(date), Ok(time)) = (date, time) {
- let datetime = date.and_time(time);
-
- // verify the timestamp field if any
- // the following is safe, `timestamp` is very limited in range
- let timestamp = datetime.timestamp() - offset as i64;
- if let Some(given_timestamp) = self.timestamp {
- // if `datetime` represents a leap second, it might be off by one second.
- if given_timestamp != timestamp &&
- !(datetime.nanosecond() >= 1_000_000_000 && given_timestamp == timestamp + 1) {
- return Err(IMPOSSIBLE);
- }
- }
-
- Ok(datetime)
- } else if let Some(timestamp) = self.timestamp {
- use super::ParseError as PE;
- use super::ParseErrorKind::{OutOfRange, Impossible};
-
- // if date and time is problematic already, there is no point proceeding.
- // we at least try to give a correct error though.
- match (date, time) {
- (Err(PE(OutOfRange)), _) | (_, Err(PE(OutOfRange))) => return Err(OUT_OF_RANGE),
- (Err(PE(Impossible)), _) | (_, Err(PE(Impossible))) => return Err(IMPOSSIBLE),
- (_, _) => {} // one of them is insufficient
- }
-
- // reconstruct date and time fields from timestamp
- let ts = try!(timestamp.checked_add(offset as i64).ok_or(OUT_OF_RANGE));
- let datetime = NaiveDateTime::from_timestamp_opt(ts, 0);
- let mut datetime = try!(datetime.ok_or(OUT_OF_RANGE));
-
- // fill year, ordinal, hour, minute and second fields from timestamp.
- // if existing fields are consistent, this will allow the full date/time reconstruction.
- let mut parsed = self.clone();
- if parsed.second == Some(60) {
- // `datetime.second()` cannot be 60, so this is the only case for a leap second.
- match datetime.second() {
- // it's okay, just do not try to overwrite the existing field.
- 59 => {}
- // `datetime` is known to be off by one second.
- 0 => { datetime = datetime - OldDuration::seconds(1); }
- // otherwise it is impossible.
- _ => return Err(IMPOSSIBLE)
- }
- // ...and we have the correct candidates for other fields.
- } else {
- try!(parsed.set_second(datetime.second() as i64));
- }
- try!(parsed.set_year (datetime.year() as i64));
- try!(parsed.set_ordinal(datetime.ordinal() as i64)); // more efficient than ymd
- try!(parsed.set_hour (datetime.hour() as i64));
- try!(parsed.set_minute (datetime.minute() as i64));
- try!(parsed.set_nanosecond(0)); // no nanosecond precision in timestamp
-
- // validate other fields (e.g. week) and return
- let date = try!(parsed.to_naive_date());
- let time = try!(parsed.to_naive_time());
- Ok(date.and_time(time))
- } else {
- // reproduce the previous error(s)
- try!(date);
- try!(time);
- unreachable!()
- }
- }
-
- /// Returns a parsed fixed time zone offset out of given fields.
- pub fn to_fixed_offset(&self) -> ParseResult<FixedOffset> {
- self.offset.and_then(FixedOffset::east_opt).ok_or(OUT_OF_RANGE)
- }
-
- /// Returns a parsed timezone-aware date and time out of given fields.
- ///
- /// This method is able to determine the combined date and time
- /// from date and time fields or a single [`timestamp`](#structfield.timestamp) field,
- /// plus a time zone offset.
- /// Either way those fields have to be consistent to each other.
- pub fn to_datetime(&self) -> ParseResult<DateTime<FixedOffset>> {
- let offset = try!(self.offset.ok_or(NOT_ENOUGH));
- let datetime = try!(self.to_naive_datetime_with_offset(offset));
- let offset = try!(FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE));
- match offset.from_local_datetime(&datetime) {
- LocalResult::None => Err(IMPOSSIBLE),
- LocalResult::Single(t) => Ok(t),
- LocalResult::Ambiguous(..) => Err(NOT_ENOUGH),
- }
- }
-
- /// Returns a parsed timezone-aware date and time out of given fields,
- /// with an additional `TimeZone` used to interpret and validate the local date.
- ///
- /// This method is able to determine the combined date and time
- /// from date and time fields or a single [`timestamp`](#structfield.timestamp) field,
- /// plus a time zone offset.
- /// Either way those fields have to be consistent to each other.
- /// If parsed fields include an UTC offset, it also has to be consistent to
- /// [`offset`](#structfield.offset).
- pub fn to_datetime_with_timezone<Tz: TimeZone>(&self, tz: &Tz) -> ParseResult<DateTime<Tz>> {
- // if we have `timestamp` specified, guess an offset from that.
- let mut guessed_offset = 0;
- if let Some(timestamp) = self.timestamp {
- // make a naive `DateTime` from given timestamp and (if any) nanosecond.
- // an empty `nanosecond` is always equal to zero, so missing nanosecond is fine.
- let nanosecond = self.nanosecond.unwrap_or(0);
- let dt = NaiveDateTime::from_timestamp_opt(timestamp, nanosecond);
- let dt = try!(dt.ok_or(OUT_OF_RANGE));
- guessed_offset = tz.offset_from_utc_datetime(&dt).fix().local_minus_utc();
- }
-
- // checks if the given `DateTime` has a consistent `Offset` with given `self.offset`.
- let check_offset = |dt: &DateTime<Tz>| {
- if let Some(offset) = self.offset {
- dt.offset().fix().local_minus_utc() == offset
- } else {
- true
- }
- };
-
- // `guessed_offset` should be correct when `self.timestamp` is given.
- // it will be 0 otherwise, but this is fine as the algorithm ignores offset for that case.
- let datetime = try!(self.to_naive_datetime_with_offset(guessed_offset));
- match tz.from_local_datetime(&datetime) {
- LocalResult::None => Err(IMPOSSIBLE),
- LocalResult::Single(t) => if check_offset(&t) {Ok(t)} else {Err(IMPOSSIBLE)},
- LocalResult::Ambiguous(min, max) => {
- // try to disambiguate two possible local dates by offset.
- match (check_offset(&min), check_offset(&max)) {
- (false, false) => Err(IMPOSSIBLE),
- (false, true) => Ok(max),
- (true, false) => Ok(min),
- (true, true) => Err(NOT_ENOUGH),
- }
- }
- }
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::Parsed;
- use super::super::{OUT_OF_RANGE, IMPOSSIBLE, NOT_ENOUGH};
- use Datelike;
- use Weekday::*;
- use naive::date::{self, NaiveDate};
- use naive::time::NaiveTime;
- use offset::TimeZone;
- use offset::utc::UTC;
- use offset::fixed::FixedOffset;
-
- #[test]
- fn test_parsed_set_fields() {
- // year*, isoyear*
- let mut p = Parsed::new();
- assert_eq!(p.set_year(1987), Ok(()));
- assert_eq!(p.set_year(1986), Err(IMPOSSIBLE));
- assert_eq!(p.set_year(1988), Err(IMPOSSIBLE));
- assert_eq!(p.set_year(1987), Ok(()));
- assert_eq!(p.set_year_div_100(20), Ok(())); // independent to `year`
- assert_eq!(p.set_year_div_100(21), Err(IMPOSSIBLE));
- assert_eq!(p.set_year_div_100(19), Err(IMPOSSIBLE));
- assert_eq!(p.set_year_mod_100(37), Ok(())); // ditto
- assert_eq!(p.set_year_mod_100(38), Err(IMPOSSIBLE));
- assert_eq!(p.set_year_mod_100(36), Err(IMPOSSIBLE));
-
- let mut p = Parsed::new();
- assert_eq!(p.set_year(0), Ok(()));
- assert_eq!(p.set_year_div_100(0), Ok(()));
- assert_eq!(p.set_year_mod_100(0), Ok(()));
-
- let mut p = Parsed::new();
- assert_eq!(p.set_year_div_100(-1), Err(OUT_OF_RANGE));
- assert_eq!(p.set_year_mod_100(-1), Err(OUT_OF_RANGE));
- assert_eq!(p.set_year(-1), Ok(()));
- assert_eq!(p.set_year(-2), Err(IMPOSSIBLE));
- assert_eq!(p.set_year(0), Err(IMPOSSIBLE));
-
- let mut p = Parsed::new();
- assert_eq!(p.set_year_div_100(0x1_0000_0008), Err(OUT_OF_RANGE));
- assert_eq!(p.set_year_div_100(8), Ok(()));
- assert_eq!(p.set_year_div_100(0x1_0000_0008), Err(OUT_OF_RANGE));
-
- // month, week*, isoweek, ordinal, day, minute, second, nanosecond, offset
- let mut p = Parsed::new();
- assert_eq!(p.set_month(7), Ok(()));
- assert_eq!(p.set_month(1), Err(IMPOSSIBLE));
- assert_eq!(p.set_month(6), Err(IMPOSSIBLE));
- assert_eq!(p.set_month(8), Err(IMPOSSIBLE));
- assert_eq!(p.set_month(12), Err(IMPOSSIBLE));
-
- let mut p = Parsed::new();
- assert_eq!(p.set_month(8), Ok(()));
- assert_eq!(p.set_month(0x1_0000_0008), Err(OUT_OF_RANGE));
-
- // hour
- let mut p = Parsed::new();
- assert_eq!(p.set_hour(12), Ok(()));
- assert_eq!(p.set_hour(11), Err(IMPOSSIBLE));
- assert_eq!(p.set_hour(13), Err(IMPOSSIBLE));
- assert_eq!(p.set_hour(12), Ok(()));
- assert_eq!(p.set_ampm(false), Err(IMPOSSIBLE));
- assert_eq!(p.set_ampm(true), Ok(()));
- assert_eq!(p.set_hour12(12), Ok(()));
- assert_eq!(p.set_hour12(0), Err(OUT_OF_RANGE)); // requires canonical representation
- assert_eq!(p.set_hour12(1), Err(IMPOSSIBLE));
- assert_eq!(p.set_hour12(11), Err(IMPOSSIBLE));
-
- let mut p = Parsed::new();
- assert_eq!(p.set_ampm(true), Ok(()));
- assert_eq!(p.set_hour12(7), Ok(()));
- assert_eq!(p.set_hour(7), Err(IMPOSSIBLE));
- assert_eq!(p.set_hour(18), Err(IMPOSSIBLE));
- assert_eq!(p.set_hour(19), Ok(()));
-
- // timestamp
- let mut p = Parsed::new();
- assert_eq!(p.set_timestamp(1_234_567_890), Ok(()));
- assert_eq!(p.set_timestamp(1_234_567_889), Err(IMPOSSIBLE));
- assert_eq!(p.set_timestamp(1_234_567_891), Err(IMPOSSIBLE));
- }
-
- #[test]
- fn test_parsed_to_naive_date() {
- macro_rules! parse {
- ($($k:ident: $v:expr),*) => (
- Parsed { $($k: Some($v),)* ..Parsed::new() }.to_naive_date()
- )
- }
-
- let ymd = |y,m,d| Ok(NaiveDate::from_ymd(y, m, d));
-
- // ymd: omission of fields
- assert_eq!(parse!(), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 1984), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 1984, month: 1), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 1984, month: 1, day: 2), ymd(1984, 1, 2));
- assert_eq!(parse!(year: 1984, day: 2), Err(NOT_ENOUGH));
- assert_eq!(parse!(year_div_100: 19), Err(NOT_ENOUGH));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 84), Err(NOT_ENOUGH));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 84, month: 1), Err(NOT_ENOUGH));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 84, month: 1, day: 2), ymd(1984, 1, 2));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 84, day: 2), Err(NOT_ENOUGH));
- assert_eq!(parse!(year_div_100: 19, month: 1, day: 2), Err(NOT_ENOUGH));
- assert_eq!(parse!(year_mod_100: 70, month: 1, day: 2), ymd(1970, 1, 2));
- assert_eq!(parse!(year_mod_100: 69, month: 1, day: 2), ymd(2069, 1, 2));
-
- // ymd: out-of-range conditions
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 84, month: 2, day: 29),
- ymd(1984, 2, 29));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 83, month: 2, day: 29),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 83, month: 13, day: 1),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 83, month: 12, day: 31),
- ymd(1983, 12, 31));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 83, month: 12, day: 32),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 83, month: 12, day: 0),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: 100, month: 1, day: 1),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year_div_100: 19, year_mod_100: -1, month: 1, day: 1),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year_div_100: 0, year_mod_100: 0, month: 1, day: 1),
- ymd(0, 1, 1));
- assert_eq!(parse!(year_div_100: -1, year_mod_100: 42, month: 1, day: 1),
- Err(OUT_OF_RANGE));
- let max_year = date::MAX.year();
- assert_eq!(parse!(year_div_100: max_year / 100,
- year_mod_100: max_year % 100, month: 1, day: 1),
- ymd(max_year, 1, 1));
- assert_eq!(parse!(year_div_100: (max_year + 1) / 100,
- year_mod_100: (max_year + 1) % 100, month: 1, day: 1),
- Err(OUT_OF_RANGE));
-
- // ymd: conflicting inputs
- assert_eq!(parse!(year: 1984, year_div_100: 19, month: 1, day: 1), ymd(1984, 1, 1));
- assert_eq!(parse!(year: 1984, year_div_100: 20, month: 1, day: 1), Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 1984, year_mod_100: 84, month: 1, day: 1), ymd(1984, 1, 1));
- assert_eq!(parse!(year: 1984, year_mod_100: 83, month: 1, day: 1), Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 1984, year_div_100: 19, year_mod_100: 84, month: 1, day: 1),
- ymd(1984, 1, 1));
- assert_eq!(parse!(year: 1984, year_div_100: 18, year_mod_100: 94, month: 1, day: 1),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 1984, year_div_100: 18, year_mod_100: 184, month: 1, day: 1),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: -1, year_div_100: 0, year_mod_100: -1, month: 1, day: 1),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: -1, year_div_100: -1, year_mod_100: 99, month: 1, day: 1),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: -1, year_div_100: 0, month: 1, day: 1), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: -1, year_mod_100: 99, month: 1, day: 1), Err(OUT_OF_RANGE));
-
- // weekdates
- assert_eq!(parse!(year: 2000, week_from_mon: 0), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2000, week_from_sun: 0), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2000, weekday: Sun), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2000, week_from_mon: 0, weekday: Fri), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2000, week_from_sun: 0, weekday: Fri), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2000, week_from_mon: 0, weekday: Sat), ymd(2000, 1, 1));
- assert_eq!(parse!(year: 2000, week_from_sun: 0, weekday: Sat), ymd(2000, 1, 1));
- assert_eq!(parse!(year: 2000, week_from_mon: 0, weekday: Sun), ymd(2000, 1, 2));
- assert_eq!(parse!(year: 2000, week_from_sun: 1, weekday: Sun), ymd(2000, 1, 2));
- assert_eq!(parse!(year: 2000, week_from_mon: 1, weekday: Mon), ymd(2000, 1, 3));
- assert_eq!(parse!(year: 2000, week_from_sun: 1, weekday: Mon), ymd(2000, 1, 3));
- assert_eq!(parse!(year: 2000, week_from_mon: 1, weekday: Sat), ymd(2000, 1, 8));
- assert_eq!(parse!(year: 2000, week_from_sun: 1, weekday: Sat), ymd(2000, 1, 8));
- assert_eq!(parse!(year: 2000, week_from_mon: 1, weekday: Sun), ymd(2000, 1, 9));
- assert_eq!(parse!(year: 2000, week_from_sun: 2, weekday: Sun), ymd(2000, 1, 9));
- assert_eq!(parse!(year: 2000, week_from_mon: 2, weekday: Mon), ymd(2000, 1, 10));
- assert_eq!(parse!(year: 2000, week_from_sun: 52, weekday: Sat), ymd(2000, 12, 30));
- assert_eq!(parse!(year: 2000, week_from_sun: 53, weekday: Sun), ymd(2000, 12, 31));
- assert_eq!(parse!(year: 2000, week_from_sun: 53, weekday: Mon), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2000, week_from_sun: 0xffffffff, weekday: Mon), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2006, week_from_sun: 0, weekday: Sat), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2006, week_from_sun: 1, weekday: Sun), ymd(2006, 1, 1));
-
- // weekdates: conflicting inputs
- assert_eq!(parse!(year: 2000, week_from_mon: 1, week_from_sun: 1, weekday: Sat),
- ymd(2000, 1, 8));
- assert_eq!(parse!(year: 2000, week_from_mon: 1, week_from_sun: 2, weekday: Sun),
- ymd(2000, 1, 9));
- assert_eq!(parse!(year: 2000, week_from_mon: 1, week_from_sun: 1, weekday: Sun),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 2000, week_from_mon: 2, week_from_sun: 2, weekday: Sun),
- Err(IMPOSSIBLE));
-
- // ISO weekdates
- assert_eq!(parse!(isoyear: 2004, isoweek: 53), Err(NOT_ENOUGH));
- assert_eq!(parse!(isoyear: 2004, isoweek: 53, weekday: Fri), ymd(2004, 12, 31));
- assert_eq!(parse!(isoyear: 2004, isoweek: 53, weekday: Sat), ymd(2005, 1, 1));
- assert_eq!(parse!(isoyear: 2004, isoweek: 0xffffffff, weekday: Sat), Err(OUT_OF_RANGE));
- assert_eq!(parse!(isoyear: 2005, isoweek: 0, weekday: Thu), Err(OUT_OF_RANGE));
- assert_eq!(parse!(isoyear: 2005, isoweek: 5, weekday: Thu), ymd(2005, 2, 3));
- assert_eq!(parse!(isoyear: 2005, weekday: Thu), Err(NOT_ENOUGH));
-
- // year and ordinal
- assert_eq!(parse!(ordinal: 123), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2000, ordinal: 0), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2000, ordinal: 1), ymd(2000, 1, 1));
- assert_eq!(parse!(year: 2000, ordinal: 60), ymd(2000, 2, 29));
- assert_eq!(parse!(year: 2000, ordinal: 61), ymd(2000, 3, 1));
- assert_eq!(parse!(year: 2000, ordinal: 366), ymd(2000, 12, 31));
- assert_eq!(parse!(year: 2000, ordinal: 367), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2000, ordinal: 0xffffffff), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2100, ordinal: 0), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2100, ordinal: 1), ymd(2100, 1, 1));
- assert_eq!(parse!(year: 2100, ordinal: 59), ymd(2100, 2, 28));
- assert_eq!(parse!(year: 2100, ordinal: 60), ymd(2100, 3, 1));
- assert_eq!(parse!(year: 2100, ordinal: 365), ymd(2100, 12, 31));
- assert_eq!(parse!(year: 2100, ordinal: 366), Err(OUT_OF_RANGE));
- assert_eq!(parse!(year: 2100, ordinal: 0xffffffff), Err(OUT_OF_RANGE));
-
- // more complex cases
- assert_eq!(parse!(year: 2014, month: 12, day: 31, ordinal: 365, isoyear: 2015, isoweek: 1,
- week_from_sun: 52, week_from_mon: 52, weekday: Wed),
- ymd(2014, 12, 31));
- assert_eq!(parse!(year: 2014, month: 12, ordinal: 365, isoyear: 2015, isoweek: 1,
- week_from_sun: 52, week_from_mon: 52),
- ymd(2014, 12, 31));
- assert_eq!(parse!(year: 2014, month: 12, day: 31, ordinal: 365, isoyear: 2014, isoweek: 53,
- week_from_sun: 52, week_from_mon: 52, weekday: Wed),
- Err(IMPOSSIBLE)); // no ISO week date 2014-W53-3
- assert_eq!(parse!(year: 2012, isoyear: 2015, isoweek: 1,
- week_from_sun: 52, week_from_mon: 52),
- Err(NOT_ENOUGH)); // ambiguous (2014-12-29, 2014-12-30, 2014-12-31)
- assert_eq!(parse!(year_div_100: 20, isoyear_mod_100: 15, ordinal: 366),
- Err(NOT_ENOUGH)); // technically unique (2014-12-31) but Chrono gives up
- }
-
- #[test]
- fn test_parsed_to_naive_time() {
- macro_rules! parse {
- ($($k:ident: $v:expr),*) => (
- Parsed { $($k: Some($v),)* ..Parsed::new() }.to_naive_time()
- )
- }
-
- let hms = |h,m,s| Ok(NaiveTime::from_hms(h, m, s));
- let hmsn = |h,m,s,n| Ok(NaiveTime::from_hms_nano(h, m, s, n));
-
- // omission of fields
- assert_eq!(parse!(), Err(NOT_ENOUGH));
- assert_eq!(parse!(hour_div_12: 0), Err(NOT_ENOUGH));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1), Err(NOT_ENOUGH));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23), hms(1,23,0));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, second: 45), hms(1,23,45));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, second: 45,
- nanosecond: 678_901_234),
- hmsn(1,23,45,678_901_234));
- assert_eq!(parse!(hour_div_12: 1, hour_mod_12: 11, minute: 45, second: 6), hms(23,45,6));
- assert_eq!(parse!(hour_mod_12: 1, minute: 23), Err(NOT_ENOUGH));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, nanosecond: 456_789_012),
- Err(NOT_ENOUGH));
-
- // out-of-range conditions
- assert_eq!(parse!(hour_div_12: 2, hour_mod_12: 0, minute: 0), Err(OUT_OF_RANGE));
- assert_eq!(parse!(hour_div_12: 1, hour_mod_12: 12, minute: 0), Err(OUT_OF_RANGE));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 60), Err(OUT_OF_RANGE));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, second: 61),
- Err(OUT_OF_RANGE));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, second: 34,
- nanosecond: 1_000_000_000),
- Err(OUT_OF_RANGE));
-
- // leap seconds
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, second: 60),
- hmsn(1,23,59,1_000_000_000));
- assert_eq!(parse!(hour_div_12: 0, hour_mod_12: 1, minute: 23, second: 60,
- nanosecond: 999_999_999),
- hmsn(1,23,59,1_999_999_999));
- }
-
- #[test]
- fn test_parsed_to_naive_datetime_with_offset() {
- macro_rules! parse {
- (offset = $offset:expr; $($k:ident: $v:expr),*) => (
- Parsed { $($k: Some($v),)* ..Parsed::new() }.to_naive_datetime_with_offset($offset)
- );
- ($($k:ident: $v:expr),*) => (parse!(offset = 0; $($k: $v),*))
- }
-
- let ymdhms = |y,m,d,h,n,s| Ok(NaiveDate::from_ymd(y, m, d).and_hms(h, n, s));
- let ymdhmsn =
- |y,m,d,h,n,s,nano| Ok(NaiveDate::from_ymd(y, m, d).and_hms_nano(h, n, s, nano));
-
- // omission of fields
- assert_eq!(parse!(), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2015, month: 1, day: 30,
- hour_div_12: 1, hour_mod_12: 2, minute: 38),
- ymdhms(2015,1,30, 14,38,0));
- assert_eq!(parse!(year: 1997, month: 1, day: 30,
- hour_div_12: 1, hour_mod_12: 2, minute: 38, second: 5),
- ymdhms(1997,1,30, 14,38,5));
- assert_eq!(parse!(year: 2012, ordinal: 34, hour_div_12: 0, hour_mod_12: 5,
- minute: 6, second: 7, nanosecond: 890_123_456),
- ymdhmsn(2012,2,3, 5,6,7,890_123_456));
- assert_eq!(parse!(timestamp: 0), ymdhms(1970,1,1, 0,0,0));
- assert_eq!(parse!(timestamp: 1, nanosecond: 0), ymdhms(1970,1,1, 0,0,1));
- assert_eq!(parse!(timestamp: 1, nanosecond: 1), Err(IMPOSSIBLE));
- assert_eq!(parse!(timestamp: 1_420_000_000), ymdhms(2014,12,31, 4,26,40));
- assert_eq!(parse!(timestamp: -0x1_0000_0000), ymdhms(1833,11,24, 17,31,44));
-
- // full fields
- assert_eq!(parse!(year: 2014, year_div_100: 20, year_mod_100: 14, month: 12, day: 31,
- ordinal: 365, isoyear: 2015, isoyear_div_100: 20, isoyear_mod_100: 15,
- isoweek: 1, week_from_sun: 52, week_from_mon: 52, weekday: Wed,
- hour_div_12: 0, hour_mod_12: 4, minute: 26, second: 40,
- nanosecond: 12_345_678, timestamp: 1_420_000_000),
- ymdhmsn(2014,12,31, 4,26,40,12_345_678));
- assert_eq!(parse!(year: 2014, year_div_100: 20, year_mod_100: 14, month: 12, day: 31,
- ordinal: 365, isoyear: 2015, isoyear_div_100: 20, isoyear_mod_100: 15,
- isoweek: 1, week_from_sun: 52, week_from_mon: 52, weekday: Wed,
- hour_div_12: 0, hour_mod_12: 4, minute: 26, second: 40,
- nanosecond: 12_345_678, timestamp: 1_419_999_999),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(offset = 32400;
- year: 2014, year_div_100: 20, year_mod_100: 14, month: 12, day: 31,
- ordinal: 365, isoyear: 2015, isoyear_div_100: 20, isoyear_mod_100: 15,
- isoweek: 1, week_from_sun: 52, week_from_mon: 52, weekday: Wed,
- hour_div_12: 0, hour_mod_12: 4, minute: 26, second: 40,
- nanosecond: 12_345_678, timestamp: 1_419_967_600),
- ymdhmsn(2014,12,31, 4,26,40,12_345_678));
-
- // more timestamps
- let max_days_from_year_1970 =
- date::MAX.signed_duration_since(NaiveDate::from_ymd(1970,1,1));
- let year_0_from_year_1970 =
- NaiveDate::from_ymd(0,1,1).signed_duration_since(NaiveDate::from_ymd(1970,1,1));
- let min_days_from_year_1970 =
- date::MIN.signed_duration_since(NaiveDate::from_ymd(1970,1,1));
- assert_eq!(parse!(timestamp: min_days_from_year_1970.num_seconds()),
- ymdhms(date::MIN.year(),1,1, 0,0,0));
- assert_eq!(parse!(timestamp: year_0_from_year_1970.num_seconds()),
- ymdhms(0,1,1, 0,0,0));
- assert_eq!(parse!(timestamp: max_days_from_year_1970.num_seconds() + 86399),
- ymdhms(date::MAX.year(),12,31, 23,59,59));
-
- // 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_799), ymdhms(2012,6,30, 23,59,59));
- assert_eq!(parse!(second: 59, timestamp: 1_341_100_800), Err(IMPOSSIBLE));
- assert_eq!(parse!(second: 60, timestamp: 1_341_100_799),
- ymdhmsn(2012,6,30, 23,59,59,1_000_000_000));
- assert_eq!(parse!(second: 60, timestamp: 1_341_100_800),
- ymdhmsn(2012,6,30, 23,59,59,1_000_000_000));
- assert_eq!(parse!(second: 0, timestamp: 1_341_100_800), ymdhms(2012,7,1, 0,0,0));
- assert_eq!(parse!(second: 1, timestamp: 1_341_100_800), Err(IMPOSSIBLE));
- assert_eq!(parse!(second: 60, timestamp: 1_341_100_801), Err(IMPOSSIBLE));
-
- // leap seconds #2: full fields
- // we need to have separate tests for them since it uses another control flow.
- assert_eq!(parse!(year: 2012, ordinal: 182, hour_div_12: 1, hour_mod_12: 11,
- minute: 59, second: 59, timestamp: 1_341_100_798),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 2012, ordinal: 182, hour_div_12: 1, hour_mod_12: 11,
- minute: 59, second: 59, timestamp: 1_341_100_799),
- ymdhms(2012,6,30, 23,59,59));
- assert_eq!(parse!(year: 2012, ordinal: 182, hour_div_12: 1, hour_mod_12: 11,
- minute: 59, second: 59, timestamp: 1_341_100_800),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 2012, ordinal: 182, hour_div_12: 1, hour_mod_12: 11,
- minute: 59, second: 60, timestamp: 1_341_100_799),
- ymdhmsn(2012,6,30, 23,59,59,1_000_000_000));
- assert_eq!(parse!(year: 2012, ordinal: 182, hour_div_12: 1, hour_mod_12: 11,
- minute: 59, second: 60, timestamp: 1_341_100_800),
- ymdhmsn(2012,6,30, 23,59,59,1_000_000_000));
- assert_eq!(parse!(year: 2012, ordinal: 183, hour_div_12: 0, hour_mod_12: 0,
- minute: 0, second: 0, timestamp: 1_341_100_800),
- ymdhms(2012,7,1, 0,0,0));
- assert_eq!(parse!(year: 2012, ordinal: 183, hour_div_12: 0, hour_mod_12: 0,
- minute: 0, second: 1, timestamp: 1_341_100_800),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(year: 2012, ordinal: 182, hour_div_12: 1, hour_mod_12: 11,
- minute: 59, second: 60, timestamp: 1_341_100_801),
- Err(IMPOSSIBLE));
-
- // error codes
- assert_eq!(parse!(year: 2015, month: 1, day: 20, weekday: Tue,
- hour_div_12: 2, hour_mod_12: 1, minute: 35, second: 20),
- Err(OUT_OF_RANGE)); // `hour_div_12` is out of range
- }
-
- #[test]
- fn test_parsed_to_datetime() {
- macro_rules! parse {
- ($($k:ident: $v:expr),*) => (
- Parsed { $($k: Some($v),)* ..Parsed::new() }.to_datetime()
- )
- }
-
- let ymdhmsn = |y,m,d,h,n,s,nano,off| Ok(FixedOffset::east(off).ymd(y, m, d)
- .and_hms_nano(h, n, s, nano));
-
- assert_eq!(parse!(offset: 0), Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 4,
- minute: 26, second: 40, nanosecond: 12_345_678),
- Err(NOT_ENOUGH));
- assert_eq!(parse!(year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 4,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 0),
- ymdhmsn(2014,12,31, 4,26,40,12_345_678, 0));
- assert_eq!(parse!(year: 2014, ordinal: 365, hour_div_12: 1, hour_mod_12: 1,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 32400),
- ymdhmsn(2014,12,31, 13,26,40,12_345_678, 32400));
- assert_eq!(parse!(year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 1,
- minute: 42, second: 4, nanosecond: 12_345_678, offset: -9876),
- ymdhmsn(2014,12,31, 1,42,4,12_345_678, -9876));
- assert_eq!(parse!(year: 2015, ordinal: 1, hour_div_12: 0, hour_mod_12: 4,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 86400),
- Err(OUT_OF_RANGE)); // `FixedOffset` does not support such huge offset
- }
-
- #[test]
- fn test_parsed_to_datetime_with_timezone() {
- macro_rules! parse {
- ($tz:expr; $($k:ident: $v:expr),*) => (
- Parsed { $($k: Some($v),)* ..Parsed::new() }.to_datetime_with_timezone(&$tz)
- )
- }
-
- // single result from ymdhms
- assert_eq!(parse!(UTC;
- year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 4,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 0),
- Ok(UTC.ymd(2014, 12, 31).and_hms_nano(4, 26, 40, 12_345_678)));
- assert_eq!(parse!(UTC;
- year: 2014, ordinal: 365, hour_div_12: 1, hour_mod_12: 1,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 32400),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(FixedOffset::east(32400);
- year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 4,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 0),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(FixedOffset::east(32400);
- year: 2014, ordinal: 365, hour_div_12: 1, hour_mod_12: 1,
- minute: 26, second: 40, nanosecond: 12_345_678, offset: 32400),
- Ok(FixedOffset::east(32400).ymd(2014, 12, 31)
- .and_hms_nano(13, 26, 40, 12_345_678)));
-
- // single result from timestamp
- assert_eq!(parse!(UTC; timestamp: 1_420_000_000, offset: 0),
- Ok(UTC.ymd(2014, 12, 31).and_hms(4, 26, 40)));
- assert_eq!(parse!(UTC; timestamp: 1_420_000_000, offset: 32400),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(FixedOffset::east(32400); timestamp: 1_420_000_000, offset: 0),
- Err(IMPOSSIBLE));
- assert_eq!(parse!(FixedOffset::east(32400); timestamp: 1_420_000_000, offset: 32400),
- Ok(FixedOffset::east(32400).ymd(2014, 12, 31).and_hms(13, 26, 40)));
-
- // TODO test with a variable time zone (for None and Ambiguous cases)
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/format/scan.rs b/vendor/chrono-0.3.0/src/format/scan.rs
deleted file mode 100644
index 345b842..0000000
--- a/vendor/chrono-0.3.0/src/format/scan.rs
+++ /dev/null
@@ -1,261 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-/*!
- * Various scanning routines for the parser.
- */
-
-use Weekday;
-use super::{ParseResult, TOO_SHORT, INVALID, OUT_OF_RANGE};
-
-/// Returns true when two slices are equal case-insensitively (in ASCII).
-/// Assumes that the `pattern` is already converted to lower case.
-fn equals(s: &str, pattern: &str) -> bool {
- let mut xs = s.as_bytes().iter().map(|&c| match c { b'A'...b'Z' => c + 32, _ => c });
- let mut ys = pattern.as_bytes().iter().cloned();
- loop {
- match (xs.next(), ys.next()) {
- (None, None) => return true,
- (None, _) | (_, None) => return false,
- (Some(x), Some(y)) if x != y => return false,
- _ => (),
- }
- }
-}
-
-/// Tries to parse the non-negative number from `min` to `max` digits.
-///
-/// The absence of digits at all is an unconditional error.
-/// More than `max` digits are consumed up to the first `max` digits.
-/// Any number that does not fit in `i64` is an error.
-pub fn number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)> {
- assert!(min <= max);
-
- // limit `s` to given number of digits
- let mut window = s.as_bytes();
- if window.len() > max { window = &window[..max]; }
-
- // scan digits
- let upto = window.iter().position(|&c| c < b'0' || b'9' < c).unwrap_or(window.len());
- if upto < min {
- return Err(if window.is_empty() {TOO_SHORT} else {INVALID});
- }
-
- // we can overflow here, which is the only possible cause of error from `parse`.
- let v: i64 = try!(s[..upto].parse().map_err(|_| OUT_OF_RANGE));
- Ok((&s[upto..], v))
-}
-
-/// Tries to consume at least one digits as a fractional second.
-/// Returns the number of whole nanoseconds (0--999,999,999).
-pub fn nanosecond(s: &str) -> ParseResult<(&str, i64)> {
- // record the number of digits consumed for later scaling.
- let origlen = s.len();
- let (s, v) = try!(number(s, 1, 9));
- let consumed = origlen - s.len();
-
- // scale the number accordingly.
- static SCALE: [i64; 10] = [0, 100_000_000, 10_000_000, 1_000_000, 100_000, 10_000,
- 1_000, 100, 10, 1];
- let v = try!(v.checked_mul(SCALE[consumed]).ok_or(OUT_OF_RANGE));
-
- // if there are more than 9 digits, skip next digits.
- let s = s.trim_left_matches(|c: char| '0' <= c && c <= '9');
-
- Ok((s, v))
-}
-
-/// Tries to parse the month index (0 through 11) with the first three ASCII letters.
-pub fn short_month0(s: &str) -> ParseResult<(&str, u8)> {
- if s.len() < 3 { return Err(TOO_SHORT); }
- let buf = s.as_bytes();
- let month0 = match (buf[0] | 32, buf[1] | 32, buf[2] | 32) {
- (b'j',b'a',b'n') => 0,
- (b'f',b'e',b'b') => 1,
- (b'm',b'a',b'r') => 2,
- (b'a',b'p',b'r') => 3,
- (b'm',b'a',b'y') => 4,
- (b'j',b'u',b'n') => 5,
- (b'j',b'u',b'l') => 6,
- (b'a',b'u',b'g') => 7,
- (b's',b'e',b'p') => 8,
- (b'o',b'c',b't') => 9,
- (b'n',b'o',b'v') => 10,
- (b'd',b'e',b'c') => 11,
- _ => return Err(INVALID)
- };
- Ok((&s[3..], month0))
-}
-
-/// Tries to parse the weekday with the first three ASCII letters.
-pub fn short_weekday(s: &str) -> ParseResult<(&str, Weekday)> {
- if s.len() < 3 { return Err(TOO_SHORT); }
- let buf = s.as_bytes();
- let weekday = match (buf[0] | 32, buf[1] | 32, buf[2] | 32) {
- (b'm',b'o',b'n') => Weekday::Mon,
- (b't',b'u',b'e') => Weekday::Tue,
- (b'w',b'e',b'd') => Weekday::Wed,
- (b't',b'h',b'u') => Weekday::Thu,
- (b'f',b'r',b'i') => Weekday::Fri,
- (b's',b'a',b't') => Weekday::Sat,
- (b's',b'u',b'n') => Weekday::Sun,
- _ => return Err(INVALID)
- };
- Ok((&s[3..], weekday))
-}
-
-/// Tries to parse the month index (0 through 11) with short or long month names.
-/// It prefers long month names to short month names when both are possible.
-pub fn short_or_long_month0(s: &str) -> ParseResult<(&str, u8)> {
- // lowercased month names, minus first three chars
- static LONG_MONTH_SUFFIXES: [&'static str; 12] =
- ["uary", "ruary", "ch", "il", "", "e", "y", "ust", "tember", "ober", "ember", "ember"];
-
- let (mut s, month0) = try!(short_month0(s));
-
- // tries to consume the suffix if possible
- let suffix = LONG_MONTH_SUFFIXES[month0 as usize];
- if s.len() >= suffix.len() && equals(&s[..suffix.len()], suffix) {
- s = &s[suffix.len()..];
- }
-
- Ok((s, month0))
-}
-
-/// Tries to parse the weekday with short or long weekday names.
-/// It prefers long weekday names to short weekday names when both are possible.
-pub fn short_or_long_weekday(s: &str) -> ParseResult<(&str, Weekday)> {
- // lowercased weekday names, minus first three chars
- static LONG_WEEKDAY_SUFFIXES: [&'static str; 7] =
- ["day", "sday", "nesday", "rsday", "day", "urday", "day"];
-
- let (mut s, weekday) = try!(short_weekday(s));
-
- // tries to consume the suffix if possible
- let suffix = LONG_WEEKDAY_SUFFIXES[weekday.num_days_from_monday() as usize];
- if s.len() >= suffix.len() && equals(&s[..suffix.len()], suffix) {
- s = &s[suffix.len()..];
- }
-
- Ok((s, weekday))
-}
-
-/// Tries to consume exactly one given character.
-pub fn char(s: &str, c1: u8) -> ParseResult<&str> {
- match s.as_bytes().first() {
- Some(&c) if c == c1 => Ok(&s[1..]),
- Some(_) => Err(INVALID),
- None => Err(TOO_SHORT),
- }
-}
-
-/// Tries to consume one or more whitespace.
-pub fn space(s: &str) -> ParseResult<&str> {
- let s_ = s.trim_left();
- if s_.len() < s.len() {
- Ok(s_)
- } else if s.is_empty() {
- Err(TOO_SHORT)
- } else {
- Err(INVALID)
- }
-}
-
-/// Consumes any number (including zero) of colon or spaces.
-pub fn colon_or_space(s: &str) -> ParseResult<&str> {
- Ok(s.trim_left_matches(|c: char| c == ':' || c.is_whitespace()))
-}
-
-/// Tries to parse `[-+]\d\d` continued by `\d\d`. Return an offset in seconds if possible.
-///
-/// The additional `colon` may be used to parse a mandatory or optional `:`
-/// between hours and minutes, and should return either a new suffix or `Err` when parsing fails.
-pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
- where F: FnMut(&str) -> ParseResult<&str> {
- fn digits(s: &str) -> ParseResult<(u8, u8)> {
- let b = s.as_bytes();
- if b.len() < 2 {
- Err(TOO_SHORT)
- } else {
- Ok((b[0], b[1]))
- }
- }
- let negative = match s.as_bytes().first() {
- Some(&b'+') => false,
- Some(&b'-') => true,
- Some(_) => return Err(INVALID),
- None => return Err(TOO_SHORT),
- };
- s = &s[1..];
-
- // hours (00--99)
- let hours = match try!(digits(s)) {
- (h1 @ b'0'...b'9', h2 @ b'0'...b'9') => ((h1 - b'0') * 10 + (h2 - b'0')) as i32,
- _ => return Err(INVALID),
- };
- s = &s[2..];
-
- // colons (and possibly other separators)
- s = try!(colon(s));
-
- // minutes (00--59)
- let minutes = match try!(digits(s)) {
- (m1 @ b'0'...b'5', m2 @ b'0'...b'9') => ((m1 - b'0') * 10 + (m2 - b'0')) as i32,
- (b'6'...b'9', b'0'...b'9') => return Err(OUT_OF_RANGE),
- _ => return Err(INVALID),
- };
- s = &s[2..];
-
- let seconds = hours * 3600 + minutes * 60;
- Ok((s, if negative {-seconds} else {seconds}))
-}
-
-/// Same to `timezone_offset` but also allows for `z`/`Z` which is same to `+00:00`.
-pub fn timezone_offset_zulu<F>(s: &str, colon: F) -> ParseResult<(&str, i32)>
- where F: FnMut(&str) -> ParseResult<&str> {
- match s.as_bytes().first() {
- Some(&b'z') | Some(&b'Z') => Ok((&s[1..], 0)),
- _ => timezone_offset(s, colon),
- }
-}
-
-/// Same to `timezone_offset` but also allows for RFC 2822 legacy timezones.
-/// May return `None` which indicates an insufficient offset data (i.e. `-0000`).
-pub fn timezone_offset_2822(s: &str) -> ParseResult<(&str, Option<i32>)> {
- // tries to parse legacy time zone names
- let upto = s.as_bytes().iter().position(|&c| match c { b'a'...b'z' | b'A'...b'Z' => false,
- _ => true }).unwrap_or(s.len());
- if upto > 0 {
- let name = &s[..upto];
- let s = &s[upto..];
- if equals(name, "gmt") || equals(name, "ut") {
- Ok((s, Some(0)))
- } else if equals(name, "est") {
- Ok((s, Some(-5 * 3600)))
- } else if equals(name, "edt") {
- Ok((s, Some(-4 * 3600)))
- } else if equals(name, "cst") {
- Ok((s, Some(-6 * 3600)))
- } else if equals(name, "cdt") {
- Ok((s, Some(-5 * 3600)))
- } else if equals(name, "mst") {
- Ok((s, Some(-7 * 3600)))
- } else if equals(name, "mdt") {
- Ok((s, Some(-6 * 3600)))
- } else if equals(name, "pst") {
- Ok((s, Some(-8 * 3600)))
- } else if equals(name, "pdt") {
- Ok((s, Some(-7 * 3600)))
- } else {
- Ok((s, None)) // recommended by RFC 2822: consume but treat it as -0000
- }
- } else {
- let (s_, offset) = try!(timezone_offset(s, |s| Ok(s)));
- if offset == 0 && s.starts_with('-') { // -0000 is not same to +0000
- Ok((s_, None))
- } else {
- Ok((s_, Some(offset)))
- }
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/format/strftime.rs b/vendor/chrono-0.3.0/src/format/strftime.rs
deleted file mode 100644
index 2002d31..0000000
--- a/vendor/chrono-0.3.0/src/format/strftime.rs
+++ /dev/null
@@ -1,442 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-/*!
-`strftime`/`strptime`-inspired date and time formatting syntax.
-
-## Specifiers
-
-The following specifiers are available both to formatting and parsing.
-
-Spec. | Example | Description
------ | ------------- | -----------
- | | **DATE SPECIFIERS:**
-`%Y` | `2001` | The full proleptic Gregorian year, zero-padded to 4 digits. [1]
-`%C` | `20` | The proleptic Gregorian year divided by 100, zero-padded to 2 digits. [2]
-`%y` | `01` | The proleptic Gregorian year modulo 100, zero-padded to 2 digits. [2]
- | |
-`%m` | `07` | Month number (01--12), zero-padded to 2 digits.
-`%b` | `Jul` | Abbreviated month name. Always 3 letters.
-`%B` | `July` | Full month name. Also accepts corresponding abbreviation in parsing.
-`%h` | `Jul` | Same to `%b`.
- | |
-`%d` | `08` | Day number (01--31), zero-padded to 2 digits.
-`%e` | ` 8` | Same to `%d` but space-padded. Same to `%_d`.
- | |
-`%a` | `Sun` | Abbreviated weekday name. Always 3 letters.
-`%A` | `Sunday` | Full weekday name. Also accepts corresponding abbreviation in parsing.
-`%w` | `0` | Sunday = 0, Monday = 1, ..., Saturday = 6.
-`%u` | `7` | Monday = 1, Tuesday = 2, ..., Sunday = 7. (ISO 8601)
- | |
-`%U` | `28` | Week number starting with Sunday (00--53), zero-padded to 2 digits. [3]
-`%W` | `27` | Same to `%U`, but week 1 starts with the first Monday in that year instead.
- | |
-`%G` | `2001` | Same to `%Y` but uses the year number in ISO 8601 week date. [4]
-`%g` | `01` | Same to `%y` but uses the year number in ISO 8601 week date. [4]
-`%V` | `27` | Same to `%U` but uses the week number in ISO 8601 week date (01--53). [4]
- | |
-`%j` | `189` | Day of the year (001--366), zero-padded to 3 digits.
- | |
-`%D` | `07/08/01` | Month-day-year format. Same to `%m/%d/%y`.
-`%x` | `07/08/01` | Same to `%D`.
-`%F` | `2001-07-08` | Year-month-day format (ISO 8601). Same to `%Y-%m-%d`.
-`%v` | ` 8-Jul-2001` | Day-month-year format. Same to `%e-%b-%Y`.
- | |
- | | **TIME SPECIFIERS:**
-`%H` | `00` | Hour number (00--23), zero-padded to 2 digits.
-`%k` | ` 0` | Same to `%H` but space-padded. Same to `%_H`.
-`%I` | `12` | Hour number in 12-hour clocks (01--12), zero-padded to 2 digits.
-`%l` | `12` | Same to `%I` but space-padded. Same to `%_I`.
- | |
-`%P` | `am` | `am` or `pm` in 12-hour clocks.
-`%p` | `AM` | `AM` or `PM` in 12-hour clocks.
- | |
-`%M` | `34` | Minute number (00--59), zero-padded to 2 digits.
-`%S` | `60` | Second number (00--60), zero-padded to 2 digits. [5]
-`%f` | `026490000` | The fractional seconds (in nanoseconds) since last whole second. [8]
-`%.f` | `.026490` | Similar to `.%f` but left-aligned. [8]
-`%.3f`| `.026` | Similar to `.%f` but left-aligned but fixed to a length of 3. [8]
-`%.6f`| `.026490` | Similar to `.%f` but left-aligned but fixed to a length of 6. [8]
-`%.9f`| `.026490000` | Similar to `.%f` but left-aligned but fixed to a length of 9. [8]
- | |
-`%R` | `00:34` | Hour-minute format. Same to `%H:%M`.
-`%T` | `00:34:60` | Hour-minute-second format. Same to `%H:%M:%S`.
-`%X` | `00:34:60` | Same to `%T`.
-`%r` | `12:34:60 AM` | Hour-minute-second format in 12-hour clocks. Same to `%I:%M:%S %p`.
- | |
- | | **TIME ZONE SPECIFIERS:**
-`%Z` | `ACST` | *Formatting only:* Local time zone name.
-`%z` | `+0930` | Offset from the local time to UTC (with UTC being `+0000`).
-`%:z` | `+09:30` | Same to `%z` but with a colon.
- | |
- | | **DATE & TIME SPECIFIERS:**
-`%c` | `Sun Jul 8 00:34:60 2001` | `ctime` date & time format. Same to `%a %b %e %T %Y` sans `\n`.
-`%+` | `2001-07-08T00:34:60.026490+09:30` | ISO 8601 / RFC 3339 date & time format. [6]
- | |
-`%s` | `994518299` | UNIX timestamp, the number of seconds since 1970-01-01 00:00 UTC. [7]
- | |
- | | **SPECIAL SPECIFIERS:**
-`%t` | | Literal tab (`\t`).
-`%n` | | Literal newline (`\n`).
-`%%` | | Literal percent sign.
-
-It is possible to override the default padding behavior of numeric specifiers `%?`.
-This is not allowed for other specifiers and will result in the `BAD_FORMAT` error.
-
-Modifier | Description
--------- | -----------
-`%-?` | Suppresses any padding including spaces and zeroes. (e.g. `%j` = `012`, `%-j` = `12`)
-`%_?` | Uses spaces as a padding. (e.g. `%j` = `012`, `%_j` = ` 12`)
-`%0?` | Uses zeroes as a padding. (e.g. `%e` = ` 9`, `%0e` = `09`)
-
-Notes:
-
-1. `%Y`:
- Negative years are allowed in formatting but not in parsing.
-
-2. `%C`, `%y`:
- This is floor division, so 100 BCE (year number -99) will print `-1` and `99` respectively.
-
-3. `%U`:
- Week 1 starts with the first Sunday in that year.
- It is possible to have week 0 for days before the first Sunday.
-
-4. `%G`, `%g`, `%V`:
- Week 1 is the first week with at least 4 days in that year.
- Week 0 does not exist, so this should be used with `%G` or `%g`.
-
-5. `%S`:
- It accounts for leap seconds, so `60` is possible.
-
-6. `%+`:
- Same to `%Y-%m-%dT%H:%M:%S%.f%:z`,
- i.e. 0, 3, 6 or 9 fractional digits for seconds and colons in the time zone offset.
-
- The typical `strftime` implementations have
- different (and locale-dependent) formats for this specifier.
- While Chrono's format for `%+` is far more stable,
- it is best to avoid this specifier if you want to control the exact output.
-
-7. `%s`:
- This is not padded and can be negative.
- For the purpose of Chrono, it only accounts for non-leap seconds
- so it slightly differs from ISO C `strftime` behavior.
-
-8. `%f`, `%.f`, `%.3f`, `%.6f`, `%.9f`:
-
- The default `%f` is right-aligned and always zero-padded to 9 digits
- for the compatibility with glibc and others,
- so it always counts the number of nanoseconds since the last whole second.
- E.g. 7ms after the last second will print `007000000`,
- and parsing `7000000` will yield the same.
-
- The variant `%.f` is left-aligned and print 0, 3, 6 or 9 fractional digits
- according to the precision.
- E.g. 70ms after the last second under `%.f` will print `.070` (note: not `.07`),
- and parsing `.07`, `.070000` etc. will yield the same.
- Note that they can print or read nothing if the fractional part is zero or
- the next character is not `.`.
-
- The variant `%.3f`, `%.6f` and `%.9f` are left-aligned and print 3, 6 or 9 fractional digits
- according to the number preceding `f`.
- E.g. 70ms after the last second under `%.3f` will print `.070` (note: not `.07`),
- and parsing `.07`, `.070000` etc. will yield the same.
- Note that they can read nothing if the fractional part is zero or
- the next character is not `.` however will print with the specified length.
-
-*/
-
-use super::{Item, Numeric, Fixed, Pad};
-
-/// Parsing iterator for `strftime`-like format strings.
-#[derive(Clone)]
-pub struct StrftimeItems<'a> {
- /// Remaining portion of the string.
- remainder: &'a str,
- /// If the current specifier is composed of multiple formatting items (e.g. `%+`),
- /// parser refers to the statically reconstructed slice of them.
- /// If `recons` is not empty they have to be returned earlier than the `remainder`.
- recons: &'static [Item<'static>],
-}
-
-impl<'a> StrftimeItems<'a> {
- /// Creates a new parsing iterator from the `strftime`-like format string.
- pub fn new(s: &'a str) -> StrftimeItems<'a> {
- static FMT_NONE: [Item<'static>; 0] = [];
- StrftimeItems { remainder: s, recons: &FMT_NONE }
- }
-}
-
-impl<'a> Iterator for StrftimeItems<'a> {
- type Item = Item<'a>;
-
- fn next(&mut self) -> Option<Item<'a>> {
- // we have some reconstructed items to return
- if !self.recons.is_empty() {
- let item = self.recons[0].clone();
- self.recons = &self.recons[1..];
- return Some(item);
- }
-
- match self.remainder.chars().next() {
- // we are done
- None => return None,
-
- // the next item is a specifier
- Some('%') => {
- self.remainder = &self.remainder[1..];
-
- macro_rules! next {
- () => (
- match self.remainder.chars().next() {
- Some(x) => {
- self.remainder = &self.remainder[x.len_utf8()..];
- x
- },
- None => return Some(Item::Error), // premature end of string
- }
- )
- }
-
- let spec = next!();
- let pad_override = match spec {
- '-' => Some(Pad::None),
- '0' => Some(Pad::Zero),
- '_' => Some(Pad::Space),
- _ => None,
- };
- let spec = if pad_override.is_some() { next!() } else { spec };
-
- macro_rules! recons {
- [$head:expr, $($tail:expr),+] => ({
- const RECONS: &'static [Item<'static>] = &[$($tail),+];
- self.recons = RECONS;
- $head
- })
- }
-
- let item = match spec {
- 'A' => fix!(LongWeekdayName),
- 'B' => fix!(LongMonthName),
- 'C' => num0!(YearDiv100),
- 'D' => recons![num0!(Month), lit!("/"), num0!(Day), lit!("/"),
- num0!(YearMod100)],
- 'F' => recons![num0!(Year), lit!("-"), num0!(Month), lit!("-"), num0!(Day)],
- 'G' => num0!(IsoYear),
- 'H' => num0!(Hour),
- 'I' => num0!(Hour12),
- 'M' => num0!(Minute),
- 'P' => fix!(LowerAmPm),
- 'R' => recons![num0!(Hour), lit!(":"), num0!(Minute)],
- 'S' => num0!(Second),
- 'T' => recons![num0!(Hour), lit!(":"), num0!(Minute), lit!(":"), num0!(Second)],
- 'U' => num0!(WeekFromSun),
- 'V' => num0!(IsoWeek),
- 'W' => num0!(WeekFromMon),
- 'X' => recons![num0!(Hour), lit!(":"), num0!(Minute), lit!(":"), num0!(Second)],
- 'Y' => num0!(Year),
- 'Z' => fix!(TimezoneName),
- 'a' => fix!(ShortWeekdayName),
- 'b' => fix!(ShortMonthName),
- 'c' => recons![fix!(ShortWeekdayName), sp!(" "), fix!(ShortMonthName),
- sp!(" "), nums!(Day), sp!(" "), num0!(Hour), lit!(":"),
- num0!(Minute), lit!(":"), num0!(Second), sp!(" "), num0!(Year)],
- 'd' => num0!(Day),
- 'e' => nums!(Day),
- 'f' => num0!(Nanosecond),
- 'g' => num0!(IsoYearMod100),
- 'h' => fix!(ShortMonthName),
- 'j' => num0!(Ordinal),
- 'k' => nums!(Hour),
- 'l' => nums!(Hour12),
- 'm' => num0!(Month),
- 'n' => sp!("\n"),
- 'p' => fix!(UpperAmPm),
- 'r' => recons![num0!(Hour12), lit!(":"), num0!(Minute), lit!(":"),
- num0!(Second), sp!(" "), fix!(UpperAmPm)],
- 's' => num!(Timestamp),
- 't' => sp!("\t"),
- 'u' => num!(WeekdayFromMon),
- 'v' => recons![nums!(Day), lit!("-"), fix!(ShortMonthName), lit!("-"),
- num0!(Year)],
- 'w' => num!(NumDaysFromSun),
- 'x' => recons![num0!(Month), lit!("/"), num0!(Day), lit!("/"),
- num0!(YearMod100)],
- 'y' => num0!(YearMod100),
- 'z' => fix!(TimezoneOffset),
- '+' => fix!(RFC3339),
- ':' => match next!() {
- 'z' => fix!(TimezoneOffsetColon),
- _ => Item::Error,
- },
- '.' => match next!() {
- '3' => match next!() {
- 'f' => fix!(Nanosecond3),
- _ => Item::Error,
- },
- '6' => match next!() {
- 'f' => fix!(Nanosecond6),
- _ => Item::Error,
- },
- '9' => match next!() {
- 'f' => fix!(Nanosecond9),
- _ => Item::Error,
- },
- 'f' => fix!(Nanosecond),
- _ => Item::Error,
- },
- '%' => lit!("%"),
- _ => Item::Error, // no such specifier
- };
-
- // adjust `item` if we have any padding modifier
- if let Some(new_pad) = pad_override {
- match item {
- Item::Numeric(ref kind, _pad) if self.recons.is_empty() =>
- Some(Item::Numeric(kind.clone(), new_pad)),
- _ => Some(Item::Error), // no reconstructed or non-numeric item allowed
- }
- } else {
- Some(item)
- }
- },
-
- // the next item is space
- Some(c) if c.is_whitespace() => {
- // `%` is not a whitespace, so `c != '%'` is redundant
- let nextspec = self.remainder.find(|c: char| !c.is_whitespace())
- .unwrap_or(self.remainder.len());
- assert!(nextspec > 0);
- let item = sp!(&self.remainder[..nextspec]);
- self.remainder = &self.remainder[nextspec..];
- Some(item)
- },
-
- // the next item is literal
- _ => {
- let nextspec = self.remainder.find(|c: char| c.is_whitespace() || c == '%')
- .unwrap_or(self.remainder.len());
- assert!(nextspec > 0);
- let item = lit!(&self.remainder[..nextspec]);
- self.remainder = &self.remainder[nextspec..];
- Some(item)
- },
- }
- }
-}
-
-#[cfg(test)]
-#[test]
-fn test_strftime_items() {
- fn parse_and_collect<'a>(s: &'a str) -> Vec<Item<'a>> {
- // map any error into `[Item::Error]`. useful for easy testing.
- let items = StrftimeItems::new(s);
- let items = items.map(|spec| if spec == Item::Error {None} else {Some(spec)});
- items.collect::<Option<Vec<_>>>().unwrap_or(vec![Item::Error])
- }
-
- assert_eq!(parse_and_collect(""), []);
- assert_eq!(parse_and_collect(" \t\n\r "), [sp!(" \t\n\r ")]);
- assert_eq!(parse_and_collect("hello?"), [lit!("hello?")]);
- assert_eq!(parse_and_collect("a b\t\nc"), [lit!("a"), sp!(" "), lit!("b"), sp!("\t\n"),
- lit!("c")]);
- assert_eq!(parse_and_collect("100%%"), [lit!("100"), lit!("%")]);
- assert_eq!(parse_and_collect("100%% ok"), [lit!("100"), lit!("%"), sp!(" "), lit!("ok")]);
- assert_eq!(parse_and_collect("%%PDF-1.0"), [lit!("%"), lit!("PDF-1.0")]);
- assert_eq!(parse_and_collect("%Y-%m-%d"), [num0!(Year), lit!("-"), num0!(Month), lit!("-"),
- num0!(Day)]);
- assert_eq!(parse_and_collect("[%F]"), parse_and_collect("[%Y-%m-%d]"));
- assert_eq!(parse_and_collect("%m %d"), [num0!(Month), sp!(" "), num0!(Day)]);
- assert_eq!(parse_and_collect("%"), [Item::Error]);
- assert_eq!(parse_and_collect("%%"), [lit!("%")]);
- assert_eq!(parse_and_collect("%%%"), [Item::Error]);
- assert_eq!(parse_and_collect("%%%%"), [lit!("%"), lit!("%")]);
- assert_eq!(parse_and_collect("foo%?"), [Item::Error]);
- assert_eq!(parse_and_collect("bar%42"), [Item::Error]);
- assert_eq!(parse_and_collect("quux% +"), [Item::Error]);
- assert_eq!(parse_and_collect("%.Z"), [Item::Error]);
- assert_eq!(parse_and_collect("%:Z"), [Item::Error]);
- assert_eq!(parse_and_collect("%-Z"), [Item::Error]);
- assert_eq!(parse_and_collect("%0Z"), [Item::Error]);
- assert_eq!(parse_and_collect("%_Z"), [Item::Error]);
- assert_eq!(parse_and_collect("%.j"), [Item::Error]);
- assert_eq!(parse_and_collect("%:j"), [Item::Error]);
- assert_eq!(parse_and_collect("%-j"), [num!(Ordinal)]);
- assert_eq!(parse_and_collect("%0j"), [num0!(Ordinal)]);
- assert_eq!(parse_and_collect("%_j"), [nums!(Ordinal)]);
- assert_eq!(parse_and_collect("%.e"), [Item::Error]);
- assert_eq!(parse_and_collect("%:e"), [Item::Error]);
- assert_eq!(parse_and_collect("%-e"), [num!(Day)]);
- assert_eq!(parse_and_collect("%0e"), [num0!(Day)]);
- assert_eq!(parse_and_collect("%_e"), [nums!(Day)]);
-}
-
-#[cfg(test)]
-#[test]
-fn test_strftime_docs() {
- use {FixedOffset, TimeZone};
-
- let dt = FixedOffset::east(34200).ymd(2001, 7, 8).and_hms_nano(0, 34, 59, 1_026_490_000);
-
- // date specifiers
- assert_eq!(dt.format("%Y").to_string(), "2001");
- assert_eq!(dt.format("%C").to_string(), "20");
- assert_eq!(dt.format("%y").to_string(), "01");
- assert_eq!(dt.format("%m").to_string(), "07");
- assert_eq!(dt.format("%b").to_string(), "Jul");
- assert_eq!(dt.format("%B").to_string(), "July");
- assert_eq!(dt.format("%h").to_string(), "Jul");
- assert_eq!(dt.format("%d").to_string(), "08");
- assert_eq!(dt.format("%e").to_string(), " 8");
- assert_eq!(dt.format("%e").to_string(), dt.format("%_d").to_string());
- assert_eq!(dt.format("%a").to_string(), "Sun");
- assert_eq!(dt.format("%A").to_string(), "Sunday");
- assert_eq!(dt.format("%w").to_string(), "0");
- assert_eq!(dt.format("%u").to_string(), "7");
- assert_eq!(dt.format("%U").to_string(), "28");
- assert_eq!(dt.format("%W").to_string(), "27");
- assert_eq!(dt.format("%G").to_string(), "2001");
- assert_eq!(dt.format("%g").to_string(), "01");
- assert_eq!(dt.format("%V").to_string(), "27");
- assert_eq!(dt.format("%j").to_string(), "189");
- assert_eq!(dt.format("%D").to_string(), "07/08/01");
- assert_eq!(dt.format("%x").to_string(), "07/08/01");
- assert_eq!(dt.format("%F").to_string(), "2001-07-08");
- assert_eq!(dt.format("%v").to_string(), " 8-Jul-2001");
-
- // time specifiers
- assert_eq!(dt.format("%H").to_string(), "00");
- assert_eq!(dt.format("%k").to_string(), " 0");
- assert_eq!(dt.format("%k").to_string(), dt.format("%_H").to_string());
- assert_eq!(dt.format("%I").to_string(), "12");
- assert_eq!(dt.format("%l").to_string(), "12");
- assert_eq!(dt.format("%l").to_string(), dt.format("%_I").to_string());
- assert_eq!(dt.format("%P").to_string(), "am");
- assert_eq!(dt.format("%p").to_string(), "AM");
- assert_eq!(dt.format("%M").to_string(), "34");
- assert_eq!(dt.format("%S").to_string(), "60");
- assert_eq!(dt.format("%f").to_string(), "026490000");
- assert_eq!(dt.format("%.f").to_string(), ".026490");
- assert_eq!(dt.format("%.3f").to_string(), ".026");
- assert_eq!(dt.format("%.6f").to_string(), ".026490");
- assert_eq!(dt.format("%.9f").to_string(), ".026490000");
- assert_eq!(dt.format("%R").to_string(), "00:34");
- assert_eq!(dt.format("%T").to_string(), "00:34:60");
- assert_eq!(dt.format("%X").to_string(), "00:34:60");
- assert_eq!(dt.format("%r").to_string(), "12:34:60 AM");
-
- // time zone specifiers
- //assert_eq!(dt.format("%Z").to_string(), "ACST");
- assert_eq!(dt.format("%z").to_string(), "+0930");
- assert_eq!(dt.format("%:z").to_string(), "+09:30");
-
- // date & time specifiers
- assert_eq!(dt.format("%c").to_string(), "Sun Jul 8 00:34:60 2001");
- assert_eq!(dt.format("%+").to_string(), "2001-07-08T00:34:60.026490+09:30");
- assert_eq!(dt.format("%s").to_string(), "994518299");
-
- // special specifiers
- assert_eq!(dt.format("%t").to_string(), "\t");
- assert_eq!(dt.format("%n").to_string(), "\n");
- assert_eq!(dt.format("%%").to_string(), "%");
-}
diff --git a/vendor/chrono-0.3.0/src/lib.rs b/vendor/chrono-0.3.0/src/lib.rs
deleted file mode 100644
index dccc908..0000000
--- a/vendor/chrono-0.3.0/src/lib.rs
+++ /dev/null
@@ -1,766 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! # Chrono 0.3.0
-//!
-//! Date and time handling for Rust.
-//! It aims to be a feature-complete superset of
-//! the [time](https://github.com/rust-lang-deprecated/time) library.
-//! In particular,
-//!
-//! * Chrono strictly adheres to ISO 8601.
-//! * Chrono is timezone-aware by default, with separate timezone-naive types.
-//! * Chrono is space-optimal and (while not being the primary goal) reasonably efficient.
-//!
-//! There were several previous attempts to bring a good date and time library to Rust,
-//! which Chrono builts upon and should acknowledge:
-//!
-//! * [Initial research on
-//! the wiki](https://github.com/rust-lang/rust-wiki-backup/blob/master/Lib-datetime.md)
-//! * Dietrich Epp's [datetime-rs](https://github.com/depp/datetime-rs)
-//! * Luis de Bethencourt's [rust-datetime](https://github.com/luisbg/rust-datetime)
-//!
-//! ## Usage
-//!
-//! Put this in your `Cargo.toml`:
-//!
-//! ```toml
-//! [dependencies]
-//! chrono = "0.3"
-//! ```
-//!
-//! Or, if you want [Serde](https://github.com/serde-rs/serde) or
-//! [rustc-serialize](https://github.com/rust-lang-nursery/rustc-serialize) support,
-//! include the features like this:
-//!
-//! ```toml
-//! [dependencies]
-//! chrono = { version = "0.3", features = ["serde", "rustc-serialize"] }
-//! ```
-//!
-//! Then put this in your crate root:
-//!
-//! ```rust
-//! extern crate chrono;
-//! ```
-//!
-//! Avoid using `use chrono::*;` as Chrono exports several modules other than types.
-//! If you prefer the glob imports, use the following instead:
-//!
-//! ```rust
-//! use chrono::prelude::*;
-//! ```
-//!
-//! ## Overview
-//!
-//! ### Duration
-//!
-//! Chrono currently uses
-//! the [`time::Duration`](https://doc.rust-lang.org/time/time/struct.Duration.html) type
-//! from the `time` crate to represent the magnitude of a time span.
-//! Since this has the same name to the newer, standard type for duration,
-//! the reference will refer this type as `OldDuration`.
-//! Note that this is an "accurate" duration represented as seconds and
-//! nanoseconds and does not represent "nominal" components such as days or
-//! months.
-//!
-//! Chrono does not yet natively support
-//! the standard [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) type,
-//! but it will be supported in the future.
-//! Meanwhile you can convert between two types with
-//! [`Duration::from_std`](https://doc.rust-lang.org/time/time/struct.Duration.html#method.from_std)
-//! and
-//! [`Duration::to_std`](https://doc.rust-lang.org/time/time/struct.Duration.html#method.to_std)
-//! methods.
-//!
-//! ### Date and Time
-//!
-//! Chrono provides a
-//! [**`DateTime`**](./datetime/struct.DateTime.html)
-//! type to represent a date and a time in a timezone.
-//!
-//! For more abstract moment-in-time tracking such as internal timekeeping
-//! that is unconcerned with timezones, consider
-//! [`time::SystemTime`](https://doc.rust-lang.org/std/time/struct.SystemTime.html),
-//! which tracks your system clock, or
-//! [`time::Instant`](https://doc.rust-lang.org/std/time/struct.Instant.html), which
-//! is an opaque but monotonically-increasing representation of a moment in time.
-//!
-//! `DateTime` is timezone-aware and must be constructed from
-//! the [**`TimeZone`**](./offset/trait.TimeZone.html) object,
-//! which defines how the local date is converted to and back from the UTC date.
-//! There are three well-known `TimeZone` implementations:
-//!
-//! * [**`UTC`**](./offset/utc/struct.UTC.html) specifies the UTC time zone. It is most efficient.
-//!
-//! * [**`Local`**](./offset/local/struct.Local.html) specifies the system local time zone.
-//!
-//! * [**`FixedOffset`**](./offset/fixed/struct.FixedOffset.html) specifies
-//! an arbitrary, fixed time zone such as UTC+09:00 or UTC-10:30.
-//! This often results from the parsed textual date and time.
-//! Since it stores the most information and does not depend on the system environment,
-//! you would want to normalize other `TimeZone`s into this type.
-//!
-//! `DateTime`s with different `TimeZone` types are distinct and do not mix,
-//! but can be converted to each other using
-//! the [`DateTime::with_timezone`](./datetime/struct.DateTime.html#method.with_timezone) method.
-//!
-//! You can get the current date and time in the UTC time zone
-//! ([`UTC::now()`](./offset/utc/struct.UTC.html#method.now))
-//! or in the local time zone
-//! ([`Local::now()`](./offset/local/struct.Local.html#method.now)).
-//!
-//! ~~~~ {.rust}
-//! use chrono::prelude::*;
-//!
-//! let utc: DateTime<UTC> = UTC::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
-//! let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
-//! # let _ = utc; let _ = local;
-//! ~~~~
-//!
-//! Alternatively, you can create your own date and time.
-//! This is a bit verbose due to Rust's lack of function and method overloading,
-//! but in turn we get a rich combination of initialization methods.
-//!
-//! ~~~~ {.rust}
-//! use chrono::prelude::*;
-//! use chrono::offset::LocalResult;
-//!
-//! let dt = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
-//! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
-//! assert_eq!(dt, UTC.yo(2014, 189).and_hms(9, 10, 11));
-//! // July 8 is Tuesday in ISO week 28 of the year 2014.
-//! assert_eq!(dt, UTC.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
-//!
-//! let dt = UTC.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); // `2014-07-08T09:10:11.012Z`
-//! assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_micro(9, 10, 11, 12_000));
-//! assert_eq!(dt, UTC.ymd(2014, 7, 8).and_hms_nano(9, 10, 11, 12_000_000));
-//!
-//! // dynamic verification
-//! assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(21, 15, 33),
-//! LocalResult::Single(UTC.ymd(2014, 7, 8).and_hms(21, 15, 33)));
-//! assert_eq!(UTC.ymd_opt(2014, 7, 8).and_hms_opt(80, 15, 33), LocalResult::None);
-//! assert_eq!(UTC.ymd_opt(2014, 7, 38).and_hms_opt(21, 15, 33), LocalResult::None);
-//!
-//! // other time zone objects can be used to construct a local datetime.
-//! // obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical.
-//! let local_dt = Local.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12);
-//! let fixed_dt = FixedOffset::east(9 * 3600).ymd(2014, 7, 8).and_hms_milli(18, 10, 11, 12);
-//! assert_eq!(dt, fixed_dt);
-//! # let _ = local_dt;
-//! ~~~~
-//!
-//! Various properties are available to the date and time, and can be altered individually.
-//! Most of them are defined in the traits [`Datelike`](./trait.Datelike.html) and
-//! [`Timelike`](./trait.Timelike.html) which you should `use` before.
-//! Addition and subtraction is also supported.
-//! The following illustrates most supported operations to the date and time:
-//!
-//! ~~~~ {.rust}
-//! # extern crate chrono; extern crate time; fn main() {
-//! use chrono::prelude::*;
-//! use time::Duration;
-//!
-//! # /* we intentionally fake the datetime...
-//! // assume this returned `2014-11-28T21:45:59.324310806+09:00`:
-//! let dt = Local::now();
-//! # */ // up to here. we now define a fixed datetime for the illustrative purpose.
-//! # let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806);
-//!
-//! // property accessors
-//! assert_eq!((dt.year(), dt.month(), dt.day()), (2014, 11, 28));
-//! assert_eq!((dt.month0(), dt.day0()), (10, 27)); // for unfortunate souls
-//! assert_eq!((dt.hour(), dt.minute(), dt.second()), (21, 45, 59));
-//! assert_eq!(dt.weekday(), Weekday::Fri);
-//! assert_eq!(dt.weekday().number_from_monday(), 5); // Mon=1, ..., Sat=7
-//! assert_eq!(dt.ordinal(), 332); // the day of year
-//! assert_eq!(dt.num_days_from_ce(), 735565); // the number of days from and including Jan 1, 1
-//!
-//! // time zone accessor and manipulation
-//! assert_eq!(dt.offset().fix().local_minus_utc(), 9 * 3600);
-//! assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600));
-//! assert_eq!(dt.with_timezone(&UTC), UTC.ymd(2014, 11, 28).and_hms_nano(12, 45, 59, 324310806));
-//!
-//! // a sample of property manipulations (validates dynamically)
-//! assert_eq!(dt.with_day(29).unwrap().weekday(), Weekday::Sat); // 2014-11-29 is Saturday
-//! assert_eq!(dt.with_day(32), None);
-//! assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November 29, 301 BCE
-//!
-//! // arithmetic operations
-//! let dt1 = UTC.ymd(2014, 11, 14).and_hms(8, 9, 10);
-//! let dt2 = UTC.ymd(2014, 11, 14).and_hms(10, 9, 8);
-//! assert_eq!(dt1.signed_duration_since(dt2), Duration::seconds(-2 * 3600 + 2));
-//! assert_eq!(dt2.signed_duration_since(dt1), Duration::seconds(2 * 3600 - 2));
-//! assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) + Duration::seconds(1_000_000_000),
-//! UTC.ymd(2001, 9, 9).and_hms(1, 46, 40));
-//! assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_000),
-//! UTC.ymd(1938, 4, 24).and_hms(22, 13, 20));
-//! # }
-//! ~~~~
-//!
-//! Formatting is done via the [`format`](./datetime/struct.DateTime.html#method.format) method,
-//! which format is equivalent to the familiar `strftime` format.
-//! (See the [`format::strftime` module documentation](./format/strftime/index.html#specifiers)
-//! for full syntax.)
-//!
-//! 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
-//! [`to_rfc3339`](./datetime/struct.DateTime.html#method.to_rfc3339) methods
-//! for well-known formats.
-//!
-//! ~~~~ {.rust}
-//! use chrono::prelude::*;
-//!
-//! let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
-//! assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09");
-//! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), "Fri Nov 28 12:00:09 2014");
-//! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string());
-//!
-//! assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC");
-//! assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000");
-//! assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00");
-//! assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
-//! ~~~~
-//!
-//! Parsing can be done with three methods:
-//!
-//! 1. The standard [`FromStr`](https://doc.rust-lang.org/std/str/trait.FromStr.html) trait
-//! (and [`parse`](https://doc.rust-lang.org/std/primitive.str.html#method.parse) method
-//! on a string) can be used for parsing `DateTime<FixedOffset>`, `DateTime<UTC>` and
-//! `DateTime<Local>` values. This parses what the `{:?}`
-//! ([`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html))
-//! format specifier prints, and requires the offset to be present.
-//!
-//! 2. [`DateTime::parse_from_str`](./datetime/struct.DateTime.html#method.parse_from_str) parses
-//! 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.
-//! It *cannot* be used when the offset can be missing.
-//! [`DateTime::parse_from_rfc2822`](./datetime/struct.DateTime.html#method.parse_from_rfc2822)
-//! and
-//! [`DateTime::parse_from_rfc3339`](./datetime/struct.DateTime.html#method.parse_from_rfc3339)
-//! are similar but for well-known formats.
-//!
-//! 3. [`Offset::datetime_from_str`](./offset/trait.TimeZone.html#method.datetime_from_str) is
-//! similar but returns `DateTime` of given offset.
-//! When the explicit offset is missing from the input, it simply uses given offset.
-//! It issues an error when the input contains an explicit offset different
-//! from the current offset.
-//!
-//! More detailed control over the parsing process is available via
-//! [`format`](./format/index.html) module.
-//!
-//! ~~~~ {.rust}
-//! use chrono::prelude::*;
-//!
-//! let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
-//! let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600));
-//!
-//! // method 1
-//! assert_eq!("2014-11-28T12:00:09Z".parse::<DateTime<UTC>>(), Ok(dt.clone()));
-//! assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<UTC>>(), Ok(dt.clone()));
-//! assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<FixedOffset>>(), Ok(fixed_dt.clone()));
-//!
-//! // method 2
-//! assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"),
-//! Ok(fixed_dt.clone()));
-//! assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
-//! Ok(fixed_dt.clone()));
-//! assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone()));
-//!
-//! // method 3
-//! assert_eq!(UTC.datetime_from_str("2014-11-28 12:00:09", "%Y-%m-%d %H:%M:%S"), Ok(dt.clone()));
-//! assert_eq!(UTC.datetime_from_str("Fri Nov 28 12:00:09 2014", "%a %b %e %T %Y"), Ok(dt.clone()));
-//!
-//! // oops, the year is missing!
-//! assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T %Y").is_err());
-//! // oops, the format string does not include the year at all!
-//! assert!(UTC.datetime_from_str("Fri Nov 28 12:00:09", "%a %b %e %T").is_err());
-//! // oops, the weekday is incorrect!
-//! assert!(UTC.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_err());
-//! ~~~~
-//!
-//! ### Individual date
-//!
-//! Chrono also provides an individual date type ([**`Date`**](./date/struct.Date.html)).
-//! It also has time zones attached, and have to be constructed via time zones.
-//! Most operations available to `DateTime` are also available to `Date` whenever appropriate.
-//!
-//! ~~~~ {.rust}
-//! use chrono::prelude::*;
-//! use chrono::offset::LocalResult;
-//!
-//! # // these *may* fail, but only very rarely. just rerun the test if you were that unfortunate ;)
-//! assert_eq!(UTC::today(), UTC::now().date());
-//! assert_eq!(Local::today(), Local::now().date());
-//!
-//! assert_eq!(UTC.ymd(2014, 11, 28).weekday(), Weekday::Fri);
-//! assert_eq!(UTC.ymd_opt(2014, 11, 31), LocalResult::None);
-//! assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(),
-//! "070809");
-//! ~~~~
-//!
-//! There is no timezone-aware `Time` due to the lack of usefulness and also the complexity.
-//!
-//! `DateTime` has [`date`](./datetime/struct.DateTime.html#method.date) method
-//! which returns a `Date` which represents its date component.
-//! There is also a [`time`](./datetime/struct.DateTime.html#method.time) method,
-//! which simply returns a naive local time described below.
-//!
-//! ### Naive date and time
-//!
-//! Chrono provides naive counterparts to `Date`, (non-existent) `Time` and `DateTime`
-//! as [**`NaiveDate`**](./naive/date/struct.NaiveDate.html),
-//! [**`NaiveTime`**](./naive/time/struct.NaiveTime.html) and
-//! [**`NaiveDateTime`**](./naive/datetime/struct.NaiveDateTime.html) respectively.
-//!
-//! They have almost equivalent interfaces as their timezone-aware twins,
-//! but are not associated to time zones obviously and can be quite low-level.
-//! They are mostly useful for building blocks for higher-level types.
-//!
-//! Timezone-aware `DateTime` and `Date` types have two methods returning naive versions:
-//! [`naive_local`](./datetime/struct.DateTime.html#method.naive_local) returns
-//! a view to the naive local time,
-//! and [`naive_utc`](./datetime/struct.DateTime.html#method.naive_utc) returns
-//! a view to the naive UTC time.
-//!
-//! ## Limitations
-//!
-//! Only proleptic Gregorian calendar (i.e. extended to support older dates) is supported.
-//! Be very careful if you really have to deal with pre-20C dates, they can be in Julian or others.
-//!
-//! Date types are limited in about +/- 262,000 years from the common epoch.
-//! Time types are limited in the nanosecond accuracy.
-//!
-//! [Leap seconds are supported in the representation but
-//! Chrono doesn't try to make use of them](./naive/time/index.html#leap-second-handling).
-//! (The main reason is that leap seconds are not really predictable.)
-//! Almost *every* operation over the possible leap seconds will ignore them.
-//! Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time) scale
-//! if you want.
-//!
-//! Chrono inherently does not support an inaccurate or partial date and time representation.
-//! Any operation that can be ambiguous will return `None` in such cases.
-//! For example, "a month later" of 2014-01-30 is not well-defined
-//! and consequently `UTC.ymd(2014, 1, 30).with_month(2)` returns `None`.
-//!
-//! Advanced time zone handling is not yet supported (but is planned in 0.4).
-
-#![doc(html_root_url = "https://docs.rs/chrono/0.3.0/")]
-
-#![cfg_attr(bench, feature(test))] // lib stability features as per RFC #507
-#![deny(missing_docs)]
-
-extern crate time as oldtime;
-extern crate num;
-#[cfg(feature = "rustc-serialize")]
-extern crate rustc_serialize;
-#[cfg(feature = "serde")]
-extern crate serde;
-
-// this reexport is to aid the transition and should not be in the prelude!
-pub use oldtime::Duration;
-
-pub use offset::{TimeZone, Offset, LocalResult};
-pub use offset::utc::UTC;
-pub use offset::fixed::FixedOffset;
-pub use offset::local::Local;
-pub use naive::date::NaiveDate;
-pub use naive::time::NaiveTime;
-pub use naive::datetime::NaiveDateTime;
-pub use date::Date;
-pub use datetime::DateTime;
-pub use format::{ParseError, ParseResult};
-
-/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
-pub mod prelude {
- pub use {Datelike, Timelike, Weekday};
- pub use offset::{TimeZone, Offset};
- pub use offset::utc::UTC;
- pub use offset::fixed::FixedOffset;
- pub use offset::local::Local;
- pub use naive::date::NaiveDate;
- pub use naive::time::NaiveTime;
- pub use naive::datetime::NaiveDateTime;
- pub use date::Date;
- pub use datetime::DateTime;
-}
-
-// useful throughout the codebase
-macro_rules! try_opt {
- ($e:expr) => (match $e { Some(v) => v, None => return None })
-}
-
-mod div;
-pub mod offset;
-pub mod naive {
- //! Date and time types which do not concern about the timezones.
- //!
- //! They are primarily building blocks for other types
- //! (e.g. [`TimeZone`](../offset/trait.TimeZone.html)),
- //! but can be also used for the simpler date and time handling.
- pub mod date;
- pub mod time;
- pub mod datetime;
-}
-pub mod date;
-pub mod datetime;
-pub mod format;
-
-/// The day of week.
-///
-/// The order of the days of week depends on the context.
-/// (This is why this type does *not* implement `PartialOrd` or `Ord` traits.)
-/// One should prefer `*_from_monday` or `*_from_sunday` methods to get the correct result.
-#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
-#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
-pub enum Weekday {
- /// Monday.
- Mon = 0,
- /// Tuesday.
- Tue = 1,
- /// Wednesday.
- Wed = 2,
- /// Thursday.
- Thu = 3,
- /// Friday.
- Fri = 4,
- /// Saturday.
- Sat = 5,
- /// Sunday.
- Sun = 6,
-}
-
-impl Weekday {
- /// The next day in the week.
- ///
- /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
- /// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
- /// `w.succ()`: | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun` | `Mon`
- #[inline]
- pub fn succ(&self) -> Weekday {
- match *self {
- Weekday::Mon => Weekday::Tue,
- Weekday::Tue => Weekday::Wed,
- Weekday::Wed => Weekday::Thu,
- Weekday::Thu => Weekday::Fri,
- Weekday::Fri => Weekday::Sat,
- Weekday::Sat => Weekday::Sun,
- Weekday::Sun => Weekday::Mon,
- }
- }
-
- /// The previous day in the week.
- ///
- /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
- /// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
- /// `w.pred()`: | `Sun` | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat`
- #[inline]
- pub fn pred(&self) -> Weekday {
- match *self {
- Weekday::Mon => Weekday::Sun,
- Weekday::Tue => Weekday::Mon,
- Weekday::Wed => Weekday::Tue,
- Weekday::Thu => Weekday::Wed,
- Weekday::Fri => Weekday::Thu,
- Weekday::Sat => Weekday::Fri,
- Weekday::Sun => Weekday::Sat,
- }
- }
-
- /// Returns a day-of-week number starting from Monday = 1. (ISO 8601 weekday number)
- ///
- /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
- /// ------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
- /// `w.number_from_monday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 7
- #[inline]
- pub fn number_from_monday(&self) -> u32 {
- match *self {
- Weekday::Mon => 1,
- Weekday::Tue => 2,
- Weekday::Wed => 3,
- Weekday::Thu => 4,
- Weekday::Fri => 5,
- Weekday::Sat => 6,
- Weekday::Sun => 7,
- }
- }
-
- /// Returns a day-of-week number starting from Sunday = 1.
- ///
- /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
- /// ------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
- /// `w.number_from_sunday()`: | 2 | 3 | 4 | 5 | 6 | 7 | 1
- #[inline]
- pub fn number_from_sunday(&self) -> u32 {
- match *self {
- Weekday::Mon => 2,
- Weekday::Tue => 3,
- Weekday::Wed => 4,
- Weekday::Thu => 5,
- Weekday::Fri => 6,
- Weekday::Sat => 7,
- Weekday::Sun => 1,
- }
- }
-
- /// Returns a day-of-week number starting from Monday = 0.
- ///
- /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
- /// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
- /// `w.num_days_from_monday()`: | 0 | 1 | 2 | 3 | 4 | 5 | 6
- #[inline]
- pub fn num_days_from_monday(&self) -> u32 {
- match *self {
- Weekday::Mon => 0,
- Weekday::Tue => 1,
- Weekday::Wed => 2,
- Weekday::Thu => 3,
- Weekday::Fri => 4,
- Weekday::Sat => 5,
- Weekday::Sun => 6,
- }
- }
-
- /// Returns a day-of-week number starting from Sunday = 0.
- ///
- /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
- /// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
- /// `w.num_days_from_sunday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 0
- #[inline]
- pub fn num_days_from_sunday(&self) -> u32 {
- match *self {
- Weekday::Mon => 1,
- Weekday::Tue => 2,
- Weekday::Wed => 3,
- Weekday::Thu => 4,
- Weekday::Fri => 5,
- Weekday::Sat => 6,
- Weekday::Sun => 0,
- }
- }
-}
-
-/// Any weekday can be represented as an integer from 0 to 6, which equals to
-/// [`Weekday::num_days_from_monday`](#method.num_days_from_monday) in this implementation.
-/// Do not heavily depend on this though; use explicit methods whenever possible.
-impl num::traits::FromPrimitive for Weekday {
- #[inline]
- fn from_i64(n: i64) -> Option<Weekday> {
- match n {
- 0 => Some(Weekday::Mon),
- 1 => Some(Weekday::Tue),
- 2 => Some(Weekday::Wed),
- 3 => Some(Weekday::Thu),
- 4 => Some(Weekday::Fri),
- 5 => Some(Weekday::Sat),
- 6 => Some(Weekday::Sun),
- _ => None,
- }
- }
-
- #[inline]
- fn from_u64(n: u64) -> Option<Weekday> {
- match n {
- 0 => Some(Weekday::Mon),
- 1 => Some(Weekday::Tue),
- 2 => Some(Weekday::Wed),
- 3 => Some(Weekday::Thu),
- 4 => Some(Weekday::Fri),
- 5 => Some(Weekday::Sat),
- 6 => Some(Weekday::Sun),
- _ => None,
- }
- }
-}
-
-
-/// The common set of methods for date component.
-pub trait Datelike: Sized {
- /// Returns the year number in the [calendar date](./naive/date/index.html#calendar-date).
- fn year(&self) -> i32;
-
- /// Returns the absolute year number starting from 1 with a boolean flag,
- /// which is false when the year predates the epoch (BCE/BC) and true otherwise (CE/AD).
- #[inline]
- fn year_ce(&self) -> (bool, u32) {
- let year = self.year();
- if year < 1 {
- (false, (1 - year) as u32)
- } else {
- (true, year as u32)
- }
- }
-
- /// Returns the month number starting from 1.
- ///
- /// The return value ranges from 1 to 12.
- fn month(&self) -> u32;
-
- /// Returns the month number starting from 0.
- ///
- /// The return value ranges from 0 to 11.
- fn month0(&self) -> u32;
-
- /// Returns the day of month starting from 1.
- ///
- /// The return value ranges from 1 to 31. (The last day of month differs by months.)
- fn day(&self) -> u32;
-
- /// Returns the day of month starting from 0.
- ///
- /// The return value ranges from 0 to 30. (The last day of month differs by months.)
- fn day0(&self) -> u32;
-
- /// Returns the day of year starting from 1.
- ///
- /// The return value ranges from 1 to 366. (The last day of year differs by years.)
- fn ordinal(&self) -> u32;
-
- /// Returns the day of year starting from 0.
- ///
- /// The return value ranges from 0 to 365. (The last day of year differs by years.)
- fn ordinal0(&self) -> u32;
-
- /// Returns the day of week.
- fn weekday(&self) -> Weekday;
-
- /// Returns the ISO week date: an adjusted year, week number and day of week.
- /// The adjusted year may differ from that of the calendar date.
- fn isoweekdate(&self) -> (i32, u32, Weekday);
-
- /// Makes a new value with the year number changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_year(&self, year: i32) -> Option<Self>;
-
- /// Makes a new value with the month number (starting from 1) changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_month(&self, month: u32) -> Option<Self>;
-
- /// Makes a new value with the month number (starting from 0) changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_month0(&self, month0: u32) -> Option<Self>;
-
- /// Makes a new value with the day of month (starting from 1) changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_day(&self, day: u32) -> Option<Self>;
-
- /// Makes a new value with the day of month (starting from 0) changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_day0(&self, day0: u32) -> Option<Self>;
-
- /// Makes a new value with the day of year (starting from 1) changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_ordinal(&self, ordinal: u32) -> Option<Self>;
-
- /// Makes a new value with the day of year (starting from 0) changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_ordinal0(&self, ordinal0: u32) -> Option<Self>;
-
- /// Returns the number of days since January 1, 1 (Day 1) in the proleptic Gregorian calendar.
- fn num_days_from_ce(&self) -> i32 {
- // we know this wouldn't overflow since year is limited to 1/2^13 of i32's full range.
- let mut year = self.year() - 1;
- let mut ndays = 0;
- if year < 0 {
- let excess = 1 + (-year) / 400;
- year += excess * 400;
- ndays -= excess * 146097;
- }
- let div_100 = year / 100;
- ndays += ((year * 1461) >> 2) - div_100 + (div_100 >> 2);
- ndays + self.ordinal() as i32
- }
-}
-
-/// The common set of methods for time component.
-pub trait Timelike: Sized {
- /// Returns the hour number from 0 to 23.
- fn hour(&self) -> u32;
-
- /// Returns the hour number from 1 to 12 with a boolean flag,
- /// which is false for AM and true for PM.
- #[inline]
- fn hour12(&self) -> (bool, u32) {
- let hour = self.hour();
- let mut hour12 = hour % 12;
- if hour12 == 0 {
- hour12 = 12;
- }
- (hour >= 12, hour12)
- }
-
- /// Returns the minute number from 0 to 59.
- fn minute(&self) -> u32;
-
- /// Returns the second number from 0 to 59.
- fn second(&self) -> u32;
-
- /// 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 [leap second](./naive/time/index.html#leap-second-handling).
- fn nanosecond(&self) -> u32;
-
- /// Makes a new value with the hour number changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_hour(&self, hour: u32) -> Option<Self>;
-
- /// Makes a new value with the minute number changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- fn with_minute(&self, min: u32) -> Option<Self>;
-
- /// Makes a new value with the second number changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- /// As with the [`second`](#tymethod.second) method,
- /// the input range is restricted to 0 through 59.
- fn with_second(&self, sec: u32) -> Option<Self>;
-
- /// Makes a new value with nanoseconds since the whole non-leap second changed.
- ///
- /// Returns `None` when the resulting value would be invalid.
- /// As with the [`nanosecond`](#tymethod.nanosecond) method,
- /// the input range can exceed 1,000,000,000 for leap seconds.
- fn with_nanosecond(&self, nano: u32) -> Option<Self>;
-
- /// Returns the number of non-leap seconds past the last midnight.
- #[inline]
- fn num_seconds_from_midnight(&self) -> u32 {
- self.hour() * 3600 + self.minute() * 60 + self.second()
- }
-}
-
-#[test]
-fn test_readme_doomsday() {
- use num::iter::range_inclusive;
-
- for y in range_inclusive(naive::date::MIN.year(), naive::date::MAX.year()) {
- // even months
- let d4 = NaiveDate::from_ymd(y, 4, 4);
- let d6 = NaiveDate::from_ymd(y, 6, 6);
- let d8 = NaiveDate::from_ymd(y, 8, 8);
- let d10 = NaiveDate::from_ymd(y, 10, 10);
- let d12 = NaiveDate::from_ymd(y, 12, 12);
-
- // nine to five, seven-eleven
- let d59 = NaiveDate::from_ymd(y, 5, 9);
- let d95 = NaiveDate::from_ymd(y, 9, 5);
- let d711 = NaiveDate::from_ymd(y, 7, 11);
- let d117 = NaiveDate::from_ymd(y, 11, 7);
-
- // "March 0"
- let d30 = NaiveDate::from_ymd(y, 3, 1).pred();
-
- let weekday = d30.weekday();
- let other_dates = [d4, d6, d8, d10, d12, d59, d95, d711, d117];
- assert!(other_dates.iter().all(|d| d.weekday() == weekday));
- }
-}
diff --git a/vendor/chrono-0.3.0/src/naive/date.rs b/vendor/chrono-0.3.0/src/naive/date.rs
deleted file mode 100644
index f8fff8f..0000000
--- a/vendor/chrono-0.3.0/src/naive/date.rs
+++ /dev/null
@@ -1,2828 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! 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::ops::{Add, Sub};
-use num::traits::ToPrimitive;
-use oldtime::Duration as OldDuration;
-
-use {Weekday, Datelike};
-use div::div_mod_floor;
-use naive::time::NaiveTime;
-use naive::datetime::NaiveDateTime;
-use format::{Item, Numeric, Pad};
-use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
-
-use self::internals::{DateImpl, Of, Mdf, YearFlags};
-
-const MAX_YEAR: i32 = internals::MAX_YEAR;
-const MIN_YEAR: i32 = internals::MIN_YEAR;
-
-// MAX_YEAR-12-31 minus 0000-01-01
-// = ((MAX_YEAR+1)-01-01 minus 0001-01-01) + (0001-01-01 minus 0000-01-01) - 1 day
-// = ((MAX_YEAR+1)-01-01 minus 0001-01-01) + 365 days
-// = MAX_YEAR * 365 + (# of leap years from 0001 to MAX_YEAR) + 365 days
-#[cfg(test)] // only used for testing
-const MAX_DAYS_FROM_YEAR_0: i32 = MAX_YEAR * 365 +
- MAX_YEAR / 4 -
- MAX_YEAR / 100 +
- MAX_YEAR / 400 + 365;
-
-// MIN_YEAR-01-01 minus 0000-01-01
-// = (MIN_YEAR+400n+1)-01-01 minus (400n+1)-01-01
-// = ((MIN_YEAR+400n+1)-01-01 minus 0001-01-01) - ((400n+1)-01-01 minus 0001-01-01)
-// = ((MIN_YEAR+400n+1)-01-01 minus 0001-01-01) - 146097n days
-//
-// n is set to 1000 for convenience.
-#[cfg(test)] // only used for testing
-const MIN_DAYS_FROM_YEAR_0: i32 = (MIN_YEAR + 400_000) * 365 +
- (MIN_YEAR + 400_000) / 4 -
- (MIN_YEAR + 400_000) / 100 +
- (MIN_YEAR + 400_000) / 400 - 146097_000;
-
-#[cfg(test)] // only used for testing, but duplicated in naive::datetime
-const MAX_BITS: usize = 44;
-
-/// ISO 8601 calendar date without timezone.
-/// Allows for every [proleptic Gregorian date](./index.html#calendar-date)
-/// from Jan 1, 262145 BCE to Dec 31, 262143 CE.
-/// Also supports the conversion from ISO 8601 ordinal and week date.
-#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
-pub struct NaiveDate {
- ymdf: DateImpl, // (year << 13) | of
-}
-
-/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
-pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ };
-/// The maximum possible `NaiveDate` (December 31, 262143 CE).
-pub const MAX: 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.
-#[test]
-fn test_date_bounds() {
- let calculated_min = NaiveDate::from_ymd(MIN_YEAR, 1, 1);
- let calculated_max = NaiveDate::from_ymd(MAX_YEAR, 12, 31);
- assert!(MIN == calculated_min,
- "`MIN` should have a year flag {:?}", calculated_min.of().flags());
- assert!(MAX == calculated_max,
- "`MAX` should have a year flag {:?}", calculated_max.of().flags());
-
- // let's also check that the entire range do not exceed 2^44 seconds
- // (sometimes used for bounding `Duration` against overflow)
- let maxsecs = MAX.signed_duration_since(MIN).num_seconds();
- let maxsecs = maxsecs + 86401; // also take care of DateTime
- assert!(maxsecs < (1 << MAX_BITS),
- "The entire `NaiveDate` range somehow exceeds 2^{} seconds", MAX_BITS);
-}
-
-impl NaiveDate {
- /// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
- fn from_of(year: i32, of: Of) -> Option<NaiveDate> {
- if year >= MIN_YEAR && year <= MAX_YEAR && of.valid() {
- let Of(of) = of;
- Some(NaiveDate { ymdf: (year << 13) | (of as DateImpl) })
- } else {
- None
- }
- }
-
- /// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
- fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
- NaiveDate::from_of(year, mdf.to_of())
- }
-
- /// Makes a new `NaiveDate` from the [calendar date](./index.html#calendar-date)
- /// (year, month and day).
- ///
- /// Panics on the out-of-range date, invalid month and/or day.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike, Weekday};
- ///
- /// let d = NaiveDate::from_ymd(2015, 3, 14);
- /// assert_eq!(d.year(), 2015);
- /// assert_eq!(d.month(), 3);
- /// assert_eq!(d.day(), 14);
- /// assert_eq!(d.ordinal(), 73); // day of year
- /// assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday
- /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
- /// ~~~~
- pub fn from_ymd(year: i32, month: u32, day: u32) -> NaiveDate {
- 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)
- /// (year, month and day).
- ///
- /// Returns `None` on the out-of-range date, invalid month and/or day.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let from_ymd_opt = NaiveDate::from_ymd_opt;
- ///
- /// assert!(from_ymd_opt(2015, 3, 14).is_some());
- /// assert!(from_ymd_opt(2015, 0, 14).is_none());
- /// assert!(from_ymd_opt(2015, 2, 29).is_none());
- /// assert!(from_ymd_opt(-4, 2, 29).is_some()); // 5 BCE is a leap year
- /// assert!(from_ymd_opt(400000, 1, 1).is_none());
- /// assert!(from_ymd_opt(-400000, 1, 1).is_none());
- /// ~~~~
- pub fn from_ymd_opt(year: i32, month: u32, day: u32) -> Option<NaiveDate> {
- let flags = YearFlags::from_year(year);
- NaiveDate::from_mdf(year, Mdf::new(month, day, flags))
- }
-
- /// Makes a new `NaiveDate` from the [ordinal date](./index.html#ordinal-date)
- /// (year and day of the year).
- ///
- /// Panics on the out-of-range date and/or invalid day of year.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike, Weekday};
- ///
- /// let d = NaiveDate::from_yo(2015, 73);
- /// assert_eq!(d.ordinal(), 73);
- /// assert_eq!(d.year(), 2015);
- /// assert_eq!(d.month(), 3);
- /// assert_eq!(d.day(), 14);
- /// assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday
- /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
- /// ~~~~
- pub fn from_yo(year: i32, ordinal: u32) -> NaiveDate {
- 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)
- /// (year and day of the year).
- ///
- /// Returns `None` on the out-of-range date and/or invalid day of year.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let from_yo_opt = NaiveDate::from_yo_opt;
- ///
- /// assert!(from_yo_opt(2015, 100).is_some());
- /// assert!(from_yo_opt(2015, 0).is_none());
- /// assert!(from_yo_opt(2015, 365).is_some());
- /// assert!(from_yo_opt(2015, 366).is_none());
- /// assert!(from_yo_opt(-4, 366).is_some()); // 5 BCE is a leap year
- /// assert!(from_yo_opt(400000, 1).is_none());
- /// assert!(from_yo_opt(-400000, 1).is_none());
- /// ~~~~
- pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
- let flags = YearFlags::from_year(year);
- NaiveDate::from_of(year, Of::new(ordinal, flags))
- }
-
- /// Makes a new `NaiveDate` from the [ISO week date](./index.html#week-date)
- /// (year, week number and day of the week).
- /// The resulting `NaiveDate` may have a different year from the input year.
- ///
- /// Panics on the out-of-range date and/or invalid week number.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike, Weekday};
- ///
- /// let d = NaiveDate::from_isoywd(2015, 11, Weekday::Sat);
- /// assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat));
- /// assert_eq!(d.year(), 2015);
- /// assert_eq!(d.month(), 3);
- /// assert_eq!(d.day(), 14);
- /// assert_eq!(d.ordinal(), 73); // day of year
- /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
- /// ~~~~
- pub fn from_isoywd(year: i32, week: u32, weekday: Weekday) -> NaiveDate {
- 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)
- /// (year, week number and day of the week).
- /// The resulting `NaiveDate` may have a different year from the input year.
- ///
- /// Returns `None` on the out-of-range date and/or invalid week number.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Weekday};
- ///
- /// let from_ymd = NaiveDate::from_ymd;
- /// let from_isoywd_opt = NaiveDate::from_isoywd_opt;
- ///
- /// assert_eq!(from_isoywd_opt(2015, 0, Weekday::Sun), None);
- /// assert_eq!(from_isoywd_opt(2015, 10, Weekday::Sun), Some(from_ymd(2015, 3, 8)));
- /// assert_eq!(from_isoywd_opt(2015, 30, Weekday::Mon), Some(from_ymd(2015, 7, 20)));
- /// assert_eq!(from_isoywd_opt(2015, 60, Weekday::Mon), None);
- ///
- /// assert_eq!(from_isoywd_opt(400000, 10, Weekday::Fri), None);
- /// assert_eq!(from_isoywd_opt(-400000, 10, Weekday::Sat), None);
- /// ~~~~
- ///
- /// The year number of ISO week date may differ from that of the calendar date.
- ///
- /// ~~~~
- /// # use chrono::{NaiveDate, Weekday};
- /// # let from_ymd = NaiveDate::from_ymd;
- /// # let from_isoywd_opt = NaiveDate::from_isoywd_opt;
- /// // Mo Tu We Th Fr Sa Su
- /// // 2014-W52 22 23 24 25 26 27 28 has 4+ days of new year,
- /// // 2015-W01 29 30 31 1 2 3 4 <- so this is the first week
- /// assert_eq!(from_isoywd_opt(2014, 52, Weekday::Sun), Some(from_ymd(2014, 12, 28)));
- /// assert_eq!(from_isoywd_opt(2014, 53, Weekday::Mon), None);
- /// assert_eq!(from_isoywd_opt(2015, 1, Weekday::Mon), Some(from_ymd(2014, 12, 29)));
- ///
- /// // 2015-W52 21 22 23 24 25 26 27 has 4+ days of old year,
- /// // 2015-W53 28 29 30 31 1 2 3 <- so this is the last week
- /// // 2016-W01 4 5 6 7 8 9 10
- /// assert_eq!(from_isoywd_opt(2015, 52, Weekday::Sun), Some(from_ymd(2015, 12, 27)));
- /// assert_eq!(from_isoywd_opt(2015, 53, Weekday::Sun), Some(from_ymd(2016, 1, 3)));
- /// assert_eq!(from_isoywd_opt(2015, 54, Weekday::Mon), None);
- /// assert_eq!(from_isoywd_opt(2016, 1, Weekday::Mon), Some(from_ymd(2016, 1, 4)));
- /// ~~~~
- pub fn from_isoywd_opt(year: i32, week: u32, weekday: Weekday) -> Option<NaiveDate> {
- let flags = YearFlags::from_year(year);
- let nweeks = flags.nisoweeks();
- if 1 <= week && week <= nweeks {
- // ordinal = week ordinal - delta
- let weekord = week * 7 + weekday as u32;
- let delta = flags.isoweek_delta();
- if weekord <= delta { // ordinal < 1, previous year
- let prevflags = YearFlags::from_year(year - 1);
- NaiveDate::from_of(year - 1, Of::new(weekord + prevflags.ndays() - delta,
- prevflags))
- } else {
- let ordinal = weekord - delta;
- let ndays = flags.ndays();
- if ordinal <= ndays { // this year
- NaiveDate::from_of(year, Of::new(ordinal, flags))
- } else { // ordinal > ndays, next year
- let nextflags = YearFlags::from_year(year + 1);
- NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags))
- }
- }
- } else {
- None
- }
- }
-
- /// Makes a new `NaiveDate` from the number of days since January 1, 1 (Day 1)
- /// in the proleptic Gregorian calendar.
- ///
- /// Panics on the out-of-range date.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike, Weekday};
- ///
- /// let d = NaiveDate::from_num_days_from_ce(735671);
- /// assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
- /// assert_eq!(d.year(), 2015);
- /// assert_eq!(d.month(), 3);
- /// assert_eq!(d.day(), 14);
- /// assert_eq!(d.ordinal(), 73); // day of year
- /// assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday
- /// ~~~~
- ///
- /// While not directly supported by Chrono,
- /// it is easy to convert from the Julian day number
- /// (January 1, 4713 BCE in the *Julian* calendar being Day 0)
- /// to Gregorian with this method.
- /// (Note that this panics when `jd` is out of range.)
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// fn jd_to_date(jd: i32) -> NaiveDate {
- /// // keep in mind that the Julian day number is 0-based
- /// // while this method requires an 1-based number.
- /// NaiveDate::from_num_days_from_ce(jd - 1721425)
- /// }
- ///
- /// // January 1, 4713 BCE in Julian = November 24, 4714 BCE in Gregorian
- /// assert_eq!(jd_to_date(0), NaiveDate::from_ymd(-4713, 11, 24));
- ///
- /// assert_eq!(jd_to_date(1721426), NaiveDate::from_ymd(1, 1, 1));
- /// assert_eq!(jd_to_date(2450000), NaiveDate::from_ymd(1995, 10, 9));
- /// assert_eq!(jd_to_date(2451545), NaiveDate::from_ymd(2000, 1, 1));
- /// ~~~~
- #[inline]
- pub fn from_num_days_from_ce(days: i32) -> NaiveDate {
- NaiveDate::from_num_days_from_ce_opt(days).expect("out-of-range date")
- }
-
- /// Makes a new `NaiveDate` from the number of days since January 1, 1 (Day 1)
- /// in the proleptic Gregorian calendar.
- ///
- /// Returns `None` on the out-of-range date.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let from_ndays_opt = NaiveDate::from_num_days_from_ce_opt;
- /// let from_ymd = NaiveDate::from_ymd;
- ///
- /// assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3)));
- /// assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1)));
- /// assert_eq!(from_ndays_opt(0), Some(from_ymd(0, 12, 31)));
- /// assert_eq!(from_ndays_opt(-1), Some(from_ymd(0, 12, 30)));
- /// assert_eq!(from_ndays_opt(100_000_000), None);
- /// assert_eq!(from_ndays_opt(-100_000_000), None);
- /// ~~~~
- pub fn from_num_days_from_ce_opt(days: i32) -> Option<NaiveDate> {
- let days = days + 365; // make December 31, 1 BCE equal to day 0
- let (year_div_400, cycle) = div_mod_floor(days, 146097);
- let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
- let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
- NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32,
- Of::new(ordinal, flags))
- }
-
- /// Parses a string with the specified format string and returns a new `NaiveDate`.
- /// See the [`format::strftime` module](../../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let parse_from_str = NaiveDate::parse_from_str;
- ///
- /// assert_eq!(parse_from_str("2015-09-05", "%Y-%m-%d"),
- /// Ok(NaiveDate::from_ymd(2015, 9, 5)));
- /// assert_eq!(parse_from_str("5sep2015", "%d%b%Y"),
- /// Ok(NaiveDate::from_ymd(2015, 9, 5)));
- /// ~~~~
- ///
- /// Time and offset is ignored for the purpose of parsing.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # let parse_from_str = NaiveDate::parse_from_str;
- /// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- /// Ok(NaiveDate::from_ymd(2014, 5, 17)));
- /// ~~~~
- ///
- /// Out-of-bound dates or insufficient fields are errors.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # let parse_from_str = NaiveDate::parse_from_str;
- /// assert!(parse_from_str("2015/9", "%Y/%m").is_err());
- /// assert!(parse_from_str("2015/9/31", "%Y/%m/%d").is_err());
- /// ~~~~
- ///
- /// All parsed fields should be consistent to each other, otherwise it's an error.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # let parse_from_str = NaiveDate::parse_from_str;
- /// assert!(parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err());
- /// ~~~~
- pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveDate> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, StrftimeItems::new(fmt)));
- parsed.to_naive_date()
- }
-
- /// Makes a new `NaiveDateTime` from the current date and given `NaiveTime`.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveTime, NaiveDateTime};
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- /// let t = NaiveTime::from_hms_milli(12, 34, 56, 789);
- ///
- /// let dt: NaiveDateTime = d.and_time(t);
- /// assert_eq!(dt.date(), d);
- /// assert_eq!(dt.time(), t);
- /// ~~~~
- #[inline]
- pub fn and_time(&self, time: NaiveTime) -> NaiveDateTime {
- NaiveDateTime::new(*self, time)
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute and second.
- ///
- /// No [leap second](../time/index.html#leap-second-handling) is allowed here;
- /// use `NaiveDate::and_hms_*` methods with a subsecond parameter instead.
- ///
- /// Panics on invalid hour, minute and/or second.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- ///
- /// let dt: NaiveDateTime = d.and_hms(12, 34, 56);
- /// assert_eq!(dt.year(), 2015);
- /// assert_eq!(dt.weekday(), Weekday::Wed);
- /// assert_eq!(dt.second(), 56);
- /// ~~~~
- #[inline]
- pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> NaiveDateTime {
- self.and_hms_opt(hour, min, sec).expect("invalid time")
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute and second.
- ///
- /// No [leap second](../time/index.html#leap-second-handling) is allowed here;
- /// use `NaiveDate::and_hms_*_opt` methods with a subsecond parameter instead.
- ///
- /// Returns `None` on invalid hour, minute and/or second.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- /// assert!(d.and_hms_opt(12, 34, 56).is_some());
- /// assert!(d.and_hms_opt(12, 34, 60).is_none()); // use `and_hms_milli_opt` instead
- /// assert!(d.and_hms_opt(12, 60, 56).is_none());
- /// assert!(d.and_hms_opt(24, 34, 56).is_none());
- /// ~~~~
- #[inline]
- pub fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<NaiveDateTime> {
- NaiveTime::from_hms_opt(hour, min, sec).map(|time| self.and_time(time))
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
- ///
- /// The millisecond part can exceed 1,000
- /// in order to represent the [leap second](../time/index.html#leap-second-handling).
- ///
- /// Panics on invalid hour, minute, second and/or millisecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- ///
- /// let dt: NaiveDateTime = d.and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.year(), 2015);
- /// assert_eq!(dt.weekday(), Weekday::Wed);
- /// assert_eq!(dt.second(), 56);
- /// assert_eq!(dt.nanosecond(), 789_000_000);
- /// ~~~~
- #[inline]
- pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> NaiveDateTime {
- self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time")
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
- ///
- /// The millisecond part can exceed 1,000
- /// in order to represent the [leap second](../time/index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid hour, minute, second and/or millisecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- /// assert!(d.and_hms_milli_opt(12, 34, 56, 789).is_some());
- /// assert!(d.and_hms_milli_opt(12, 34, 59, 1_789).is_some()); // leap second
- /// assert!(d.and_hms_milli_opt(12, 34, 59, 2_789).is_none());
- /// assert!(d.and_hms_milli_opt(12, 34, 60, 789).is_none());
- /// assert!(d.and_hms_milli_opt(12, 60, 56, 789).is_none());
- /// assert!(d.and_hms_milli_opt(24, 34, 56, 789).is_none());
- /// ~~~~
- #[inline]
- pub fn and_hms_milli_opt(&self, hour: u32, min: u32, sec: u32,
- milli: u32) -> Option<NaiveDateTime> {
- NaiveTime::from_hms_milli_opt(hour, min, sec, milli).map(|time| self.and_time(time))
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
- ///
- /// The microsecond part can exceed 1,000,000
- /// in order to represent the [leap second](../time/index.html#leap-second-handling).
- ///
- /// Panics on invalid hour, minute, second and/or microsecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- ///
- /// let dt: NaiveDateTime = d.and_hms_micro(12, 34, 56, 789_012);
- /// assert_eq!(dt.year(), 2015);
- /// assert_eq!(dt.weekday(), Weekday::Wed);
- /// assert_eq!(dt.second(), 56);
- /// assert_eq!(dt.nanosecond(), 789_012_000);
- /// ~~~~
- #[inline]
- pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> NaiveDateTime {
- self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time")
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
- ///
- /// The microsecond part can exceed 1,000,000
- /// in order to represent the [leap second](../time/index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid hour, minute, second and/or microsecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- /// assert!(d.and_hms_micro_opt(12, 34, 56, 789_012).is_some());
- /// assert!(d.and_hms_micro_opt(12, 34, 59, 1_789_012).is_some()); // leap second
- /// assert!(d.and_hms_micro_opt(12, 34, 59, 2_789_012).is_none());
- /// assert!(d.and_hms_micro_opt(12, 34, 60, 789_012).is_none());
- /// assert!(d.and_hms_micro_opt(12, 60, 56, 789_012).is_none());
- /// assert!(d.and_hms_micro_opt(24, 34, 56, 789_012).is_none());
- /// ~~~~
- #[inline]
- pub fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32,
- micro: u32) -> Option<NaiveDateTime> {
- NaiveTime::from_hms_micro_opt(hour, min, sec, micro).map(|time| self.and_time(time))
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
- ///
- /// The nanosecond part can exceed 1,000,000,000
- /// in order to represent the [leap second](../time/index.html#leap-second-handling).
- ///
- /// Panics on invalid hour, minute, second and/or nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- ///
- /// let dt: NaiveDateTime = d.and_hms_nano(12, 34, 56, 789_012_345);
- /// assert_eq!(dt.year(), 2015);
- /// assert_eq!(dt.weekday(), Weekday::Wed);
- /// assert_eq!(dt.second(), 56);
- /// assert_eq!(dt.nanosecond(), 789_012_345);
- /// ~~~~
- #[inline]
- pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> NaiveDateTime {
- self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time")
- }
-
- /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
- ///
- /// The nanosecond part can exceed 1,000,000,000
- /// in order to represent the [leap second](../time/index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid hour, minute, second and/or nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- /// assert!(d.and_hms_nano_opt(12, 34, 56, 789_012_345).is_some());
- /// assert!(d.and_hms_nano_opt(12, 34, 59, 1_789_012_345).is_some()); // leap second
- /// assert!(d.and_hms_nano_opt(12, 34, 59, 2_789_012_345).is_none());
- /// assert!(d.and_hms_nano_opt(12, 34, 60, 789_012_345).is_none());
- /// assert!(d.and_hms_nano_opt(12, 60, 56, 789_012_345).is_none());
- /// assert!(d.and_hms_nano_opt(24, 34, 56, 789_012_345).is_none());
- /// ~~~~
- #[inline]
- pub fn and_hms_nano_opt(&self, hour: u32, min: u32, sec: u32,
- nano: u32) -> Option<NaiveDateTime> {
- NaiveTime::from_hms_nano_opt(hour, min, sec, nano).map(|time| self.and_time(time))
- }
-
- /// Returns the packed month-day-flags.
- #[inline]
- fn mdf(&self) -> Mdf {
- self.of().to_mdf()
- }
-
- /// Returns the packed ordinal-flags.
- #[inline]
- fn of(&self) -> Of {
- Of((self.ymdf & 0b1111_11111_1111) as u32)
- }
-
- /// Makes a new `NaiveDate` with the packed month-day-flags changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- #[inline]
- fn with_mdf(&self, mdf: Mdf) -> Option<NaiveDate> {
- self.with_of(mdf.to_of())
- }
-
- /// Makes a new `NaiveDate` with the packed ordinal-flags changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- #[inline]
- fn with_of(&self, of: Of) -> Option<NaiveDate> {
- if of.valid() {
- let Of(of) = of;
- Some(NaiveDate { ymdf: (self.ymdf & !0b111111111_1111) | of as DateImpl })
- } else {
- None
- }
- }
-
- /// Makes a new `NaiveDate` for the next calendar date.
- ///
- /// Panics when `self` is the last representable date.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).succ(), NaiveDate::from_ymd(2015, 6, 4));
- /// assert_eq!(NaiveDate::from_ymd(2015, 6, 30).succ(), NaiveDate::from_ymd(2015, 7, 1));
- /// assert_eq!(NaiveDate::from_ymd(2015, 12, 31).succ(), NaiveDate::from_ymd(2016, 1, 1));
- /// ~~~~
- #[inline]
- pub fn succ(&self) -> NaiveDate {
- self.succ_opt().expect("out of bound")
- }
-
- /// Makes a new `NaiveDate` for the next calendar date.
- ///
- /// Returns `None` when `self` is the last representable date.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- /// use chrono::naive::date::MAX;
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).succ_opt(),
- /// Some(NaiveDate::from_ymd(2015, 6, 4)));
- /// assert_eq!(MAX.succ_opt(), None);
- /// ~~~~
- #[inline]
- pub fn succ_opt(&self) -> Option<NaiveDate> {
- self.with_of(self.of().succ()).or_else(|| NaiveDate::from_ymd_opt(self.year() + 1, 1, 1))
- }
-
- /// Makes a new `NaiveDate` for the previous calendar date.
- ///
- /// Panics when `self` is the first representable date.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred(), NaiveDate::from_ymd(2015, 6, 2));
- /// assert_eq!(NaiveDate::from_ymd(2015, 6, 1).pred(), NaiveDate::from_ymd(2015, 5, 31));
- /// assert_eq!(NaiveDate::from_ymd(2015, 1, 1).pred(), NaiveDate::from_ymd(2014, 12, 31));
- /// ~~~~
- #[inline]
- pub fn pred(&self) -> NaiveDate {
- self.pred_opt().expect("out of bound")
- }
-
- /// Makes a new `NaiveDate` for the previous calendar date.
- ///
- /// Returns `None` when `self` is the first representable date.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- /// use chrono::naive::date::MIN;
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred_opt(),
- /// Some(NaiveDate::from_ymd(2015, 6, 2)));
- /// assert_eq!(MIN.pred_opt(), None);
- /// ~~~~
- #[inline]
- pub fn pred_opt(&self) -> Option<NaiveDate> {
- self.with_of(self.of().pred()).or_else(|| NaiveDate::from_ymd_opt(self.year() - 1, 12, 31))
- }
-
- /// Adds the `days` part of given `Duration` to the current date.
- ///
- /// Returns `None` when it will result in overflow.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveDate;
- /// use chrono::naive::date::MAX;
- /// use time::Duration;
- ///
- /// let d = NaiveDate::from_ymd(2015, 9, 5);
- /// assert_eq!(d.checked_add_signed(Duration::days(40)),
- /// Some(NaiveDate::from_ymd(2015, 10, 15)));
- /// assert_eq!(d.checked_add_signed(Duration::days(-40)),
- /// 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!(MAX.checked_add_signed(Duration::days(1)), None);
- /// # }
- /// ~~~~
- pub fn checked_add_signed(self, rhs: OldDuration) -> Option<NaiveDate> {
- let year = self.year();
- let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400);
- let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());
- let cycle = try_opt!((cycle as i32).checked_add(try_opt!(rhs.num_days().to_i32())));
- let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146097);
- year_div_400 += cycle_div_400y;
-
- let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
- let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
- NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32,
- Of::new(ordinal, flags))
- }
-
- /// Subtracts the `days` part of given `Duration` from the current date.
- ///
- /// Returns `None` when it will result in overflow.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveDate;
- /// use chrono::naive::date::MIN;
- /// use time::Duration;
- ///
- /// let d = NaiveDate::from_ymd(2015, 9, 5);
- /// assert_eq!(d.checked_sub_signed(Duration::days(40)),
- /// Some(NaiveDate::from_ymd(2015, 7, 27)));
- /// assert_eq!(d.checked_sub_signed(Duration::days(-40)),
- /// 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!(MIN.checked_sub_signed(Duration::days(1)), None);
- /// # }
- /// ~~~~
- pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<NaiveDate> {
- let year = self.year();
- let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400);
- let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());
- let cycle = try_opt!((cycle as i32).checked_sub(try_opt!(rhs.num_days().to_i32())));
- let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146097);
- year_div_400 += cycle_div_400y;
-
- let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
- let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
- NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32,
- Of::new(ordinal, flags))
- }
-
- /// Subtracts another `NaiveDate` from the current date.
- /// Returns a `Duration` of integral numbers.
- ///
- /// This does not overflow or underflow at all,
- /// as all possible output fits in the range of `Duration`.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveDate;
- /// use time::Duration;
- ///
- /// let from_ymd = NaiveDate::from_ymd;
- /// let since = NaiveDate::signed_duration_since;
- ///
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), Duration::zero());
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)), Duration::days(1));
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)), Duration::days(-1));
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)), Duration::days(100));
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)), Duration::days(365));
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), Duration::days(365*4 + 1));
- /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), Duration::days(365*400 + 97));
- /// # }
- /// ~~~~
- pub fn signed_duration_since(self, rhs: NaiveDate) -> OldDuration {
- let year1 = self.year();
- let year2 = rhs.year();
- let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400);
- let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400);
- let cycle1 = internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()) as i64;
- let cycle2 = internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()) as i64;
- OldDuration::days((year1_div_400 as i64 - year2_div_400 as i64) * 146097 +
- (cycle1 - cycle2))
- }
-
- /// Formats the date with the specified formatting items.
- /// Otherwise it is same to the ordinary `format` method.
- ///
- /// The `Iterator` of items should be `Clone`able,
- /// since the resulting `DelayedFormat` value may be formatted multiple times.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- /// use chrono::format::strftime::StrftimeItems;
- ///
- /// let fmt = StrftimeItems::new("%Y-%m-%d");
- /// let d = NaiveDate::from_ymd(2015, 9, 5);
- /// assert_eq!(d.format_with_items(fmt.clone()).to_string(), "2015-09-05");
- /// assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
- /// ~~~~
- ///
- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # use chrono::format::strftime::StrftimeItems;
- /// # let fmt = StrftimeItems::new("%Y-%m-%d").clone();
- /// # let d = NaiveDate::from_ymd(2015, 9, 5);
- /// assert_eq!(format!("{}", d.format_with_items(fmt)), "2015-09-05");
- /// ~~~~
- #[inline]
- pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
- where I: Iterator<Item=Item<'a>> + Clone {
- DelayedFormat::new(Some(*self), None, items)
- }
-
- /// Formats the date with the specified format string.
- /// See the [`format::strftime` module](../../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// This returns a `DelayedFormat`,
- /// which gets converted to a string only when actual formatting happens.
- /// You may use the `to_string` method to get a `String`,
- /// or just feed it into `print!` and other formatting macros.
- /// (In this way it avoids the redundant memory allocation.)
- ///
- /// A wrong format string does *not* issue an error immediately.
- /// Rather, converting or formatting the `DelayedFormat` fails.
- /// You are recommended to immediately use `DelayedFormat` for this reason.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let d = NaiveDate::from_ymd(2015, 9, 5);
- /// assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
- /// assert_eq!(d.format("%A, %-d %B, %C%y").to_string(), "Saturday, 5 September, 2015");
- /// ~~~~
- ///
- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # let d = NaiveDate::from_ymd(2015, 9, 5);
- /// assert_eq!(format!("{}", d.format("%Y-%m-%d")), "2015-09-05");
- /// assert_eq!(format!("{}", d.format("%A, %-d %B, %C%y")), "Saturday, 5 September, 2015");
- /// ~~~~
- #[inline]
- pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
- self.format_with_items(StrftimeItems::new(fmt))
- }
-}
-
-impl Datelike for NaiveDate {
- /// Returns the year number in the [calendar date](./index.html#calendar-date).
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).year(), 2015);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).year(), -308); // 309 BCE
- /// ~~~~
- #[inline]
- fn year(&self) -> i32 {
- self.ymdf >> 13
- }
-
- /// Returns the month number starting from 1.
- ///
- /// The return value ranges from 1 to 12.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).month(), 9);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).month(), 3);
- /// ~~~~
- #[inline]
- fn month(&self) -> u32 {
- self.mdf().month()
- }
-
- /// Returns the month number starting from 0.
- ///
- /// The return value ranges from 0 to 11.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).month0(), 8);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).month0(), 2);
- /// ~~~~
- #[inline]
- fn month0(&self) -> u32 {
- self.mdf().month() - 1
- }
-
- /// Returns the day of month starting from 1.
- ///
- /// The return value ranges from 1 to 31. (The last day of month differs by months.)
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).day(), 8);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day(), 14);
- /// ~~~~
- ///
- /// Combined with [`NaiveDate::pred`](./struct.NaiveDate.html#method.pred),
- /// one can determine the number of days in a particular month.
- /// (Note that this panics when `year` is out of range.)
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// fn ndays_in_month(year: i32, month: u32) -> u32 {
- /// // the first day of the next month...
- /// let (y, m) = if month == 12 { (year + 1, 1) } else { (year, month + 1) };
- /// let d = NaiveDate::from_ymd(y, m, 1);
- ///
- /// // ...is preceded by the last day of the original month
- /// d.pred().day()
- /// }
- ///
- /// assert_eq!(ndays_in_month(2015, 8), 31);
- /// assert_eq!(ndays_in_month(2015, 9), 30);
- /// assert_eq!(ndays_in_month(2015, 12), 31);
- /// assert_eq!(ndays_in_month(2016, 2), 29);
- /// assert_eq!(ndays_in_month(2017, 2), 28);
- /// ~~~~
- #[inline]
- fn day(&self) -> u32 {
- self.mdf().day()
- }
-
- /// Returns the day of month starting from 0.
- ///
- /// The return value ranges from 0 to 30. (The last day of month differs by months.)
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).day0(), 7);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day0(), 13);
- /// ~~~~
- #[inline]
- fn day0(&self) -> u32 {
- self.mdf().day() - 1
- }
-
- /// Returns the day of year starting from 1.
- ///
- /// The return value ranges from 1 to 366. (The last day of year differs by years.)
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).ordinal(), 251);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal(), 74);
- /// ~~~~
- ///
- /// Combined with [`NaiveDate::pred`](./struct.NaiveDate.html#method.pred),
- /// one can determine the number of days in a particular year.
- /// (Note that this panics when `year` is out of range.)
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// fn ndays_in_year(year: i32) -> u32 {
- /// // the first day of the next year...
- /// let d = NaiveDate::from_ymd(year + 1, 1, 1);
- ///
- /// // ...is preceded by the last day of the original year
- /// d.pred().ordinal()
- /// }
- ///
- /// assert_eq!(ndays_in_year(2015), 365);
- /// assert_eq!(ndays_in_year(2016), 366);
- /// assert_eq!(ndays_in_year(2017), 365);
- /// assert_eq!(ndays_in_year(2000), 366);
- /// assert_eq!(ndays_in_year(2100), 365);
- /// ~~~~
- #[inline]
- fn ordinal(&self) -> u32 {
- self.of().ordinal()
- }
-
- /// Returns the day of year starting from 0.
- ///
- /// The return value ranges from 0 to 365. (The last day of year differs by years.)
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).ordinal0(), 250);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal0(), 73);
- /// ~~~~
- #[inline]
- fn ordinal0(&self) -> u32 {
- self.of().ordinal() - 1
- }
-
- /// Returns the day of week.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike, Weekday};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).weekday(), Weekday::Tue);
- /// assert_eq!(NaiveDate::from_ymd(-308, 3, 14).weekday(), Weekday::Fri);
- /// ~~~~
- #[inline]
- fn weekday(&self) -> Weekday {
- self.of().weekday()
- }
-
- fn isoweekdate(&self) -> (i32, u32, Weekday) {
- let of = self.of();
- let year = self.year();
- let (rawweek, weekday) = of.isoweekdate_raw();
- if rawweek < 1 { // previous year
- let prevlastweek = YearFlags::from_year(year - 1).nisoweeks();
- (year - 1, prevlastweek, weekday)
- } else {
- let lastweek = of.flags().nisoweeks();
- if rawweek > lastweek { // next year
- (year + 1, 1, weekday)
- } else {
- (year, rawweek, weekday)
- }
- }
- }
-
- /// Makes a new `NaiveDate` with the year number changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_year(2016),
- /// Some(NaiveDate::from_ymd(2016, 9, 8)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_year(-308),
- /// Some(NaiveDate::from_ymd(-308, 9, 8)));
- /// ~~~~
- ///
- /// A leap day (February 29) is a good example that this method can return `None`.
- ///
- /// ~~~~
- /// # use chrono::{NaiveDate, Datelike};
- /// assert!(NaiveDate::from_ymd(2016, 2, 29).with_year(2015).is_none());
- /// assert!(NaiveDate::from_ymd(2016, 2, 29).with_year(2020).is_some());
- /// ~~~~
- #[inline]
- fn with_year(&self, year: i32) -> Option<NaiveDate> {
- // we need to operate with `mdf` since we should keep the month and day number as is
- let mdf = self.mdf();
-
- // adjust the flags as needed
- let flags = YearFlags::from_year(year);
- let mdf = mdf.with_flags(flags);
-
- NaiveDate::from_mdf(year, mdf)
- }
-
- /// Makes a new `NaiveDate` with the month number (starting from 1) changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month(10),
- /// Some(NaiveDate::from_ymd(2015, 10, 8)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month(13), None); // no month 13
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 30).with_month(2), None); // no February 30
- /// ~~~~
- #[inline]
- fn with_month(&self, month: u32) -> Option<NaiveDate> {
- self.with_mdf(self.mdf().with_month(month))
- }
-
- /// Makes a new `NaiveDate` with the month number (starting from 0) changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month0(9),
- /// Some(NaiveDate::from_ymd(2015, 10, 8)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month0(12), None); // no month 13
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 30).with_month0(1), None); // no February 30
- /// ~~~~
- #[inline]
- fn with_month0(&self, month0: u32) -> Option<NaiveDate> {
- self.with_mdf(self.mdf().with_month(month0 + 1))
- }
-
- /// Makes a new `NaiveDate` with the day of month (starting from 1) changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day(30),
- /// Some(NaiveDate::from_ymd(2015, 9, 30)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day(31),
- /// None); // no September 31
- /// ~~~~
- #[inline]
- fn with_day(&self, day: u32) -> Option<NaiveDate> {
- self.with_mdf(self.mdf().with_day(day))
- }
-
- /// Makes a new `NaiveDate` with the day of month (starting from 0) changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day0(29),
- /// Some(NaiveDate::from_ymd(2015, 9, 30)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day0(30),
- /// None); // no September 31
- /// ~~~~
- #[inline]
- fn with_day0(&self, day0: u32) -> Option<NaiveDate> {
- self.with_mdf(self.mdf().with_day(day0 + 1))
- }
-
- /// Makes a new `NaiveDate` with the day of year (starting from 1) changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal(60),
- /// Some(NaiveDate::from_ymd(2015, 3, 1)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal(366),
- /// None); // 2015 had only 365 days
- ///
- /// assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal(60),
- /// Some(NaiveDate::from_ymd(2016, 2, 29)));
- /// assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal(366),
- /// Some(NaiveDate::from_ymd(2016, 12, 31)));
- /// ~~~~
- #[inline]
- fn with_ordinal(&self, ordinal: u32) -> Option<NaiveDate> {
- self.with_of(self.of().with_ordinal(ordinal))
- }
-
- /// Makes a new `NaiveDate` with the day of year (starting from 0) changed.
- ///
- /// Returns `None` when the resulting `NaiveDate` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, Datelike};
- ///
- /// assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal0(59),
- /// Some(NaiveDate::from_ymd(2015, 3, 1)));
- /// assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal0(365),
- /// None); // 2015 had only 365 days
- ///
- /// assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal0(59),
- /// Some(NaiveDate::from_ymd(2016, 2, 29)));
- /// assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal0(365),
- /// Some(NaiveDate::from_ymd(2016, 12, 31)));
- /// ~~~~
- #[inline]
- fn with_ordinal0(&self, ordinal0: u32) -> Option<NaiveDate> {
- self.with_of(self.of().with_ordinal(ordinal0 + 1))
- }
-}
-
-/// `NaiveDate` can be used as a key to the hash maps.
-impl hash::Hash for NaiveDate {
- fn hash<H: hash::Hasher>(&self, state: &mut H) {
- // don't need to strip flags, as we can safely assume that it is correct
- self.ymdf.hash(state);
- }
-}
-
-/// An addition of `Duration` to `NaiveDate` discards the fractional days,
-/// rounding to the closest integral number of days towards `Duration::zero()`.
-///
-/// Panics on underflow or overflow.
-/// Use [`NaiveDate::checked_add_signed`](#method.checked_add_signed) to detect that.
-///
-/// # Example
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// use chrono::NaiveDate;
-/// use time::Duration;
-///
-/// let from_ymd = NaiveDate::from_ymd;
-///
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::zero(), from_ymd(2014, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::seconds(86399), from_ymd(2014, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::seconds(-86399), from_ymd(2014, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::days(1), from_ymd(2014, 1, 2));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::days(-1), from_ymd(2013, 12, 31));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::days(364), from_ymd(2014, 12, 31));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::days(365*4 + 1), from_ymd(2018, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) + Duration::days(365*400 + 97), from_ymd(2414, 1, 1));
-/// # }
-/// ~~~~
-impl Add<OldDuration> for NaiveDate {
- type Output = NaiveDate;
-
- #[inline]
- fn add(self, rhs: OldDuration) -> NaiveDate {
- self.checked_add_signed(rhs).expect("`NaiveDate + Duration` overflowed")
- }
-}
-
-/// A subtraction of `Duration` from `NaiveDate` discards the fractional days,
-/// rounding to the closest integral number of days towards `Duration::zero()`.
-/// It is same to the addition with a negated `Duration`.
-///
-/// Panics on underflow or overflow.
-/// Use [`NaiveDate::checked_sub_signed`](#method.checked_sub_signed) to detect that.
-///
-/// # Example
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// use chrono::NaiveDate;
-/// use time::Duration;
-///
-/// let from_ymd = NaiveDate::from_ymd;
-///
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::zero(), from_ymd(2014, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::seconds(86399), from_ymd(2014, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::seconds(-86399), from_ymd(2014, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::days(1), from_ymd(2013, 12, 31));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::days(-1), from_ymd(2014, 1, 2));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::days(364), from_ymd(2013, 1, 2));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::days(365*4 + 1), from_ymd(2010, 1, 1));
-/// assert_eq!(from_ymd(2014, 1, 1) - Duration::days(365*400 + 97), from_ymd(1614, 1, 1));
-/// # }
-/// ~~~~
-impl Sub<OldDuration> for NaiveDate {
- type Output = NaiveDate;
-
- #[inline]
- fn sub(self, rhs: OldDuration) -> NaiveDate {
- self.checked_sub_signed(rhs).expect("`NaiveDate - Duration` overflowed")
- }
-}
-
-/// The `Debug` output of the naive date `d` is same to
-/// [`d.format("%Y-%m-%d")`](../../format/strftime/index.html).
-///
-/// The string printed can be readily parsed via the `parse` method on `str`.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveDate;
-///
-/// assert_eq!(format!("{:?}", NaiveDate::from_ymd(2015, 9, 5)), "2015-09-05");
-/// assert_eq!(format!("{:?}", NaiveDate::from_ymd( 0, 1, 1)), "0000-01-01");
-/// assert_eq!(format!("{:?}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31");
-/// ~~~~
-///
-/// ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
-///
-/// ~~~~
-/// # use chrono::NaiveDate;
-/// assert_eq!(format!("{:?}", NaiveDate::from_ymd( -1, 1, 1)), "-0001-01-01");
-/// assert_eq!(format!("{:?}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");
-/// ~~~~
-impl fmt::Debug for NaiveDate {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let year = self.year();
- let mdf = self.mdf();
- if 0 <= year && year <= 9999 {
- write!(f, "{:04}-{:02}-{:02}", year, mdf.month(), mdf.day())
- } else {
- // ISO 8601 requires the explicit sign for out-of-range years
- write!(f, "{:+05}-{:02}-{:02}", year, mdf.month(), mdf.day())
- }
- }
-}
-
-/// The `Display` output of the naive date `d` is same to
-/// [`d.format("%Y-%m-%d")`](../../format/strftime/index.html).
-///
-/// The string printed can be readily parsed via the `parse` method on `str`.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveDate;
-///
-/// assert_eq!(format!("{}", NaiveDate::from_ymd(2015, 9, 5)), "2015-09-05");
-/// assert_eq!(format!("{}", NaiveDate::from_ymd( 0, 1, 1)), "0000-01-01");
-/// assert_eq!(format!("{}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31");
-/// ~~~~
-///
-/// ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
-///
-/// ~~~~
-/// # use chrono::NaiveDate;
-/// assert_eq!(format!("{}", NaiveDate::from_ymd( -1, 1, 1)), "-0001-01-01");
-/// assert_eq!(format!("{}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");
-/// ~~~~
-impl fmt::Display for NaiveDate {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self, f) }
-}
-
-/// Parsing a `str` into a `NaiveDate` uses the same format,
-/// [`%Y-%m-%d`](../../format/strftime/index.html), as in `Debug` and `Display`.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveDate;
-///
-/// let d = NaiveDate::from_ymd(2015, 9, 18);
-/// assert_eq!("2015-09-18".parse::<NaiveDate>(), Ok(d));
-///
-/// let d = NaiveDate::from_ymd(12345, 6, 7);
-/// assert_eq!("+12345-6-7".parse::<NaiveDate>(), Ok(d));
-///
-/// assert!("foo".parse::<NaiveDate>().is_err());
-/// ~~~~
-impl str::FromStr for NaiveDate {
- type Err = ParseError;
-
- fn from_str(s: &str) -> ParseResult<NaiveDate> {
- const ITEMS: &'static [Item<'static>] = &[
- Item::Space(""), Item::Numeric(Numeric::Year, Pad::Zero),
- Item::Space(""), Item::Literal("-"),
- Item::Space(""), Item::Numeric(Numeric::Month, Pad::Zero),
- Item::Space(""), Item::Literal("-"),
- Item::Space(""), Item::Numeric(Numeric::Day, Pad::Zero),
- Item::Space(""),
- ];
-
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, ITEMS.iter().cloned()));
- parsed.to_naive_date()
- }
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_encodable_json<F, E>(to_string: F)
- where F: Fn(&NaiveDate) -> Result<String, E>, E: ::std::fmt::Debug
-{
- assert_eq!(to_string(&NaiveDate::from_ymd(2014, 7, 24)).ok(),
- Some(r#""2014-07-24""#.into()));
- assert_eq!(to_string(&NaiveDate::from_ymd(0, 1, 1)).ok(),
- Some(r#""0000-01-01""#.into()));
- assert_eq!(to_string(&NaiveDate::from_ymd(-1, 12, 31)).ok(),
- Some(r#""-0001-12-31""#.into()));
- assert_eq!(to_string(&MIN).ok(),
- Some(r#""-262144-01-01""#.into()));
- assert_eq!(to_string(&MAX).ok(),
- Some(r#""+262143-12-31""#.into()));
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_decodable_json<F, E>(from_str: F)
- where F: Fn(&str) -> Result<NaiveDate, E>, E: ::std::fmt::Debug
-{
- use std::{i32, i64};
-
- assert_eq!(from_str(r#""2016-07-08""#).ok(), Some(NaiveDate::from_ymd(2016, 7, 8)));
- assert_eq!(from_str(r#""2016-7-8""#).ok(), Some(NaiveDate::from_ymd(2016, 7, 8)));
- assert_eq!(from_str(r#""+002016-07-08""#).ok(), Some(NaiveDate::from_ymd(2016, 7, 8)));
- 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#""-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#""+262143-12-31""#).ok(), Some(MAX));
-
- // bad formats
- assert!(from_str(r#""""#).is_err());
- assert!(from_str(r#""20001231""#).is_err());
- assert!(from_str(r#""2000-00-00""#).is_err());
- assert!(from_str(r#""2000-02-30""#).is_err());
- assert!(from_str(r#""2001-02-29""#).is_err());
- assert!(from_str(r#""2002-002-28""#).is_err());
- assert!(from_str(r#""yyyy-mm-dd""#).is_err());
- assert!(from_str(r#"0"#).is_err());
- assert!(from_str(r#"20.01"#).is_err());
- assert!(from_str(&i32::MIN.to_string()).is_err());
- assert!(from_str(&i32::MAX.to_string()).is_err());
- assert!(from_str(&i64::MIN.to_string()).is_err());
- assert!(from_str(&i64::MAX.to_string()).is_err());
- assert!(from_str(r#"{}"#).is_err());
- // pre-0.3.0 rustc-serialize format is now invalid
- assert!(from_str(r#"{"ymdf":20}"#).is_err());
- assert!(from_str(r#"null"#).is_err());
-}
-
-#[cfg(feature = "rustc-serialize")]
-mod rustc_serialize {
- use super::NaiveDate;
- use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
-
- impl Encodable for NaiveDate {
- fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
- format!("{:?}", self).encode(s)
- }
- }
-
- impl Decodable for NaiveDate {
- fn decode<D: Decoder>(d: &mut D) -> Result<NaiveDate, D::Error> {
- d.read_str()?.parse().map_err(|_| d.error("invalid date"))
- }
- }
-
- #[cfg(test)] use rustc_serialize::json;
-
- #[test]
- fn test_encodable() {
- super::test_encodable_json(json::encode);
- }
-
- #[test]
- fn test_decodable() {
- super::test_decodable_json(json::decode);
- }
-}
-
-#[cfg(feature = "serde")]
-mod serde {
- use std::fmt;
- use super::NaiveDate;
- use serde::{ser, de};
-
- // TODO not very optimized for space (binary formats would want something better)
-
- impl ser::Serialize for NaiveDate {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where S: ser::Serializer
- {
- serializer.serialize_str(&format!("{:?}", self))
- }
- }
-
- struct NaiveDateVisitor;
-
- impl de::Visitor for NaiveDateVisitor {
- type Value = NaiveDate;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result
- {
- write!(formatter, "a formatted date string")
- }
-
- fn visit_str<E>(self, value: &str) -> Result<NaiveDate, E>
- where E: de::Error
- {
- value.parse().map_err(|err| E::custom(format!("{}", err)))
- }
- }
-
- impl de::Deserialize for NaiveDate {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where D: de::Deserializer
- {
- deserializer.deserialize_str(NaiveDateVisitor)
- }
- }
-
- #[cfg(test)] extern crate serde_json;
- #[cfg(test)] extern crate bincode;
-
- #[test]
- fn test_serde_serialize() {
- super::test_encodable_json(self::serde_json::to_string);
- }
-
- #[test]
- fn test_serde_deserialize() {
- super::test_decodable_json(self::serde_json::from_str);
- }
-
- #[test]
- fn test_serde_bincode() {
- // Bincode is relevant to test separately from JSON because
- // it is not self-describing.
- use self::bincode::SizeLimit;
- use self::bincode::serde::{serialize, deserialize};
-
- let d = NaiveDate::from_ymd(2014, 7, 24);
- let encoded = serialize(&d, SizeLimit::Infinite).unwrap();
- let decoded: NaiveDate = deserialize(&encoded).unwrap();
- assert_eq!(d, decoded);
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::NaiveDate;
- use super::{MIN, MIN_YEAR, MIN_DAYS_FROM_YEAR_0};
- use super::{MAX, MAX_YEAR, MAX_DAYS_FROM_YEAR_0};
- use {Datelike, Weekday};
- use std::{i32, u32};
- use oldtime::Duration;
-
- #[test]
- fn test_date_from_ymd() {
- let ymd_opt = |y,m,d| NaiveDate::from_ymd_opt(y, m, d);
-
- assert!(ymd_opt(2012, 0, 1).is_none());
- assert!(ymd_opt(2012, 1, 1).is_some());
- assert!(ymd_opt(2012, 2, 29).is_some());
- assert!(ymd_opt(2014, 2, 29).is_none());
- assert!(ymd_opt(2014, 3, 0).is_none());
- assert!(ymd_opt(2014, 3, 1).is_some());
- assert!(ymd_opt(2014, 3, 31).is_some());
- assert!(ymd_opt(2014, 3, 32).is_none());
- assert!(ymd_opt(2014, 12, 31).is_some());
- assert!(ymd_opt(2014, 13, 1).is_none());
- }
-
- #[test]
- fn test_date_from_yo() {
- let yo_opt = |y,o| NaiveDate::from_yo_opt(y, o);
- let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
-
- assert_eq!(yo_opt(2012, 0), None);
- assert_eq!(yo_opt(2012, 1), Some(ymd(2012, 1, 1)));
- assert_eq!(yo_opt(2012, 2), Some(ymd(2012, 1, 2)));
- assert_eq!(yo_opt(2012, 32), Some(ymd(2012, 2, 1)));
- assert_eq!(yo_opt(2012, 60), Some(ymd(2012, 2, 29)));
- assert_eq!(yo_opt(2012, 61), Some(ymd(2012, 3, 1)));
- assert_eq!(yo_opt(2012, 100), Some(ymd(2012, 4, 9)));
- assert_eq!(yo_opt(2012, 200), Some(ymd(2012, 7, 18)));
- assert_eq!(yo_opt(2012, 300), Some(ymd(2012, 10, 26)));
- assert_eq!(yo_opt(2012, 366), Some(ymd(2012, 12, 31)));
- assert_eq!(yo_opt(2012, 367), None);
-
- assert_eq!(yo_opt(2014, 0), None);
- assert_eq!(yo_opt(2014, 1), Some(ymd(2014, 1, 1)));
- assert_eq!(yo_opt(2014, 2), Some(ymd(2014, 1, 2)));
- assert_eq!(yo_opt(2014, 32), Some(ymd(2014, 2, 1)));
- assert_eq!(yo_opt(2014, 59), Some(ymd(2014, 2, 28)));
- assert_eq!(yo_opt(2014, 60), Some(ymd(2014, 3, 1)));
- assert_eq!(yo_opt(2014, 100), Some(ymd(2014, 4, 10)));
- assert_eq!(yo_opt(2014, 200), Some(ymd(2014, 7, 19)));
- assert_eq!(yo_opt(2014, 300), Some(ymd(2014, 10, 27)));
- assert_eq!(yo_opt(2014, 365), Some(ymd(2014, 12, 31)));
- assert_eq!(yo_opt(2014, 366), None);
- }
-
- #[test]
- fn test_date_from_isoywd() {
- let isoywd_opt = |y,w,d| NaiveDate::from_isoywd_opt(y, w, d);
- let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
-
- assert_eq!(isoywd_opt(2004, 0, Weekday::Sun), None);
- assert_eq!(isoywd_opt(2004, 1, Weekday::Mon), Some(ymd(2003, 12, 29)));
- assert_eq!(isoywd_opt(2004, 1, Weekday::Sun), Some(ymd(2004, 1, 4)));
- assert_eq!(isoywd_opt(2004, 2, Weekday::Mon), Some(ymd(2004, 1, 5)));
- assert_eq!(isoywd_opt(2004, 2, Weekday::Sun), Some(ymd(2004, 1, 11)));
- assert_eq!(isoywd_opt(2004, 52, Weekday::Mon), Some(ymd(2004, 12, 20)));
- assert_eq!(isoywd_opt(2004, 52, Weekday::Sun), Some(ymd(2004, 12, 26)));
- assert_eq!(isoywd_opt(2004, 53, Weekday::Mon), Some(ymd(2004, 12, 27)));
- assert_eq!(isoywd_opt(2004, 53, Weekday::Sun), Some(ymd(2005, 1, 2)));
- assert_eq!(isoywd_opt(2004, 54, Weekday::Mon), None);
-
- assert_eq!(isoywd_opt(2011, 0, Weekday::Sun), None);
- assert_eq!(isoywd_opt(2011, 1, Weekday::Mon), Some(ymd(2011, 1, 3)));
- assert_eq!(isoywd_opt(2011, 1, Weekday::Sun), Some(ymd(2011, 1, 9)));
- assert_eq!(isoywd_opt(2011, 2, Weekday::Mon), Some(ymd(2011, 1, 10)));
- assert_eq!(isoywd_opt(2011, 2, Weekday::Sun), Some(ymd(2011, 1, 16)));
-
- assert_eq!(isoywd_opt(2018, 51, Weekday::Mon), Some(ymd(2018, 12, 17)));
- assert_eq!(isoywd_opt(2018, 51, Weekday::Sun), Some(ymd(2018, 12, 23)));
- assert_eq!(isoywd_opt(2018, 52, Weekday::Mon), Some(ymd(2018, 12, 24)));
- assert_eq!(isoywd_opt(2018, 52, Weekday::Sun), Some(ymd(2018, 12, 30)));
- assert_eq!(isoywd_opt(2018, 53, Weekday::Mon), None);
- }
-
- #[test]
- fn test_date_from_isoywd_and_isoweekdate() {
- for year in 2000..2401 {
- for week in 1..54 {
- for &weekday in [Weekday::Mon, Weekday::Tue, Weekday::Wed, Weekday::Thu,
- Weekday::Fri, Weekday::Sat, Weekday::Sun].iter() {
- let d = NaiveDate::from_isoywd_opt(year, week, weekday);
- if d.is_some() {
- let d = d.unwrap();
- assert_eq!(d.weekday(), weekday);
- let (year_, week_, weekday_) = d.isoweekdate();
- assert_eq!(year_, year);
- assert_eq!(week_, week);
- assert_eq!(weekday_, weekday);
- }
- }
- }
- }
-
- for year in 2000..2401 {
- for month in 1..13 {
- for day in 1..32 {
- let d = NaiveDate::from_ymd_opt(year, month, day);
- if d.is_some() {
- let d = d.unwrap();
- let (year_, week_, weekday_) = d.isoweekdate();
- let d_ = NaiveDate::from_isoywd(year_, week_, weekday_);
- assert_eq!(d, d_);
- }
- }
- }
- }
- }
-
- #[test]
- fn test_date_from_num_days_from_ce() {
- let from_ndays_from_ce = |days| NaiveDate::from_num_days_from_ce_opt(days);
- assert_eq!(from_ndays_from_ce(1), Some(NaiveDate::from_ymd(1, 1, 1)));
- assert_eq!(from_ndays_from_ce(2), Some(NaiveDate::from_ymd(1, 1, 2)));
- assert_eq!(from_ndays_from_ce(31), Some(NaiveDate::from_ymd(1, 1, 31)));
- assert_eq!(from_ndays_from_ce(32), Some(NaiveDate::from_ymd(1, 2, 1)));
- assert_eq!(from_ndays_from_ce(59), Some(NaiveDate::from_ymd(1, 2, 28)));
- assert_eq!(from_ndays_from_ce(60), Some(NaiveDate::from_ymd(1, 3, 1)));
- assert_eq!(from_ndays_from_ce(365), Some(NaiveDate::from_ymd(1, 12, 31)));
- assert_eq!(from_ndays_from_ce(365*1 + 1), Some(NaiveDate::from_ymd(2, 1, 1)));
- assert_eq!(from_ndays_from_ce(365*2 + 1), Some(NaiveDate::from_ymd(3, 1, 1)));
- assert_eq!(from_ndays_from_ce(365*3 + 1), Some(NaiveDate::from_ymd(4, 1, 1)));
- assert_eq!(from_ndays_from_ce(365*4 + 2), Some(NaiveDate::from_ymd(5, 1, 1)));
- assert_eq!(from_ndays_from_ce(146097 + 1), Some(NaiveDate::from_ymd(401, 1, 1)));
- assert_eq!(from_ndays_from_ce(146097*5 + 1), Some(NaiveDate::from_ymd(2001, 1, 1)));
- assert_eq!(from_ndays_from_ce(719163), Some(NaiveDate::from_ymd(1970, 1, 1)));
- assert_eq!(from_ndays_from_ce(0), Some(NaiveDate::from_ymd(0, 12, 31))); // 1 BCE
- assert_eq!(from_ndays_from_ce(-365), Some(NaiveDate::from_ymd(0, 1, 1)));
- assert_eq!(from_ndays_from_ce(-366), Some(NaiveDate::from_ymd(-1, 12, 31))); // 2 BCE
-
- for days in (-9999..10001).map(|x| x * 100) {
- 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.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.num_days_from_ce() + 1), None);
- }
-
- #[test]
- fn test_date_fields() {
- fn check(year: i32, month: u32, day: u32, ordinal: u32) {
- let d1 = NaiveDate::from_ymd(year, month, day);
- assert_eq!(d1.year(), year);
- assert_eq!(d1.month(), month);
- assert_eq!(d1.day(), day);
- assert_eq!(d1.ordinal(), ordinal);
-
- let d2 = NaiveDate::from_yo(year, ordinal);
- assert_eq!(d2.year(), year);
- assert_eq!(d2.month(), month);
- assert_eq!(d2.day(), day);
- assert_eq!(d2.ordinal(), ordinal);
-
- assert_eq!(d1, d2);
- }
-
- check(2012, 1, 1, 1);
- check(2012, 1, 2, 2);
- check(2012, 2, 1, 32);
- check(2012, 2, 29, 60);
- check(2012, 3, 1, 61);
- check(2012, 4, 9, 100);
- check(2012, 7, 18, 200);
- check(2012, 10, 26, 300);
- check(2012, 12, 31, 366);
-
- check(2014, 1, 1, 1);
- check(2014, 1, 2, 2);
- check(2014, 2, 1, 32);
- check(2014, 2, 28, 59);
- check(2014, 3, 1, 60);
- check(2014, 4, 10, 100);
- check(2014, 7, 19, 200);
- check(2014, 10, 27, 300);
- check(2014, 12, 31, 365);
- }
-
- #[test]
- fn test_date_weekday() {
- assert_eq!(NaiveDate::from_ymd(1582, 10, 15).weekday(), Weekday::Fri);
- // May 20, 1875 = ISO 8601 reference date
- assert_eq!(NaiveDate::from_ymd(1875, 5, 20).weekday(), Weekday::Thu);
- assert_eq!(NaiveDate::from_ymd(2000, 1, 1).weekday(), Weekday::Sat);
- }
-
- #[test]
- fn test_date_with_fields() {
- let d = NaiveDate::from_ymd(2000, 2, 29);
- assert_eq!(d.with_year(-400), Some(NaiveDate::from_ymd(-400, 2, 29)));
- assert_eq!(d.with_year(-100), None);
- assert_eq!(d.with_year(1600), Some(NaiveDate::from_ymd(1600, 2, 29)));
- assert_eq!(d.with_year(1900), None);
- assert_eq!(d.with_year(2000), Some(NaiveDate::from_ymd(2000, 2, 29)));
- assert_eq!(d.with_year(2001), None);
- assert_eq!(d.with_year(2004), Some(NaiveDate::from_ymd(2004, 2, 29)));
- assert_eq!(d.with_year(i32::MAX), None);
-
- let d = NaiveDate::from_ymd(2000, 4, 30);
- assert_eq!(d.with_month(0), None);
- assert_eq!(d.with_month(1), Some(NaiveDate::from_ymd(2000, 1, 30)));
- assert_eq!(d.with_month(2), None);
- assert_eq!(d.with_month(3), Some(NaiveDate::from_ymd(2000, 3, 30)));
- assert_eq!(d.with_month(4), Some(NaiveDate::from_ymd(2000, 4, 30)));
- assert_eq!(d.with_month(12), Some(NaiveDate::from_ymd(2000, 12, 30)));
- assert_eq!(d.with_month(13), None);
- assert_eq!(d.with_month(u32::MAX), None);
-
- let d = NaiveDate::from_ymd(2000, 2, 8);
- assert_eq!(d.with_day(0), None);
- assert_eq!(d.with_day(1), Some(NaiveDate::from_ymd(2000, 2, 1)));
- assert_eq!(d.with_day(29), Some(NaiveDate::from_ymd(2000, 2, 29)));
- assert_eq!(d.with_day(30), None);
- assert_eq!(d.with_day(u32::MAX), None);
-
- let d = NaiveDate::from_ymd(2000, 5, 5);
- assert_eq!(d.with_ordinal(0), None);
- assert_eq!(d.with_ordinal(1), Some(NaiveDate::from_ymd(2000, 1, 1)));
- assert_eq!(d.with_ordinal(60), Some(NaiveDate::from_ymd(2000, 2, 29)));
- assert_eq!(d.with_ordinal(61), Some(NaiveDate::from_ymd(2000, 3, 1)));
- assert_eq!(d.with_ordinal(366), Some(NaiveDate::from_ymd(2000, 12, 31)));
- assert_eq!(d.with_ordinal(367), None);
- assert_eq!(d.with_ordinal(u32::MAX), None);
- }
-
- #[test]
- fn test_date_num_days_from_ce() {
- assert_eq!(NaiveDate::from_ymd(1, 1, 1).num_days_from_ce(), 1);
-
- for year in -9999..10001 {
- assert_eq!(NaiveDate::from_ymd(year, 1, 1).num_days_from_ce(),
- NaiveDate::from_ymd(year - 1, 12, 31).num_days_from_ce() + 1);
- }
- }
-
- #[test]
- fn test_date_succ() {
- let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
- assert_eq!(ymd(2014, 5, 6).succ_opt(), Some(ymd(2014, 5, 7)));
- 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(2016, 2, 28).succ_opt(), Some(ymd(2016, 2, 29)));
- assert_eq!(ymd(MAX.year(), 12, 31).succ_opt(), None);
- }
-
- #[test]
- fn test_date_pred() {
- let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
- assert_eq!(ymd(2016, 3, 1).pred_opt(), Some(ymd(2016, 2, 29)));
- 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, 5, 7).pred_opt(), Some(ymd(2014, 5, 6)));
- assert_eq!(ymd(MIN.year(), 1, 1).pred_opt(), None);
- }
-
- #[test]
- fn test_date_add() {
- fn check((y1,m1,d1): (i32, u32, u32), rhs: Duration, ymd: Option<(i32, u32, u32)>) {
- let lhs = NaiveDate::from_ymd(y1, m1, d1);
- let sum = ymd.map(|(y,m,d)| NaiveDate::from_ymd(y, m, d));
- assert_eq!(lhs.checked_add_signed(rhs), sum);
- assert_eq!(lhs.checked_sub_signed(-rhs), sum);
- }
-
- check((2014, 1, 1), Duration::zero(), Some((2014, 1, 1)));
- check((2014, 1, 1), Duration::seconds(86399), Some((2014, 1, 1)));
- // always round towards zero
- check((2014, 1, 1), Duration::seconds(-86399), Some((2014, 1, 1)));
- check((2014, 1, 1), Duration::days(1), Some((2014, 1, 2)));
- check((2014, 1, 1), Duration::days(-1), Some((2013, 12, 31)));
- check((2014, 1, 1), Duration::days(364), Some((2014, 12, 31)));
- check((2014, 1, 1), Duration::days(365*4 + 1), Some((2018, 1, 1)));
- check((2014, 1, 1), Duration::days(365*400 + 97), Some((2414, 1, 1)));
-
- check((-7, 1, 1), Duration::days(365*12 + 3), Some((5, 1, 1)));
-
- // overflow check
- check((0, 1, 1), Duration::days(MAX_DAYS_FROM_YEAR_0 as i64), Some((MAX_YEAR, 12, 31)));
- check((0, 1, 1), Duration::days(MAX_DAYS_FROM_YEAR_0 as i64 + 1), None);
- check((0, 1, 1), Duration::max_value(), None);
- check((0, 1, 1), Duration::days(MIN_DAYS_FROM_YEAR_0 as i64), Some((MIN_YEAR, 1, 1)));
- check((0, 1, 1), Duration::days(MIN_DAYS_FROM_YEAR_0 as i64 - 1), None);
- check((0, 1, 1), Duration::min_value(), None);
- }
-
- #[test]
- fn test_date_sub() {
- fn check((y1,m1,d1): (i32, u32, u32), (y2,m2,d2): (i32, u32, u32), diff: Duration) {
- let lhs = NaiveDate::from_ymd(y1, m1, d1);
- let rhs = NaiveDate::from_ymd(y2, m2, d2);
- assert_eq!(lhs.signed_duration_since(rhs), diff);
- assert_eq!(rhs.signed_duration_since(lhs), -diff);
- }
-
- check((2014, 1, 1), (2014, 1, 1), Duration::zero());
- check((2014, 1, 2), (2014, 1, 1), Duration::days(1));
- check((2014, 12, 31), (2014, 1, 1), Duration::days(364));
- check((2015, 1, 3), (2014, 1, 1), Duration::days(365 + 2));
- check((2018, 1, 1), (2014, 1, 1), Duration::days(365*4 + 1));
- check((2414, 1, 1), (2014, 1, 1), Duration::days(365*400 + 97));
-
- check((MAX_YEAR, 12, 31), (0, 1, 1), Duration::days(MAX_DAYS_FROM_YEAR_0 as i64));
- check((MIN_YEAR, 1, 1), (0, 1, 1), Duration::days(MIN_DAYS_FROM_YEAR_0 as i64));
- }
-
- #[test]
- fn test_date_fmt() {
- assert_eq!(format!("{:?}", NaiveDate::from_ymd(2012, 3, 4)), "2012-03-04");
- assert_eq!(format!("{:?}", NaiveDate::from_ymd(0, 3, 4)), "0000-03-04");
- assert_eq!(format!("{:?}", NaiveDate::from_ymd(-307, 3, 4)), "-0307-03-04");
- assert_eq!(format!("{:?}", NaiveDate::from_ymd(12345, 3, 4)), "+12345-03-04");
-
- assert_eq!(NaiveDate::from_ymd(2012, 3, 4).to_string(), "2012-03-04");
- assert_eq!(NaiveDate::from_ymd(0, 3, 4).to_string(), "0000-03-04");
- assert_eq!(NaiveDate::from_ymd(-307, 3, 4).to_string(), "-0307-03-04");
- assert_eq!(NaiveDate::from_ymd(12345, 3, 4).to_string(), "+12345-03-04");
-
- // the format specifier should have no effect on `NaiveTime`
- assert_eq!(format!("{:+30?}", NaiveDate::from_ymd(1234, 5, 6)), "1234-05-06");
- assert_eq!(format!("{:30?}", NaiveDate::from_ymd(12345, 6, 7)), "+12345-06-07");
- }
-
- #[test]
- fn test_date_from_str() {
- // valid cases
- let valid = [
- "-0000000123456-1-2",
- " -123456 - 1 - 2 ",
- "-12345-1-2",
- "-1234-12-31",
- "-7-6-5",
- "350-2-28",
- "360-02-29",
- "0360-02-29",
- "2015-2 -18",
- "+70-2-18",
- "+70000-2-18",
- "+00007-2-18",
- ];
- for &s in &valid {
- let d = match s.parse::<NaiveDate>() {
- Ok(d) => d,
- Err(e) => panic!("parsing `{}` has failed: {}", s, e)
- };
- let s_ = format!("{:?}", d);
- // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
- let d_ = match s_.parse::<NaiveDate>() {
- Ok(d) => d,
- Err(e) => panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}",
- s, d, e)
- };
- assert!(d == d_, "`{}` is parsed into `{:?}`, but reparsed result \
- `{:?}` does not match", s, d, d_);
- }
-
- // some invalid cases
- // since `ParseErrorKind` is private, all we can do is to check if there was an error
- assert!("".parse::<NaiveDate>().is_err());
- assert!("x".parse::<NaiveDate>().is_err());
- assert!("2014".parse::<NaiveDate>().is_err());
- assert!("2014-01".parse::<NaiveDate>().is_err());
- assert!("2014-01-00".parse::<NaiveDate>().is_err());
- assert!("2014-13-57".parse::<NaiveDate>().is_err());
- assert!("9999999-9-9".parse::<NaiveDate>().is_err()); // out-of-bounds
- }
-
- #[test]
- fn test_date_parse_from_str() {
- let ymd = |y,m,d| NaiveDate::from_ymd(y,m,d);
- assert_eq!(NaiveDate::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- Ok(ymd(2014, 5, 7))); // ignore time and offset
- assert_eq!(NaiveDate::parse_from_str("2015-W06-1=2015-033", "%G-W%V-%u = %Y-%j"),
- Ok(ymd(2015, 2, 2)));
- assert_eq!(NaiveDate::parse_from_str("Fri, 09 Aug 13", "%a, %d %b %y"),
- Ok(ymd(2013, 8, 9)));
- assert!(NaiveDate::parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err());
- assert!(NaiveDate::parse_from_str("2014-57", "%Y-%m-%d").is_err());
- assert!(NaiveDate::parse_from_str("2014", "%Y").is_err()); // insufficient
- }
-
- #[test]
- fn test_date_format() {
- let d = NaiveDate::from_ymd(2012, 3, 4);
- assert_eq!(d.format("%Y,%C,%y,%G,%g").to_string(), "2012,20,12,2012,12");
- assert_eq!(d.format("%m,%b,%h,%B").to_string(), "03,Mar,Mar,March");
- assert_eq!(d.format("%d,%e").to_string(), "04, 4");
- assert_eq!(d.format("%U,%W,%V").to_string(), "10,09,09");
- assert_eq!(d.format("%a,%A,%w,%u").to_string(), "Sun,Sunday,0,7");
- assert_eq!(d.format("%j").to_string(), "064"); // since 2012 is a leap year
- assert_eq!(d.format("%D,%x").to_string(), "03/04/12,03/04/12");
- assert_eq!(d.format("%F").to_string(), "2012-03-04");
- assert_eq!(d.format("%v").to_string(), " 4-Mar-2012");
- assert_eq!(d.format("%t%n%%%n%t").to_string(), "\t\n%\n\t");
-
- // non-four-digit years
- assert_eq!(NaiveDate::from_ymd(12345, 1, 1).format("%Y").to_string(), "+12345");
- assert_eq!(NaiveDate::from_ymd(1234, 1, 1).format("%Y").to_string(), "1234");
- assert_eq!(NaiveDate::from_ymd(123, 1, 1).format("%Y").to_string(), "0123");
- assert_eq!(NaiveDate::from_ymd(12, 1, 1).format("%Y").to_string(), "0012");
- assert_eq!(NaiveDate::from_ymd(1, 1, 1).format("%Y").to_string(), "0001");
- assert_eq!(NaiveDate::from_ymd(0, 1, 1).format("%Y").to_string(), "0000");
- assert_eq!(NaiveDate::from_ymd(-1, 1, 1).format("%Y").to_string(), "-0001");
- assert_eq!(NaiveDate::from_ymd(-12, 1, 1).format("%Y").to_string(), "-0012");
- assert_eq!(NaiveDate::from_ymd(-123, 1, 1).format("%Y").to_string(), "-0123");
- assert_eq!(NaiveDate::from_ymd(-1234, 1, 1).format("%Y").to_string(), "-1234");
- assert_eq!(NaiveDate::from_ymd(-12345, 1, 1).format("%Y").to_string(), "-12345");
-
- // corner cases
- assert_eq!(NaiveDate::from_ymd(2007, 12, 31).format("%G,%g,%U,%W,%V").to_string(),
- "2008,08,53,53,01");
- assert_eq!(NaiveDate::from_ymd(2010, 1, 3).format("%G,%g,%U,%W,%V").to_string(),
- "2009,09,01,00,53");
- }
-}
-
-/// The internal implementation of the calendar and ordinal date.
-///
-/// The current implementation is optimized for determining year, month, day and day of week.
-/// 4-bit `YearFlags` map to one of 14 possible classes of year in the Gregorian calendar,
-/// which are included in every packed `NaiveDate` instance.
-/// The conversion between the packed calendar date (`Mdf`) and the ordinal date (`Of`) is
-/// based on the moderately-sized lookup table (~1.5KB)
-/// and the packed representation is chosen for the efficient lookup.
-/// Every internal data structure does not validate its input,
-/// but the conversion keeps the valid value valid and the invalid value invalid
-/// so that the user-facing `NaiveDate` can validate the input as late as possible.
-#[allow(dead_code)] // some internal methods have been left for consistency
-mod internals {
- use std::{i32, fmt};
- use num::traits::FromPrimitive;
- use Weekday;
- use div::{div_rem, mod_floor};
-
- /// The internal date representation. This also includes the packed `Mdf` value.
- pub type DateImpl = i32;
-
- pub const MAX_YEAR: DateImpl = i32::MAX >> 13;
- pub const MIN_YEAR: DateImpl = i32::MIN >> 13;
-
- /// The year flags (aka the dominical letter).
- ///
- /// There are 14 possible classes of year in the Gregorian calendar:
- /// common and leap years starting with Monday through Sunday.
- /// The `YearFlags` stores this information into 4 bits `abbb`,
- /// where `a` is `1` for the common year (simplifies the `Of` validation)
- /// and `bbb` is a non-zero `Weekday` (mapping `Mon` to 7) of the last day in the past year
- /// (simplifies the day of week calculation from the 1-based ordinal).
- #[derive(PartialEq, Eq, Copy, Clone)]
- pub struct YearFlags(pub u8);
-
- pub const A: YearFlags = YearFlags(0o15); pub const AG: YearFlags = YearFlags(0o05);
- pub const B: YearFlags = YearFlags(0o14); pub const BA: YearFlags = YearFlags(0o04);
- pub const C: YearFlags = YearFlags(0o13); pub const CB: YearFlags = YearFlags(0o03);
- pub const D: YearFlags = YearFlags(0o12); pub const DC: YearFlags = YearFlags(0o02);
- pub const E: YearFlags = YearFlags(0o11); pub const ED: YearFlags = YearFlags(0o01);
- pub const F: YearFlags = YearFlags(0o17); pub const FE: YearFlags = YearFlags(0o07);
- pub const G: YearFlags = YearFlags(0o16); pub const GF: YearFlags = YearFlags(0o06);
-
- static YEAR_TO_FLAGS: [YearFlags; 400] = [
- BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F,
- ED, C, B, A, GF, E, D, C, BA, G, F, E, DC, B, A, G, FE, D, C, B,
- AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G, F, E,
- DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A,
- GF, E, D, C, BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, // 100
- C, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A,
- GF, E, D, C, BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D,
- CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G, F, E, DC, B, A, G,
- FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C,
- BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, // 200
- E, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C,
- BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F,
- ED, C, B, A, GF, E, D, C, BA, G, F, E, DC, B, A, G, FE, D, C, B,
- AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G, F, E,
- DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, // 300
- G, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G, F, E,
- DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A,
- GF, E, D, C, BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D,
- CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G, F, E, DC, B, A, G,
- FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, // 400
- ];
-
- static YEAR_DELTAS: [u8; 401] = [
- 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
- 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,
- 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15,
- 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20,
- 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, // 100
- 25, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29,
- 29, 30, 30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34,
- 34, 35, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39,
- 39, 40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44,
- 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49, // 200
- 49, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52, 52, 53, 53, 53,
- 53, 54, 54, 54, 54, 55, 55, 55, 55, 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58,
- 58, 59, 59, 59, 59, 60, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63,
- 63, 64, 64, 64, 64, 65, 65, 65, 65, 66, 66, 66, 66, 67, 67, 67, 67, 68, 68, 68,
- 68, 69, 69, 69, 69, 70, 70, 70, 70, 71, 71, 71, 71, 72, 72, 72, 72, 73, 73, 73, // 300
- 73, 73, 73, 73, 73, 74, 74, 74, 74, 75, 75, 75, 75, 76, 76, 76, 76, 77, 77, 77,
- 77, 78, 78, 78, 78, 79, 79, 79, 79, 80, 80, 80, 80, 81, 81, 81, 81, 82, 82, 82,
- 82, 83, 83, 83, 83, 84, 84, 84, 84, 85, 85, 85, 85, 86, 86, 86, 86, 87, 87, 87,
- 87, 88, 88, 88, 88, 89, 89, 89, 89, 90, 90, 90, 90, 91, 91, 91, 91, 92, 92, 92,
- 92, 93, 93, 93, 93, 94, 94, 94, 94, 95, 95, 95, 95, 96, 96, 96, 96, 97, 97, 97, 97 // 400+1
- ];
-
- pub fn cycle_to_yo(cycle: u32) -> (u32, u32) {
- let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365);
- let delta = YEAR_DELTAS[year_mod_400 as usize] as u32;
- if ordinal0 < delta {
- year_mod_400 -= 1;
- ordinal0 += 365 - YEAR_DELTAS[year_mod_400 as usize] as u32;
- } else {
- ordinal0 -= delta;
- }
- (year_mod_400, ordinal0 + 1)
- }
-
- pub fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 {
- year_mod_400 * 365 + YEAR_DELTAS[year_mod_400 as usize] as u32 + ordinal - 1
- }
-
- impl YearFlags {
- #[inline]
- pub fn from_year(year: i32) -> YearFlags {
- let year = mod_floor(year, 400);
- YearFlags::from_year_mod_400(year)
- }
-
- #[inline]
- pub fn from_year_mod_400(year: i32) -> YearFlags {
- YEAR_TO_FLAGS[year as usize]
- }
-
- #[inline]
- pub fn ndays(&self) -> u32 {
- let YearFlags(flags) = *self;
- 366 - (flags >> 3) as u32
- }
-
- #[inline]
- pub fn isoweek_delta(&self) -> u32 {
- let YearFlags(flags) = *self;
- let mut delta = flags as u32 & 0b111;
- if delta < 3 { delta += 7; }
- delta
- }
-
- #[inline]
- pub fn nisoweeks(&self) -> u32 {
- let YearFlags(flags) = *self;
- 52 + ((0b00000100_00000110 >> flags as usize) & 1)
- }
- }
-
- impl fmt::Debug for YearFlags {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let YearFlags(flags) = *self;
- match flags {
- 0o15 => "A".fmt(f), 0o05 => "AG".fmt(f),
- 0o14 => "B".fmt(f), 0o04 => "BA".fmt(f),
- 0o13 => "C".fmt(f), 0o03 => "CB".fmt(f),
- 0o12 => "D".fmt(f), 0o02 => "DC".fmt(f),
- 0o11 => "E".fmt(f), 0o01 => "ED".fmt(f),
- 0o10 => "F?".fmt(f), 0o00 => "FE?".fmt(f), // non-canonical
- 0o17 => "F".fmt(f), 0o07 => "FE".fmt(f),
- 0o16 => "G".fmt(f), 0o06 => "GF".fmt(f),
- _ => write!(f, "YearFlags({})", flags),
- }
- }
- }
-
- pub const MIN_OL: u32 = 1 << 1;
- pub const MAX_OL: u32 = 366 << 1; // larger than the non-leap last day `(365 << 1) | 1`
- pub const MIN_MDL: u32 = (1 << 6) | (1 << 1);
- pub const MAX_MDL: u32 = (12 << 6) | (31 << 1) | 1;
-
- const XX: i8 = -128;
- static MDL_TO_OL: [i8; (MAX_MDL as usize + 1)] = [
- XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
- XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
- XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
- XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, // 0
- XX, XX, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 1
- XX, XX, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
- 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
- 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
- 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, XX, XX, XX, XX, XX, // 2
- XX, XX, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74,
- 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74,
- 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74,
- 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, // 3
- XX, XX, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76,
- 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76,
- 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76,
- 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, XX, XX, // 4
- XX, XX, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80,
- 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80,
- 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80,
- 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, // 5
- XX, XX, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82,
- 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82,
- 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82,
- 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, XX, XX, // 6
- XX, XX, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86,
- 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86,
- 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86,
- 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, // 7
- XX, XX, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88,
- 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88,
- 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88,
- 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, // 8
- XX, XX, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90,
- 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90,
- 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90,
- 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, XX, XX, // 9
- XX, XX, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94,
- 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94,
- 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94,
- 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, // 10
- XX, XX, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96,
- 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96,
- 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96,
- 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, XX, XX, // 11
- XX, XX, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100,
- 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100,
- 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100,
- 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, // 12
- ];
-
- static OL_TO_MDL: [u8; (MAX_OL as usize + 1)] = [
- 0, 0, // 0
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 1
- 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
- 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
- 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
- 66, 66, 66, 66, 66, 66, 66, 66, 66, // 2
- 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74,
- 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74,
- 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74,
- 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, 74, 72, // 3
- 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76,
- 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76,
- 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76,
- 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, 76, 74, // 4
- 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80,
- 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80,
- 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80,
- 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, 80, 78, // 5
- 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82,
- 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82,
- 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82,
- 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, 82, 80, // 6
- 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86,
- 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86,
- 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86,
- 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, 86, 84, // 7
- 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88,
- 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88,
- 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88,
- 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, 88, 86, // 8
- 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90,
- 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90,
- 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90,
- 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, 90, 88, // 9
- 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94,
- 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94,
- 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94,
- 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, 94, 92, // 10
- 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96,
- 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96,
- 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96,
- 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, 96, 94, // 11
- 100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100,
- 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100,
- 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100,
- 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98,100, 98, // 12
- ];
-
- /// Ordinal (day of year) and year flags: `(ordinal << 4) | flags`.
- ///
- /// The whole bits except for the least 3 bits are referred as `Ol` (ordinal and leap flag),
- /// which is an index to the `OL_TO_MDL` lookup table.
- #[derive(PartialEq, PartialOrd, Copy, Clone)]
- pub struct Of(pub u32);
-
- impl Of {
- #[inline]
- fn clamp_ordinal(ordinal: u32) -> u32 {
- if ordinal > 366 {0} else {ordinal}
- }
-
- #[inline]
- pub fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Of {
- let ordinal = Of::clamp_ordinal(ordinal);
- Of((ordinal << 4) | (flags as u32))
- }
-
- #[inline]
- pub fn from_mdf(Mdf(mdf): Mdf) -> Of {
- let mdl = mdf >> 3;
- match MDL_TO_OL.get(mdl as usize) {
- Some(&v) => Of(mdf.wrapping_sub((v as i32 as u32 & 0x3ff) << 3)),
- None => Of(0)
- }
- }
-
- #[inline]
- pub fn valid(&self) -> bool {
- let Of(of) = *self;
- let ol = of >> 3;
- MIN_OL <= ol && ol <= MAX_OL
- }
-
- #[inline]
- pub fn ordinal(&self) -> u32 {
- let Of(of) = *self;
- of >> 4
- }
-
- #[inline]
- pub fn with_ordinal(&self, ordinal: u32) -> Of {
- let ordinal = Of::clamp_ordinal(ordinal);
- let Of(of) = *self;
- Of((of & 0b1111) | (ordinal << 4))
- }
-
- #[inline]
- pub fn flags(&self) -> YearFlags {
- let Of(of) = *self;
- YearFlags((of & 0b1111) as u8)
- }
-
- #[inline]
- pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Of {
- let Of(of) = *self;
- Of((of & !0b1111) | (flags as u32))
- }
-
- #[inline]
- pub fn weekday(&self) -> Weekday {
- let Of(of) = *self;
- Weekday::from_u32(((of >> 4) + (of & 0b111)) % 7).unwrap()
- }
-
- #[inline]
- pub fn isoweekdate_raw(&self) -> (u32, Weekday) {
- // week ordinal = ordinal + delta
- let Of(of) = *self;
- let weekord = (of >> 4).wrapping_add(self.flags().isoweek_delta());
- (weekord / 7, Weekday::from_u32(weekord % 7).unwrap())
- }
-
- #[inline]
- pub fn to_mdf(&self) -> Mdf {
- Mdf::from_of(*self)
- }
-
- #[inline]
- pub fn succ(&self) -> Of {
- let Of(of) = *self;
- Of(of + (1 << 4))
- }
-
- #[inline]
- pub fn pred(&self) -> Of {
- let Of(of) = *self;
- Of(of - (1 << 4))
- }
- }
-
- impl fmt::Debug for Of {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let Of(of) = *self;
- write!(f, "Of(({} << 4) | {:#04o} /*{:?}*/)",
- of >> 4, of & 0b1111, YearFlags((of & 0b1111) as u8))
- }
- }
-
- /// Month, day of month and year flags: `(month << 9) | (day << 4) | flags`
- ///
- /// The whole bits except for the least 3 bits are referred as `Mdl`
- /// (month, day of month and leap flag),
- /// which is an index to the `MDL_TO_OL` lookup table.
- #[derive(PartialEq, PartialOrd, Copy, Clone)]
- pub struct Mdf(pub u32);
-
- impl Mdf {
- #[inline]
- fn clamp_month(month: u32) -> u32 {
- if month > 12 {0} else {month}
- }
-
- #[inline]
- fn clamp_day(day: u32) -> u32 {
- if day > 31 {0} else {day}
- }
-
- #[inline]
- pub fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Mdf {
- let month = Mdf::clamp_month(month);
- let day = Mdf::clamp_day(day);
- Mdf((month << 9) | (day << 4) | (flags as u32))
- }
-
- #[inline]
- pub fn from_of(Of(of): Of) -> Mdf {
- let ol = of >> 3;
- match OL_TO_MDL.get(ol as usize) {
- Some(&v) => Mdf(of + ((v as u32) << 3)),
- None => Mdf(0)
- }
- }
-
- #[inline]
- pub fn valid(&self) -> bool {
- let Mdf(mdf) = *self;
- let mdl = mdf >> 3;
- match MDL_TO_OL.get(mdl as usize) {
- Some(&v) => v >= 0,
- None => false
- }
- }
-
- #[inline]
- pub fn month(&self) -> u32 {
- let Mdf(mdf) = *self;
- mdf >> 9
- }
-
- #[inline]
- pub fn with_month(&self, month: u32) -> Mdf {
- let month = Mdf::clamp_month(month);
- let Mdf(mdf) = *self;
- Mdf((mdf & 0b11111_1111) | (month << 9))
- }
-
- #[inline]
- pub fn day(&self) -> u32 {
- let Mdf(mdf) = *self;
- (mdf >> 4) & 0b11111
- }
-
- #[inline]
- pub fn with_day(&self, day: u32) -> Mdf {
- let day = Mdf::clamp_day(day);
- let Mdf(mdf) = *self;
- Mdf((mdf & !0b11111_0000) | (day << 4))
- }
-
- #[inline]
- pub fn flags(&self) -> YearFlags {
- let Mdf(mdf) = *self;
- YearFlags((mdf & 0b1111) as u8)
- }
-
- #[inline]
- pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
- let Mdf(mdf) = *self;
- Mdf((mdf & !0b1111) | (flags as u32))
- }
-
- #[inline]
- pub fn to_of(&self) -> Of {
- Of::from_mdf(*self)
- }
- }
-
- impl fmt::Debug for Mdf {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let Mdf(mdf) = *self;
- write!(f, "Mdf(({} << 9) | ({} << 4) | {:#04o} /*{:?}*/)",
- mdf >> 9, (mdf >> 4) & 0b11111, mdf & 0b1111, YearFlags((mdf & 0b1111) as u8))
- }
- }
-
- #[cfg(test)]
- mod tests {
- #[cfg(bench)] extern crate test;
-
- use Weekday;
- use super::{Of, Mdf};
- use super::{YearFlags, A, B, C, D, E, F, G, AG, BA, CB, DC, ED, FE, GF};
- use num::iter::range_inclusive;
- use std::u32;
-
- const NONLEAP_FLAGS: [YearFlags; 7] = [A, B, C, D, E, F, G];
- const LEAP_FLAGS: [YearFlags; 7] = [AG, BA, CB, DC, ED, FE, GF];
- const FLAGS: [YearFlags; 14] = [A, B, C, D, E, F, G, AG, BA, CB, DC, ED, FE, GF];
-
- #[test]
- fn test_year_flags_ndays_from_year() {
- assert_eq!(YearFlags::from_year(2014).ndays(), 365);
- assert_eq!(YearFlags::from_year(2012).ndays(), 366);
- assert_eq!(YearFlags::from_year(2000).ndays(), 366);
- assert_eq!(YearFlags::from_year(1900).ndays(), 365);
- assert_eq!(YearFlags::from_year(1600).ndays(), 366);
- assert_eq!(YearFlags::from_year( 1).ndays(), 365);
- assert_eq!(YearFlags::from_year( 0).ndays(), 366); // 1 BCE (proleptic Gregorian)
- assert_eq!(YearFlags::from_year( -1).ndays(), 365); // 2 BCE
- assert_eq!(YearFlags::from_year( -4).ndays(), 366); // 5 BCE
- assert_eq!(YearFlags::from_year( -99).ndays(), 365); // 100 BCE
- assert_eq!(YearFlags::from_year(-100).ndays(), 365); // 101 BCE
- assert_eq!(YearFlags::from_year(-399).ndays(), 365); // 400 BCE
- assert_eq!(YearFlags::from_year(-400).ndays(), 366); // 401 BCE
- }
-
- #[test]
- fn test_year_flags_nisoweeks() {
- assert_eq!(A.nisoweeks(), 52);
- assert_eq!(B.nisoweeks(), 52);
- assert_eq!(C.nisoweeks(), 52);
- assert_eq!(D.nisoweeks(), 53);
- assert_eq!(E.nisoweeks(), 52);
- assert_eq!(F.nisoweeks(), 52);
- assert_eq!(G.nisoweeks(), 52);
- assert_eq!(AG.nisoweeks(), 52);
- assert_eq!(BA.nisoweeks(), 52);
- assert_eq!(CB.nisoweeks(), 52);
- assert_eq!(DC.nisoweeks(), 53);
- assert_eq!(ED.nisoweeks(), 53);
- assert_eq!(FE.nisoweeks(), 52);
- assert_eq!(GF.nisoweeks(), 52);
- }
-
- #[cfg(bench)]
- #[bench]
- fn bench_year_flags_from_year(bh: &mut test::Bencher) {
- bh.iter(|| {
- for year in -999i32..1000 {
- YearFlags::from_year(year);
- }
- });
- }
-
- #[test]
- fn test_of() {
- fn check(expected: bool, flags: YearFlags, ordinal1: u32, ordinal2: u32) {
- for ordinal in range_inclusive(ordinal1, ordinal2) {
- let of = Of::new(ordinal, flags);
- assert!(of.valid() == expected,
- "ordinal {} = {:?} should be {} for dominical year {:?}",
- ordinal, of, if expected {"valid"} else {"invalid"}, flags);
- }
- }
-
- for &flags in NONLEAP_FLAGS.iter() {
- check(false, flags, 0, 0);
- check(true, flags, 1, 365);
- check(false, flags, 366, 1024);
- check(false, flags, u32::MAX, u32::MAX);
- }
-
- for &flags in LEAP_FLAGS.iter() {
- check(false, flags, 0, 0);
- check(true, flags, 1, 366);
- check(false, flags, 367, 1024);
- check(false, flags, u32::MAX, u32::MAX);
- }
- }
-
- #[test]
- fn test_mdf_valid() {
- fn check(expected: bool, flags: YearFlags, month1: u32, day1: u32,
- month2: u32, day2: u32) {
- for month in range_inclusive(month1, month2) {
- for day in range_inclusive(day1, day2) {
- let mdf = Mdf::new(month, day, flags);
- assert!(mdf.valid() == expected,
- "month {} day {} = {:?} should be {} for dominical year {:?}",
- month, day, mdf, if expected {"valid"} else {"invalid"}, flags);
- }
- }
- }
-
- for &flags in NONLEAP_FLAGS.iter() {
- check(false, flags, 0, 0, 0, 1024);
- check(false, flags, 0, 0, 16, 0);
- check(true, flags, 1, 1, 1, 31); check(false, flags, 1, 32, 1, 1024);
- check(true, flags, 2, 1, 2, 28); check(false, flags, 2, 29, 2, 1024);
- check(true, flags, 3, 1, 3, 31); check(false, flags, 3, 32, 3, 1024);
- check(true, flags, 4, 1, 4, 30); check(false, flags, 4, 31, 4, 1024);
- check(true, flags, 5, 1, 5, 31); check(false, flags, 5, 32, 5, 1024);
- check(true, flags, 6, 1, 6, 30); check(false, flags, 6, 31, 6, 1024);
- check(true, flags, 7, 1, 7, 31); check(false, flags, 7, 32, 7, 1024);
- check(true, flags, 8, 1, 8, 31); check(false, flags, 8, 32, 8, 1024);
- check(true, flags, 9, 1, 9, 30); check(false, flags, 9, 31, 9, 1024);
- check(true, flags, 10, 1, 10, 31); check(false, flags, 10, 32, 10, 1024);
- check(true, flags, 11, 1, 11, 30); check(false, flags, 11, 31, 11, 1024);
- check(true, flags, 12, 1, 12, 31); check(false, flags, 12, 32, 12, 1024);
- check(false, flags, 13, 0, 16, 1024);
- check(false, flags, u32::MAX, 0, u32::MAX, 1024);
- check(false, flags, 0, u32::MAX, 16, u32::MAX);
- check(false, flags, u32::MAX, u32::MAX, u32::MAX, u32::MAX);
- }
-
- for &flags in LEAP_FLAGS.iter() {
- check(false, flags, 0, 0, 0, 1024);
- check(false, flags, 0, 0, 16, 0);
- check(true, flags, 1, 1, 1, 31); check(false, flags, 1, 32, 1, 1024);
- check(true, flags, 2, 1, 2, 29); check(false, flags, 2, 30, 2, 1024);
- check(true, flags, 3, 1, 3, 31); check(false, flags, 3, 32, 3, 1024);
- check(true, flags, 4, 1, 4, 30); check(false, flags, 4, 31, 4, 1024);
- check(true, flags, 5, 1, 5, 31); check(false, flags, 5, 32, 5, 1024);
- check(true, flags, 6, 1, 6, 30); check(false, flags, 6, 31, 6, 1024);
- check(true, flags, 7, 1, 7, 31); check(false, flags, 7, 32, 7, 1024);
- check(true, flags, 8, 1, 8, 31); check(false, flags, 8, 32, 8, 1024);
- check(true, flags, 9, 1, 9, 30); check(false, flags, 9, 31, 9, 1024);
- check(true, flags, 10, 1, 10, 31); check(false, flags, 10, 32, 10, 1024);
- check(true, flags, 11, 1, 11, 30); check(false, flags, 11, 31, 11, 1024);
- check(true, flags, 12, 1, 12, 31); check(false, flags, 12, 32, 12, 1024);
- check(false, flags, 13, 0, 16, 1024);
- check(false, flags, u32::MAX, 0, u32::MAX, 1024);
- check(false, flags, 0, u32::MAX, 16, u32::MAX);
- check(false, flags, u32::MAX, u32::MAX, u32::MAX, u32::MAX);
- }
- }
-
- #[test]
- fn test_of_fields() {
- for &flags in FLAGS.iter() {
- for ordinal in range_inclusive(1u32, 366) {
- let of = Of::new(ordinal, flags);
- if of.valid() {
- assert_eq!(of.ordinal(), ordinal);
- }
- }
- }
- }
-
- #[test]
- fn test_of_with_fields() {
- fn check(flags: YearFlags, ordinal: u32) {
- let of = Of::new(ordinal, flags);
-
- for ordinal in range_inclusive(0u32, 1024) {
- let of = of.with_ordinal(ordinal);
- assert_eq!(of.valid(), Of::new(ordinal, flags).valid());
- if of.valid() {
- assert_eq!(of.ordinal(), ordinal);
- }
- }
- }
-
- for &flags in NONLEAP_FLAGS.iter() {
- check(flags, 1);
- check(flags, 365);
- }
- for &flags in LEAP_FLAGS.iter() {
- check(flags, 1);
- check(flags, 366);
- }
- }
-
- #[test]
- fn test_of_weekday() {
- assert_eq!(Of::new(1, A).weekday(), Weekday::Sun);
- assert_eq!(Of::new(1, B).weekday(), Weekday::Sat);
- assert_eq!(Of::new(1, C).weekday(), Weekday::Fri);
- assert_eq!(Of::new(1, D).weekday(), Weekday::Thu);
- assert_eq!(Of::new(1, E).weekday(), Weekday::Wed);
- assert_eq!(Of::new(1, F).weekday(), Weekday::Tue);
- assert_eq!(Of::new(1, G).weekday(), Weekday::Mon);
- assert_eq!(Of::new(1, AG).weekday(), Weekday::Sun);
- assert_eq!(Of::new(1, BA).weekday(), Weekday::Sat);
- assert_eq!(Of::new(1, CB).weekday(), Weekday::Fri);
- assert_eq!(Of::new(1, DC).weekday(), Weekday::Thu);
- assert_eq!(Of::new(1, ED).weekday(), Weekday::Wed);
- assert_eq!(Of::new(1, FE).weekday(), Weekday::Tue);
- assert_eq!(Of::new(1, GF).weekday(), Weekday::Mon);
-
- for &flags in FLAGS.iter() {
- let mut prev = Of::new(1, flags).weekday();
- for ordinal in range_inclusive(2u32, flags.ndays()) {
- let of = Of::new(ordinal, flags);
- let expected = prev.succ();
- assert_eq!(of.weekday(), expected);
- prev = expected;
- }
- }
- }
-
- #[test]
- fn test_mdf_fields() {
- for &flags in FLAGS.iter() {
- for month in range_inclusive(1u32, 12) {
- for day in range_inclusive(1u32, 31) {
- let mdf = Mdf::new(month, day, flags);
- if mdf.valid() {
- assert_eq!(mdf.month(), month);
- assert_eq!(mdf.day(), day);
- }
- }
- }
- }
- }
-
- #[test]
- fn test_mdf_with_fields() {
- fn check(flags: YearFlags, month: u32, day: u32) {
- let mdf = Mdf::new(month, day, flags);
-
- for month in range_inclusive(0u32, 16) {
- let mdf = mdf.with_month(month);
- assert_eq!(mdf.valid(), Mdf::new(month, day, flags).valid());
- if mdf.valid() {
- assert_eq!(mdf.month(), month);
- assert_eq!(mdf.day(), day);
- }
- }
-
- for day in range_inclusive(0u32, 1024) {
- let mdf = mdf.with_day(day);
- assert_eq!(mdf.valid(), Mdf::new(month, day, flags).valid());
- if mdf.valid() {
- assert_eq!(mdf.month(), month);
- assert_eq!(mdf.day(), day);
- }
- }
- }
-
- for &flags in NONLEAP_FLAGS.iter() {
- check(flags, 1, 1);
- check(flags, 1, 31);
- check(flags, 2, 1);
- check(flags, 2, 28);
- check(flags, 2, 29);
- check(flags, 12, 31);
- }
- for &flags in LEAP_FLAGS.iter() {
- check(flags, 1, 1);
- check(flags, 1, 31);
- check(flags, 2, 1);
- check(flags, 2, 29);
- check(flags, 2, 30);
- check(flags, 12, 31);
- }
- }
-
- #[test]
- fn test_of_isoweekdate_raw() {
- for &flags in FLAGS.iter() {
- // January 4 should be in the first week
- let (week, _) = Of::new(4 /* January 4 */, flags).isoweekdate_raw();
- assert_eq!(week, 1);
- }
- }
-
- #[test]
- fn test_of_to_mdf() {
- for i in range_inclusive(0u32, 8192) {
- let of = Of(i);
- assert_eq!(of.valid(), of.to_mdf().valid());
- }
- }
-
- #[test]
- fn test_mdf_to_of() {
- for i in range_inclusive(0u32, 8192) {
- let mdf = Mdf(i);
- assert_eq!(mdf.valid(), mdf.to_of().valid());
- }
- }
-
- #[test]
- fn test_of_to_mdf_to_of() {
- for i in range_inclusive(0u32, 8192) {
- let of = Of(i);
- if of.valid() {
- assert_eq!(of, of.to_mdf().to_of());
- }
- }
- }
-
- #[test]
- fn test_mdf_to_of_to_mdf() {
- for i in range_inclusive(0u32, 8192) {
- let mdf = Mdf(i);
- if mdf.valid() {
- assert_eq!(mdf, mdf.to_of().to_mdf());
- }
- }
- }
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/naive/datetime.rs b/vendor/chrono-0.3.0/src/naive/datetime.rs
deleted file mode 100644
index 81ac8af..0000000
--- a/vendor/chrono-0.3.0/src/naive/datetime.rs
+++ /dev/null
@@ -1,1685 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! ISO 8601 date and time without timezone.
-
-use std::{str, fmt, hash};
-use std::ops::{Add, Sub};
-use num::traits::ToPrimitive;
-use oldtime::Duration as OldDuration;
-
-use {Weekday, Timelike, Datelike};
-use div::div_mod_floor;
-use naive::time::NaiveTime;
-use naive::date::NaiveDate;
-use format::{Item, Numeric, Pad, Fixed};
-use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
-
-/// The tight upper bound guarantees that a duration with `|Duration| >= 2^MAX_SECS_BITS`
-/// will always overflow the addition with any date and time type.
-///
-/// So why is this needed? `Duration::seconds(rhs)` may overflow, and we don't have
-/// an alternative returning `Option` or `Result`. Thus we need some early bound to avoid
-/// touching that call when we are already sure that it WILL overflow...
-const MAX_SECS_BITS: usize = 44;
-
-/// ISO 8601 combined date and time without timezone.
-///
-/// # Example
-///
-/// `NaiveDateTime` is commonly created from [`NaiveDate`](../date/struct.NaiveDate.html).
-///
-/// ~~~~
-/// use chrono::{NaiveDate, NaiveDateTime};
-///
-/// let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11);
-/// # let _ = dt;
-/// ~~~~
-///
-/// You can use typical [date-like](../../trait.Datelike.html) and
-/// [time-like](../../trait.Timelike.html) methods,
-/// provided that relevant traits are in the scope.
-///
-/// ~~~~
-/// # use chrono::{NaiveDate, NaiveDateTime};
-/// # let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11);
-/// use chrono::{Datelike, Timelike, Weekday};
-///
-/// assert_eq!(dt.weekday(), Weekday::Fri);
-/// assert_eq!(dt.num_seconds_from_midnight(), 33011);
-/// ~~~~
-#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
-pub struct NaiveDateTime {
- date: NaiveDate,
- time: NaiveTime,
-}
-
-impl NaiveDateTime {
- /// Makes a new `NaiveDateTime` from date and time components.
- /// Equivalent to [`date.and_time(time)`](../date/struct.NaiveDate.html#method.and_time)
- /// and many other helper constructors on `NaiveDate`.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveTime, NaiveDateTime};
- ///
- /// let d = NaiveDate::from_ymd(2015, 6, 3);
- /// let t = NaiveTime::from_hms_milli(12, 34, 56, 789);
- ///
- /// let dt = NaiveDateTime::new(d, t);
- /// assert_eq!(dt.date(), d);
- /// assert_eq!(dt.time(), t);
- /// ~~~~
- #[inline]
- pub fn new(date: NaiveDate, time: NaiveTime) -> NaiveDateTime {
- NaiveDateTime { date: date, time: time }
- }
-
- /// Makes a new `NaiveDateTime` corresponding to a UTC date and time,
- /// from the number of non-leap seconds
- /// since the midnight UTC on January 1, 1970 (aka "UNIX timestamp")
- /// and the number of nanoseconds since the last whole non-leap second.
- ///
- /// For a non-naive version of this function see
- /// [`TimeZone::timestamp`](../../offset/trait.TimeZone.html#method.timestamp).
- ///
- /// 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
- /// timestamp" cannot represent a leap second unambiguously.)
- ///
- /// Panics on the out-of-range number of seconds and/or invalid nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDateTime, NaiveDate};
- ///
- /// let dt = NaiveDateTime::from_timestamp(0, 42_000_000);
- /// assert_eq!(dt, NaiveDate::from_ymd(1970, 1, 1).and_hms_milli(0, 0, 0, 42));
- ///
- /// let dt = NaiveDateTime::from_timestamp(1_000_000_000, 0);
- /// assert_eq!(dt, NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40));
- /// ~~~~
- #[inline]
- pub fn from_timestamp(secs: i64, nsecs: u32) -> NaiveDateTime {
- let datetime = NaiveDateTime::from_timestamp_opt(secs, nsecs);
- datetime.expect("invalid or out-of-range datetime")
- }
-
- /// Makes a new `NaiveDateTime` corresponding to a UTC date and time,
- /// from the number of non-leap seconds
- /// since the midnight UTC on January 1, 1970 (aka "UNIX timestamp")
- /// and the number of nanoseconds since the last whole non-leap second.
- ///
- /// 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 timestamp" cannot represent a leap second unambiguously.)
- ///
- /// Returns `None` on the out-of-range number of seconds and/or invalid nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDateTime, NaiveDate};
- /// use std::i64;
- ///
- /// let from_timestamp_opt = NaiveDateTime::from_timestamp_opt;
- ///
- /// assert!(from_timestamp_opt(0, 0).is_some());
- /// assert!(from_timestamp_opt(0, 999_999_999).is_some());
- /// assert!(from_timestamp_opt(0, 1_500_000_000).is_some()); // leap second
- /// assert!(from_timestamp_opt(0, 2_000_000_000).is_none());
- /// assert!(from_timestamp_opt(i64::MAX, 0).is_none());
- /// ~~~~
- #[inline]
- pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
- let (days, secs) = div_mod_floor(secs, 86400);
- let date = days.to_i32().and_then(|days| days.checked_add(719163))
- .and_then(|days_ce| NaiveDate::from_num_days_from_ce_opt(days_ce));
- let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
- match (date, time) {
- (Some(date), Some(time)) => Some(NaiveDateTime { date: date, time: time }),
- (_, _) => None,
- }
- }
-
- /// Parses a string with the specified format string and returns a new `NaiveDateTime`.
- /// See the [`format::strftime` module](../../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDateTime, NaiveDate};
- ///
- /// let parse_from_str = NaiveDateTime::parse_from_str;
- ///
- /// assert_eq!(parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S"),
- /// Ok(NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4)));
- /// assert_eq!(parse_from_str("5sep2015pm012345.6789", "%d%b%Y%p%I%M%S%.f"),
- /// Ok(NaiveDate::from_ymd(2015, 9, 5).and_hms_micro(13, 23, 45, 678_900)));
- /// ~~~~
- ///
- /// Offset is ignored for the purpose of parsing.
- ///
- /// ~~~~
- /// # use chrono::{NaiveDateTime, NaiveDate};
- /// # let parse_from_str = NaiveDateTime::parse_from_str;
- /// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- /// Ok(NaiveDate::from_ymd(2014, 5, 17).and_hms(12, 34, 56)));
- /// ~~~~
- ///
- /// [Leap seconds](./index.html#leap-second-handling) are correctly handled by
- /// treating any time of the form `hh:mm:60` as a leap second.
- /// (This equally applies to the formatting, so the round trip is possible.)
- ///
- /// ~~~~
- /// # use chrono::{NaiveDateTime, NaiveDate};
- /// # let parse_from_str = NaiveDateTime::parse_from_str;
- /// assert_eq!(parse_from_str("2015-07-01 08:59:60.123", "%Y-%m-%d %H:%M:%S%.f"),
- /// Ok(NaiveDate::from_ymd(2015, 7, 1).and_hms_milli(8, 59, 59, 1_123)));
- /// ~~~~
- ///
- /// Missing seconds are assumed to be zero,
- /// but out-of-bound times or insufficient fields are errors otherwise.
- ///
- /// ~~~~
- /// # use chrono::{NaiveDateTime, NaiveDate};
- /// # let parse_from_str = NaiveDateTime::parse_from_str;
- /// assert_eq!(parse_from_str("94/9/4 7:15", "%y/%m/%d %H:%M"),
- /// Ok(NaiveDate::from_ymd(1994, 9, 4).and_hms(7, 15, 0)));
- ///
- /// assert!(parse_from_str("04m33s", "%Mm%Ss").is_err());
- /// assert!(parse_from_str("94/9/4 12", "%y/%m/%d %H").is_err());
- /// assert!(parse_from_str("94/9/4 17:60", "%y/%m/%d %H:%M").is_err());
- /// assert!(parse_from_str("94/9/4 24:00:00", "%y/%m/%d %H:%M:%S").is_err());
- /// ~~~~
- ///
- /// All parsed fields should be consistent to each other, otherwise it's an error.
- ///
- /// ~~~~
- /// # use chrono::NaiveDateTime;
- /// # let parse_from_str = NaiveDateTime::parse_from_str;
- /// let fmt = "%Y-%m-%d %H:%M:%S = UNIX timestamp %s";
- /// assert!(parse_from_str("2001-09-09 01:46:39 = UNIX timestamp 999999999", fmt).is_ok());
- /// assert!(parse_from_str("1970-01-01 00:00:00 = UNIX timestamp 1", fmt).is_err());
- /// ~~~~
- pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveDateTime> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, StrftimeItems::new(fmt)));
- parsed.to_naive_datetime_with_offset(0) // no offset adjustment
- }
-
- /// Retrieves a date component.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11);
- /// assert_eq!(dt.date(), NaiveDate::from_ymd(2016, 7, 8));
- /// ~~~~
- #[inline]
- pub fn date(&self) -> NaiveDate {
- self.date
- }
-
- /// Retrieves a time component.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveTime};
- ///
- /// let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11);
- /// assert_eq!(dt.time(), NaiveTime::from_hms(9, 10, 11));
- /// ~~~~
- #[inline]
- pub fn time(&self) -> NaiveTime {
- self.time
- }
-
- /// Returns the number of non-leap seconds since the midnight on January 1, 1970.
- ///
- /// Note that this does *not* account for the timezone!
- /// The true "UNIX timestamp" would count seconds since the midnight *UTC* on the epoch.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let dt = NaiveDate::from_ymd(1970, 1, 1).and_hms_milli(0, 0, 1, 980);
- /// assert_eq!(dt.timestamp(), 1);
- ///
- /// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40);
- /// assert_eq!(dt.timestamp(), 1_000_000_000);
- /// ~~~~
- #[inline]
- pub fn timestamp(&self) -> i64 {
- let ndays = self.date.num_days_from_ce() as i64;
- let nseconds = self.time.num_seconds_from_midnight() as i64;
- (ndays - 719163) * 86400 + nseconds
- }
-
- /// Returns the number of milliseconds since the last whole non-leap second.
- ///
- /// The return value ranges from 0 to 999,
- /// or for [leap seconds](../time/index.html#leap-second-handling), to 1,999.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_nano(9, 10, 11, 123_456_789);
- /// assert_eq!(dt.timestamp_subsec_millis(), 123);
- ///
- /// let dt = NaiveDate::from_ymd(2015, 7, 1).and_hms_nano(8, 59, 59, 1_234_567_890);
- /// assert_eq!(dt.timestamp_subsec_millis(), 1_234);
- /// ~~~~
- #[inline]
- pub fn timestamp_subsec_millis(&self) -> u32 {
- self.timestamp_subsec_nanos() / 1_000_000
- }
-
- /// Returns the number of microseconds since the last whole non-leap second.
- ///
- /// The return value ranges from 0 to 999,999,
- /// or for [leap seconds](../time/index.html#leap-second-handling), to 1,999,999.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_nano(9, 10, 11, 123_456_789);
- /// assert_eq!(dt.timestamp_subsec_micros(), 123_456);
- ///
- /// let dt = NaiveDate::from_ymd(2015, 7, 1).and_hms_nano(8, 59, 59, 1_234_567_890);
- /// assert_eq!(dt.timestamp_subsec_micros(), 1_234_567);
- /// ~~~~
- #[inline]
- pub fn timestamp_subsec_micros(&self) -> u32 {
- self.timestamp_subsec_nanos() / 1_000
- }
-
- /// Returns the number of nanoseconds since the last whole non-leap second.
- ///
- /// 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.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_nano(9, 10, 11, 123_456_789);
- /// assert_eq!(dt.timestamp_subsec_nanos(), 123_456_789);
- ///
- /// let dt = NaiveDate::from_ymd(2015, 7, 1).and_hms_nano(8, 59, 59, 1_234_567_890);
- /// assert_eq!(dt.timestamp_subsec_nanos(), 1_234_567_890);
- /// ~~~~
- #[inline]
- pub fn timestamp_subsec_nanos(&self) -> u32 {
- self.time.nanosecond()
- }
-
- /// Adds given `Duration` to the current date and time.
- ///
- /// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling),
- /// the addition assumes that **there is no leap second ever**,
- /// except when the `NaiveDateTime` itself represents a leap second
- /// in which case the assumption becomes that **there is exactly a single leap second ever**.
- ///
- /// Returns `None` when it will result in overflow.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveDate;
- /// use time::Duration;
- ///
- /// let from_ymd = NaiveDate::from_ymd;
- ///
- /// let d = from_ymd(2016, 7, 8);
- /// let hms = |h, m, s| d.and_hms(h, m, s);
- /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::zero()),
- /// Some(hms(3, 5, 7)));
- /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(1)),
- /// Some(hms(3, 5, 8)));
- /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(-1)),
- /// Some(hms(3, 5, 6)));
- /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(3600 + 60)),
- /// Some(hms(4, 6, 7)));
- /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(86400)),
- /// Some(from_ymd(2016, 7, 9).and_hms(3, 5, 7)));
- ///
- /// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
- /// assert_eq!(hmsm(3, 5, 7, 980).checked_add_signed(Duration::milliseconds(450)),
- /// Some(hmsm(3, 5, 8, 430)));
- /// # }
- /// ~~~~
- ///
- /// Overflow returns `None`.
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// # use chrono::NaiveDate;
- /// # use time::Duration;
- /// # let hms = |h, m, s| NaiveDate::from_ymd(2016, 7, 8).and_hms(h, m, s);
- /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::days(1_000_000_000)), None);
- /// # }
- /// ~~~~
- ///
- /// Leap seconds are handled,
- /// but the addition assumes that it is the only leap second happened.
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// # use chrono::NaiveDate;
- /// # use time::Duration;
- /// # let from_ymd = NaiveDate::from_ymd;
- /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
- /// let leap = hmsm(3, 5, 59, 1_300);
- /// assert_eq!(leap.checked_add_signed(Duration::zero()),
- /// Some(hmsm(3, 5, 59, 1_300)));
- /// assert_eq!(leap.checked_add_signed(Duration::milliseconds(-500)),
- /// Some(hmsm(3, 5, 59, 800)));
- /// assert_eq!(leap.checked_add_signed(Duration::milliseconds(500)),
- /// Some(hmsm(3, 5, 59, 1_800)));
- /// assert_eq!(leap.checked_add_signed(Duration::milliseconds(800)),
- /// Some(hmsm(3, 6, 0, 100)));
- /// assert_eq!(leap.checked_add_signed(Duration::seconds(10)),
- /// Some(hmsm(3, 6, 9, 300)));
- /// assert_eq!(leap.checked_add_signed(Duration::seconds(-10)),
- /// Some(hmsm(3, 5, 50, 300)));
- /// assert_eq!(leap.checked_add_signed(Duration::days(1)),
- /// Some(from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300)));
- /// # }
- /// ~~~~
- pub fn checked_add_signed(self, rhs: OldDuration) -> Option<NaiveDateTime> {
- let (time, rhs) = self.time.overflowing_add_signed(rhs);
-
- // early checking to avoid overflow in OldDuration::seconds
- if rhs <= (-1 << MAX_SECS_BITS) || rhs >= (1 << MAX_SECS_BITS) {
- return None;
- }
-
- let date = try_opt!(self.date.checked_add_signed(OldDuration::seconds(rhs)));
- Some(NaiveDateTime { date: date, time: 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),
- /// the subtraction assumes that **there is no leap second ever**,
- /// except when the `NaiveDateTime` itself represents a leap second
- /// in which case the assumption becomes that **there is exactly a single leap second ever**.
- ///
- /// Returns `None` when it will result in overflow.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveDate;
- /// use time::Duration;
- ///
- /// let from_ymd = NaiveDate::from_ymd;
- ///
- /// let d = from_ymd(2016, 7, 8);
- /// let hms = |h, m, s| d.and_hms(h, m, s);
- /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::zero()),
- /// Some(hms(3, 5, 7)));
- /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(1)),
- /// Some(hms(3, 5, 6)));
- /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(-1)),
- /// Some(hms(3, 5, 8)));
- /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(3600 + 60)),
- /// Some(hms(2, 4, 7)));
- /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(86400)),
- /// Some(from_ymd(2016, 7, 7).and_hms(3, 5, 7)));
- ///
- /// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
- /// assert_eq!(hmsm(3, 5, 7, 450).checked_sub_signed(Duration::milliseconds(670)),
- /// Some(hmsm(3, 5, 6, 780)));
- /// # }
- /// ~~~~
- ///
- /// Overflow returns `None`.
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// # use chrono::NaiveDate;
- /// # use time::Duration;
- /// # let hms = |h, m, s| NaiveDate::from_ymd(2016, 7, 8).and_hms(h, m, s);
- /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::days(1_000_000_000)), None);
- /// # }
- /// ~~~~
- ///
- /// Leap seconds are handled,
- /// but the subtraction assumes that it is the only leap second happened.
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// # use chrono::NaiveDate;
- /// # use time::Duration;
- /// # let from_ymd = NaiveDate::from_ymd;
- /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
- /// let leap = hmsm(3, 5, 59, 1_300);
- /// assert_eq!(leap.checked_sub_signed(Duration::zero()),
- /// Some(hmsm(3, 5, 59, 1_300)));
- /// assert_eq!(leap.checked_sub_signed(Duration::milliseconds(200)),
- /// Some(hmsm(3, 5, 59, 1_100)));
- /// assert_eq!(leap.checked_sub_signed(Duration::milliseconds(500)),
- /// Some(hmsm(3, 5, 59, 800)));
- /// assert_eq!(leap.checked_sub_signed(Duration::seconds(60)),
- /// Some(hmsm(3, 5, 0, 300)));
- /// assert_eq!(leap.checked_sub_signed(Duration::days(1)),
- /// Some(from_ymd(2016, 7, 7).and_hms_milli(3, 6, 0, 300)));
- /// # }
- /// ~~~~
- pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<NaiveDateTime> {
- let (time, rhs) = self.time.overflowing_sub_signed(rhs);
-
- // early checking to avoid overflow in OldDuration::seconds
- if rhs <= (-1 << MAX_SECS_BITS) || rhs >= (1 << MAX_SECS_BITS) {
- return None;
- }
-
- let date = try_opt!(self.date.checked_sub_signed(OldDuration::seconds(rhs)));
- Some(NaiveDateTime { date: date, time: time })
- }
-
- /// Subtracts another `NaiveDateTime` from the current date and time.
- /// This does not overflow or underflow at all.
- ///
- /// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling),
- /// the subtraction assumes that **there is no leap second ever**,
- /// except when any of the `NaiveDateTime`s themselves represents a leap second
- /// in which case the assumption becomes that
- /// **there are exactly one (or two) leap second(s) ever**.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveDate;
- /// use time::Duration;
- ///
- /// let from_ymd = NaiveDate::from_ymd;
- ///
- /// let d = from_ymd(2016, 7, 8);
- /// assert_eq!(d.and_hms(3, 5, 7).signed_duration_since(d.and_hms(2, 4, 6)),
- /// Duration::seconds(3600 + 60 + 1));
- ///
- /// // July 8 is 190th day in the year 2016
- /// let d0 = from_ymd(2016, 1, 1);
- /// assert_eq!(d.and_hms_milli(0, 7, 6, 500).signed_duration_since(d0.and_hms(0, 0, 0)),
- /// Duration::seconds(189 * 86400 + 7 * 60 + 6) + Duration::milliseconds(500));
- /// # }
- /// ~~~~
- ///
- /// Leap seconds are handled, but the subtraction assumes that
- /// there were no other leap seconds happened.
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// # use chrono::NaiveDate;
- /// # use time::Duration;
- /// # let from_ymd = NaiveDate::from_ymd;
- /// let leap = from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500);
- /// assert_eq!(leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms(23, 0, 0)),
- /// Duration::seconds(3600) + Duration::milliseconds(500));
- /// assert_eq!(from_ymd(2015, 7, 1).and_hms(1, 0, 0).signed_duration_since(leap),
- /// Duration::seconds(3600) - Duration::milliseconds(500));
- /// # }
- /// ~~~~
- pub fn signed_duration_since(self, rhs: NaiveDateTime) -> OldDuration {
- self.date.signed_duration_since(rhs.date) + self.time.signed_duration_since(rhs.time)
- }
-
- /// Formats the combined date and time with the specified formatting items.
- /// Otherwise it is same to the ordinary [`format`](#method.format) method.
- ///
- /// The `Iterator` of items should be `Clone`able,
- /// since the resulting `DelayedFormat` value may be formatted multiple times.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- /// use chrono::format::strftime::StrftimeItems;
- ///
- /// let fmt = StrftimeItems::new("%Y-%m-%d %H:%M:%S");
- /// let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4);
- /// assert_eq!(dt.format_with_items(fmt.clone()).to_string(), "2015-09-05 23:56:04");
- /// assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
- /// ~~~~
- ///
- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # use chrono::format::strftime::StrftimeItems;
- /// # let fmt = StrftimeItems::new("%Y-%m-%d %H:%M:%S").clone();
- /// # let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4);
- /// assert_eq!(format!("{}", dt.format_with_items(fmt)), "2015-09-05 23:56:04");
- /// ~~~~
- #[inline]
- pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
- where I: Iterator<Item=Item<'a>> + Clone {
- DelayedFormat::new(Some(self.date), Some(self.time), items)
- }
-
- /// Formats the combined date and time with the specified format string.
- /// See the [`format::strftime` module](../../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// This returns a `DelayedFormat`,
- /// which gets converted to a string only when actual formatting happens.
- /// You may use the `to_string` method to get a `String`,
- /// or just feed it into `print!` and other formatting macros.
- /// (In this way it avoids the redundant memory allocation.)
- ///
- /// A wrong format string does *not* issue an error immediately.
- /// Rather, converting or formatting the `DelayedFormat` fails.
- /// You are recommended to immediately use `DelayedFormat` for this reason.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveDate;
- ///
- /// let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4);
- /// assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
- /// assert_eq!(dt.format("around %l %p on %b %-d").to_string(), "around 11 PM on Sep 5");
- /// ~~~~
- ///
- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
- ///
- /// ~~~~
- /// # use chrono::NaiveDate;
- /// # let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4);
- /// assert_eq!(format!("{}", dt.format("%Y-%m-%d %H:%M:%S")), "2015-09-05 23:56:04");
- /// assert_eq!(format!("{}", dt.format("around %l %p on %b %-d")), "around 11 PM on Sep 5");
- /// ~~~~
- #[inline]
- pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
- self.format_with_items(StrftimeItems::new(fmt))
- }
-}
-
-impl Datelike for NaiveDateTime {
- /// Returns the year number in the [calendar date](./index.html#calendar-date).
- ///
- /// See also the [`NaiveDate::year`](../date/struct.NaiveDate.html#method.year) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.year(), 2015);
- /// ~~~~
- #[inline]
- fn year(&self) -> i32 {
- self.date.year()
- }
-
- /// Returns the month number starting from 1.
- ///
- /// The return value ranges from 1 to 12.
- ///
- /// See also the [`NaiveDate::month`](../date/struct.NaiveDate.html#method.month) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.month(), 9);
- /// ~~~~
- #[inline]
- fn month(&self) -> u32 {
- self.date.month()
- }
-
- /// Returns the month number starting from 0.
- ///
- /// The return value ranges from 0 to 11.
- ///
- /// See also the [`NaiveDate::month0`](../date/struct.NaiveDate.html#method.month0) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.month0(), 8);
- /// ~~~~
- #[inline]
- fn month0(&self) -> u32 {
- self.date.month0()
- }
-
- /// Returns the day of month starting from 1.
- ///
- /// 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.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.day(), 25);
- /// ~~~~
- #[inline]
- fn day(&self) -> u32 {
- self.date.day()
- }
-
- /// Returns the day of month starting from 0.
- ///
- /// 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.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.day0(), 24);
- /// ~~~~
- #[inline]
- fn day0(&self) -> u32 {
- self.date.day0()
- }
-
- /// Returns the day of year starting from 1.
- ///
- /// 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.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.ordinal(), 268);
- /// ~~~~
- #[inline]
- fn ordinal(&self) -> u32 {
- self.date.ordinal()
- }
-
- /// Returns the day of year starting from 0.
- ///
- /// 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.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.ordinal0(), 267);
- /// ~~~~
- #[inline]
- fn ordinal0(&self) -> u32 {
- self.date.ordinal0()
- }
-
- /// Returns the day of week.
- ///
- /// See also the [`NaiveDate::weekday`](../date/struct.NaiveDate.html#method.weekday) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike, Weekday};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.weekday(), Weekday::Fri);
- /// ~~~~
- #[inline]
- fn weekday(&self) -> Weekday {
- self.date.weekday()
- }
-
- #[inline]
- fn isoweekdate(&self) -> (i32, u32, Weekday) {
- self.date.isoweekdate()
- }
-
- /// Makes a new `NaiveDateTime` with the year number changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_year`](../date/struct.NaiveDate.html#method.with_year) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_year(2016), Some(NaiveDate::from_ymd(2016, 9, 25).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_year(-308), Some(NaiveDate::from_ymd(-308, 9, 25).and_hms(12, 34, 56)));
- /// ~~~~
- #[inline]
- fn with_year(&self, year: i32) -> Option<NaiveDateTime> {
- self.date.with_year(year).map(|d| NaiveDateTime { date: d, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the month number (starting from 1) changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_month`](../date/struct.NaiveDate.html#method.with_month) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_month(10), Some(NaiveDate::from_ymd(2015, 10, 30).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_month(13), None); // no month 13
- /// assert_eq!(dt.with_month(2), None); // no February 30
- /// ~~~~
- #[inline]
- fn with_month(&self, month: u32) -> Option<NaiveDateTime> {
- self.date.with_month(month).map(|d| NaiveDateTime { date: d, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the month number (starting from 0) changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_month0`](../date/struct.NaiveDate.html#method.with_month0) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_month0(9), Some(NaiveDate::from_ymd(2015, 10, 30).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_month0(12), None); // no month 13
- /// assert_eq!(dt.with_month0(1), None); // no February 30
- /// ~~~~
- #[inline]
- fn with_month0(&self, month0: u32) -> Option<NaiveDateTime> {
- self.date.with_month0(month0).map(|d| NaiveDateTime { date: d, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the day of month (starting from 1) changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_day`](../date/struct.NaiveDate.html#method.with_day) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_day(30), Some(NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_day(31), None); // no September 31
- /// ~~~~
- #[inline]
- fn with_day(&self, day: u32) -> Option<NaiveDateTime> {
- self.date.with_day(day).map(|d| NaiveDateTime { date: d, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the day of month (starting from 0) changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_day0`](../date/struct.NaiveDate.html#method.with_day0) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_day0(29), Some(NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_day0(30), None); // no September 31
- /// ~~~~
- #[inline]
- fn with_day0(&self, day0: u32) -> Option<NaiveDateTime> {
- self.date.with_day0(day0).map(|d| NaiveDateTime { date: d, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the day of year (starting from 1) changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_ordinal`](../date/struct.NaiveDate.html#method.with_ordinal) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_ordinal(60),
- /// Some(NaiveDate::from_ymd(2015, 3, 1).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_ordinal(366), None); // 2015 had only 365 days
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 9, 8).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_ordinal(60),
- /// Some(NaiveDate::from_ymd(2016, 2, 29).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_ordinal(366),
- /// Some(NaiveDate::from_ymd(2016, 12, 31).and_hms(12, 34, 56)));
- /// ~~~~
- #[inline]
- fn with_ordinal(&self, ordinal: u32) -> Option<NaiveDateTime> {
- self.date.with_ordinal(ordinal).map(|d| NaiveDateTime { date: d, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the day of year (starting from 0) changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveDate::with_ordinal0`](../date/struct.NaiveDate.html#method.with_ordinal0) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Datelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_ordinal0(59),
- /// Some(NaiveDate::from_ymd(2015, 3, 1).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_ordinal0(365), None); // 2015 had only 365 days
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 9, 8).and_hms(12, 34, 56);
- /// assert_eq!(dt.with_ordinal0(59),
- /// Some(NaiveDate::from_ymd(2016, 2, 29).and_hms(12, 34, 56)));
- /// assert_eq!(dt.with_ordinal0(365),
- /// Some(NaiveDate::from_ymd(2016, 12, 31).and_hms(12, 34, 56)));
- /// ~~~~
- #[inline]
- fn with_ordinal0(&self, ordinal0: u32) -> Option<NaiveDateTime> {
- self.date.with_ordinal0(ordinal0).map(|d| NaiveDateTime { date: d, ..*self })
- }
-}
-
-impl Timelike for NaiveDateTime {
- /// Returns the hour number from 0 to 23.
- ///
- /// See also the [`NaiveTime::hour`](../time/struct.NaiveTime.html#method.hour) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.hour(), 12);
- /// ~~~~
- #[inline]
- fn hour(&self) -> u32 {
- self.time.hour()
- }
-
- /// Returns the minute number from 0 to 59.
- ///
- /// See also the [`NaiveTime::minute`](../time/struct.NaiveTime.html#method.minute) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.minute(), 34);
- /// ~~~~
- #[inline]
- fn minute(&self) -> u32 {
- self.time.minute()
- }
-
- /// Returns the second number from 0 to 59.
- ///
- /// See also the [`NaiveTime::second`](../time/struct.NaiveTime.html#method.second) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.second(), 56);
- /// ~~~~
- #[inline]
- fn second(&self) -> u32 {
- self.time.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 [leap second](./naive/time/index.html#leap-second-handling).
- ///
- /// See also the
- /// [`NaiveTime::nanosecond`](../time/struct.NaiveTime.html#method.nanosecond) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.nanosecond(), 789_000_000);
- /// ~~~~
- #[inline]
- fn nanosecond(&self) -> u32 {
- self.time.nanosecond()
- }
-
- /// Makes a new `NaiveDateTime` with the hour number changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveTime::with_hour`](../time/struct.NaiveTime.html#method.with_hour) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.with_hour(7),
- /// Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(7, 34, 56, 789)));
- /// assert_eq!(dt.with_hour(24), None);
- /// ~~~~
- #[inline]
- fn with_hour(&self, hour: u32) -> Option<NaiveDateTime> {
- self.time.with_hour(hour).map(|t| NaiveDateTime { time: t, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the minute number changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- ///
- /// See also the
- /// [`NaiveTime::with_minute`](../time/struct.NaiveTime.html#method.with_minute) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.with_minute(45),
- /// Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 45, 56, 789)));
- /// assert_eq!(dt.with_minute(60), None);
- /// ~~~~
- #[inline]
- fn with_minute(&self, min: u32) -> Option<NaiveDateTime> {
- self.time.with_minute(min).map(|t| NaiveDateTime { time: t, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with the second number changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- /// As with the [`second`](#method.second) method,
- /// the input range is restricted to 0 through 59.
- ///
- /// See also the
- /// [`NaiveTime::with_second`](../time/struct.NaiveTime.html#method.with_second) method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.with_second(17),
- /// Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 17, 789)));
- /// assert_eq!(dt.with_second(60), None);
- /// ~~~~
- #[inline]
- fn with_second(&self, sec: u32) -> Option<NaiveDateTime> {
- self.time.with_second(sec).map(|t| NaiveDateTime { time: t, ..*self })
- }
-
- /// Makes a new `NaiveDateTime` with nanoseconds since the whole non-leap second changed.
- ///
- /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
- /// As with the [`nanosecond`](#method.nanosecond) method,
- /// the input range can exceed 1,000,000,000 for leap seconds.
- ///
- /// See also the
- /// [`NaiveTime::with_nanosecond`](../time/struct.NaiveTime.html#method.with_nanosecond)
- /// method.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveDate, NaiveDateTime, Timelike};
- ///
- /// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
- /// assert_eq!(dt.with_nanosecond(333_333_333),
- /// Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_nano(12, 34, 56, 333_333_333)));
- /// assert_eq!(dt.with_nanosecond(1_333_333_333), // leap second
- /// Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_nano(12, 34, 56, 1_333_333_333)));
- /// assert_eq!(dt.with_nanosecond(2_000_000_000), None);
- /// ~~~~
- #[inline]
- fn with_nanosecond(&self, nano: u32) -> Option<NaiveDateTime> {
- self.time.with_nanosecond(nano).map(|t| NaiveDateTime { time: t, ..*self })
- }
-}
-
-/// `NaiveDateTime` can be used as a key to the hash maps (in principle).
-///
-/// Practically this also takes account of fractional seconds, so it is not recommended.
-/// (For the obvious reason this also distinguishes leap seconds from non-leap seconds.)
-impl hash::Hash for NaiveDateTime {
- fn hash<H: hash::Hasher>(&self, state: &mut H) {
- self.date.hash(state);
- self.time.hash(state);
- }
-}
-
-/// An addition of `Duration` to `NaiveDateTime` yields another `NaiveDateTime`.
-///
-/// As a part of Chrono's [leap second handling](../time/index.html#leap-second-handling),
-/// the addition assumes that **there is no leap second ever**,
-/// except when the `NaiveDateTime` itself represents a leap second
-/// in which case the assumption becomes that **there is exactly a single leap second ever**.
-///
-/// Panics on underflow or overflow.
-/// Use [`NaiveDateTime::checked_add_signed`](#method.checked_add_signed) to detect that.
-///
-/// # Example
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// use chrono::NaiveDate;
-/// use time::Duration;
-///
-/// let from_ymd = NaiveDate::from_ymd;
-///
-/// let d = from_ymd(2016, 7, 8);
-/// let hms = |h, m, s| d.and_hms(h, m, s);
-/// assert_eq!(hms(3, 5, 7) + Duration::zero(), hms(3, 5, 7));
-/// assert_eq!(hms(3, 5, 7) + Duration::seconds(1), hms(3, 5, 8));
-/// assert_eq!(hms(3, 5, 7) + Duration::seconds(-1), hms(3, 5, 6));
-/// assert_eq!(hms(3, 5, 7) + Duration::seconds(3600 + 60), hms(4, 6, 7));
-/// assert_eq!(hms(3, 5, 7) + Duration::seconds(86400),
-/// from_ymd(2016, 7, 9).and_hms(3, 5, 7));
-/// assert_eq!(hms(3, 5, 7) + Duration::days(365),
-/// from_ymd(2017, 7, 8).and_hms(3, 5, 7));
-///
-/// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
-/// assert_eq!(hmsm(3, 5, 7, 980) + Duration::milliseconds(450), hmsm(3, 5, 8, 430));
-/// # }
-/// ~~~~
-///
-/// Leap seconds are handled,
-/// but the addition assumes that it is the only leap second happened.
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// # use chrono::NaiveDate;
-/// # use time::Duration;
-/// # let from_ymd = NaiveDate::from_ymd;
-/// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
-/// let leap = hmsm(3, 5, 59, 1_300);
-/// assert_eq!(leap + Duration::zero(), hmsm(3, 5, 59, 1_300));
-/// assert_eq!(leap + Duration::milliseconds(-500), hmsm(3, 5, 59, 800));
-/// assert_eq!(leap + Duration::milliseconds(500), hmsm(3, 5, 59, 1_800));
-/// assert_eq!(leap + Duration::milliseconds(800), hmsm(3, 6, 0, 100));
-/// assert_eq!(leap + Duration::seconds(10), hmsm(3, 6, 9, 300));
-/// assert_eq!(leap + Duration::seconds(-10), hmsm(3, 5, 50, 300));
-/// assert_eq!(leap + Duration::days(1),
-/// from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300));
-/// # }
-/// ~~~~
-impl Add<OldDuration> for NaiveDateTime {
- type Output = NaiveDateTime;
-
- #[inline]
- fn add(self, rhs: OldDuration) -> NaiveDateTime {
- self.checked_add_signed(rhs).expect("`NaiveDateTime + Duration` overflowed")
- }
-}
-
-/// A subtraction of `Duration` from `NaiveDateTime` yields another `NaiveDateTime`.
-/// 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),
-/// the addition assumes that **there is no leap second ever**,
-/// except when the `NaiveDateTime` itself represents a leap second
-/// in which case the assumption becomes that **there is exactly a single leap second ever**.
-///
-/// Panics on underflow or overflow.
-/// Use [`NaiveDateTime::checked_sub_signed`](#method.checked_sub_signed) to detect that.
-///
-/// # Example
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// use chrono::NaiveDate;
-/// use time::Duration;
-///
-/// let from_ymd = NaiveDate::from_ymd;
-///
-/// let d = from_ymd(2016, 7, 8);
-/// let hms = |h, m, s| d.and_hms(h, m, s);
-/// assert_eq!(hms(3, 5, 7) - Duration::zero(), hms(3, 5, 7));
-/// assert_eq!(hms(3, 5, 7) - Duration::seconds(1), hms(3, 5, 6));
-/// assert_eq!(hms(3, 5, 7) - Duration::seconds(-1), hms(3, 5, 8));
-/// assert_eq!(hms(3, 5, 7) - Duration::seconds(3600 + 60), hms(2, 4, 7));
-/// assert_eq!(hms(3, 5, 7) - Duration::seconds(86400),
-/// from_ymd(2016, 7, 7).and_hms(3, 5, 7));
-/// assert_eq!(hms(3, 5, 7) - Duration::days(365),
-/// from_ymd(2015, 7, 9).and_hms(3, 5, 7));
-///
-/// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
-/// assert_eq!(hmsm(3, 5, 7, 450) - Duration::milliseconds(670), hmsm(3, 5, 6, 780));
-/// # }
-/// ~~~~
-///
-/// Leap seconds are handled,
-/// but the subtraction assumes that it is the only leap second happened.
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// # use chrono::NaiveDate;
-/// # use time::Duration;
-/// # let from_ymd = NaiveDate::from_ymd;
-/// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli(h, m, s, milli);
-/// let leap = hmsm(3, 5, 59, 1_300);
-/// assert_eq!(leap - Duration::zero(), hmsm(3, 5, 59, 1_300));
-/// assert_eq!(leap - Duration::milliseconds(200), hmsm(3, 5, 59, 1_100));
-/// assert_eq!(leap - Duration::milliseconds(500), hmsm(3, 5, 59, 800));
-/// assert_eq!(leap - Duration::seconds(60), hmsm(3, 5, 0, 300));
-/// assert_eq!(leap - Duration::days(1),
-/// from_ymd(2016, 7, 7).and_hms_milli(3, 6, 0, 300));
-/// # }
-/// ~~~~
-impl Sub<OldDuration> for NaiveDateTime {
- type Output = NaiveDateTime;
-
- #[inline]
- fn sub(self, rhs: OldDuration) -> NaiveDateTime {
- self.checked_sub_signed(rhs).expect("`NaiveDateTime - Duration` overflowed")
- }
-}
-
-/// The `Debug` output of the naive date and time `dt` is same to
-/// [`dt.format("%Y-%m-%dT%H:%M:%S%.f")`](../../format/strftime/index.html).
-///
-/// The string printed can be readily parsed via the `parse` method on `str`.
-///
-/// It should be noted that, for leap seconds not on the minute boundary,
-/// it may print a representation not distinguishable from non-leap seconds.
-/// This doesn't matter in practice, since such leap seconds never happened.
-/// (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.)
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveDate;
-///
-/// let dt = NaiveDate::from_ymd(2016, 11, 15).and_hms(7, 39, 24);
-/// assert_eq!(format!("{:?}", dt), "2016-11-15T07:39:24");
-/// ~~~~
-///
-/// Leap seconds may also be used.
-///
-/// ~~~~
-/// # use chrono::NaiveDate;
-/// let dt = NaiveDate::from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500);
-/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60.500");
-/// ~~~~
-impl fmt::Debug for NaiveDateTime {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{:?}T{:?}", self.date, self.time)
- }
-}
-
-/// The `Debug` output of the naive date and time `dt` is same to
-/// [`dt.format("%Y-%m-%d %H:%M:%S%.f")`](../../format/strftime/index.html).
-///
-/// It should be noted that, for leap seconds not on the minute boundary,
-/// it may print a representation not distinguishable from non-leap seconds.
-/// This doesn't matter in practice, since such leap seconds never happened.
-/// (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.)
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveDate;
-///
-/// let dt = NaiveDate::from_ymd(2016, 11, 15).and_hms(7, 39, 24);
-/// assert_eq!(format!("{}", dt), "2016-11-15 07:39:24");
-/// ~~~~
-///
-/// Leap seconds may also be used.
-///
-/// ~~~~
-/// # use chrono::NaiveDate;
-/// let dt = NaiveDate::from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500);
-/// assert_eq!(format!("{}", dt), "2015-06-30 23:59:60.500");
-/// ~~~~
-impl fmt::Display for NaiveDateTime {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{} {}", self.date, self.time)
- }
-}
-
-/// Parsing a `str` into a `NaiveDateTime` uses the same format,
-/// [`%Y-%m-%dT%H:%M:%S%.f`](../../format/strftime/index.html), as in `Debug`.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::{NaiveDateTime, NaiveDate};
-///
-/// let dt = NaiveDate::from_ymd(2015, 9, 18).and_hms(23, 56, 4);
-/// assert_eq!("2015-09-18T23:56:04".parse::<NaiveDateTime>(), Ok(dt));
-///
-/// let dt = NaiveDate::from_ymd(12345, 6, 7).and_hms_milli(7, 59, 59, 1_500); // leap second
-/// assert_eq!("+12345-6-7T7:59:60.5".parse::<NaiveDateTime>(), Ok(dt));
-///
-/// assert!("foo".parse::<NaiveDateTime>().is_err());
-/// ~~~~
-impl str::FromStr for NaiveDateTime {
- type Err = ParseError;
-
- fn from_str(s: &str) -> ParseResult<NaiveDateTime> {
- const ITEMS: &'static [Item<'static>] = &[
- Item::Space(""), Item::Numeric(Numeric::Year, Pad::Zero),
- Item::Space(""), Item::Literal("-"),
- Item::Space(""), Item::Numeric(Numeric::Month, Pad::Zero),
- Item::Space(""), Item::Literal("-"),
- Item::Space(""), Item::Numeric(Numeric::Day, Pad::Zero),
- Item::Space(""), Item::Literal("T"), // XXX shouldn't this be case-insensitive?
- Item::Space(""), Item::Numeric(Numeric::Hour, Pad::Zero),
- Item::Space(""), Item::Literal(":"),
- Item::Space(""), Item::Numeric(Numeric::Minute, Pad::Zero),
- Item::Space(""), Item::Literal(":"),
- Item::Space(""), Item::Numeric(Numeric::Second, Pad::Zero),
- Item::Fixed(Fixed::Nanosecond), Item::Space(""),
- ];
-
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, ITEMS.iter().cloned()));
- parsed.to_naive_datetime_with_offset(0)
- }
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_encodable_json<F, E>(to_string: F)
- where F: Fn(&NaiveDateTime) -> Result<String, E>, E: ::std::fmt::Debug
-{
- use naive::date;
-
- assert_eq!(
- to_string(&NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90)).ok(),
- Some(r#""2016-07-08T09:10:48.090""#.into()));
- assert_eq!(
- to_string(&NaiveDate::from_ymd(2014, 7, 24).and_hms(12, 34, 6)).ok(),
- Some(r#""2014-07-24T12:34:06""#.into()));
- assert_eq!(
- to_string(&NaiveDate::from_ymd(0, 1, 1).and_hms_milli(0, 0, 59, 1_000)).ok(),
- Some(r#""0000-01-01T00:00:60""#.into()));
- assert_eq!(
- 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()));
- assert_eq!(
- to_string(&date::MIN.and_hms(0, 0, 0)).ok(),
- Some(r#""-262144-01-01T00:00:00""#.into()));
- assert_eq!(
- to_string(&date::MAX.and_hms_nano(23, 59, 59, 1_999_999_999)).ok(),
- Some(r#""+262143-12-31T23:59:60.999999999""#.into()));
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_decodable_json<F, E>(from_str: F)
- where F: Fn(&str) -> Result<NaiveDateTime, E>, E: ::std::fmt::Debug
-{
- use naive::date;
-
- assert_eq!(
- from_str(r#""2016-07-08T09:10:48.090""#).ok(),
- Some(NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90)));
- assert_eq!(
- from_str(r#""2016-7-8T9:10:48.09""#).ok(),
- Some(NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90)));
- assert_eq!(
- from_str(r#""2014-07-24T12:34:06""#).ok(),
- Some(NaiveDate::from_ymd(2014, 7, 24).and_hms(12, 34, 6)));
- assert_eq!(
- from_str(r#""0000-01-01T00:00:60""#).ok(),
- Some(NaiveDate::from_ymd(0, 1, 1).and_hms_milli(0, 0, 59, 1_000)));
- assert_eq!(
- from_str(r#""0-1-1T0:0:60""#).ok(),
- Some(NaiveDate::from_ymd(0, 1, 1).and_hms_milli(0, 0, 59, 1_000)));
- assert_eq!(
- from_str(r#""-0001-12-31T23:59:59.000000007""#).ok(),
- Some(NaiveDate::from_ymd(-1, 12, 31).and_hms_nano(23, 59, 59, 7)));
- assert_eq!(
- from_str(r#""-262144-01-01T00:00:00""#).ok(),
- Some(date::MIN.and_hms(0, 0, 0)));
- assert_eq!(
- from_str(r#""+262143-12-31T23:59:60.999999999""#).ok(),
- Some(date::MAX.and_hms_nano(23, 59, 59, 1_999_999_999)));
- assert_eq!(
- 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)));
-
- // bad formats
- assert!(from_str(r#""""#).is_err());
- assert!(from_str(r#""2016-07-08""#).is_err());
- assert!(from_str(r#""09:10:48.090""#).is_err());
- assert!(from_str(r#""20160708T091048.090""#).is_err());
- assert!(from_str(r#""2000-00-00T00:00:00""#).is_err());
- assert!(from_str(r#""2000-02-30T00:00:00""#).is_err());
- assert!(from_str(r#""2001-02-29T00:00:00""#).is_err());
- assert!(from_str(r#""2002-02-28T24:00:00""#).is_err());
- assert!(from_str(r#""2002-02-28T23:60:00""#).is_err());
- assert!(from_str(r#""2002-02-28T23:59:61""#).is_err());
- assert!(from_str(r#""2016-07-08T09:10:48,090""#).is_err());
- assert!(from_str(r#""2016-07-08 09:10:48.090""#).is_err());
- assert!(from_str(r#""2016-007-08T09:10:48.090""#).is_err());
- assert!(from_str(r#""yyyy-mm-ddThh:mm:ss.fffffffff""#).is_err());
- assert!(from_str(r#"0"#).is_err());
- assert!(from_str(r#"20160708000000"#).is_err());
- assert!(from_str(r#"{}"#).is_err());
- // pre-0.3.0 rustc-serialize format is now invalid
- assert!(from_str(r#"{"date":{"ymdf":20},"time":{"secs":0,"frac":0}}"#).is_err());
- assert!(from_str(r#"null"#).is_err());
-}
-
-#[cfg(feature = "rustc-serialize")]
-mod rustc_serialize {
- use super::NaiveDateTime;
- use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
-
- impl Encodable for NaiveDateTime {
- fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
- format!("{:?}", self).encode(s)
- }
- }
-
- impl Decodable for NaiveDateTime {
- fn decode<D: Decoder>(d: &mut D) -> Result<NaiveDateTime, D::Error> {
- d.read_str()?.parse().map_err(|_| d.error("invalid date and time"))
- }
- }
-
- #[cfg(test)] use rustc_serialize::json;
-
- #[test]
- fn test_encodable() {
- super::test_encodable_json(json::encode);
- }
-
- #[test]
- fn test_decodable() {
- super::test_decodable_json(json::decode);
- }
-}
-
-#[cfg(feature = "serde")]
-mod serde {
- use std::fmt;
- use super::NaiveDateTime;
- use serde::{ser, de};
-
- // TODO not very optimized for space (binary formats would want something better)
-
- impl ser::Serialize for NaiveDateTime {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where S: ser::Serializer
- {
- serializer.serialize_str(&format!("{:?}", self))
- }
- }
-
- struct NaiveDateTimeVisitor;
-
- impl de::Visitor for NaiveDateTimeVisitor {
- type Value = NaiveDateTime;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result
- {
- write!(formatter, "a formatted date and time string")
- }
-
- fn visit_str<E>(self, value: &str) -> Result<NaiveDateTime, E>
- where E: de::Error
- {
- value.parse().map_err(|err| E::custom(format!("{}", err)))
- }
- }
-
- impl de::Deserialize for NaiveDateTime {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where D: de::Deserializer
- {
- deserializer.deserialize_str(NaiveDateTimeVisitor)
- }
- }
-
- #[cfg(test)] extern crate serde_json;
- #[cfg(test)] extern crate bincode;
-
- #[test]
- fn test_serde_serialize() {
- super::test_encodable_json(self::serde_json::to_string);
- }
-
- #[test]
- fn test_serde_deserialize() {
- super::test_decodable_json(self::serde_json::from_str);
- }
-
- #[test]
- fn test_serde_bincode() {
- // Bincode is relevant to test separately from JSON because
- // it is not self-describing.
- use naive::date::NaiveDate;
- use self::bincode::SizeLimit;
- use self::bincode::serde::{serialize, deserialize};
-
- let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90);
- let encoded = serialize(&dt, SizeLimit::Infinite).unwrap();
- let decoded: NaiveDateTime = deserialize(&encoded).unwrap();
- assert_eq!(dt, decoded);
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::NaiveDateTime;
- use Datelike;
- use naive::date as naive_date;
- use naive::date::NaiveDate;
- use std::i64;
- use oldtime::Duration;
-
- #[test]
- fn test_datetime_from_timestamp() {
- let from_timestamp = |secs| NaiveDateTime::from_timestamp_opt(secs, 0);
- let ymdhms = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s);
- assert_eq!(from_timestamp(-1), Some(ymdhms(1969, 12, 31, 23, 59, 59)));
- assert_eq!(from_timestamp(0), Some(ymdhms(1970, 1, 1, 0, 0, 0)));
- assert_eq!(from_timestamp(1), Some(ymdhms(1970, 1, 1, 0, 0, 1)));
- assert_eq!(from_timestamp(1_000_000_000), Some(ymdhms(2001, 9, 9, 1, 46, 40)));
- assert_eq!(from_timestamp(0x7fffffff), Some(ymdhms(2038, 1, 19, 3, 14, 7)));
- assert_eq!(from_timestamp(i64::MIN), None);
- assert_eq!(from_timestamp(i64::MAX), None);
- }
-
- #[test]
- fn test_datetime_add() {
- fn check((y,m,d,h,n,s): (i32,u32,u32,u32,u32,u32), rhs: Duration,
- result: Option<(i32,u32,u32,u32,u32,u32)>) {
- let lhs = NaiveDate::from_ymd(y, m, d).and_hms(h, n, s);
- let sum = result.map(|(y,m,d,h,n,s)| NaiveDate::from_ymd(y, m, d).and_hms(h, n, s));
- assert_eq!(lhs.checked_add_signed(rhs), sum);
- assert_eq!(lhs.checked_sub_signed(-rhs), sum);
- };
-
- check((2014,5,6, 7,8,9), Duration::seconds(3600 + 60 + 1), Some((2014,5,6, 8,9,10)));
- check((2014,5,6, 7,8,9), Duration::seconds(-(3600 + 60 + 1)), Some((2014,5,6, 6,7,8)));
- check((2014,5,6, 7,8,9), Duration::seconds(86399), Some((2014,5,7, 7,8,8)));
- check((2014,5,6, 7,8,9), Duration::seconds(86400 * 10), Some((2014,5,16, 7,8,9)));
- check((2014,5,6, 7,8,9), Duration::seconds(-86400 * 10), Some((2014,4,26, 7,8,9)));
- check((2014,5,6, 7,8,9), Duration::seconds(86400 * 10), Some((2014,5,16, 7,8,9)));
-
- // overflow check
- // 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.)
- let max_days_from_year_0 =
- 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((naive_date::MAX.year(),12,31, 0,0,0)));
- 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)));
- 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);
-
- let min_days_from_year_0 =
- 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((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), Duration::min_value(), None);
- }
-
- #[test]
- fn test_datetime_sub() {
- let ymdhms = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s);
- let since = NaiveDateTime::signed_duration_since;
- assert_eq!(since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 9)),
- Duration::zero());
- assert_eq!(since(ymdhms(2014, 5, 6, 7, 8, 10), ymdhms(2014, 5, 6, 7, 8, 9)),
- Duration::seconds(1));
- assert_eq!(since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)),
- Duration::seconds(-1));
- assert_eq!(since(ymdhms(2014, 5, 7, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)),
- Duration::seconds(86399));
- assert_eq!(since(ymdhms(2001, 9, 9, 1, 46, 39), ymdhms(1970, 1, 1, 0, 0, 0)),
- Duration::seconds(999_999_999));
- }
-
- #[test]
- fn test_datetime_timestamp() {
- let to_timestamp = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s).timestamp();
- assert_eq!(to_timestamp(1969, 12, 31, 23, 59, 59), -1);
- assert_eq!(to_timestamp(1970, 1, 1, 0, 0, 0), 0);
- assert_eq!(to_timestamp(1970, 1, 1, 0, 0, 1), 1);
- assert_eq!(to_timestamp(2001, 9, 9, 1, 46, 40), 1_000_000_000);
- assert_eq!(to_timestamp(2038, 1, 19, 3, 14, 7), 0x7fffffff);
- }
-
- #[test]
- fn test_datetime_from_str() {
- // valid cases
- let valid = [
- "2015-2-18T23:16:9.15",
- "-77-02-18T23:16:09",
- " +82701 - 05 - 6 T 15 : 9 : 60.898989898989 ",
- ];
- for &s in &valid {
- let d = match s.parse::<NaiveDateTime>() {
- Ok(d) => d,
- Err(e) => panic!("parsing `{}` has failed: {}", s, e)
- };
- let s_ = format!("{:?}", d);
- // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
- let d_ = match s_.parse::<NaiveDateTime>() {
- Ok(d) => d,
- Err(e) => panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}",
- s, d, e)
- };
- assert!(d == d_, "`{}` is parsed into `{:?}`, but reparsed result \
- `{:?}` does not match", s, d, d_);
- }
-
- // some invalid cases
- // since `ParseErrorKind` is private, all we can do is to check if there was an error
- assert!("".parse::<NaiveDateTime>().is_err());
- assert!("x".parse::<NaiveDateTime>().is_err());
- assert!("15".parse::<NaiveDateTime>().is_err());
- assert!("15:8:9".parse::<NaiveDateTime>().is_err());
- assert!("15-8-9".parse::<NaiveDateTime>().is_err());
- assert!("2015-15-15T15:15:15".parse::<NaiveDateTime>().is_err());
- assert!("2012-12-12T12:12:12x".parse::<NaiveDateTime>().is_err());
- assert!("2012-123-12T12:12:12".parse::<NaiveDateTime>().is_err());
- assert!("+ 82701-123-12T12:12:12".parse::<NaiveDateTime>().is_err());
- assert!("+802701-123-12T12:12:12".parse::<NaiveDateTime>().is_err()); // out-of-bound
- }
-
- #[test]
- fn test_datetime_parse_from_str() {
- let ymdhms = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s);
- assert_eq!(NaiveDateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- Ok(ymdhms(2014, 5, 7, 12, 34, 56))); // ignore offset
- assert_eq!(NaiveDateTime::parse_from_str("2015-W06-1 000000", "%G-W%V-%u%H%M%S"),
- Ok(ymdhms(2015, 2, 2, 0, 0, 0)));
- assert_eq!(NaiveDateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT",
- "%a, %d %b %Y %H:%M:%S GMT"),
- Ok(ymdhms(2013, 8, 9, 23, 54, 35)));
- assert!(NaiveDateTime::parse_from_str("Sat, 09 Aug 2013 23:54:35 GMT",
- "%a, %d %b %Y %H:%M:%S GMT").is_err());
- assert!(NaiveDateTime::parse_from_str("2014-5-7 12:3456", "%Y-%m-%d %H:%M:%S").is_err());
- assert!(NaiveDateTime::parse_from_str("12:34:56", "%H:%M:%S").is_err()); // insufficient
- }
-
- #[test]
- fn test_datetime_format() {
- let dt = NaiveDate::from_ymd(2010, 9, 8).and_hms_milli(7, 6, 54, 321);
- assert_eq!(dt.format("%c").to_string(), "Wed Sep 8 07:06:54 2010");
- assert_eq!(dt.format("%s").to_string(), "1283929614");
- assert_eq!(dt.format("%t%n%%%n%t").to_string(), "\t\n%\n\t");
-
- // a horror of leap second: coming near to you.
- let dt = NaiveDate::from_ymd(2012, 6, 30).and_hms_milli(23, 59, 59, 1_000);
- assert_eq!(dt.format("%c").to_string(), "Sat Jun 30 23:59:60 2012");
- assert_eq!(dt.format("%s").to_string(), "1341100799"); // not 1341100800, it's intentional.
- }
-
- #[test]
- fn test_datetime_add_sub_invariant() { // issue #37
- let base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0);
- let t = -946684799990000;
- let time = base + Duration::microseconds(t);
- assert_eq!(t, time.signed_duration_since(base).num_microseconds().unwrap());
- }
-}
diff --git a/vendor/chrono-0.3.0/src/naive/time.rs b/vendor/chrono-0.3.0/src/naive/time.rs
deleted file mode 100644
index 7becf85..0000000
--- a/vendor/chrono-0.3.0/src/naive/time.rs
+++ /dev/null
@@ -1,1638 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! 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::ops::{Add, Sub};
-use oldtime::Duration as OldDuration;
-
-use Timelike;
-use div::div_mod_floor;
-use format::{Item, Numeric, Pad, Fixed};
-use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems};
-
-/// ISO 8601 time without timezone.
-/// Allows for the nanosecond precision and optional leap second representation.
-///
-/// <a name="leap-second-what?"></a>
-/// Chrono has a notable policy on the [leap second handling](./index.html#leap-second-handling),
-/// designed to be maximally useful for typical users.
-#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
-pub struct NaiveTime {
- secs: u32,
- frac: u32,
-}
-
-impl NaiveTime {
- /// Makes a new `NaiveTime` from hour, minute and second.
- ///
- /// No [leap second](./index.html#leap-second-handling) is allowed here;
- /// use `NaiveTime::from_hms_*` methods with a subsecond parameter instead.
- ///
- /// Panics on invalid hour, minute and/or second.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let t = NaiveTime::from_hms(23, 56, 4);
- /// assert_eq!(t.hour(), 23);
- /// assert_eq!(t.minute(), 56);
- /// assert_eq!(t.second(), 4);
- /// assert_eq!(t.nanosecond(), 0);
- /// ~~~~
- #[inline]
- pub fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime {
- NaiveTime::from_hms_opt(hour, min, sec).expect("invalid time")
- }
-
- /// Makes a new `NaiveTime` from hour, minute and second.
- ///
- /// No [leap second](./index.html#leap-second-handling) is allowed here;
- /// use `NaiveTime::from_hms_*_opt` methods with a subsecond parameter instead.
- ///
- /// Returns `None` on invalid hour, minute and/or second.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let from_hms_opt = NaiveTime::from_hms_opt;
- ///
- /// assert!(from_hms_opt(0, 0, 0).is_some());
- /// assert!(from_hms_opt(23, 59, 59).is_some());
- /// assert!(from_hms_opt(24, 0, 0).is_none());
- /// assert!(from_hms_opt(23, 60, 0).is_none());
- /// assert!(from_hms_opt(23, 59, 60).is_none());
- /// ~~~~
- #[inline]
- pub fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<NaiveTime> {
- NaiveTime::from_hms_nano_opt(hour, min, sec, 0)
- }
-
- /// Makes a new `NaiveTime` from hour, minute, second and millisecond.
- ///
- /// The millisecond part can exceed 1,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Panics on invalid hour, minute, second and/or millisecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let t = NaiveTime::from_hms_milli(23, 56, 4, 12);
- /// assert_eq!(t.hour(), 23);
- /// assert_eq!(t.minute(), 56);
- /// assert_eq!(t.second(), 4);
- /// assert_eq!(t.nanosecond(), 12_000_000);
- /// ~~~~
- #[inline]
- pub fn from_hms_milli(hour: u32, min: u32, sec: u32, milli: u32) -> NaiveTime {
- NaiveTime::from_hms_milli_opt(hour, min, sec, milli).expect("invalid time")
- }
-
- /// Makes a new `NaiveTime` from hour, minute, second and millisecond.
- ///
- /// The millisecond part can exceed 1,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid hour, minute, second and/or millisecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let from_hmsm_opt = NaiveTime::from_hms_milli_opt;
- ///
- /// assert!(from_hmsm_opt(0, 0, 0, 0).is_some());
- /// assert!(from_hmsm_opt(23, 59, 59, 999).is_some());
- /// assert!(from_hmsm_opt(23, 59, 59, 1_999).is_some()); // a leap second after 23:59:59
- /// assert!(from_hmsm_opt(24, 0, 0, 0).is_none());
- /// assert!(from_hmsm_opt(23, 60, 0, 0).is_none());
- /// assert!(from_hmsm_opt(23, 59, 60, 0).is_none());
- /// assert!(from_hmsm_opt(23, 59, 59, 2_000).is_none());
- /// ~~~~
- #[inline]
- pub fn from_hms_milli_opt(hour: u32, min: u32, sec: u32, milli: u32) -> Option<NaiveTime> {
- milli.checked_mul(1_000_000)
- .and_then(|nano| NaiveTime::from_hms_nano_opt(hour, min, sec, nano))
- }
-
- /// Makes a new `NaiveTime` from hour, minute, second and microsecond.
- ///
- /// The microsecond part can exceed 1,000,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Panics on invalid hour, minute, second and/or microsecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let t = NaiveTime::from_hms_micro(23, 56, 4, 12_345);
- /// assert_eq!(t.hour(), 23);
- /// assert_eq!(t.minute(), 56);
- /// assert_eq!(t.second(), 4);
- /// assert_eq!(t.nanosecond(), 12_345_000);
- /// ~~~~
- #[inline]
- pub fn from_hms_micro(hour: u32, min: u32, sec: u32, micro: u32) -> NaiveTime {
- NaiveTime::from_hms_micro_opt(hour, min, sec, micro).expect("invalid time")
- }
-
- /// Makes a new `NaiveTime` from hour, minute, second and microsecond.
- ///
- /// The microsecond part can exceed 1,000,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid hour, minute, second and/or microsecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let from_hmsu_opt = NaiveTime::from_hms_micro_opt;
- ///
- /// assert!(from_hmsu_opt(0, 0, 0, 0).is_some());
- /// assert!(from_hmsu_opt(23, 59, 59, 999_999).is_some());
- /// assert!(from_hmsu_opt(23, 59, 59, 1_999_999).is_some()); // a leap second after 23:59:59
- /// assert!(from_hmsu_opt(24, 0, 0, 0).is_none());
- /// assert!(from_hmsu_opt(23, 60, 0, 0).is_none());
- /// assert!(from_hmsu_opt(23, 59, 60, 0).is_none());
- /// assert!(from_hmsu_opt(23, 59, 59, 2_000_000).is_none());
- /// ~~~~
- #[inline]
- pub fn from_hms_micro_opt(hour: u32, min: u32, sec: u32, micro: u32) -> Option<NaiveTime> {
- micro.checked_mul(1_000)
- .and_then(|nano| NaiveTime::from_hms_nano_opt(hour, min, sec, nano))
- }
-
- /// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
- ///
- /// The nanosecond part can exceed 1,000,000,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Panics on invalid hour, minute, second and/or nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let t = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(t.hour(), 23);
- /// assert_eq!(t.minute(), 56);
- /// assert_eq!(t.second(), 4);
- /// assert_eq!(t.nanosecond(), 12_345_678);
- /// ~~~~
- #[inline]
- pub fn from_hms_nano(hour: u32, min: u32, sec: u32, nano: u32) -> NaiveTime {
- NaiveTime::from_hms_nano_opt(hour, min, sec, nano).expect("invalid time")
- }
-
- /// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
- ///
- /// The nanosecond part can exceed 1,000,000,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid hour, minute, second and/or nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let from_hmsn_opt = NaiveTime::from_hms_nano_opt;
- ///
- /// assert!(from_hmsn_opt(0, 0, 0, 0).is_some());
- /// assert!(from_hmsn_opt(23, 59, 59, 999_999_999).is_some());
- /// assert!(from_hmsn_opt(23, 59, 59, 1_999_999_999).is_some()); // a leap second after 23:59:59
- /// assert!(from_hmsn_opt(24, 0, 0, 0).is_none());
- /// assert!(from_hmsn_opt(23, 60, 0, 0).is_none());
- /// assert!(from_hmsn_opt(23, 59, 60, 0).is_none());
- /// assert!(from_hmsn_opt(23, 59, 59, 2_000_000_000).is_none());
- /// ~~~~
- #[inline]
- pub fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveTime> {
- if hour >= 24 || min >= 60 || sec >= 60 || nano >= 2_000_000_000 { return None; }
- let secs = hour * 3600 + min * 60 + sec;
- Some(NaiveTime { secs: secs, frac: nano })
- }
-
- /// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
- ///
- /// The nanosecond part can exceed 1,000,000,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Panics on invalid number of seconds and/or nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let t = NaiveTime::from_num_seconds_from_midnight(86164, 12_345_678);
- /// assert_eq!(t.hour(), 23);
- /// assert_eq!(t.minute(), 56);
- /// assert_eq!(t.second(), 4);
- /// assert_eq!(t.nanosecond(), 12_345_678);
- /// ~~~~
- #[inline]
- pub fn from_num_seconds_from_midnight(secs: u32, nano: u32) -> NaiveTime {
- NaiveTime::from_num_seconds_from_midnight_opt(secs, nano).expect("invalid time")
- }
-
- /// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
- ///
- /// The nanosecond part can exceed 1,000,000,000
- /// in order to represent the [leap second](./index.html#leap-second-handling).
- ///
- /// Returns `None` on invalid number of seconds and/or nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let from_nsecs_opt = NaiveTime::from_num_seconds_from_midnight_opt;
- ///
- /// assert!(from_nsecs_opt(0, 0).is_some());
- /// assert!(from_nsecs_opt(86399, 999_999_999).is_some());
- /// assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59
- /// assert!(from_nsecs_opt(86400, 0).is_none());
- /// assert!(from_nsecs_opt(86399, 2_000_000_000).is_none());
- /// ~~~~
- #[inline]
- pub fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> {
- if secs >= 86400 || nano >= 2_000_000_000 { return None; }
- Some(NaiveTime { secs: secs, frac: nano })
- }
-
- /// Parses a string with the specified format string and returns a new `NaiveTime`.
- /// See the [`format::strftime` module](../../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let parse_from_str = NaiveTime::parse_from_str;
- ///
- /// assert_eq!(parse_from_str("23:56:04", "%H:%M:%S"),
- /// Ok(NaiveTime::from_hms(23, 56, 4)));
- /// assert_eq!(parse_from_str("pm012345.6789", "%p%I%M%S%.f"),
- /// Ok(NaiveTime::from_hms_micro(13, 23, 45, 678_900)));
- /// ~~~~
- ///
- /// Date and offset is ignored for the purpose of parsing.
- ///
- /// ~~~~
- /// # use chrono::NaiveTime;
- /// # let parse_from_str = NaiveTime::parse_from_str;
- /// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- /// Ok(NaiveTime::from_hms(12, 34, 56)));
- /// ~~~~
- ///
- /// [Leap seconds](./index.html#leap-second-handling) are correctly handled by
- /// treating any time of the form `hh:mm:60` as a leap second.
- /// (This equally applies to the formatting, so the round trip is possible.)
- ///
- /// ~~~~
- /// # use chrono::NaiveTime;
- /// # let parse_from_str = NaiveTime::parse_from_str;
- /// assert_eq!(parse_from_str("08:59:60.123", "%H:%M:%S%.f"),
- /// Ok(NaiveTime::from_hms_milli(8, 59, 59, 1_123)));
- /// ~~~~
- ///
- /// Missing seconds are assumed to be zero,
- /// but out-of-bound times or insufficient fields are errors otherwise.
- ///
- /// ~~~~
- /// # use chrono::NaiveTime;
- /// # let parse_from_str = NaiveTime::parse_from_str;
- /// assert_eq!(parse_from_str("7:15", "%H:%M"),
- /// Ok(NaiveTime::from_hms(7, 15, 0)));
- ///
- /// assert!(parse_from_str("04m33s", "%Mm%Ss").is_err());
- /// assert!(parse_from_str("12", "%H").is_err());
- /// assert!(parse_from_str("17:60", "%H:%M").is_err());
- /// assert!(parse_from_str("24:00:00", "%H:%M:%S").is_err());
- /// ~~~~
- ///
- /// All parsed fields should be consistent to each other, otherwise it's an error.
- /// Here `%H` is for 24-hour clocks, unlike `%I`,
- /// and thus can be independently determined without AM/PM.
- ///
- /// ~~~~
- /// # use chrono::NaiveTime;
- /// # let parse_from_str = NaiveTime::parse_from_str;
- /// assert!(parse_from_str("13:07 AM", "%H:%M %p").is_err());
- /// ~~~~
- pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveTime> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, StrftimeItems::new(fmt)));
- parsed.to_naive_time()
- }
-
- /// Adds given `Duration` to the current time,
- /// and also returns the number of *seconds*
- /// in the integral number of days ignored from the addition.
- /// (We cannot return `Duration` because it is subject to overflow or underflow.)
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveTime;
- /// use time::Duration;
- ///
- /// let from_hms = NaiveTime::from_hms;
- ///
- /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(11)),
- /// (from_hms(14, 4, 5), 0));
- /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(23)),
- /// (from_hms(2, 4, 5), 86400));
- /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(-7)),
- /// (from_hms(20, 4, 5), -86400));
- /// # }
- /// ~~~~
- pub fn overflowing_add_signed(&self, mut rhs: OldDuration) -> (NaiveTime, i64) {
- let mut secs = self.secs;
- let mut frac = self.frac;
-
- // check if `self` is a leap second and adding `rhs` would escape that leap second.
- // if it's the case, update `self` and `rhs` to involve no leap second;
- // otherwise the addition immediately finishes.
- if frac >= 1_000_000_000 {
- let rfrac = 2_000_000_000 - frac;
- if rhs >= OldDuration::nanoseconds(rfrac as i64) {
- rhs = rhs - OldDuration::nanoseconds(rfrac as i64);
- secs += 1;
- frac = 0;
- } else if rhs < OldDuration::nanoseconds(-(frac as i64)) {
- rhs = rhs + OldDuration::nanoseconds(frac as i64);
- frac = 0;
- } else {
- frac = (frac as i64 + rhs.num_nanoseconds().unwrap()) as u32;
- debug_assert!(frac < 2_000_000_000);
- return (NaiveTime { secs: secs, frac: frac }, 0);
- }
- }
- debug_assert!(secs <= 86400);
- debug_assert!(frac < 1_000_000_000);
-
- let rhssecs = rhs.num_seconds();
- let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap();
- debug_assert!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac) == rhs);
- let rhssecsinday = rhssecs % 86400;
- let mut morerhssecs = rhssecs - rhssecsinday;
- let rhssecs = rhssecsinday as i32;
- let rhsfrac = rhsfrac as i32;
- debug_assert!(-86400 < rhssecs && rhssecs < 86400);
- debug_assert!(morerhssecs % 86400 == 0);
- debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000);
-
- let mut secs = secs as i32 + rhssecs;
- let mut frac = frac as i32 + rhsfrac;
- debug_assert!(-86400 < secs && secs < 2 * 86400);
- debug_assert!(-1_000_000_000 < frac && frac < 2_000_000_000);
-
- if frac < 0 {
- frac += 1_000_000_000;
- secs -= 1;
- } else if frac >= 1_000_000_000 {
- frac -= 1_000_000_000;
- secs += 1;
- }
- debug_assert!(-86400 <= secs && secs < 2 * 86400);
- debug_assert!(0 <= frac && frac < 1_000_000_000);
-
- if secs < 0 {
- secs += 86400;
- morerhssecs -= 86400;
- } else if secs >= 86400 {
- secs -= 86400;
- morerhssecs += 86400;
- }
- debug_assert!(0 <= secs && secs < 86400);
-
- (NaiveTime { secs: secs as u32, frac: frac as u32 }, morerhssecs)
- }
-
- /// Subtracts given `Duration` from the current time,
- /// and also returns the number of *seconds*
- /// in the integral number of days ignored from the subtraction.
- /// (We cannot return `Duration` because it is subject to overflow or underflow.)
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveTime;
- /// use time::Duration;
- ///
- /// let from_hms = NaiveTime::from_hms;
- ///
- /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(2)),
- /// (from_hms(1, 4, 5), 0));
- /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(17)),
- /// (from_hms(10, 4, 5), 86400));
- /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(-22)),
- /// (from_hms(1, 4, 5), -86400));
- /// # }
- /// ~~~~
- #[inline]
- pub fn overflowing_sub_signed(&self, rhs: OldDuration) -> (NaiveTime, i64) {
- let (time, rhs) = self.overflowing_add_signed(-rhs);
- (time, -rhs) // safe to negate, rhs is within +/- (2^63 / 1000)
- }
-
- /// Subtracts another `NaiveTime` from the current time.
- /// Returns a `Duration` within +/- 1 day.
- /// This does not overflow or underflow at all.
- ///
- /// As a part of Chrono's [leap second handling](./index.html#leap-second-handling),
- /// the subtraction assumes that **there is no leap second ever**,
- /// except when any of the `NaiveTime`s themselves represents a leap second
- /// in which case the assumption becomes that
- /// **there are exactly one (or two) leap second(s) ever**.
- ///
- /// # Example
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// use chrono::NaiveTime;
- /// use time::Duration;
- ///
- /// let from_hmsm = NaiveTime::from_hms_milli;
- /// let since = NaiveTime::signed_duration_since;
- ///
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)),
- /// Duration::zero());
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
- /// Duration::milliseconds(25));
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
- /// Duration::milliseconds(975));
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)),
- /// Duration::seconds(7));
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)),
- /// Duration::seconds(5 * 60));
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
- /// Duration::seconds(3 * 3600));
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)),
- /// Duration::seconds(-3600));
- /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
- /// Duration::seconds(3600 + 60 + 1) + Duration::milliseconds(100));
- /// # }
- /// ~~~~
- ///
- /// Leap seconds are handled, but the subtraction assumes that
- /// there were no other leap seconds happened.
- ///
- /// ~~~~
- /// # extern crate chrono; extern crate time; fn main() {
- /// # use chrono::NaiveTime;
- /// # use time::Duration;
- /// # let from_hmsm = NaiveTime::from_hms_milli;
- /// # let since = NaiveTime::signed_duration_since;
- /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)),
- /// Duration::seconds(1));
- /// assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)),
- /// Duration::milliseconds(1500));
- /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)),
- /// Duration::seconds(60));
- /// assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)),
- /// Duration::seconds(1));
- /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)),
- /// Duration::seconds(61));
- /// # }
- /// ~~~~
- pub fn signed_duration_since(self, rhs: NaiveTime) -> OldDuration {
- // | | :leap| | | | | | | :leap| |
- // | | : | | | | | | | : | |
- // ----+----+-----*---+----+----+----+----+----+----+-------*-+----+----
- // | `rhs` | | `self`
- // |======================================>| |
- // | | `self.secs - rhs.secs` |`self.frac`
- // |====>| | |======>|
- // `rhs.frac`|========================================>|
- // | | | `self - rhs` | |
-
- use std::cmp::Ordering;
-
- let secs = self.secs as i64 - rhs.secs as i64;
- let frac = self.frac as i64 - rhs.frac as i64;
-
- // `secs` may contain a leap second yet to be counted
- let adjust = match self.secs.cmp(&rhs.secs) {
- Ordering::Greater => if rhs.frac >= 1_000_000_000 { 1 } else { 0 },
- Ordering::Equal => 0,
- Ordering::Less => if self.frac >= 1_000_000_000 { -1 } else { 0 },
- };
-
- OldDuration::seconds(secs + adjust) + OldDuration::nanoseconds(frac)
- }
-
- /// Formats the time with the specified formatting items.
- /// Otherwise it is same to the ordinary [`format`](#method.format) method.
- ///
- /// The `Iterator` of items should be `Clone`able,
- /// since the resulting `DelayedFormat` value may be formatted multiple times.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- /// use chrono::format::strftime::StrftimeItems;
- ///
- /// let fmt = StrftimeItems::new("%H:%M:%S");
- /// let t = NaiveTime::from_hms(23, 56, 4);
- /// assert_eq!(t.format_with_items(fmt.clone()).to_string(), "23:56:04");
- /// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
- /// ~~~~
- ///
- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
- ///
- /// ~~~~
- /// # use chrono::NaiveTime;
- /// # use chrono::format::strftime::StrftimeItems;
- /// # let fmt = StrftimeItems::new("%H:%M:%S").clone();
- /// # let t = NaiveTime::from_hms(23, 56, 4);
- /// assert_eq!(format!("{}", t.format_with_items(fmt)), "23:56:04");
- /// ~~~~
- #[inline]
- pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
- where I: Iterator<Item=Item<'a>> + Clone {
- DelayedFormat::new(None, Some(*self), items)
- }
-
- /// Formats the time with the specified format string.
- /// See the [`format::strftime` module](../../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// This returns a `DelayedFormat`,
- /// which gets converted to a string only when actual formatting happens.
- /// You may use the `to_string` method to get a `String`,
- /// or just feed it into `print!` and other formatting macros.
- /// (In this way it avoids the redundant memory allocation.)
- ///
- /// A wrong format string does *not* issue an error immediately.
- /// Rather, converting or formatting the `DelayedFormat` fails.
- /// You are recommended to immediately use `DelayedFormat` for this reason.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::NaiveTime;
- ///
- /// let t = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
- /// assert_eq!(t.format("%H:%M:%S%.6f").to_string(), "23:56:04.012345");
- /// assert_eq!(t.format("%-I:%M %p").to_string(), "11:56 PM");
- /// ~~~~
- ///
- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
- ///
- /// ~~~~
- /// # use chrono::NaiveTime;
- /// # let t = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(format!("{}", t.format("%H:%M:%S")), "23:56:04");
- /// assert_eq!(format!("{}", t.format("%H:%M:%S%.6f")), "23:56:04.012345");
- /// assert_eq!(format!("{}", t.format("%-I:%M %p")), "11:56 PM");
- /// ~~~~
- #[inline]
- pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
- self.format_with_items(StrftimeItems::new(fmt))
- }
-
- /// Returns a triple of the hour, minute and second numbers.
- fn hms(&self) -> (u32, u32, u32) {
- let (mins, sec) = div_mod_floor(self.secs, 60);
- let (hour, min) = div_mod_floor(mins, 60);
- (hour, min, sec)
- }
-}
-
-impl Timelike for NaiveTime {
- /// Returns the hour number from 0 to 23.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// assert_eq!(NaiveTime::from_hms(0, 0, 0).hour(), 0);
- /// assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).hour(), 23);
- /// ~~~~
- #[inline]
- fn hour(&self) -> u32 {
- self.hms().0
- }
-
- /// Returns the minute number from 0 to 59.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// assert_eq!(NaiveTime::from_hms(0, 0, 0).minute(), 0);
- /// assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).minute(), 56);
- /// ~~~~
- #[inline]
- fn minute(&self) -> u32 {
- self.hms().1
- }
-
- /// Returns the second number from 0 to 59.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// assert_eq!(NaiveTime::from_hms(0, 0, 0).second(), 0);
- /// assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).second(), 4);
- /// ~~~~
- ///
- /// This method never returns 60 even when it is a leap second.
- /// ([Why?](./index.html#leap-second-handling))
- /// Use the proper [formatting method](#method.format) to get a human-readable representation.
- ///
- /// ~~~~
- /// # use chrono::{NaiveTime, Timelike};
- /// let leap = NaiveTime::from_hms_milli(23, 59, 59, 1_000);
- /// assert_eq!(leap.second(), 59);
- /// assert_eq!(leap.format("%H:%M:%S").to_string(), "23:59:60");
- /// ~~~~
- #[inline]
- fn second(&self) -> u32 {
- self.hms().2
- }
-
- /// 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 [leap second](./naive/time/index.html#leap-second-handling).
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// assert_eq!(NaiveTime::from_hms(0, 0, 0).nanosecond(), 0);
- /// assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).nanosecond(), 12_345_678);
- /// ~~~~
- ///
- /// Leap seconds may have seemingly out-of-range return values.
- /// You can reduce the range with `time.nanosecond() % 1_000_000_000`, or
- /// use the proper [formatting method](#method.format) to get a human-readable representation.
- ///
- /// ~~~~
- /// # use chrono::{NaiveTime, Timelike};
- /// let leap = NaiveTime::from_hms_milli(23, 59, 59, 1_000);
- /// assert_eq!(leap.nanosecond(), 1_000_000_000);
- /// assert_eq!(leap.format("%H:%M:%S%.9f").to_string(), "23:59:60.000000000");
- /// ~~~~
- #[inline]
- fn nanosecond(&self) -> u32 {
- self.frac
- }
-
- /// Makes a new `NaiveTime` with the hour number changed.
- ///
- /// Returns `None` when the resulting `NaiveTime` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(dt.with_hour(7), Some(NaiveTime::from_hms_nano(7, 56, 4, 12_345_678)));
- /// assert_eq!(dt.with_hour(24), None);
- /// ~~~~
- #[inline]
- fn with_hour(&self, hour: u32) -> Option<NaiveTime> {
- if hour >= 24 { return None; }
- let secs = hour * 3600 + self.secs % 3600;
- Some(NaiveTime { secs: secs, ..*self })
- }
-
- /// Makes a new `NaiveTime` with the minute number changed.
- ///
- /// Returns `None` when the resulting `NaiveTime` would be invalid.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(dt.with_minute(45), Some(NaiveTime::from_hms_nano(23, 45, 4, 12_345_678)));
- /// assert_eq!(dt.with_minute(60), None);
- /// ~~~~
- #[inline]
- fn with_minute(&self, min: u32) -> Option<NaiveTime> {
- if min >= 60 { return None; }
- let secs = self.secs / 3600 * 3600 + min * 60 + self.secs % 60;
- Some(NaiveTime { secs: secs, ..*self })
- }
-
- /// Makes a new `NaiveTime` with the second number changed.
- ///
- /// Returns `None` when the resulting `NaiveTime` would be invalid.
- /// As with the [`second`](#method.second) method,
- /// the input range is restricted to 0 through 59.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(dt.with_second(17), Some(NaiveTime::from_hms_nano(23, 56, 17, 12_345_678)));
- /// assert_eq!(dt.with_second(60), None);
- /// ~~~~
- #[inline]
- fn with_second(&self, sec: u32) -> Option<NaiveTime> {
- if sec >= 60 { return None; }
- let secs = self.secs / 60 * 60 + sec;
- Some(NaiveTime { secs: secs, ..*self })
- }
-
- /// Makes a new `NaiveTime` with nanoseconds since the whole non-leap second changed.
- ///
- /// Returns `None` when the resulting `NaiveTime` would be invalid.
- /// As with the [`nanosecond`](#method.nanosecond) method,
- /// the input range can exceed 1,000,000,000 for leap seconds.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(dt.with_nanosecond(333_333_333),
- /// Some(NaiveTime::from_hms_nano(23, 56, 4, 333_333_333)));
- /// assert_eq!(dt.with_nanosecond(2_000_000_000), None);
- /// ~~~~
- ///
- /// Leap seconds can theoretically follow *any* whole second.
- /// The following would be a proper leap second at the time zone offset of UTC-00:03:57
- /// (there are several historical examples comparable to this "non-sense" offset),
- /// and therefore is allowed.
- ///
- /// ~~~~
- /// # use chrono::{NaiveTime, Timelike};
- /// # let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
- /// assert_eq!(dt.with_nanosecond(1_333_333_333),
- /// Some(NaiveTime::from_hms_nano(23, 56, 4, 1_333_333_333)));
- /// ~~~~
- #[inline]
- fn with_nanosecond(&self, nano: u32) -> Option<NaiveTime> {
- if nano >= 2_000_000_000 { return None; }
- Some(NaiveTime { frac: nano, ..*self })
- }
-
- /// Returns the number of non-leap seconds past the last midnight.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{NaiveTime, Timelike};
- ///
- /// assert_eq!(NaiveTime::from_hms(1, 2, 3).num_seconds_from_midnight(),
- /// 3723);
- /// assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).num_seconds_from_midnight(),
- /// 86164);
- /// assert_eq!(NaiveTime::from_hms_milli(23, 59, 59, 1_000).num_seconds_from_midnight(),
- /// 86399);
- /// ~~~~
- #[inline]
- fn num_seconds_from_midnight(&self) -> u32 {
- self.secs // do not repeat the calculation!
- }
-}
-
-/// `NaiveTime` can be used as a key to the hash maps (in principle).
-///
-/// Practically this also takes account of fractional seconds, so it is not recommended.
-/// (For the obvious reason this also distinguishes leap seconds from non-leap seconds.)
-impl hash::Hash for NaiveTime {
- fn hash<H: hash::Hasher>(&self, state: &mut H) {
- self.secs.hash(state);
- self.frac.hash(state);
- }
-}
-
-/// An addition of `Duration` to `NaiveTime` wraps around and never overflows or underflows.
-/// In particular the addition ignores integral number of days.
-///
-/// As a part of Chrono's [leap second handling](./index.html#leap-second-handling),
-/// the addition assumes that **there is no leap second ever**,
-/// except when the `NaiveTime` itself represents a leap second
-/// in which case the assumption becomes that **there is exactly a single leap second ever**.
-///
-/// # Example
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// use chrono::NaiveTime;
-/// use time::Duration;
-///
-/// let from_hmsm = NaiveTime::from_hms_milli;
-///
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::zero(), from_hmsm(3, 5, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(1), from_hmsm(3, 5, 8, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-1), from_hmsm(3, 5, 6, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(60 + 4), from_hmsm(3, 6, 11, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(7*60*60 - 6*60), from_hmsm(9, 59, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::milliseconds(80), from_hmsm(3, 5, 7, 80));
-/// assert_eq!(from_hmsm(3, 5, 7, 950) + Duration::milliseconds(280), from_hmsm(3, 5, 8, 230));
-/// assert_eq!(from_hmsm(3, 5, 7, 950) + Duration::milliseconds(-980), from_hmsm(3, 5, 6, 970));
-/// # }
-/// ~~~~
-///
-/// The addition wraps around.
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// # use chrono::NaiveTime;
-/// # use time::Duration;
-/// # let from_hmsm = NaiveTime::from_hms_milli;
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(22*60*60), from_hmsm(1, 5, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-8*60*60), from_hmsm(19, 5, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::days(800), from_hmsm(3, 5, 7, 0));
-/// # }
-/// ~~~~
-///
-/// Leap seconds are handled, but the addition assumes that it is the only leap second happened.
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// # use chrono::NaiveTime;
-/// # use time::Duration;
-/// # let from_hmsm = NaiveTime::from_hms_milli;
-/// let leap = from_hmsm(3, 5, 59, 1_300);
-/// assert_eq!(leap + Duration::zero(), from_hmsm(3, 5, 59, 1_300));
-/// assert_eq!(leap + Duration::milliseconds(-500), from_hmsm(3, 5, 59, 800));
-/// assert_eq!(leap + Duration::milliseconds(500), from_hmsm(3, 5, 59, 1_800));
-/// assert_eq!(leap + Duration::milliseconds(800), from_hmsm(3, 6, 0, 100));
-/// assert_eq!(leap + Duration::seconds(10), from_hmsm(3, 6, 9, 300));
-/// assert_eq!(leap + Duration::seconds(-10), from_hmsm(3, 5, 50, 300));
-/// assert_eq!(leap + Duration::days(1), from_hmsm(3, 5, 59, 300));
-/// # }
-/// ~~~~
-impl Add<OldDuration> for NaiveTime {
- type Output = NaiveTime;
-
- #[inline]
- fn add(self, rhs: OldDuration) -> NaiveTime {
- self.overflowing_add_signed(rhs).0
- }
-}
-
-/// A subtraction of `Duration` from `NaiveTime` wraps around and never overflows or underflows.
-/// In particular the addition ignores integral number of days.
-/// It is same to the addition with a negated `Duration`.
-///
-/// As a part of Chrono's [leap second handling](./index.html#leap-second-handling),
-/// the addition assumes that **there is no leap second ever**,
-/// except when the `NaiveTime` itself represents a leap second
-/// in which case the assumption becomes that **there is exactly a single leap second ever**.
-///
-/// # Example
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// use chrono::NaiveTime;
-/// use time::Duration;
-///
-/// let from_hmsm = NaiveTime::from_hms_milli;
-///
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::zero(), from_hmsm(3, 5, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(1), from_hmsm(3, 5, 6, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(60 + 5), from_hmsm(3, 4, 2, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(2*60*60 + 6*60), from_hmsm(0, 59, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::milliseconds(80), from_hmsm(3, 5, 6, 920));
-/// assert_eq!(from_hmsm(3, 5, 7, 950) - Duration::milliseconds(280), from_hmsm(3, 5, 7, 670));
-/// # }
-/// ~~~~
-///
-/// The subtraction wraps around.
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// # use chrono::NaiveTime;
-/// # use time::Duration;
-/// # let from_hmsm = NaiveTime::from_hms_milli;
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(8*60*60), from_hmsm(19, 5, 7, 0));
-/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::days(800), from_hmsm(3, 5, 7, 0));
-/// # }
-/// ~~~~
-///
-/// Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.
-///
-/// ~~~~
-/// # extern crate chrono; extern crate time; fn main() {
-/// # use chrono::NaiveTime;
-/// # use time::Duration;
-/// # let from_hmsm = NaiveTime::from_hms_milli;
-/// let leap = from_hmsm(3, 5, 59, 1_300);
-/// assert_eq!(leap - Duration::zero(), from_hmsm(3, 5, 59, 1_300));
-/// assert_eq!(leap - Duration::milliseconds(200), from_hmsm(3, 5, 59, 1_100));
-/// assert_eq!(leap - Duration::milliseconds(500), from_hmsm(3, 5, 59, 800));
-/// assert_eq!(leap - Duration::seconds(60), from_hmsm(3, 5, 0, 300));
-/// assert_eq!(leap - Duration::days(1), from_hmsm(3, 6, 0, 300));
-/// # }
-/// ~~~~
-impl Sub<OldDuration> for NaiveTime {
- type Output = NaiveTime;
-
- #[inline]
- fn sub(self, rhs: OldDuration) -> NaiveTime {
- self.overflowing_sub_signed(rhs).0
- }
-}
-
-/// The `Debug` output of the naive time `t` is same to
-/// [`t.format("%H:%M:%S%.f")`](../../format/strftime/index.html).
-///
-/// The string printed can be readily parsed via the `parse` method on `str`.
-///
-/// It should be noted that, for leap seconds not on the minute boundary,
-/// it may print a representation not distinguishable from non-leap seconds.
-/// This doesn't matter in practice, since such leap seconds never happened.
-/// (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.)
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveTime;
-///
-/// assert_eq!(format!("{:?}", NaiveTime::from_hms(23, 56, 4)), "23:56:04");
-/// assert_eq!(format!("{:?}", NaiveTime::from_hms_milli(23, 56, 4, 12)), "23:56:04.012");
-/// assert_eq!(format!("{:?}", NaiveTime::from_hms_micro(23, 56, 4, 1234)), "23:56:04.001234");
-/// assert_eq!(format!("{:?}", NaiveTime::from_hms_nano(23, 56, 4, 123456)), "23:56:04.000123456");
-/// ~~~~
-///
-/// Leap seconds may also be used.
-///
-/// ~~~~
-/// # use chrono::NaiveTime;
-/// assert_eq!(format!("{:?}", NaiveTime::from_hms_milli(6, 59, 59, 1_500)), "06:59:60.500");
-/// ~~~~
-impl fmt::Debug for NaiveTime {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let (hour, min, sec) = self.hms();
- let (sec, nano) = if self.frac >= 1_000_000_000 {
- (sec + 1, self.frac - 1_000_000_000)
- } else {
- (sec, self.frac)
- };
-
- try!(write!(f, "{:02}:{:02}:{:02}", hour, min, sec));
- if nano == 0 {
- Ok(())
- } else if nano % 1_000_000 == 0 {
- write!(f, ".{:03}", nano / 1_000_000)
- } else if nano % 1_000 == 0 {
- write!(f, ".{:06}", nano / 1_000)
- } else {
- write!(f, ".{:09}", nano)
- }
- }
-}
-
-/// The `Display` output of the naive time `t` is same to
-/// [`t.format("%H:%M:%S%.f")`](../../format/strftime/index.html).
-///
-/// The string printed can be readily parsed via the `parse` method on `str`.
-///
-/// It should be noted that, for leap seconds not on the minute boundary,
-/// it may print a representation not distinguishable from non-leap seconds.
-/// This doesn't matter in practice, since such leap seconds never happened.
-/// (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.)
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveTime;
-///
-/// assert_eq!(format!("{}", NaiveTime::from_hms(23, 56, 4)), "23:56:04");
-/// assert_eq!(format!("{}", NaiveTime::from_hms_milli(23, 56, 4, 12)), "23:56:04.012");
-/// assert_eq!(format!("{}", NaiveTime::from_hms_micro(23, 56, 4, 1234)), "23:56:04.001234");
-/// assert_eq!(format!("{}", NaiveTime::from_hms_nano(23, 56, 4, 123456)), "23:56:04.000123456");
-/// ~~~~
-///
-/// Leap seconds may also be used.
-///
-/// ~~~~
-/// # use chrono::NaiveTime;
-/// assert_eq!(format!("{}", NaiveTime::from_hms_milli(6, 59, 59, 1_500)), "06:59:60.500");
-/// ~~~~
-impl fmt::Display for NaiveTime {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self, f) }
-}
-
-/// Parsing a `str` into a `NaiveTime` uses the same format,
-/// [`%H:%M:%S%.f`](../../format/strftime/index.html), as in `Debug` and `Display`.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::NaiveTime;
-///
-/// let t = NaiveTime::from_hms(23, 56, 4);
-/// assert_eq!("23:56:04".parse::<NaiveTime>(), Ok(t));
-///
-/// let t = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
-/// assert_eq!("23:56:4.012345678".parse::<NaiveTime>(), Ok(t));
-///
-/// let t = NaiveTime::from_hms_nano(23, 59, 59, 1_234_567_890); // leap second
-/// assert_eq!("23:59:60.23456789".parse::<NaiveTime>(), Ok(t));
-///
-/// assert!("foo".parse::<NaiveTime>().is_err());
-/// ~~~~
-impl str::FromStr for NaiveTime {
- type Err = ParseError;
-
- fn from_str(s: &str) -> ParseResult<NaiveTime> {
- const ITEMS: &'static [Item<'static>] = &[
- Item::Space(""), Item::Numeric(Numeric::Hour, Pad::Zero),
- Item::Space(""), Item::Literal(":"),
- Item::Space(""), Item::Numeric(Numeric::Minute, Pad::Zero),
- Item::Space(""), Item::Literal(":"),
- Item::Space(""), Item::Numeric(Numeric::Second, Pad::Zero),
- Item::Fixed(Fixed::Nanosecond), Item::Space(""),
- ];
-
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, ITEMS.iter().cloned()));
- parsed.to_naive_time()
- }
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_encodable_json<F, E>(to_string: F)
- where F: Fn(&NaiveTime) -> Result<String, E>, E: ::std::fmt::Debug
-{
- assert_eq!(to_string(&NaiveTime::from_hms(0, 0, 0)).ok(),
- Some(r#""00:00:00""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms_milli(0, 0, 0, 950)).ok(),
- Some(r#""00:00:00.950""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms_milli(0, 0, 59, 1_000)).ok(),
- Some(r#""00:00:60""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms(0, 1, 2)).ok(),
- Some(r#""00:01:02""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms_nano(3, 5, 7, 98765432)).ok(),
- Some(r#""03:05:07.098765432""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms(7, 8, 9)).ok(),
- Some(r#""07:08:09""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms_micro(12, 34, 56, 789)).ok(),
- Some(r#""12:34:56.000789""#.into()));
- assert_eq!(to_string(&NaiveTime::from_hms_nano(23, 59, 59, 1_999_999_999)).ok(),
- Some(r#""23:59:60.999999999""#.into()));
-}
-
-#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
-fn test_decodable_json<F, E>(from_str: F)
- where F: Fn(&str) -> Result<NaiveTime, E>, E: ::std::fmt::Debug
-{
- assert_eq!(from_str(r#""00:00:00""#).ok(),
- Some(NaiveTime::from_hms(0, 0, 0)));
- assert_eq!(from_str(r#""0:0:0""#).ok(),
- Some(NaiveTime::from_hms(0, 0, 0)));
- assert_eq!(from_str(r#""00:00:00.950""#).ok(),
- Some(NaiveTime::from_hms_milli(0, 0, 0, 950)));
- assert_eq!(from_str(r#""0:0:0.95""#).ok(),
- Some(NaiveTime::from_hms_milli(0, 0, 0, 950)));
- assert_eq!(from_str(r#""00:00:60""#).ok(),
- Some(NaiveTime::from_hms_milli(0, 0, 59, 1_000)));
- assert_eq!(from_str(r#""00:01:02""#).ok(),
- Some(NaiveTime::from_hms(0, 1, 2)));
- assert_eq!(from_str(r#""03:05:07.098765432""#).ok(),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 98765432)));
- assert_eq!(from_str(r#""07:08:09""#).ok(),
- Some(NaiveTime::from_hms(7, 8, 9)));
- assert_eq!(from_str(r#""12:34:56.000789""#).ok(),
- Some(NaiveTime::from_hms_micro(12, 34, 56, 789)));
- assert_eq!(from_str(r#""23:59:60.999999999""#).ok(),
- Some(NaiveTime::from_hms_nano(23, 59, 59, 1_999_999_999)));
- assert_eq!(from_str(r#""23:59:60.9999999999997""#).ok(), // excess digits are ignored
- Some(NaiveTime::from_hms_nano(23, 59, 59, 1_999_999_999)));
-
- // bad formats
- assert!(from_str(r#""""#).is_err());
- assert!(from_str(r#""000000""#).is_err());
- assert!(from_str(r#""00:00:61""#).is_err());
- assert!(from_str(r#""00:60:00""#).is_err());
- assert!(from_str(r#""24:00:00""#).is_err());
- assert!(from_str(r#""23:59:59,1""#).is_err());
- assert!(from_str(r#""012:34:56""#).is_err());
- assert!(from_str(r#""hh:mm:ss""#).is_err());
- assert!(from_str(r#"0"#).is_err());
- assert!(from_str(r#"86399"#).is_err());
- assert!(from_str(r#"{}"#).is_err());
- // pre-0.3.0 rustc-serialize format is now invalid
- assert!(from_str(r#"{"secs":0,"frac":0}"#).is_err());
- assert!(from_str(r#"null"#).is_err());
-}
-
-#[cfg(feature = "rustc-serialize")]
-mod rustc_serialize {
- use super::NaiveTime;
- use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
-
- impl Encodable for NaiveTime {
- fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
- format!("{:?}", self).encode(s)
- }
- }
-
- impl Decodable for NaiveTime {
- fn decode<D: Decoder>(d: &mut D) -> Result<NaiveTime, D::Error> {
- d.read_str()?.parse().map_err(|_| d.error("invalid time"))
- }
- }
-
- #[cfg(test)] use rustc_serialize::json;
-
- #[test]
- fn test_encodable() {
- super::test_encodable_json(json::encode);
- }
-
- #[test]
- fn test_decodable() {
- super::test_decodable_json(json::decode);
- }
-}
-
-#[cfg(feature = "serde")]
-mod serde {
- use std::fmt;
- use super::NaiveTime;
- use serde::{ser, de};
-
- // 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)
-
- impl ser::Serialize for NaiveTime {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where S: ser::Serializer
- {
- serializer.serialize_str(&format!("{:?}", self))
- }
- }
-
- struct NaiveTimeVisitor;
-
- impl de::Visitor for NaiveTimeVisitor {
- type Value = NaiveTime;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result
- {
- write!(formatter, "a formatted time string")
- }
-
- fn visit_str<E>(self, value: &str) -> Result<NaiveTime, E>
- where E: de::Error
- {
- value.parse().map_err(|err| E::custom(format!("{}", err)))
- }
- }
-
- impl de::Deserialize for NaiveTime {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where D: de::Deserializer
- {
- deserializer.deserialize_str(NaiveTimeVisitor)
- }
- }
-
- #[cfg(test)] extern crate serde_json;
- #[cfg(test)] extern crate bincode;
-
- #[test]
- fn test_serde_serialize() {
- super::test_encodable_json(self::serde_json::to_string);
- }
-
- #[test]
- fn test_serde_deserialize() {
- super::test_decodable_json(self::serde_json::from_str);
- }
-
- #[test]
- fn test_serde_bincode() {
- // Bincode is relevant to test separately from JSON because
- // it is not self-describing.
- use self::bincode::SizeLimit;
- use self::bincode::serde::{serialize, deserialize};
-
- let t = NaiveTime::from_hms_nano(3, 5, 7, 98765432);
- let encoded = serialize(&t, SizeLimit::Infinite).unwrap();
- let decoded: NaiveTime = deserialize(&encoded).unwrap();
- assert_eq!(t, decoded);
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::NaiveTime;
- use Timelike;
- use std::u32;
- use oldtime::Duration;
-
- #[test]
- fn test_time_from_hms_milli() {
- assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 0),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 0)));
- assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 777),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 777_000_000)));
- assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 1_999),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 1_999_000_000)));
- assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 2_000), None);
- assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 5_000), None); // overflow check
- assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, u32::MAX), None);
- }
-
- #[test]
- fn test_time_from_hms_micro() {
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 0),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 0)));
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 333),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 333_000)));
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 777_777),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 777_777_000)));
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 1_999_999),
- Some(NaiveTime::from_hms_nano(3, 5, 7, 1_999_999_000)));
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 2_000_000), None);
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 5_000_000), None); // overflow check
- assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, u32::MAX), None);
- }
-
- #[test]
- fn test_time_hms() {
- assert_eq!(NaiveTime::from_hms(3, 5, 7).hour(), 3);
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_hour(0),
- Some(NaiveTime::from_hms(0, 5, 7)));
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_hour(23),
- Some(NaiveTime::from_hms(23, 5, 7)));
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_hour(24), None);
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_hour(u32::MAX), None);
-
- assert_eq!(NaiveTime::from_hms(3, 5, 7).minute(), 5);
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_minute(0),
- Some(NaiveTime::from_hms(3, 0, 7)));
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_minute(59),
- Some(NaiveTime::from_hms(3, 59, 7)));
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_minute(60), None);
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_minute(u32::MAX), None);
-
- assert_eq!(NaiveTime::from_hms(3, 5, 7).second(), 7);
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_second(0),
- Some(NaiveTime::from_hms(3, 5, 0)));
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_second(59),
- Some(NaiveTime::from_hms(3, 5, 59)));
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_second(60), None);
- assert_eq!(NaiveTime::from_hms(3, 5, 7).with_second(u32::MAX), None);
- }
-
- #[test]
- fn test_time_add() {
- macro_rules! check {
- ($lhs:expr, $rhs:expr, $sum:expr) => ({
- assert_eq!($lhs + $rhs, $sum);
- //assert_eq!($rhs + $lhs, $sum);
- })
- }
-
- let hmsm = |h,m,s,mi| NaiveTime::from_hms_milli(h, m, s, mi);
-
- check!(hmsm(3, 5, 7, 900), Duration::zero(), hmsm(3, 5, 7, 900));
- check!(hmsm(3, 5, 7, 900), Duration::milliseconds(100), hmsm(3, 5, 8, 0));
- check!(hmsm(3, 5, 7, 1_300), Duration::milliseconds(-1800), hmsm(3, 5, 6, 500));
- check!(hmsm(3, 5, 7, 1_300), Duration::milliseconds(-800), hmsm(3, 5, 7, 500));
- check!(hmsm(3, 5, 7, 1_300), Duration::milliseconds(-100), hmsm(3, 5, 7, 1_200));
- check!(hmsm(3, 5, 7, 1_300), Duration::milliseconds(100), hmsm(3, 5, 7, 1_400));
- check!(hmsm(3, 5, 7, 1_300), Duration::milliseconds(800), hmsm(3, 5, 8, 100));
- check!(hmsm(3, 5, 7, 1_300), Duration::milliseconds(1800), hmsm(3, 5, 9, 100));
- check!(hmsm(3, 5, 7, 900), Duration::seconds(86399), hmsm(3, 5, 6, 900)); // overwrap
- check!(hmsm(3, 5, 7, 900), Duration::seconds(-86399), hmsm(3, 5, 8, 900));
- check!(hmsm(3, 5, 7, 900), Duration::days(12345), hmsm(3, 5, 7, 900));
- check!(hmsm(3, 5, 7, 1_300), Duration::days(1), hmsm(3, 5, 7, 300));
- check!(hmsm(3, 5, 7, 1_300), Duration::days(-1), hmsm(3, 5, 8, 300));
-
- // regression tests for #37
- check!(hmsm(0, 0, 0, 0), Duration::milliseconds(-990), hmsm(23, 59, 59, 10));
- check!(hmsm(0, 0, 0, 0), Duration::milliseconds(-9990), hmsm(23, 59, 50, 10));
- }
-
- #[test]
- fn test_time_overflowing_add() {
- let hmsm = NaiveTime::from_hms_milli;
-
- assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(11)),
- (hmsm(14, 4, 5, 678), 0));
- assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(23)),
- (hmsm(2, 4, 5, 678), 86400));
- assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(-7)),
- (hmsm(20, 4, 5, 678), -86400));
-
- // overflowing_add_signed with leap seconds may be counter-intuitive
- assert_eq!(hmsm(3, 4, 5, 1_678).overflowing_add_signed(Duration::days(1)),
- (hmsm(3, 4, 5, 678), 86400));
- assert_eq!(hmsm(3, 4, 5, 1_678).overflowing_add_signed(Duration::days(-1)),
- (hmsm(3, 4, 6, 678), -86400));
- }
-
- #[test]
- fn test_time_sub() {
- macro_rules! check {
- ($lhs:expr, $rhs:expr, $diff:expr) => ({
- // `time1 - time2 = duration` is equivalent to `time2 - time1 = -duration`
- assert_eq!($lhs.signed_duration_since($rhs), $diff);
- assert_eq!($rhs.signed_duration_since($lhs), -$diff);
- })
- }
-
- let hmsm = |h,m,s,mi| NaiveTime::from_hms_milli(h, m, s, mi);
-
- check!(hmsm(3, 5, 7, 900), hmsm(3, 5, 7, 900), Duration::zero());
- check!(hmsm(3, 5, 7, 900), hmsm(3, 5, 7, 600), Duration::milliseconds(300));
- check!(hmsm(3, 5, 7, 200), hmsm(2, 4, 6, 200), Duration::seconds(3600 + 60 + 1));
- check!(hmsm(3, 5, 7, 200), hmsm(2, 4, 6, 300),
- Duration::seconds(3600 + 60) + Duration::milliseconds(900));
-
- // treats the leap second as if it coincides with the prior non-leap second,
- // as required by `time1 - time2 = duration` and `time2 - time1 = -duration` equivalence.
- check!(hmsm(3, 5, 7, 200), hmsm(3, 5, 6, 1_800), Duration::milliseconds(400));
- check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 1_800), Duration::milliseconds(1400));
- check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 800), Duration::milliseconds(1400));
-
- // additional equality: `time1 + duration = time2` is equivalent to
- // `time2 - time1 = duration` IF AND ONLY IF `time2` represents a non-leap second.
- assert_eq!(hmsm(3, 5, 6, 800) + Duration::milliseconds(400), hmsm(3, 5, 7, 200));
- assert_eq!(hmsm(3, 5, 6, 1_800) + Duration::milliseconds(400), hmsm(3, 5, 7, 200));
- }
-
- #[test]
- fn test_time_fmt() {
- assert_eq!(format!("{}", NaiveTime::from_hms_milli(23, 59, 59, 999)), "23:59:59.999");
- assert_eq!(format!("{}", NaiveTime::from_hms_milli(23, 59, 59, 1_000)), "23:59:60");
- assert_eq!(format!("{}", NaiveTime::from_hms_milli(23, 59, 59, 1_001)), "23:59:60.001");
- assert_eq!(format!("{}", NaiveTime::from_hms_micro(0, 0, 0, 43210)), "00:00:00.043210");
- assert_eq!(format!("{}", NaiveTime::from_hms_nano(0, 0, 0, 6543210)), "00:00:00.006543210");
-
- // the format specifier should have no effect on `NaiveTime`
- assert_eq!(format!("{:30}", NaiveTime::from_hms_milli(3, 5, 7, 9)), "03:05:07.009");
- }
-
- #[test]
- fn test_date_from_str() {
- // valid cases
- let valid = [
- "0:0:0",
- "0:0:0.0000000",
- "0:0:0.0000003",
- " 4 : 3 : 2.1 ",
- " 09:08:07 ",
- " 9:8:07 ",
- "23:59:60.373929310237",
- ];
- for &s in &valid {
- let d = match s.parse::<NaiveTime>() {
- Ok(d) => d,
- Err(e) => panic!("parsing `{}` has failed: {}", s, e)
- };
- let s_ = format!("{:?}", d);
- // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
- let d_ = match s_.parse::<NaiveTime>() {
- Ok(d) => d,
- Err(e) => panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}",
- s, d, e)
- };
- assert!(d == d_, "`{}` is parsed into `{:?}`, but reparsed result \
- `{:?}` does not match", s, d, d_);
- }
-
- // some invalid cases
- // since `ParseErrorKind` is private, all we can do is to check if there was an error
- assert!("".parse::<NaiveTime>().is_err());
- assert!("x".parse::<NaiveTime>().is_err());
- assert!("15".parse::<NaiveTime>().is_err());
- assert!("15:8".parse::<NaiveTime>().is_err());
- assert!("15:8:x".parse::<NaiveTime>().is_err());
- assert!("15:8:9x".parse::<NaiveTime>().is_err());
- assert!("23:59:61".parse::<NaiveTime>().is_err());
- assert!("12:34:56.x".parse::<NaiveTime>().is_err());
- assert!("12:34:56. 0".parse::<NaiveTime>().is_err());
- }
-
- #[test]
- fn test_time_parse_from_str() {
- let hms = |h,m,s| NaiveTime::from_hms(h,m,s);
- assert_eq!(NaiveTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
- Ok(hms(12, 34, 56))); // ignore date and offset
- assert_eq!(NaiveTime::parse_from_str("PM 12:59", "%P %H:%M"),
- Ok(hms(12, 59, 0)));
- assert!(NaiveTime::parse_from_str("12:3456", "%H:%M:%S").is_err());
- }
-
- #[test]
- fn test_time_format() {
- let t = NaiveTime::from_hms_nano(3, 5, 7, 98765432);
- assert_eq!(t.format("%H,%k,%I,%l,%P,%p").to_string(), "03, 3,03, 3,am,AM");
- assert_eq!(t.format("%M").to_string(), "05");
- assert_eq!(t.format("%S,%f,%.f").to_string(), "07,098765432,.098765432");
- assert_eq!(t.format("%.3f,%.6f,%.9f").to_string(), ".098,.098765,.098765432");
- assert_eq!(t.format("%R").to_string(), "03:05");
- assert_eq!(t.format("%T,%X").to_string(), "03:05:07,03:05:07");
- assert_eq!(t.format("%r").to_string(), "03:05:07 AM");
- assert_eq!(t.format("%t%n%%%n%t").to_string(), "\t\n%\n\t");
-
- let t = NaiveTime::from_hms_micro(3, 5, 7, 432100);
- assert_eq!(t.format("%S,%f,%.f").to_string(), "07,432100000,.432100");
- assert_eq!(t.format("%.3f,%.6f,%.9f").to_string(), ".432,.432100,.432100000");
-
- let t = NaiveTime::from_hms_milli(3, 5, 7, 210);
- assert_eq!(t.format("%S,%f,%.f").to_string(), "07,210000000,.210");
- assert_eq!(t.format("%.3f,%.6f,%.9f").to_string(), ".210,.210000,.210000000");
-
- let t = NaiveTime::from_hms(3, 5, 7);
- assert_eq!(t.format("%S,%f,%.f").to_string(), "07,000000000,");
- assert_eq!(t.format("%.3f,%.6f,%.9f").to_string(), ".000,.000000,.000000000");
-
- // corner cases
- assert_eq!(NaiveTime::from_hms(13, 57, 9).format("%r").to_string(), "01:57:09 PM");
- assert_eq!(NaiveTime::from_hms_milli(23, 59, 59, 1_000).format("%X").to_string(),
- "23:59:60");
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/offset/fixed.rs b/vendor/chrono-0.3.0/src/offset/fixed.rs
deleted file mode 100644
index 884da6c..0000000
--- a/vendor/chrono-0.3.0/src/offset/fixed.rs
+++ /dev/null
@@ -1,226 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! The time zone which has a fixed offset from UTC.
-
-use std::ops::{Add, Sub};
-use std::fmt;
-use oldtime::Duration as OldDuration;
-
-use Timelike;
-use div::div_mod_floor;
-use naive::time::NaiveTime;
-use naive::date::NaiveDate;
-use naive::datetime::NaiveDateTime;
-use datetime::DateTime;
-use super::{TimeZone, Offset, LocalResult};
-
-/// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59.
-///
-/// Using the [`TimeZone`](../../../chrono/offset/trait.TimeZone.html) methods
-/// on a `FixedOffset` struct is the preferred way to construct
-/// `DateTime<FixedOffset>` instances. See the [`east`](#method.east) and
-/// [`west`](#method.west) methods for examples.
-#[derive(Copy, Clone, PartialEq, Eq)]
-pub struct FixedOffset {
- local_minus_utc: i32,
-}
-
-impl FixedOffset {
- /// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference.
- /// The negative `secs` means the Western Hemisphere.
- ///
- /// Panics on the out-of-bound `secs`.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{FixedOffset, TimeZone};
- /// let hour = 3600;
- /// let datetime = FixedOffset::east(5 * hour).ymd(2016, 11, 08)
- /// .and_hms(0, 0, 0);
- /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
- /// ~~~~
- pub fn east(secs: i32) -> FixedOffset {
- FixedOffset::east_opt(secs).expect("FixedOffset::east out of bounds")
- }
-
- /// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference.
- /// The negative `secs` means the Western Hemisphere.
- ///
- /// Returns `None` on the out-of-bound `secs`.
- pub fn east_opt(secs: i32) -> Option<FixedOffset> {
- if -86400 < secs && secs < 86400 {
- Some(FixedOffset { local_minus_utc: secs })
- } else {
- None
- }
- }
-
- /// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference.
- /// The negative `secs` means the Eastern Hemisphere.
- ///
- /// Panics on the out-of-bound `secs`.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{FixedOffset, TimeZone};
- /// let hour = 3600;
- /// let datetime = FixedOffset::west(5 * hour).ymd(2016, 11, 08)
- /// .and_hms(0, 0, 0);
- /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
- /// ~~~~
- pub fn west(secs: i32) -> FixedOffset {
- FixedOffset::west_opt(secs).expect("FixedOffset::west out of bounds")
- }
-
- /// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference.
- /// The negative `secs` means the Eastern Hemisphere.
- ///
- /// Returns `None` on the out-of-bound `secs`.
- pub fn west_opt(secs: i32) -> Option<FixedOffset> {
- if -86400 < secs && secs < 86400 {
- Some(FixedOffset { local_minus_utc: -secs })
- } else {
- None
- }
- }
-
- /// Returns the number of seconds to add to convert from UTC to the local time.
- pub fn local_minus_utc(&self) -> i32 {
- self.local_minus_utc
- }
-
- /// Returns the number of seconds to add to convert from the local time to UTC.
- pub fn utc_minus_local(&self) -> i32 {
- -self.local_minus_utc
- }
-}
-
-impl TimeZone for FixedOffset {
- type Offset = FixedOffset;
-
- fn from_offset(offset: &FixedOffset) -> FixedOffset { *offset }
-
- fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<FixedOffset> {
- LocalResult::Single(*self)
- }
- fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<FixedOffset> {
- LocalResult::Single(*self)
- }
-
- fn offset_from_utc_date(&self, _utc: &NaiveDate) -> FixedOffset { *self }
- fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> FixedOffset { *self }
-}
-
-impl Offset for FixedOffset {
- fn fix(&self) -> FixedOffset { *self }
-}
-
-impl fmt::Debug for FixedOffset {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let offset = self.local_minus_utc;
- let (sign, offset) = if offset < 0 {('-', -offset)} else {('+', offset)};
- let (mins, sec) = div_mod_floor(offset, 60);
- let (hour, min) = div_mod_floor(mins, 60);
- if sec == 0 {
- write!(f, "{}{:02}:{:02}", sign, hour, min)
- } else {
- write!(f, "{}{:02}:{:02}:{:02}", sign, hour, min, sec)
- }
- }
-}
-
-impl fmt::Display for FixedOffset {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self, f) }
-}
-
-// addition or subtraction of FixedOffset to/from Timelike values is same to
-// adding or subtracting the offset's local_minus_utc value
-// but keep keeps the leap second information.
-// this should be implemented more efficiently, but for the time being, this is generic right now.
-
-fn add_with_leapsecond<T>(lhs: &T, rhs: i32) -> T
- where T: Timelike + Add<OldDuration, Output=T>
-{
- // extract and temporarily remove the fractional part and later recover it
- let nanos = lhs.nanosecond();
- let lhs = lhs.with_nanosecond(0).unwrap();
- (lhs + OldDuration::seconds(rhs as i64)).with_nanosecond(nanos).unwrap()
-}
-
-impl Add<FixedOffset> for NaiveTime {
- type Output = NaiveTime;
-
- #[inline]
- fn add(self, rhs: FixedOffset) -> NaiveTime {
- add_with_leapsecond(&self, rhs.local_minus_utc)
- }
-}
-
-impl Sub<FixedOffset> for NaiveTime {
- type Output = NaiveTime;
-
- #[inline]
- fn sub(self, rhs: FixedOffset) -> NaiveTime {
- add_with_leapsecond(&self, -rhs.local_minus_utc)
- }
-}
-
-impl Add<FixedOffset> for NaiveDateTime {
- type Output = NaiveDateTime;
-
- #[inline]
- fn add(self, rhs: FixedOffset) -> NaiveDateTime {
- add_with_leapsecond(&self, rhs.local_minus_utc)
- }
-}
-
-impl Sub<FixedOffset> for NaiveDateTime {
- type Output = NaiveDateTime;
-
- #[inline]
- fn sub(self, rhs: FixedOffset) -> NaiveDateTime {
- add_with_leapsecond(&self, -rhs.local_minus_utc)
- }
-}
-
-impl<Tz: TimeZone> Add<FixedOffset> for DateTime<Tz> {
- type Output = DateTime<Tz>;
-
- #[inline]
- fn add(self, rhs: FixedOffset) -> DateTime<Tz> {
- add_with_leapsecond(&self, rhs.local_minus_utc)
- }
-}
-
-impl<Tz: TimeZone> Sub<FixedOffset> for DateTime<Tz> {
- type Output = DateTime<Tz>;
-
- #[inline]
- fn sub(self, rhs: FixedOffset) -> DateTime<Tz> {
- add_with_leapsecond(&self, -rhs.local_minus_utc)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use offset::TimeZone;
- use super::FixedOffset;
-
- #[test]
- fn test_date_extreme_offset() {
- // starting from 0.3 we don't have an offset exceeding one day.
- // this makes everything easier!
- assert_eq!(format!("{:?}", FixedOffset::east(86399).ymd(2012, 2, 29)),
- "2012-02-29+23:59:59".to_string());
- assert_eq!(format!("{:?}", FixedOffset::east(86399).ymd(2012, 2, 29).and_hms(5, 6, 7)),
- "2012-02-29T05:06:07+23:59:59".to_string());
- assert_eq!(format!("{:?}", FixedOffset::west(86399).ymd(2012, 3, 4)),
- "2012-03-04-23:59:59".to_string());
- assert_eq!(format!("{:?}", FixedOffset::west(86399).ymd(2012, 3, 4).and_hms(5, 6, 7)),
- "2012-03-04T05:06:07-23:59:59".to_string());
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/offset/local.rs b/vendor/chrono-0.3.0/src/offset/local.rs
deleted file mode 100644
index dd037f8..0000000
--- a/vendor/chrono-0.3.0/src/offset/local.rs
+++ /dev/null
@@ -1,186 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! The local (system) time zone.
-
-use oldtime;
-use oldtime::Duration as OldDuration;
-
-use {Datelike, Timelike};
-use naive::date::NaiveDate;
-use naive::time::NaiveTime;
-use naive::datetime::NaiveDateTime;
-use date::Date;
-use datetime::DateTime;
-use super::{TimeZone, LocalResult};
-use super::fixed::FixedOffset;
-
-/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
-/// This assumes that `time` is working correctly, i.e. any error is fatal.
-fn tm_to_datetime(mut tm: oldtime::Tm) -> DateTime<Local> {
- if tm.tm_sec >= 60 {
- tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
- tm.tm_sec = 59;
- }
-
- #[cfg(not(windows))]
- fn tm_to_naive_date(tm: &oldtime::Tm) -> NaiveDate {
- // from_yo is more efficient than from_ymd (since it's the internal representation).
- NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1)
- }
-
- #[cfg(windows)]
- fn tm_to_naive_date(tm: &oldtime::Tm) -> NaiveDate {
- // ...but tm_yday is broken in Windows (issue #85)
- NaiveDate::from_ymd(tm.tm_year + 1900, tm.tm_mon as u32 + 1, tm.tm_mday as u32)
- }
-
- let date = tm_to_naive_date(&tm);
- let time = NaiveTime::from_hms_nano(tm.tm_hour as u32, tm.tm_min as u32,
- tm.tm_sec as u32, tm.tm_nsec as u32);
- let offset = FixedOffset::east(tm.tm_utcoff);
- DateTime::from_utc(date.and_time(time) + OldDuration::seconds(-tm.tm_utcoff as i64), offset)
-}
-
-/// Converts a local `NaiveDateTime` to the `time::Timespec`.
-fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> oldtime::Timespec {
- // well, this exploits an undocumented `Tm::to_timespec` behavior
- // to get the exact function we want (either `timegm` or `mktime`).
- // the number 1 is arbitrary but should be non-zero to trigger `mktime`.
- let tm_utcoff = if local {1} else {0};
-
- let tm = oldtime::Tm {
- tm_sec: d.second() as i32,
- tm_min: d.minute() as i32,
- tm_hour: d.hour() as i32,
- tm_mday: d.day() as i32,
- tm_mon: d.month0() as i32, // yes, C is that strange...
- tm_year: d.year() - 1900, // this doesn't underflow, we know that d is `NaiveDateTime`.
- tm_wday: 0, // to_local ignores this
- tm_yday: 0, // and this
- tm_isdst: -1,
- tm_utcoff: tm_utcoff,
- // do not set this, OS APIs are heavily inconsistent in terms of leap second handling
- tm_nsec: 0,
- };
-
- tm.to_timespec()
-}
-
-/// The local timescale. This is implemented via the standard `time` crate.
-///
-/// Using the [`TimeZone`](../../../chrono/offset/trait.TimeZone.html) methods
-/// on the Local struct is the preferred way to construct `DateTime<Local>`
-/// instances.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::{Local, DateTime, TimeZone};
-///
-/// let dt: DateTime<Local> = Local::now();
-/// let dt: DateTime<Local> = Local.timestamp(0, 0);
-/// ~~~~
-#[derive(Copy, Clone)]
-pub struct Local;
-
-impl Local {
- /// Returns a `Date` which corresponds to the current date.
- pub fn today() -> Date<Local> {
- Local::now().date()
- }
-
- /// Returns a `DateTime` which corresponds to the current date.
- pub fn now() -> DateTime<Local> {
- tm_to_datetime(oldtime::now())
- }
-}
-
-impl TimeZone for Local {
- type Offset = FixedOffset;
-
- fn from_offset(_offset: &FixedOffset) -> Local { Local }
-
- // they are easier to define in terms of the finished date and time unlike other offsets
- fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<FixedOffset> {
- self.from_local_date(local).map(|date| *date.offset())
- }
-
- fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<FixedOffset> {
- self.from_local_datetime(local).map(|datetime| *datetime.offset())
- }
-
- fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset {
- *self.from_utc_date(utc).offset()
- }
-
- fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset {
- *self.from_utc_datetime(utc).offset()
- }
-
- // override them for avoiding redundant works
- fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Local>> {
- // this sounds very strange, but required for keeping `TimeZone::ymd` sane.
- // in the other words, we use the offset at the local midnight
- // but keep the actual date unaltered (much like `FixedOffset`).
- let midnight = self.from_local_datetime(&local.and_hms(0, 0, 0));
- midnight.map(|datetime| Date::from_utc(*local, *datetime.offset()))
- }
-
- fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
- let timespec = datetime_to_timespec(local, true);
-
- // datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
- let mut tm = oldtime::at(timespec);
- assert_eq!(tm.tm_nsec, 0);
- tm.tm_nsec = local.nanosecond() as i32;
-
- LocalResult::Single(tm_to_datetime(tm))
- }
-
- fn from_utc_date(&self, utc: &NaiveDate) -> Date<Local> {
- let midnight = self.from_utc_datetime(&utc.and_hms(0, 0, 0));
- Date::from_utc(*utc, *midnight.offset())
- }
-
- fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
- let timespec = datetime_to_timespec(utc, false);
-
- // datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
- let mut tm = oldtime::at(timespec);
- assert_eq!(tm.tm_nsec, 0);
- tm.tm_nsec = utc.nanosecond() as i32;
-
- tm_to_datetime(tm)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use Datelike;
- use offset::TimeZone;
- use super::Local;
-
- #[test]
- fn test_local_date_sanity_check() { // issue #27
- assert_eq!(Local.ymd(2999, 12, 28).day(), 28);
- }
-
- #[test]
- fn test_leap_second() { // issue #123
- let today = Local::today();
-
- let dt = today.and_hms_milli(1, 2, 59, 1000);
- let timestr = dt.time().to_string();
- // the OS API may or may not support the leap second,
- // but there are only two sensible options.
- assert!(timestr == "01:02:60" || timestr == "01:03:00",
- "unexpected timestr {:?}", timestr);
-
- let dt = today.and_hms_milli(1, 2, 3, 1234);
- let timestr = dt.time().to_string();
- assert!(timestr == "01:02:03.234" || timestr == "01:02:04.234",
- "unexpected timestr {:?}", timestr);
- }
-}
-
diff --git a/vendor/chrono-0.3.0/src/offset/mod.rs b/vendor/chrono-0.3.0/src/offset/mod.rs
deleted file mode 100644
index 965497a..0000000
--- a/vendor/chrono-0.3.0/src/offset/mod.rs
+++ /dev/null
@@ -1,379 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-/*!
- * The time zone, which calculates offsets from the local time to UTC.
- *
- * There are four operations provided by the `TimeZone` trait:
- *
- * 1. Converting the local `NaiveDateTime` to `DateTime<Tz>`
- * 2. Converting the UTC `NaiveDateTime` to `DateTime<Tz>`
- * 3. Converting `DateTime<Tz>` to the local `NaiveDateTime`
- * 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.
- * 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).
- * Technically speaking `TimeZone` has a total knowledge about given timescale,
- * but `Offset` is used as a cache to avoid the repeated conversion
- * and provides implementations for 1 and 3.
- * An `TimeZone` instance can be reconstructed from the corresponding `Offset` instance.
- */
-
-use std::fmt;
-
-use Weekday;
-use naive::date::NaiveDate;
-use naive::time::NaiveTime;
-use naive::datetime::NaiveDateTime;
-use date::Date;
-use datetime::DateTime;
-use format::{parse, Parsed, ParseResult, StrftimeItems};
-use self::fixed::FixedOffset;
-
-/// The conversion result from the local time to the timezone-aware datetime types.
-#[derive(Clone, PartialEq, Debug)]
-pub enum LocalResult<T> {
- /// Given local time representation is invalid.
- /// This can occur when, for example, the positive timezone transition.
- None,
- /// Given local time representation has a single unique result.
- Single(T),
- /// Given local time representation has multiple results and thus ambiguous.
- /// This can occur when, for example, the negative timezone transition.
- Ambiguous(T /*min*/, T /*max*/),
-}
-
-impl<T> LocalResult<T> {
- /// Returns `Some` only when the conversion result is unique, or `None` otherwise.
- pub fn single(self) -> Option<T> {
- match self { LocalResult::Single(t) => Some(t), _ => None }
- }
-
- /// Returns `Some` for the earliest possible conversion result, or `None` if none.
- pub fn earliest(self) -> Option<T> {
- match self { LocalResult::Single(t) | LocalResult::Ambiguous(t,_) => Some(t), _ => None }
- }
-
- /// Returns `Some` for the latest possible conversion result, or `None` if none.
- pub fn latest(self) -> Option<T> {
- match self { LocalResult::Single(t) | LocalResult::Ambiguous(_,t) => Some(t), _ => None }
- }
-
- /// Maps a `LocalResult<T>` into `LocalResult<U>` with given function.
- pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> LocalResult<U> {
- match self {
- LocalResult::None => LocalResult::None,
- LocalResult::Single(v) => LocalResult::Single(f(v)),
- LocalResult::Ambiguous(min, max) => LocalResult::Ambiguous(f(min), f(max)),
- }
- }
-}
-
-impl<Tz: TimeZone> LocalResult<Date<Tz>> {
- /// Makes a new `DateTime` from the current date and given `NaiveTime`.
- /// The offset in the current date is preserved.
- ///
- /// Propagates any error. Ambiguous result would be discarded.
- #[inline]
- pub fn and_time(self, time: NaiveTime) -> LocalResult<DateTime<Tz>> {
- match self {
- LocalResult::Single(d) => d.and_time(time)
- .map_or(LocalResult::None, LocalResult::Single),
- _ => LocalResult::None,
- }
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute and second.
- /// The offset in the current date is preserved.
- ///
- /// Propagates any error. Ambiguous result would be discarded.
- #[inline]
- pub fn and_hms_opt(self, hour: u32, min: u32, sec: u32) -> LocalResult<DateTime<Tz>> {
- match self {
- LocalResult::Single(d) => d.and_hms_opt(hour, min, sec)
- .map_or(LocalResult::None, LocalResult::Single),
- _ => LocalResult::None,
- }
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
- /// The millisecond part can exceed 1,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Propagates any error. Ambiguous result would be discarded.
- #[inline]
- pub fn and_hms_milli_opt(self, hour: u32, min: u32, sec: u32,
- milli: u32) -> LocalResult<DateTime<Tz>> {
- match self {
- LocalResult::Single(d) => d.and_hms_milli_opt(hour, min, sec, milli)
- .map_or(LocalResult::None, LocalResult::Single),
- _ => LocalResult::None,
- }
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
- /// The microsecond part can exceed 1,000,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Propagates any error. Ambiguous result would be discarded.
- #[inline]
- pub fn and_hms_micro_opt(self, hour: u32, min: u32, sec: u32,
- micro: u32) -> LocalResult<DateTime<Tz>> {
- match self {
- LocalResult::Single(d) => d.and_hms_micro_opt(hour, min, sec, micro)
- .map_or(LocalResult::None, LocalResult::Single),
- _ => LocalResult::None,
- }
- }
-
- /// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
- /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
- /// The offset in the current date is preserved.
- ///
- /// Propagates any error. Ambiguous result would be discarded.
- #[inline]
- pub fn and_hms_nano_opt(self, hour: u32, min: u32, sec: u32,
- nano: u32) -> LocalResult<DateTime<Tz>> {
- match self {
- LocalResult::Single(d) => d.and_hms_nano_opt(hour, min, sec, nano)
- .map_or(LocalResult::None, LocalResult::Single),
- _ => LocalResult::None,
- }
- }
-
-}
-
-impl<T: fmt::Debug> LocalResult<T> {
- /// Returns the single unique conversion result, or panics accordingly.
- pub fn unwrap(self) -> T {
- match self {
- LocalResult::None => panic!("No such local time"),
- LocalResult::Single(t) => t,
- LocalResult::Ambiguous(t1,t2) => {
- panic!("Ambiguous local time, ranging from {:?} to {:?}", t1, t2)
- }
- }
- }
-}
-
-/// The offset from the local time to UTC.
-pub trait Offset: Sized + Clone + fmt::Debug {
- /// Returns the fixed offset from UTC to the local time stored.
- fn fix(&self) -> FixedOffset;
-}
-
-/// The time zone.
-///
-/// The methods here are the primarily constructors for [`Date`](../date/struct.Date.html) and
-/// [`DateTime`](../datetime/struct.DateTime.html) types.
-pub trait TimeZone: Sized + Clone {
- /// An associated offset type.
- /// This type is used to store the actual offset in date and time types.
- /// The original `TimeZone` value can be recovered via `TimeZone::from_offset`.
- type Offset: Offset;
-
- /// Makes a new `Date` from year, month, day and the current time zone.
- /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
- ///
- /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
- /// but it will propagate to the `DateTime` values constructed via this date.
- ///
- /// Panics on the out-of-range date, invalid month and/or day.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{UTC, TimeZone};
- ///
- /// assert_eq!(UTC.ymd(2015, 5, 15).to_string(), "2015-05-15UTC");
- /// ~~~~
- fn ymd(&self, year: i32, month: u32, day: u32) -> Date<Self> {
- self.ymd_opt(year, month, day).unwrap()
- }
-
- /// Makes a new `Date` from year, month, day and the current time zone.
- /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
- ///
- /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
- /// but it will propagate to the `DateTime` values constructed via this date.
- ///
- /// Returns `None` on the out-of-range date, invalid month and/or day.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{UTC, LocalResult, TimeZone};
- ///
- /// assert_eq!(UTC.ymd_opt(2015, 5, 15).unwrap().to_string(), "2015-05-15UTC");
- /// assert_eq!(UTC.ymd_opt(2000, 0, 0), LocalResult::None);
- /// ~~~~
- fn ymd_opt(&self, year: i32, month: u32, day: u32) -> LocalResult<Date<Self>> {
- match NaiveDate::from_ymd_opt(year, month, day) {
- Some(d) => self.from_local_date(&d),
- None => LocalResult::None,
- }
- }
-
- /// Makes a new `Date` from year, day of year (DOY or "ordinal") and the current time zone.
- /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
- ///
- /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
- /// but it will propagate to the `DateTime` values constructed via this date.
- ///
- /// Panics on the out-of-range date and/or invalid DOY.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{UTC, TimeZone};
- ///
- /// assert_eq!(UTC.yo(2015, 135).to_string(), "2015-05-15UTC");
- /// ~~~~
- fn yo(&self, year: i32, ordinal: u32) -> Date<Self> {
- self.yo_opt(year, ordinal).unwrap()
- }
-
- /// Makes a new `Date` from year, day of year (DOY or "ordinal") and the current time zone.
- /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
- ///
- /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
- /// but it will propagate to the `DateTime` values constructed via this date.
- ///
- /// Returns `None` on the out-of-range date and/or invalid DOY.
- fn yo_opt(&self, year: i32, ordinal: u32) -> LocalResult<Date<Self>> {
- match NaiveDate::from_yo_opt(year, ordinal) {
- Some(d) => self.from_local_date(&d),
- None => LocalResult::None,
- }
- }
-
- /// Makes a new `Date` from ISO week date (year and week number), day of the week (DOW) and
- /// the current time zone.
- /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
- /// The resulting `Date` may have a different year from the input year.
- ///
- /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
- /// but it will propagate to the `DateTime` values constructed via this date.
- ///
- /// Panics on the out-of-range date and/or invalid week number.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{UTC, Weekday, TimeZone};
- ///
- /// assert_eq!(UTC.isoywd(2015, 20, Weekday::Fri).to_string(), "2015-05-15UTC");
- /// ~~~~
- fn isoywd(&self, year: i32, week: u32, weekday: Weekday) -> Date<Self> {
- self.isoywd_opt(year, week, weekday).unwrap()
- }
-
- /// Makes a new `Date` from ISO week date (year and week number), day of the week (DOW) and
- /// the current time zone.
- /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
- /// The resulting `Date` may have a different year from the input year.
- ///
- /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
- /// but it will propagate to the `DateTime` values constructed via this date.
- ///
- /// Returns `None` on the out-of-range date and/or invalid week number.
- fn isoywd_opt(&self, year: i32, week: u32, weekday: Weekday) -> LocalResult<Date<Self>> {
- match NaiveDate::from_isoywd_opt(year, week, weekday) {
- Some(d) => self.from_local_date(&d),
- None => LocalResult::None,
- }
- }
-
- /// Makes a new `DateTime` from the number of non-leap seconds
- /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
- /// and the number of nanoseconds since the last whole non-leap second.
- ///
- /// Panics on the out-of-range number of seconds and/or invalid nanosecond.
- ///
- /// # Example
- ///
- /// ~~~~
- /// use chrono::{UTC, TimeZone};
- ///
- /// assert_eq!(UTC.timestamp(1431648000, 0).to_string(), "2015-05-15 00:00:00 UTC");
- /// ~~~~
- fn timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self> {
- self.timestamp_opt(secs, nsecs).unwrap()
- }
-
- /// Makes a new `DateTime` from the number of non-leap seconds
- /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
- /// and the number of nanoseconds since the last whole non-leap second.
- ///
- /// Returns `None` on the out-of-range number of seconds and/or invalid nanosecond.
- fn timestamp_opt(&self, secs: i64, nsecs: u32) -> LocalResult<DateTime<Self>> {
- match NaiveDateTime::from_timestamp_opt(secs, nsecs) {
- Some(dt) => LocalResult::Single(self.from_utc_datetime(&dt)),
- None => LocalResult::None,
- }
- }
-
- /// Parses a string with the specified format string and
- /// returns a `DateTime` with the current offset.
- /// See the [`format::strftime` module](../format/strftime/index.html)
- /// on the supported escape sequences.
- ///
- /// If the format does not include offsets, the current offset is assumed;
- /// otherwise the input should have a matching UTC offset.
- ///
- /// See also `DateTime::parse_from_str` which gives a local `DateTime`
- /// with parsed `FixedOffset`.
- fn datetime_from_str(&self, s: &str, fmt: &str) -> ParseResult<DateTime<Self>> {
- let mut parsed = Parsed::new();
- try!(parse(&mut parsed, s, StrftimeItems::new(fmt)));
- parsed.to_datetime_with_timezone(self)
- }
-
- /// Reconstructs the time zone from the offset.
- fn from_offset(offset: &Self::Offset) -> Self;
-
- /// Creates the offset(s) for given local `NaiveDate` if possible.
- fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<Self::Offset>;
-
- /// Creates the offset(s) for given local `NaiveDateTime` if possible.
- fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<Self::Offset>;
-
- /// Converts the local `NaiveDate` to the timezone-aware `Date` if possible.
- fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Self>> {
- self.offset_from_local_date(local).map(|offset| {
- // since FixedOffset is within +/- 1 day, the date is never affected
- Date::from_utc(*local, offset)
- })
- }
-
- /// Converts the local `NaiveDateTime` to the timezone-aware `DateTime` if possible.
- fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Self>> {
- self.offset_from_local_datetime(local).map(|offset| {
- DateTime::from_utc(*local - offset.fix(), offset)
- })
- }
-
- /// Creates the offset for given UTC `NaiveDate`. This cannot fail.
- fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset;
-
- /// Creates the offset for given UTC `NaiveDateTime`. This cannot fail.
- fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset;
-
- /// Converts the UTC `NaiveDate` to the local time.
- /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time).
- fn from_utc_date(&self, utc: &NaiveDate) -> Date<Self> {
- Date::from_utc(*utc, self.offset_from_utc_date(utc))
- }
-
- /// Converts the UTC `NaiveDateTime` to the local time.
- /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time).
- fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Self> {
- DateTime::from_utc(*utc, self.offset_from_utc_datetime(utc))
- }
-}
-
-pub mod utc;
-pub mod fixed;
-pub mod local;
-
diff --git a/vendor/chrono-0.3.0/src/offset/utc.rs b/vendor/chrono-0.3.0/src/offset/utc.rs
deleted file mode 100644
index aa028fc..0000000
--- a/vendor/chrono-0.3.0/src/offset/utc.rs
+++ /dev/null
@@ -1,75 +0,0 @@
-// This is a part of Chrono.
-// See README.md and LICENSE.txt for details.
-
-//! The UTC (Coordinated Universal Time) time zone.
-
-use std::fmt;
-use oldtime;
-
-use naive::date::NaiveDate;
-use naive::datetime::NaiveDateTime;
-use date::Date;
-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.
-/// It is also used as an offset (which is also a dummy type).
-///
-/// Using the [`TimeZone`](../../../chrono/offset/trait.TimeZone.html) methods
-/// on the UTC struct is the preferred way to construct `DateTime<UTC>`
-/// instances.
-///
-/// # Example
-///
-/// ~~~~
-/// use chrono::{DateTime, TimeZone, NaiveDateTime, UTC};
-///
-/// let dt = DateTime::<UTC>::from_utc(NaiveDateTime::from_timestamp(61, 0), UTC);
-///
-/// assert_eq!(UTC.timestamp(61, 0), dt);
-/// assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 1, 1), dt);
-/// ~~~~
-#[derive(Copy, Clone, PartialEq, Eq)]
-pub struct UTC;
-
-impl UTC {
- /// Returns a `Date` which corresponds to the current date.
- pub fn today() -> Date<UTC> { UTC::now().date() }
-
- /// Returns a `DateTime` which corresponds to the current date.
- pub fn now() -> DateTime<UTC> {
- let spec = oldtime::get_time();
- let naive = NaiveDateTime::from_timestamp(spec.sec, spec.nsec as u32);
- DateTime::from_utc(naive, UTC)
- }
-}
-
-impl TimeZone for UTC {
- type Offset = UTC;
-
- fn from_offset(_state: &UTC) -> UTC { UTC }
-
- fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<UTC> {
- LocalResult::Single(UTC)
- }
- fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<UTC> {
- LocalResult::Single(UTC)
- }
-
- fn offset_from_utc_date(&self, _utc: &NaiveDate) -> UTC { UTC }
- fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> UTC { UTC}
-}
-
-impl Offset for UTC {
- fn fix(&self) -> FixedOffset { FixedOffset::east(0) }
-}
-
-impl fmt::Debug for UTC {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Z") }
-}
-
-impl fmt::Display for UTC {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "UTC") }
-}
-
diff --git a/vendor/num-0.1.42/.cargo-checksum.json b/vendor/num-0.1.42/.cargo-checksum.json
deleted file mode 100644
index 87627b2..0000000
--- a/vendor/num-0.1.42/.cargo-checksum.json
+++ /dev/null
@@ -1 +0,0 @@
-{"files":{"Cargo.toml":"130555bf89fff3fc711d74a456e1f271d51948f09d9b746ed287743b4b43b39a","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"450e061d3970fe1393717ee58be1ec0374b75393c95e5b0bd967f7abdfb983c5","RELEASES.md":"28b613e89cb5a8d6ec4cf761f632e632b8331d680f9c346df51c578ccb64717b","bors.toml":"1c81ede536a37edd30fe4e622ff0531b25372403ac9475a5d6c50f14156565a2","ci/rustup.sh":"d746c6b56162295462759a44803c8db9d78ec5b96f78814de149cddd3726ee85","ci/test_full.sh":"3d58fd64948fc436928cf1796b30af8474ce43d42ec19972c009237d0682fb24","src/lib.rs":"cd5b80d265ae1da7806cac3d266e088e94f83815a76e787aed97e87e0b875ab9"},"package":"4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"}
\ No newline at end of file
diff --git a/vendor/num-0.1.42/Cargo.toml b/vendor/num-0.1.42/Cargo.toml
deleted file mode 100644
index 88620c3..0000000
--- a/vendor/num-0.1.42/Cargo.toml
+++ /dev/null
@@ -1,56 +0,0 @@
-# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
-#
-# When uploading crates to the registry Cargo will automatically
-# "normalize" Cargo.toml files for maximal compatibility
-# with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g. crates.io) dependencies
-#
-# If you believe there's an error in this file please file an
-# issue against the rust-lang/cargo repository. If you're
-# editing this file be aware that the upstream Cargo.toml
-# will likely look very different (and much more reasonable)
-
-[package]
-name = "num"
-version = "0.1.42"
-authors = ["The Rust Project Developers"]
-description = "A collection of numeric types and traits for Rust, including bigint,\ncomplex, rational, range iterators, generic integers, and more!\n"
-homepage = "https://github.com/rust-num/num"
-documentation = "https://docs.rs/num"
-readme = "README.md"
-keywords = ["mathematics", "numerics", "bignum"]
-categories = ["algorithms", "data-structures", "science"]
-license = "MIT/Apache-2.0"
-repository = "https://github.com/rust-num/num"
-[dependencies.num-bigint]
-version = "0.1.42"
-optional = true
-
-[dependencies.num-complex]
-version = "0.1.42"
-optional = true
-
-[dependencies.num-integer]
-version = "0.1.36"
-
-[dependencies.num-iter]
-version = "0.1.35"
-
-[dependencies.num-rational]
-version = "0.1.42"
-optional = true
-
-[dependencies.num-traits]
-version = "0.2.0"
-
-[dev-dependencies]
-
-[features]
-bigint = ["num-bigint"]
-complex = ["num-complex"]
-default = ["bigint", "complex", "rational", "rustc-serialize"]
-rational = ["num-rational"]
-rustc-serialize = ["num-bigint/rustc-serialize", "num-complex/rustc-serialize", "num-rational/rustc-serialize"]
-serde = ["num-bigint/serde", "num-complex/serde", "num-rational/serde"]
-[badges.travis-ci]
-repository = "rust-num/num"
diff --git a/vendor/num-0.1.42/LICENSE-APACHE b/vendor/num-0.1.42/LICENSE-APACHE
deleted file mode 100644
index 16fe87b..0000000
--- a/vendor/num-0.1.42/LICENSE-APACHE
+++ /dev/null
@@ -1,201 +0,0 @@
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
-TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
-1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
-2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
-3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
-4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
-5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
-6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
-7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
-8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
-9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
-END OF TERMS AND CONDITIONS
-
-APPENDIX: How to apply the Apache License to your work.
-
- To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "[]"
- replaced with your own identifying information. (Don't include
- the brackets!) The text should be enclosed in the appropriate
- comment syntax for the file format. We also recommend that a
- file or class name and description of purpose be included on the
- same "printed page" as the copyright notice for easier
- identification within third-party archives.
-
-Copyright [yyyy] [name of copyright owner]
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
diff --git a/vendor/num-0.1.42/LICENSE-MIT b/vendor/num-0.1.42/LICENSE-MIT
deleted file mode 100644
index 39d4bdb..0000000
--- a/vendor/num-0.1.42/LICENSE-MIT
+++ /dev/null
@@ -1,25 +0,0 @@
-Copyright (c) 2014 The Rust Project Developers
-
-Permission is hereby granted, free of charge, to any
-person obtaining a copy of this software and associated
-documentation files (the "Software"), to deal in the
-Software without restriction, including without
-limitation the rights to use, copy, modify, merge,
-publish, distribute, sublicense, and/or sell copies of
-the Software, and to permit persons to whom the Software
-is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice
-shall be included in all copies or substantial portions
-of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
-ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
-TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
-PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
-IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-DEALINGS IN THE SOFTWARE.
diff --git a/vendor/num-0.1.42/README.md b/vendor/num-0.1.42/README.md
deleted file mode 100644
index eaac524..0000000
--- a/vendor/num-0.1.42/README.md
+++ /dev/null
@@ -1,61 +0,0 @@
-# num
-
-[](https://crates.io/crates/num)
-[](https://docs.rs/num)
-
-[](https://travis-ci.org/rust-num/num)
-
-A collection of numeric types and traits for Rust.
-
-This includes new types for big integers, rationals, and complex numbers,
-new traits for generic programming on numeric properties like `Integer`,
-and generic range iterators.
-
-`num` is a meta-crate, re-exporting items from these sub-crates:
-
-- [`num-bigint`](https://github.com/rust-num/num-bigint)
- [](https://crates.io/crates/num-bigint)
-
-- [`num-complex`](https://github.com/rust-num/num-complex)
- [](https://crates.io/crates/num-complex)
-
-- [`num-integer`](https://github.com/rust-num/num-integer)
- [](https://crates.io/crates/num-integer)
-
-- [`num-iter`](https://github.com/rust-num/num-iter)
- [](https://crates.io/crates/num-iter)
-
-- [`num-rational`](https://github.com/rust-num/num-rational)
- [](https://crates.io/crates/num-rational)
-
-- [`num-traits`](https://github.com/rust-num/num-traits)
- [](https://crates.io/crates/num-traits)
-
-There is also a `proc-macro` crate for deriving some numeric traits:
-
-- [`num-derive`](https://github.com/rust-num/num-derive)
- [](https://crates.io/crates/num-derive)
-
-## Usage
-
-Add this to your `Cargo.toml`:
-
-```toml
-[dependencies]
-num = "0.1"
-```
-
-and this to your crate root:
-
-```rust
-extern crate num;
-```
-
-## Releases
-
-Release notes are available in [RELEASES.md](RELEASES.md).
-
-## Compatibility
-
-Most of the `num` crates are tested for rustc 1.8 and greater.
-The exception is `num-derive` which requires at least rustc 1.15.
diff --git a/vendor/num-0.1.42/RELEASES.md b/vendor/num-0.1.42/RELEASES.md
deleted file mode 100644
index cb55431..0000000
--- a/vendor/num-0.1.42/RELEASES.md
+++ /dev/null
@@ -1,14 +0,0 @@
-# Release 0.1.42
-
-- [All of the num sub-crates now have their own source repositories][num-356].
-- Updated num sub-crates to their latest versions.
-
-**Contributors**: @cuviper
-
-[num-356]: https://github.com/rust-num/num/pull/356
-
-
-# Prior releases
-
-No prior release notes were kept. Thanks all the same to the many
-contributors that have made this crate what it is!
diff --git a/vendor/num-0.1.42/bors.toml b/vendor/num-0.1.42/bors.toml
deleted file mode 100644
index ca08e81..0000000
--- a/vendor/num-0.1.42/bors.toml
+++ /dev/null
@@ -1,3 +0,0 @@
-status = [
- "continuous-integration/travis-ci/push",
-]
diff --git a/vendor/num-0.1.42/ci/rustup.sh b/vendor/num-0.1.42/ci/rustup.sh
deleted file mode 100755
index 31e5290..0000000
--- a/vendor/num-0.1.42/ci/rustup.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/bin/sh
-# Use rustup to locally run the same suite of tests as .travis.yml.
-# (You should first install/update 1.8.0, stable, beta, and nightly.)
-
-set -ex
-
-export TRAVIS_RUST_VERSION
-for TRAVIS_RUST_VERSION in 1.8.0 stable beta nightly; do
- run="rustup run $TRAVIS_RUST_VERSION"
- if [ "$TRAVIS_RUST_VERSION" = 1.8.0 ]; then
- # rand 0.3.22 started depending on rand 0.4, which requires rustc 1.15
- # manually hacking the lockfile due to the limitations of cargo#2773
- $run cargo generate-lockfile
- $run sed -i -e 's/"rand 0.[34].[0-9]\+/"rand 0.3.20/' Cargo.lock
- $run sed -i -e '/^name = "rand"/,/^$/s/version = "0.3.[0-9]\+"/version = "0.3.20"/' Cargo.lock
- fi
- $run cargo build --verbose
- $run $PWD/ci/test_full.sh
-done
diff --git a/vendor/num-0.1.42/ci/test_full.sh b/vendor/num-0.1.42/ci/test_full.sh
deleted file mode 100755
index 661b172..0000000
--- a/vendor/num-0.1.42/ci/test_full.sh
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/bin/bash
-
-set -ex
-
-echo Testing num on rustc ${TRAVIS_RUST_VERSION}
-
-# num should build and test everywhere.
-cargo build --verbose
-cargo test --verbose
-
-# It should build with minimal features too.
-cargo build --no-default-features
-cargo test --no-default-features
-
-# Each isolated feature should also work everywhere.
-for feature in bigint complex rational rustc-serialize serde; do
- cargo build --verbose --no-default-features --features="$feature"
- cargo test --verbose --no-default-features --features="$feature"
-done
-
-# Downgrade serde and build test the 0.7.0 channel as well
-cargo update -p serde --precise 0.7.0
-cargo build --verbose --features "serde"
diff --git a/vendor/num-0.1.42/src/lib.rs b/vendor/num-0.1.42/src/lib.rs
deleted file mode 100644
index 03156ff..0000000
--- a/vendor/num-0.1.42/src/lib.rs
+++ /dev/null
@@ -1,113 +0,0 @@
-// Copyright 2014-2016 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! A collection of numeric types and traits for Rust.
-//!
-//! This includes new types for big integers, rationals, and complex numbers,
-//! new traits for generic programming on numeric properties like `Integer`,
-//! and generic range iterators.
-//!
-//! ## Example
-//!
-//! This example uses the BigRational type and [Newton's method][newt] to
-//! approximate a square root to arbitrary precision:
-//!
-//! ```
-//! extern crate num;
-//! # #[cfg(all(feature = "bigint", feature="rational"))]
-//! # mod test {
-//!
-//! use num::FromPrimitive;
-//! use num::bigint::BigInt;
-//! use num::rational::{Ratio, BigRational};
-//!
-//! # pub
-//! fn approx_sqrt(number: u64, iterations: usize) -> BigRational {
-//! let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
-//! let mut approx = start.clone();
-//!
-//! for _ in 0..iterations {
-//! approx = (&approx + (&start / &approx)) /
-//! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
-//! }
-//!
-//! approx
-//! }
-//! # }
-//! # #[cfg(not(all(feature = "bigint", feature="rational")))]
-//! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } }
-//! # use test::approx_sqrt;
-//!
-//! fn main() {
-//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
-//! }
-//!
-//! ```
-//!
-//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
-//!
-//! ## Compatibility
-//!
-//! The `num` crate is tested for rustc 1.8 and greater.
-
-#![doc(html_root_url = "https://docs.rs/num/0.1")]
-
-extern crate num_traits;
-extern crate num_integer;
-extern crate num_iter;
-#[cfg(feature = "num-complex")]
-extern crate num_complex;
-#[cfg(feature = "num-bigint")]
-extern crate num_bigint;
-#[cfg(feature = "num-rational")]
-extern crate num_rational;
-
-#[cfg(feature = "num-bigint")]
-pub use num_bigint::{BigInt, BigUint};
-#[cfg(feature = "num-rational")]
-pub use num_rational::Rational;
-#[cfg(all(feature = "num-rational", feature="num-bigint"))]
-pub use num_rational::BigRational;
-#[cfg(feature = "num-complex")]
-pub use num_complex::Complex;
-pub use num_integer::Integer;
-pub use num_iter::{range, range_inclusive, range_step, range_step_inclusive};
-pub use num_traits::{Num, Zero, One, Signed, Unsigned, Bounded,
- one, zero, abs, abs_sub, signum,
- Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
- PrimInt, Float, ToPrimitive, FromPrimitive, NumCast, cast,
- pow, checked_pow, clamp};
-
-#[cfg(feature = "num-bigint")]
-pub mod bigint {
- pub use num_bigint::*;
-}
-
-#[cfg(feature = "num-complex")]
-pub mod complex {
- pub use num_complex::*;
-}
-
-pub mod integer {
- pub use num_integer::*;
-}
-
-pub mod iter {
- pub use num_iter::*;
-}
-
-pub mod traits {
- pub use num_traits::*;
-}
-
-#[cfg(feature = "num-rational")]
-pub mod rational {
- pub use num_rational::*;
-}