parent
9768b289f0
commit
7b9b0c4437
70
README.md
70
README.md
|
@ -105,7 +105,7 @@ the [**`TimeZone`**](https://docs.rs/chrono/0.4.0/chrono/offset/trait.TimeZone.h
|
|||
which defines how the local date is converted to and back from the UTC date.
|
||||
There are three well-known `TimeZone` implementations:
|
||||
|
||||
* [**`UTC`**](https://docs.rs/chrono/0.4.0/chrono/offset/struct.UTC.html) specifies the UTC time zone. It is most efficient.
|
||||
* [**`Utc`**](https://docs.rs/chrono/0.4.0/chrono/offset/struct.Utc.html) specifies the UTC time zone. It is most efficient.
|
||||
|
||||
* [**`Local`**](https://docs.rs/chrono/0.4.0/chrono/offset/struct.Local.html) specifies the system local time zone.
|
||||
|
||||
|
@ -120,14 +120,14 @@ but can be converted to each other using
|
|||
the [`DateTime::with_timezone`](https://docs.rs/chrono/0.4.0/chrono/struct.DateTime.html#method.with_timezone) method.
|
||||
|
||||
You can get the current date and time in the UTC time zone
|
||||
([`UTC::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/struct.UTC.html#method.now))
|
||||
([`Utc::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/struct.Utc.html#method.now))
|
||||
or in the local time zone
|
||||
([`Local::now()`](https://docs.rs/chrono/0.4.0/chrono/offset/struct.Local.html#method.now)).
|
||||
|
||||
```rust
|
||||
use chrono::prelude::*;
|
||||
|
||||
let utc: DateTime<UTC> = UTC::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
|
||||
let utc: DateTime<Utc> = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
|
||||
let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
|
||||
```
|
||||
|
||||
|
@ -139,21 +139,21 @@ but in turn we get a rich combination of initialization methods.
|
|||
use chrono::prelude::*;
|
||||
use chrono::offset::LocalResult;
|
||||
|
||||
let dt = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
|
||||
let dt = Utc.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
|
||||
// July 8 is 188th day of the year 2014 (`o` for "ordinal")
|
||||
assert_eq!(dt, UTC.yo(2014, 189).and_hms(9, 10, 11));
|
||||
assert_eq!(dt, Utc.yo(2014, 189).and_hms(9, 10, 11));
|
||||
// July 8 is Tuesday in ISO week 28 of the year 2014.
|
||||
assert_eq!(dt, UTC.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
|
||||
assert_eq!(dt, Utc.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
|
||||
|
||||
let dt = UTC.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); // `2014-07-08T09:10:11.012Z`
|
||||
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));
|
||||
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);
|
||||
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.
|
||||
|
@ -187,7 +187,7 @@ assert_eq!(dt.num_days_from_ce(), 735565); // the number of days from and includ
|
|||
// 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));
|
||||
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
|
||||
|
@ -195,14 +195,14 @@ 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);
|
||||
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));
|
||||
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.4.0/chrono/struct.DateTime.html#method.format) method,
|
||||
|
@ -218,7 +218,7 @@ for well-known formats.
|
|||
```rust
|
||||
use chrono::prelude::*;
|
||||
|
||||
let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09");
|
||||
assert_eq!(dt.format("%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());
|
||||
|
@ -233,7 +233,7 @@ 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
|
||||
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.
|
||||
|
@ -259,12 +259,12 @@ More detailed control over the parsing process is available via
|
|||
```rust
|
||||
use chrono::prelude::*;
|
||||
|
||||
let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600));
|
||||
|
||||
// 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-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
|
||||
|
@ -275,15 +275,15 @@ assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
|
|||
assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone()));
|
||||
|
||||
// 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()));
|
||||
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());
|
||||
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());
|
||||
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());
|
||||
assert!(Utc.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_err());
|
||||
```
|
||||
|
||||
### Individual date
|
||||
|
@ -296,12 +296,12 @@ Most operations available to `DateTime` are also available to `Date` whenever ap
|
|||
use chrono::prelude::*;
|
||||
use chrono::offset::LocalResult;
|
||||
|
||||
assert_eq!(UTC::today(), UTC::now().date());
|
||||
assert_eq!(Utc::today(), Utc::now().date());
|
||||
assert_eq!(Local::today(), Local::now().date());
|
||||
|
||||
assert_eq!(UTC.ymd(2014, 11, 28).weekday(), Weekday::Fri);
|
||||
assert_eq!(UTC.ymd_opt(2014, 11, 31), LocalResult::None);
|
||||
assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(),
|
||||
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");
|
||||
```
|
||||
|
||||
|
@ -347,7 +347,7 @@ 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`.
|
||||
and consequently `Utc.ymd(2014, 1, 30).with_month(2)` returns `None`.
|
||||
|
||||
Advanced time zone handling is not yet supported.
|
||||
For now you can try the [Chrono-tz](https://github.com/chronotope/chrono-tz/) crate instead.
|
||||
|
|
|
@ -9,7 +9,7 @@ use std::ops::{Add, Sub};
|
|||
use oldtime::Duration as OldDuration;
|
||||
|
||||
use {Weekday, Datelike};
|
||||
use offset::{TimeZone, UTC};
|
||||
use offset::{TimeZone, Utc};
|
||||
use naive::{self, NaiveDate, NaiveTime, IsoWeek};
|
||||
use DateTime;
|
||||
use format::{Item, DelayedFormat, StrftimeItems};
|
||||
|
@ -45,9 +45,9 @@ pub struct Date<Tz: TimeZone> {
|
|||
}
|
||||
|
||||
/// The minimum possible `Date`.
|
||||
pub const MIN_DATE: Date<UTC> = Date { date: naive::MIN_DATE, offset: UTC };
|
||||
pub const MIN_DATE: Date<Utc> = Date { date: naive::MIN_DATE, offset: Utc };
|
||||
/// The maximum possible `Date`.
|
||||
pub const MAX_DATE: Date<UTC> = Date { date: naive::MAX_DATE, offset: UTC };
|
||||
pub const MAX_DATE: Date<Utc> = Date { date: naive::MAX_DATE, offset: Utc };
|
||||
|
||||
impl<Tz: TimeZone> Date<Tz> {
|
||||
/// Makes a new `Date` with given *UTC* date and offset.
|
||||
|
|
172
src/datetime.rs
172
src/datetime.rs
|
@ -11,7 +11,7 @@ use std::ops::Deref;
|
|||
use oldtime::Duration as OldDuration;
|
||||
|
||||
use {Weekday, Timelike, Datelike};
|
||||
use offset::{TimeZone, Offset, UTC, Local, FixedOffset};
|
||||
use offset::{TimeZone, Offset, Utc, Local, FixedOffset};
|
||||
use naive::{NaiveTime, NaiveDateTime, IsoWeek};
|
||||
use Date;
|
||||
use format::{Item, Numeric, Pad, Fixed};
|
||||
|
@ -59,10 +59,10 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
|||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{DateTime, TimeZone, NaiveDateTime, UTC};
|
||||
/// 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);
|
||||
/// 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.
|
||||
|
@ -405,11 +405,11 @@ impl str::FromStr for DateTime<FixedOffset> {
|
|||
}
|
||||
}
|
||||
|
||||
impl str::FromStr for DateTime<UTC> {
|
||||
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))
|
||||
fn from_str(s: &str) -> ParseResult<DateTime<Utc>> {
|
||||
s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&Utc))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -422,12 +422,12 @@ impl str::FromStr for DateTime<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>,
|
||||
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(),
|
||||
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(),
|
||||
|
@ -437,10 +437,10 @@ fn test_encodable_json<FUTC, FFixed, E>(to_string_utc: FUTC, to_string_fixed: FF
|
|||
}
|
||||
|
||||
#[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>,
|
||||
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
|
||||
|
@ -451,9 +451,9 @@ fn test_decodable_json<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
|
|||
}
|
||||
|
||||
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))));
|
||||
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))));
|
||||
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))));
|
||||
|
@ -464,20 +464,20 @@ fn test_decodable_json<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
|
|||
// the conversion didn't change the instant itself
|
||||
assert_eq!(local_from_str(r#""2014-07-24T12:34:06Z""#)
|
||||
.expect("local shouuld parse"),
|
||||
UTC.ymd(2014, 7, 24).and_hms(12, 34, 6));
|
||||
Utc.ymd(2014, 7, 24).and_hms(12, 34, 6));
|
||||
assert_eq!(local_from_str(r#""2014-07-24T13:57:06+01:23""#)
|
||||
.expect("local should parse with offset"),
|
||||
UTC.ymd(2014, 7, 24).and_hms(12, 34, 6));
|
||||
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(all(test, feature = "rustc-serialize"))]
|
||||
fn test_decodable_json_timestamps<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
|
||||
fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
|
||||
fixed_from_str: FFixed,
|
||||
local_from_str: FLocal)
|
||||
where FUTC: Fn(&str) -> Result<TsSeconds<UTC>, E>,
|
||||
where FUtc: Fn(&str) -> Result<TsSeconds<Utc>, E>,
|
||||
FFixed: Fn(&str) -> Result<TsSeconds<FixedOffset>, E>,
|
||||
FLocal: Fn(&str) -> Result<TsSeconds<Local>, E>,
|
||||
E: ::std::fmt::Debug
|
||||
|
@ -487,9 +487,9 @@ fn test_decodable_json_timestamps<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
|
|||
}
|
||||
|
||||
assert_eq!(norm(&utc_from_str("0").ok().map(DateTime::from)),
|
||||
norm(&Some(UTC.ymd(1970, 1, 1).and_hms(0, 0, 0))));
|
||||
norm(&Some(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0))));
|
||||
assert_eq!(norm(&utc_from_str("-1").ok().map(DateTime::from)),
|
||||
norm(&Some(UTC.ymd(1969, 12, 31).and_hms(23, 59, 59))));
|
||||
norm(&Some(Utc.ymd(1969, 12, 31).and_hms(23, 59, 59))));
|
||||
|
||||
assert_eq!(norm(&fixed_from_str("0").ok().map(DateTime::from)),
|
||||
norm(&Some(FixedOffset::east(0).ymd(1970, 1, 1).and_hms(0, 0, 0))));
|
||||
|
@ -497,16 +497,16 @@ fn test_decodable_json_timestamps<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
|
|||
norm(&Some(FixedOffset::east(0).ymd(1969, 12, 31).and_hms(23, 59, 59))));
|
||||
|
||||
assert_eq!(*fixed_from_str("0").expect("0 timestamp should parse"),
|
||||
UTC.ymd(1970, 1, 1).and_hms(0, 0, 0));
|
||||
Utc.ymd(1970, 1, 1).and_hms(0, 0, 0));
|
||||
assert_eq!(*local_from_str("-1").expect("-1 timestamp should parse"),
|
||||
UTC.ymd(1969, 12, 31).and_hms(23, 59, 59));
|
||||
Utc.ymd(1969, 12, 31).and_hms(23, 59, 59));
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustc-serialize")]
|
||||
mod rustc_serialize {
|
||||
use std::fmt;
|
||||
use super::{DateTime, TsSeconds};
|
||||
use offset::{TimeZone, LocalResult, UTC, Local, FixedOffset};
|
||||
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
|
||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||
|
||||
impl<Tz: TimeZone> Encodable for DateTime<Tz> {
|
||||
|
@ -543,18 +543,18 @@ mod rustc_serialize {
|
|||
}
|
||||
}
|
||||
|
||||
impl Decodable for DateTime<UTC> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<UTC>, D::Error> {
|
||||
impl Decodable for DateTime<Utc> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Utc>, D::Error> {
|
||||
d.read_str()?
|
||||
.parse::<DateTime<FixedOffset>>()
|
||||
.map(|dt| dt.with_timezone(&UTC))
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.map_err(|_| d.error("invalid date and time"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for TsSeconds<UTC> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<UTC>, D::Error> {
|
||||
from(UTC.timestamp_opt(d.read_i64()?, 0), d)
|
||||
impl Decodable for TsSeconds<Utc> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> {
|
||||
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
|
||||
.map(|dt| TsSeconds(dt))
|
||||
}
|
||||
}
|
||||
|
@ -570,7 +570,7 @@ mod rustc_serialize {
|
|||
|
||||
impl Decodable for TsSeconds<Local> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Local>, D::Error> {
|
||||
from(UTC.timestamp_opt(d.read_i64()?, 0), d)
|
||||
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
|
||||
.map(|dt| TsSeconds(dt.with_timezone(&Local)))
|
||||
}
|
||||
}
|
||||
|
@ -602,7 +602,7 @@ mod rustc_serialize {
|
|||
pub mod serde {
|
||||
use std::fmt;
|
||||
use super::DateTime;
|
||||
use offset::{TimeZone, LocalResult, UTC, Local, FixedOffset};
|
||||
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
|
||||
use serdelib::{ser, de};
|
||||
|
||||
/// Ser/de to/from timestamps in seconds
|
||||
|
@ -619,16 +619,16 @@ pub mod serde {
|
|||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// # #[macro_use] extern crate serde_json;
|
||||
/// # extern crate chrono;
|
||||
/// # use chrono::{TimeZone, DateTime, UTC};
|
||||
/// # use chrono::{TimeZone, DateTime, Utc};
|
||||
/// use chrono::serde::ts_seconds;
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_seconds")]
|
||||
/// time: DateTime<UTC>
|
||||
/// time: DateTime<Utc>
|
||||
/// }
|
||||
///
|
||||
/// # fn example() -> Result<S, serde_json::Error> {
|
||||
/// let time = UTC.ymd(2015, 5, 15).and_hms(10, 0, 0);
|
||||
/// let time = Utc.ymd(2015, 5, 15).and_hms(10, 0, 0);
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
|
@ -645,7 +645,7 @@ pub mod serde {
|
|||
use std::fmt;
|
||||
use serdelib::{ser, de};
|
||||
|
||||
use {DateTime, UTC, FixedOffset};
|
||||
use {DateTime, Utc, FixedOffset};
|
||||
use offset::TimeZone;
|
||||
use super::from;
|
||||
|
||||
|
@ -663,12 +663,12 @@ pub mod serde {
|
|||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// # #[macro_use] extern crate serde_json;
|
||||
/// # extern crate chrono;
|
||||
/// # use chrono::{DateTime, UTC};
|
||||
/// # use chrono::{DateTime, Utc};
|
||||
/// use chrono::serde::ts_seconds::deserialize as from_ts;
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_ts")]
|
||||
/// time: DateTime<UTC>
|
||||
/// time: DateTime<Utc>
|
||||
/// }
|
||||
///
|
||||
/// # fn example() -> Result<S, serde_json::Error> {
|
||||
|
@ -677,10 +677,10 @@ pub mod serde {
|
|||
/// # }
|
||||
/// # fn main() { example().unwrap(); }
|
||||
/// ```
|
||||
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<UTC>, D::Error>
|
||||
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
|
||||
where D: de::Deserializer<'de>
|
||||
{
|
||||
Ok(try!(d.deserialize_i64(SecondsTimestampVisitor).map(|dt| dt.with_timezone(&UTC))))
|
||||
Ok(try!(d.deserialize_i64(SecondsTimestampVisitor).map(|dt| dt.with_timezone(&Utc))))
|
||||
}
|
||||
|
||||
/// Serialize a UTC datetime into an integer number of seconds since the epoch
|
||||
|
@ -697,17 +697,17 @@ pub mod serde {
|
|||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// # #[macro_use] extern crate serde_json;
|
||||
/// # extern crate chrono;
|
||||
/// # use chrono::{TimeZone, DateTime, UTC};
|
||||
/// # use chrono::{TimeZone, DateTime, Utc};
|
||||
/// use chrono::serde::ts_seconds::serialize as to_ts;
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_ts")]
|
||||
/// time: DateTime<UTC>
|
||||
/// time: DateTime<Utc>
|
||||
/// }
|
||||
///
|
||||
/// # fn example() -> Result<String, serde_json::Error> {
|
||||
/// let my_s = S {
|
||||
/// time: UTC.ymd(2015, 5, 15).and_hms(10, 0, 0),
|
||||
/// time: Utc.ymd(2015, 5, 15).and_hms(10, 0, 0),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
|
@ -715,7 +715,7 @@ pub mod serde {
|
|||
/// # }
|
||||
/// # fn main() { example().unwrap(); }
|
||||
/// ```
|
||||
pub fn serialize<S>(dt: &DateTime<UTC>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
pub fn serialize<S>(dt: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: ser::Serializer
|
||||
{
|
||||
serializer.serialize_i64(dt.timestamp())
|
||||
|
@ -819,11 +819,11 @@ pub mod serde {
|
|||
///
|
||||
/// The serialized value can be either a string representation or a unix
|
||||
/// timestamp
|
||||
impl<'de> de::Deserialize<'de> for DateTime<UTC> {
|
||||
impl<'de> de::Deserialize<'de> for DateTime<Utc> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: de::Deserializer<'de>
|
||||
{
|
||||
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC))
|
||||
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Utc))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -860,9 +860,9 @@ pub mod serde {
|
|||
// it is not self-describing.
|
||||
use self::bincode::{Infinite, serialize, deserialize};
|
||||
|
||||
let dt = UTC.ymd(2014, 7, 24).and_hms(12, 34, 6);
|
||||
let dt = Utc.ymd(2014, 7, 24).and_hms(12, 34, 6);
|
||||
let encoded = serialize(&dt, Infinite).unwrap();
|
||||
let decoded: DateTime<UTC> = deserialize(&encoded).unwrap();
|
||||
let decoded: DateTime<Utc> = deserialize(&encoded).unwrap();
|
||||
assert_eq!(dt, decoded);
|
||||
assert_eq!(dt.offset(), decoded.offset());
|
||||
}
|
||||
|
@ -873,52 +873,52 @@ mod tests {
|
|||
use super::DateTime;
|
||||
use Datelike;
|
||||
use naive::{NaiveTime, NaiveDate};
|
||||
use offset::{TimeZone, UTC, Local, FixedOffset};
|
||||
use offset::{TimeZone, Utc, Local, 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);
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
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);
|
||||
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]
|
||||
|
@ -948,7 +948,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_datetime_with_timezone() {
|
||||
let local_now = Local::now();
|
||||
let utc_now = local_now.with_timezone(&UTC);
|
||||
let utc_now = local_now.with_timezone(&Utc);
|
||||
let local_now2 = utc_now.with_timezone(&Local);
|
||||
assert_eq!(local_now, local_now2);
|
||||
}
|
||||
|
@ -957,9 +957,9 @@ mod tests {
|
|||
#[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(),
|
||||
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(),
|
||||
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");
|
||||
|
@ -988,11 +988,11 @@ mod tests {
|
|||
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());
|
||||
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.
|
||||
}
|
||||
|
@ -1005,22 +1005,22 @@ mod tests {
|
|||
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",
|
||||
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)));
|
||||
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());
|
||||
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 a = Utc::now();
|
||||
let b = a;
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
@ -1030,7 +1030,7 @@ mod tests {
|
|||
use std::thread;
|
||||
|
||||
// UTC is known to be `Send`.
|
||||
let a = UTC::now();
|
||||
let a = Utc::now();
|
||||
thread::spawn(move || {
|
||||
let _ = a;
|
||||
}).join().unwrap();
|
||||
|
@ -1038,7 +1038,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_subsecond_part() {
|
||||
let datetime = UTC.ymd(2014, 7, 8).and_hms_nano(9, 10, 11, 1234567);
|
||||
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());
|
||||
|
|
|
@ -655,32 +655,32 @@ fn test_rfc2822() {
|
|||
#[cfg(test)]
|
||||
#[test]
|
||||
fn parse_rfc850() {
|
||||
use ::{UTC, TimeZone};
|
||||
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);
|
||||
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));
|
||||
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"),
|
||||
(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));
|
||||
assert_eq!(Ok(val.0), Utc.datetime_from_str(val.1, RFC850_FMT));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -642,7 +642,7 @@ mod tests {
|
|||
use Datelike;
|
||||
use Weekday::*;
|
||||
use naive::{MIN_DATE, MAX_DATE, NaiveDate, NaiveTime};
|
||||
use offset::{TimeZone, UTC, FixedOffset};
|
||||
use offset::{TimeZone, Utc, FixedOffset};
|
||||
|
||||
#[test]
|
||||
fn test_parsed_set_fields() {
|
||||
|
@ -1055,11 +1055,11 @@ mod tests {
|
|||
}
|
||||
|
||||
// single result from ymdhms
|
||||
assert_eq!(parse!(UTC;
|
||||
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;
|
||||
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));
|
||||
|
@ -1074,9 +1074,9 @@ mod tests {
|
|||
.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),
|
||||
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));
|
||||
|
|
74
src/lib.rs
74
src/lib.rs
|
@ -91,7 +91,7 @@
|
|||
//! which defines how the local date is converted to and back from the UTC date.
|
||||
//! There are three well-known `TimeZone` implementations:
|
||||
//!
|
||||
//! * [**`UTC`**](./offset/struct.UTC.html) specifies the UTC time zone. It is most efficient.
|
||||
//! * [**`Utc`**](./offset/struct.Utc.html) specifies the UTC time zone. It is most efficient.
|
||||
//!
|
||||
//! * [**`Local`**](./offset/struct.Local.html) specifies the system local time zone.
|
||||
//!
|
||||
|
@ -106,14 +106,14 @@
|
|||
//! the [`DateTime::with_timezone`](./struct.DateTime.html#method.with_timezone) method.
|
||||
//!
|
||||
//! You can get the current date and time in the UTC time zone
|
||||
//! ([`UTC::now()`](./offset/struct.UTC.html#method.now))
|
||||
//! ([`Utc::now()`](./offset/struct.Utc.html#method.now))
|
||||
//! or in the local time zone
|
||||
//! ([`Local::now()`](./offset/struct.Local.html#method.now)).
|
||||
//!
|
||||
//! ```rust
|
||||
//! use chrono::prelude::*;
|
||||
//!
|
||||
//! let utc: DateTime<UTC> = UTC::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
|
||||
//! let utc: DateTime<Utc> = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
|
||||
//! let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
|
||||
//! # let _ = utc; let _ = local;
|
||||
//! ```
|
||||
|
@ -126,21 +126,21 @@
|
|||
//! use chrono::prelude::*;
|
||||
//! use chrono::offset::LocalResult;
|
||||
//!
|
||||
//! let dt = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
|
||||
//! let dt = Utc.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z`
|
||||
//! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
|
||||
//! assert_eq!(dt, UTC.yo(2014, 189).and_hms(9, 10, 11));
|
||||
//! assert_eq!(dt, Utc.yo(2014, 189).and_hms(9, 10, 11));
|
||||
//! // July 8 is Tuesday in ISO week 28 of the year 2014.
|
||||
//! assert_eq!(dt, UTC.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
|
||||
//! assert_eq!(dt, Utc.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11));
|
||||
//!
|
||||
//! let dt = UTC.ymd(2014, 7, 8).and_hms_milli(9, 10, 11, 12); // `2014-07-08T09:10:11.012Z`
|
||||
//! 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));
|
||||
//! 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);
|
||||
//! 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.
|
||||
|
@ -179,7 +179,7 @@
|
|||
//! // 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));
|
||||
//! 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
|
||||
|
@ -187,14 +187,14 @@
|
|||
//! 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);
|
||||
//! 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));
|
||||
//! 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));
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
|
@ -211,7 +211,7 @@
|
|||
//! ```rust
|
||||
//! use chrono::prelude::*;
|
||||
//!
|
||||
//! let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
//! let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
//! assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09");
|
||||
//! assert_eq!(dt.format("%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());
|
||||
|
@ -226,7 +226,7 @@
|
|||
//!
|
||||
//! 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
|
||||
//! 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.
|
||||
|
@ -252,12 +252,12 @@
|
|||
//! ```rust
|
||||
//! use chrono::prelude::*;
|
||||
//!
|
||||
//! let dt = UTC.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
//! let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9);
|
||||
//! let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600));
|
||||
//!
|
||||
//! // 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-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
|
||||
|
@ -268,15 +268,15 @@
|
|||
//! 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()));
|
||||
//! 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());
|
||||
//! 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());
|
||||
//! 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());
|
||||
//! assert!(Utc.datetime_from_str("Sat Nov 28 12:00:09 2014", "%a %b %e %T %Y").is_err());
|
||||
//! ```
|
||||
//!
|
||||
//! ### Individual date
|
||||
|
@ -290,12 +290,12 @@
|
|||
//! 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!(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(),
|
||||
//! 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");
|
||||
//! ```
|
||||
//!
|
||||
|
@ -341,7 +341,7 @@
|
|||
//! 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`.
|
||||
//! and consequently `Utc.ymd(2014, 1, 30).with_month(2)` returns `None`.
|
||||
//!
|
||||
//! Advanced time zone handling is not yet supported.
|
||||
//! For now you can try the [Chrono-tz](https://github.com/chronotope/chrono-tz/) crate instead.
|
||||
|
@ -361,7 +361,7 @@ extern crate serde as serdelib;
|
|||
// this reexport is to aid the transition and should not be in the prelude!
|
||||
pub use oldtime::Duration;
|
||||
|
||||
#[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, UTC, FixedOffset, Local};
|
||||
#[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, Utc, FixedOffset, Local};
|
||||
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
|
||||
pub use date::{Date, MIN_DATE, MAX_DATE};
|
||||
pub use datetime::DateTime;
|
||||
|
@ -372,7 +372,7 @@ pub use format::{ParseError, ParseResult};
|
|||
pub mod prelude {
|
||||
#[doc(no_inline)] pub use {Datelike, Timelike, Weekday};
|
||||
#[doc(no_inline)] pub use {TimeZone, Offset};
|
||||
#[doc(no_inline)] pub use {UTC, FixedOffset, Local};
|
||||
#[doc(no_inline)] pub use {Utc, FixedOffset, Local};
|
||||
#[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime};
|
||||
#[doc(no_inline)] pub use Date;
|
||||
#[doc(no_inline)] pub use DateTime;
|
||||
|
|
|
@ -1571,7 +1571,7 @@ pub mod serde {
|
|||
/// # extern crate serde_json;
|
||||
/// # extern crate serde;
|
||||
/// # extern crate chrono;
|
||||
/// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, UTC};
|
||||
/// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, Utc};
|
||||
/// use chrono::naive::serde::ts_seconds;
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
|
@ -1614,7 +1614,7 @@ pub mod serde {
|
|||
/// # #[macro_use] extern crate serde_json;
|
||||
/// # extern crate serde;
|
||||
/// # extern crate chrono;
|
||||
/// # use chrono::{NaiveDateTime, UTC};
|
||||
/// # use chrono::{NaiveDateTime, Utc};
|
||||
/// # use serde::Deserialize;
|
||||
/// use chrono::naive::serde::ts_seconds::deserialize as from_ts;
|
||||
/// #[derive(Deserialize)]
|
||||
|
@ -1650,7 +1650,7 @@ pub mod serde {
|
|||
/// # #[macro_use] extern crate serde_json;
|
||||
/// # #[macro_use] extern crate serde;
|
||||
/// # extern crate chrono;
|
||||
/// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, UTC};
|
||||
/// # use chrono::{TimeZone, NaiveDate, NaiveDateTime, Utc};
|
||||
/// # use serde::Serialize;
|
||||
/// use chrono::naive::serde::ts_seconds::serialize as to_ts;
|
||||
/// #[derive(Serialize)]
|
||||
|
|
|
@ -58,13 +58,13 @@ use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItem
|
|||
/// All methods accepting fractional seconds will accept such values.
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{NaiveDate, NaiveTime, UTC, TimeZone};
|
||||
/// 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 dt2 = Utc.ymd(2015, 6, 30).and_hms_nano(23, 59, 59, 1_000_000_000);
|
||||
/// # let _ = (t, dt1, dt2);
|
||||
/// ~~~~
|
||||
///
|
||||
|
@ -141,9 +141,9 @@ use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItem
|
|||
/// will be represented as the second part being 60, as required by ISO 8601.
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{UTC, TimeZone};
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
///
|
||||
/// let dt = UTC.ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_000);
|
||||
/// let dt = Utc.ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_000);
|
||||
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");
|
||||
/// ~~~~
|
||||
///
|
||||
|
@ -155,12 +155,12 @@ use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItem
|
|||
/// and would be read back to the next non-leap second.
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{DateTime, UTC, TimeZone};
|
||||
/// use chrono::{DateTime, Utc, TimeZone};
|
||||
///
|
||||
/// let dt = UTC.ymd(2015, 6, 30).and_hms_milli(23, 56, 4, 1_000);
|
||||
/// 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);
|
||||
/// 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);
|
||||
/// ~~~~
|
||||
|
|
|
@ -178,9 +178,9 @@ pub trait TimeZone: Sized + Clone {
|
|||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{UTC, TimeZone};
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
///
|
||||
/// assert_eq!(UTC.ymd(2015, 5, 15).to_string(), "2015-05-15UTC");
|
||||
/// 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()
|
||||
|
@ -197,10 +197,10 @@ pub trait TimeZone: Sized + Clone {
|
|||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{UTC, LocalResult, TimeZone};
|
||||
/// 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);
|
||||
/// 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) {
|
||||
|
@ -220,9 +220,9 @@ pub trait TimeZone: Sized + Clone {
|
|||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{UTC, TimeZone};
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
///
|
||||
/// assert_eq!(UTC.yo(2015, 135).to_string(), "2015-05-15UTC");
|
||||
/// 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()
|
||||
|
@ -255,9 +255,9 @@ pub trait TimeZone: Sized + Clone {
|
|||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{UTC, Weekday, TimeZone};
|
||||
/// use chrono::{Utc, Weekday, TimeZone};
|
||||
///
|
||||
/// assert_eq!(UTC.isoywd(2015, 20, Weekday::Fri).to_string(), "2015-05-15UTC");
|
||||
/// 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()
|
||||
|
@ -288,9 +288,9 @@ pub trait TimeZone: Sized + Clone {
|
|||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{UTC, TimeZone};
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
///
|
||||
/// assert_eq!(UTC.timestamp(1431648000, 0).to_string(), "2015-05-15 00:00:00 UTC");
|
||||
/// 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()
|
||||
|
@ -371,7 +371,7 @@ mod utc;
|
|||
mod fixed;
|
||||
mod local;
|
||||
|
||||
pub use self::utc::UTC;
|
||||
pub use self::utc::Utc;
|
||||
pub use self::fixed::FixedOffset;
|
||||
pub use self::local::Local;
|
||||
|
||||
|
|
|
@ -14,59 +14,59 @@ use super::{TimeZone, Offset, LocalResult, FixedOffset};
|
|||
/// It is also used as an offset (which is also a dummy type).
|
||||
///
|
||||
/// Using the [`TimeZone`](./trait.TimeZone.html) methods
|
||||
/// on the UTC struct is the preferred way to construct `DateTime<UTC>`
|
||||
/// on the UTC struct is the preferred way to construct `DateTime<Utc>`
|
||||
/// instances.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ~~~~
|
||||
/// use chrono::{DateTime, TimeZone, NaiveDateTime, UTC};
|
||||
/// use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
|
||||
///
|
||||
/// let dt = DateTime::<UTC>::from_utc(NaiveDateTime::from_timestamp(61, 0), 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);
|
||||
/// 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;
|
||||
pub struct Utc;
|
||||
|
||||
impl UTC {
|
||||
impl Utc {
|
||||
/// Returns a `Date` which corresponds to the current date.
|
||||
pub fn today() -> Date<UTC> { UTC::now().date() }
|
||||
pub fn today() -> Date<Utc> { Utc::now().date() }
|
||||
|
||||
/// Returns a `DateTime` which corresponds to the current date.
|
||||
pub fn now() -> DateTime<UTC> {
|
||||
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)
|
||||
DateTime::from_utc(naive, Utc)
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeZone for UTC {
|
||||
type Offset = UTC;
|
||||
impl TimeZone for Utc {
|
||||
type Offset = Utc;
|
||||
|
||||
fn from_offset(_state: &UTC) -> UTC { 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_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_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}
|
||||
fn offset_from_utc_date(&self, _utc: &NaiveDate) -> Utc { Utc }
|
||||
fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> Utc { Utc }
|
||||
}
|
||||
|
||||
impl Offset for UTC {
|
||||
impl Offset for Utc {
|
||||
fn fix(&self) -> FixedOffset { FixedOffset::east(0) }
|
||||
}
|
||||
|
||||
impl fmt::Debug for UTC {
|
||||
impl fmt::Debug for Utc {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Z") }
|
||||
}
|
||||
|
||||
impl fmt::Display for UTC {
|
||||
impl fmt::Display for Utc {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "UTC") }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue