Merge pull request #164 from quodlibetor/enable-clippy
Enable Clippy for all builds in Travis
This commit is contained in:
commit
91d5dd46cf
16
.travis.sh
16
.travis.sh
|
@ -46,11 +46,25 @@ build_only() {
|
||||||
channel build -v --features 'serde bincode'
|
channel build -v --features 'serde bincode'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run_clippy() {
|
||||||
|
# cached installation will not work on a later nightly
|
||||||
|
if [ -n "${TRAVIS}" ] && ! cargo install clippy --debug --force; then
|
||||||
|
echo "COULD NOT COMPILE CLIPPY, IGNORING CLIPPY TESTS"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
cargo clippy --features 'serde bincode rustc-serialize' -- -Dclippy
|
||||||
|
}
|
||||||
|
|
||||||
rustc --version
|
rustc --version
|
||||||
cargo --version
|
cargo --version
|
||||||
|
|
||||||
CHANNEL=nightly
|
CHANNEL=nightly
|
||||||
build_and_test
|
if [ "x${CLIPPY}" = xy ] ; then
|
||||||
|
run_clippy
|
||||||
|
else
|
||||||
|
build_and_test
|
||||||
|
fi
|
||||||
|
|
||||||
CHANNEL=beta
|
CHANNEL=beta
|
||||||
build_and_test
|
build_and_test
|
||||||
|
|
|
@ -12,9 +12,15 @@ os:
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rust: nightly
|
- rust: nightly
|
||||||
|
env: CLIPPY=n
|
||||||
|
include:
|
||||||
|
- rust: nightly
|
||||||
|
env: CLIPPY=y
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- LD_LIBRARY_PATH: /usr/local/lib
|
- LD_LIBRARY_PATH: /usr/local/lib
|
||||||
|
- CLIPPY: n
|
||||||
script: ./.travis.sh
|
script: ./.travis.sh
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
|
@ -184,7 +184,7 @@ impl<Tz: TimeZone> Date<Tz> {
|
||||||
|
|
||||||
/// Retrieves an associated offset from UTC.
|
/// Retrieves an associated offset from UTC.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn offset<'a>(&'a self) -> &'a Tz::Offset {
|
pub fn offset(&self) -> &Tz::Offset {
|
||||||
&self.offset
|
&self.offset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
use std::{str, fmt, hash};
|
use std::{str, fmt, hash};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::ops::{Add, Sub};
|
use std::ops::{Add, Sub};
|
||||||
#[cfg(feature = "rustc-serialize")]
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use oldtime::Duration as OldDuration;
|
use oldtime::Duration as OldDuration;
|
||||||
|
|
||||||
|
@ -29,30 +27,6 @@ pub struct DateTime<Tz: TimeZone> {
|
||||||
offset: Tz::Offset,
|
offset: Tz::Offset,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A DateTime that can be deserialized from a timestamp
|
|
||||||
///
|
|
||||||
/// A timestamp here is seconds since the epoch
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
|
||||||
pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);
|
|
||||||
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
|
||||||
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
|
|
||||||
/// Pull the inner DateTime<Tz> out
|
|
||||||
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
|
|
||||||
obj.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
|
||||||
impl<Tz: TimeZone> Deref for TsSeconds<Tz> {
|
|
||||||
type Target = DateTime<Tz>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl<Tz: TimeZone> DateTime<Tz> {
|
impl<Tz: TimeZone> DateTime<Tz> {
|
||||||
/// Makes a new `DateTime` with given *UTC* datetime and offset.
|
/// Makes a new `DateTime` with given *UTC* datetime and offset.
|
||||||
/// The local datetime should be constructed via the `TimeZone` trait.
|
/// The local datetime should be constructed via the `TimeZone` trait.
|
||||||
|
@ -124,7 +98,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||||
|
|
||||||
/// Retrieves an associated offset from UTC.
|
/// Retrieves an associated offset from UTC.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn offset<'a>(&'a self) -> &'a Tz::Offset {
|
pub fn offset(&self) -> &Tz::Offset {
|
||||||
&self.offset
|
&self.offset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -517,9 +491,9 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
|
||||||
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,
|
fixed_from_str: FFixed,
|
||||||
local_from_str: FLocal)
|
local_from_str: FLocal)
|
||||||
where FUtc: Fn(&str) -> Result<TsSeconds<Utc>, E>,
|
where FUtc: Fn(&str) -> Result<rustc_serialize::TsSeconds<Utc>, E>,
|
||||||
FFixed: Fn(&str) -> Result<TsSeconds<FixedOffset>, E>,
|
FFixed: Fn(&str) -> Result<rustc_serialize::TsSeconds<FixedOffset>, E>,
|
||||||
FLocal: Fn(&str) -> Result<TsSeconds<Local>, E>,
|
FLocal: Fn(&str) -> Result<rustc_serialize::TsSeconds<Local>, E>,
|
||||||
E: ::std::fmt::Debug
|
E: ::std::fmt::Debug
|
||||||
{
|
{
|
||||||
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
|
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
|
||||||
|
@ -543,9 +517,10 @@ fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
#[cfg(feature = "rustc-serialize")]
|
||||||
mod rustc_serialize {
|
pub mod rustc_serialize {
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use super::{DateTime, TsSeconds};
|
use std::ops::Deref;
|
||||||
|
use super::DateTime;
|
||||||
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
|
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
|
||||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||||
|
|
||||||
|
@ -579,7 +554,7 @@ mod rustc_serialize {
|
||||||
impl Decodable for TsSeconds<FixedOffset> {
|
impl Decodable for TsSeconds<FixedOffset> {
|
||||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<FixedOffset>, D::Error> {
|
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<FixedOffset>, D::Error> {
|
||||||
from(FixedOffset::east(0).timestamp_opt(d.read_i64()?, 0), d)
|
from(FixedOffset::east(0).timestamp_opt(d.read_i64()?, 0), d)
|
||||||
.map(|dt| TsSeconds(dt))
|
.map(TsSeconds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -592,10 +567,31 @@ mod rustc_serialize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A `DateTime` that can be deserialized from a timestamp
|
||||||
|
///
|
||||||
|
/// A timestamp here is seconds since the epoch
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);
|
||||||
|
|
||||||
|
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
|
||||||
|
/// Pull the inner DateTime<Tz> out
|
||||||
|
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
|
||||||
|
obj.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Tz: TimeZone> Deref for TsSeconds<Tz> {
|
||||||
|
type Target = DateTime<Tz>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Decodable for TsSeconds<Utc> {
|
impl Decodable for TsSeconds<Utc> {
|
||||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> {
|
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> {
|
||||||
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
|
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
|
||||||
.map(|dt| TsSeconds(dt))
|
.map(TsSeconds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -685,7 +681,7 @@ pub mod serde {
|
||||||
use {DateTime, Utc, FixedOffset};
|
use {DateTime, Utc, FixedOffset};
|
||||||
use offset::{LocalResult, TimeZone};
|
use offset::{LocalResult, TimeZone};
|
||||||
|
|
||||||
/// Deserialize a DateTime from a seconds timestamp
|
/// Deserialize a `DateTime` from a seconds timestamp
|
||||||
///
|
///
|
||||||
/// Intended for use with `serde`s `deserialize_with` attribute.
|
/// Intended for use with `serde`s `deserialize_with` attribute.
|
||||||
///
|
///
|
||||||
|
@ -771,19 +767,19 @@ pub mod serde {
|
||||||
fn visit_i64<E>(self, value: i64) -> Result<DateTime<FixedOffset>, E>
|
fn visit_i64<E>(self, value: i64) -> Result<DateTime<FixedOffset>, E>
|
||||||
where E: de::Error
|
where E: de::Error
|
||||||
{
|
{
|
||||||
from(FixedOffset::east(0).timestamp_opt(value, 0), value)
|
from(FixedOffset::east(0).timestamp_opt(value, 0), &value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a timestamp in seconds since the epoch
|
/// Deserialize a timestamp in seconds since the epoch
|
||||||
fn visit_u64<E>(self, value: u64) -> Result<DateTime<FixedOffset>, E>
|
fn visit_u64<E>(self, value: u64) -> Result<DateTime<FixedOffset>, E>
|
||||||
where E: de::Error
|
where E: de::Error
|
||||||
{
|
{
|
||||||
from(FixedOffset::east(0).timestamp_opt(value as i64, 0), value)
|
from(FixedOffset::east(0).timestamp_opt(value as i64, 0), &value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// try!-like function to convert a LocalResult into a serde-ish Result
|
// try!-like function to convert a LocalResult into a serde-ish Result
|
||||||
fn from<T, E, V>(me: LocalResult<T>, ts: V) -> Result<T, E>
|
fn from<T, E, V>(me: LocalResult<T>, ts: &V) -> Result<T, E>
|
||||||
where E: de::Error,
|
where E: de::Error,
|
||||||
V: fmt::Display,
|
V: fmt::Display,
|
||||||
T: fmt::Display,
|
T: fmt::Display,
|
||||||
|
|
|
@ -230,9 +230,9 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
|
||||||
|
|
||||||
Item::Numeric(spec, _pad) => {
|
Item::Numeric(spec, _pad) => {
|
||||||
use super::Numeric::*;
|
use super::Numeric::*;
|
||||||
|
type Setter = fn(&mut Parsed, i64) -> ParseResult<()>;
|
||||||
|
|
||||||
let (width, signed, set): (usize, bool,
|
let (width, signed, set): (usize, bool, Setter) = match spec {
|
||||||
fn(&mut Parsed, i64) -> ParseResult<()>) = match spec {
|
|
||||||
Year => (4, true, Parsed::set_year),
|
Year => (4, true, Parsed::set_year),
|
||||||
YearDiv100 => (2, false, Parsed::set_year_div_100),
|
YearDiv100 => (2, false, Parsed::set_year_div_100),
|
||||||
YearMod100 => (2, false, Parsed::set_year_mod_100),
|
YearMod100 => (2, false, Parsed::set_year_mod_100),
|
||||||
|
|
|
@ -538,7 +538,7 @@ impl Parsed {
|
||||||
// it's okay, just do not try to overwrite the existing field.
|
// it's okay, just do not try to overwrite the existing field.
|
||||||
59 => {}
|
59 => {}
|
||||||
// `datetime` is known to be off by one second.
|
// `datetime` is known to be off by one second.
|
||||||
0 => { datetime = datetime - OldDuration::seconds(1); }
|
0 => { datetime -= OldDuration::seconds(1); }
|
||||||
// otherwise it is impossible.
|
// otherwise it is impossible.
|
||||||
_ => return Err(IMPOSSIBLE)
|
_ => return Err(IMPOSSIBLE)
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,8 @@ pub fn number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)> {
|
||||||
if window.len() > max { window = &window[..max]; }
|
if window.len() > max { window = &window[..max]; }
|
||||||
|
|
||||||
// scan digits
|
// scan digits
|
||||||
let upto = window.iter().position(|&c| c < b'0' || b'9' < c).unwrap_or(window.len());
|
let upto = window.iter().position(|&c| c < b'0' || b'9' < c)
|
||||||
|
.unwrap_or_else(|| window.len());
|
||||||
if upto < min {
|
if upto < min {
|
||||||
return Err(if window.is_empty() {TOO_SHORT} else {INVALID});
|
return Err(if window.is_empty() {TOO_SHORT} else {INVALID});
|
||||||
}
|
}
|
||||||
|
@ -224,28 +225,24 @@ pub fn timezone_offset_zulu<F>(s: &str, colon: F) -> ParseResult<(&str, i32)>
|
||||||
pub fn timezone_offset_2822(s: &str) -> ParseResult<(&str, Option<i32>)> {
|
pub fn timezone_offset_2822(s: &str) -> ParseResult<(&str, Option<i32>)> {
|
||||||
// tries to parse legacy time zone names
|
// 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,
|
let upto = s.as_bytes().iter().position(|&c| match c { b'a'...b'z' | b'A'...b'Z' => false,
|
||||||
_ => true }).unwrap_or(s.len());
|
_ => true })
|
||||||
|
.unwrap_or_else(|| s.len());
|
||||||
if upto > 0 {
|
if upto > 0 {
|
||||||
let name = &s[..upto];
|
let name = &s[..upto];
|
||||||
let s = &s[upto..];
|
let s = &s[upto..];
|
||||||
|
let offset_hours = |o| Ok((s, Some(o * 3600)));
|
||||||
if equals(name, "gmt") || equals(name, "ut") {
|
if equals(name, "gmt") || equals(name, "ut") {
|
||||||
Ok((s, Some(0)))
|
offset_hours(0)
|
||||||
} else if equals(name, "est") {
|
|
||||||
Ok((s, Some(-5 * 3600)))
|
|
||||||
} else if equals(name, "edt") {
|
} else if equals(name, "edt") {
|
||||||
Ok((s, Some(-4 * 3600)))
|
offset_hours(-4)
|
||||||
} else if equals(name, "cst") {
|
} else if equals(name, "est") || equals(name, "cdt") {
|
||||||
Ok((s, Some(-6 * 3600)))
|
offset_hours(-5)
|
||||||
} else if equals(name, "cdt") {
|
} else if equals(name, "cst") || equals(name, "mdt") {
|
||||||
Ok((s, Some(-5 * 3600)))
|
offset_hours(-6)
|
||||||
} else if equals(name, "mst") {
|
} else if equals(name, "mst") || equals(name, "pdt") {
|
||||||
Ok((s, Some(-7 * 3600)))
|
offset_hours(-7)
|
||||||
} else if equals(name, "mdt") {
|
|
||||||
Ok((s, Some(-6 * 3600)))
|
|
||||||
} else if equals(name, "pst") {
|
} else if equals(name, "pst") {
|
||||||
Ok((s, Some(-8 * 3600)))
|
offset_hours(-8)
|
||||||
} else if equals(name, "pdt") {
|
|
||||||
Ok((s, Some(-7 * 3600)))
|
|
||||||
} else {
|
} else {
|
||||||
Ok((s, None)) // recommended by RFC 2822: consume but treat it as -0000
|
Ok((s, None)) // recommended by RFC 2822: consume but treat it as -0000
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,7 @@ Notes:
|
||||||
use super::{Item, Numeric, Fixed, Pad};
|
use super::{Item, Numeric, Fixed, Pad};
|
||||||
|
|
||||||
/// Parsing iterator for `strftime`-like format strings.
|
/// Parsing iterator for `strftime`-like format strings.
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct StrftimeItems<'a> {
|
pub struct StrftimeItems<'a> {
|
||||||
/// Remaining portion of the string.
|
/// Remaining portion of the string.
|
||||||
remainder: &'a str,
|
remainder: &'a str,
|
||||||
|
@ -180,7 +180,7 @@ impl<'a> Iterator for StrftimeItems<'a> {
|
||||||
|
|
||||||
match self.remainder.chars().next() {
|
match self.remainder.chars().next() {
|
||||||
// we are done
|
// we are done
|
||||||
None => return None,
|
None => None,
|
||||||
|
|
||||||
// the next item is a specifier
|
// the next item is a specifier
|
||||||
Some('%') => {
|
Some('%') => {
|
||||||
|
@ -237,7 +237,7 @@ impl<'a> Iterator for StrftimeItems<'a> {
|
||||||
'Y' => num0!(Year),
|
'Y' => num0!(Year),
|
||||||
'Z' => fix!(TimezoneName),
|
'Z' => fix!(TimezoneName),
|
||||||
'a' => fix!(ShortWeekdayName),
|
'a' => fix!(ShortWeekdayName),
|
||||||
'b' => fix!(ShortMonthName),
|
'b' | 'h' => fix!(ShortMonthName),
|
||||||
'c' => recons![fix!(ShortWeekdayName), sp!(" "), fix!(ShortMonthName),
|
'c' => recons![fix!(ShortWeekdayName), sp!(" "), fix!(ShortMonthName),
|
||||||
sp!(" "), nums!(Day), sp!(" "), num0!(Hour), lit!(":"),
|
sp!(" "), nums!(Day), sp!(" "), num0!(Hour), lit!(":"),
|
||||||
num0!(Minute), lit!(":"), num0!(Second), sp!(" "), num0!(Year)],
|
num0!(Minute), lit!(":"), num0!(Second), sp!(" "), num0!(Year)],
|
||||||
|
@ -245,7 +245,6 @@ impl<'a> Iterator for StrftimeItems<'a> {
|
||||||
'e' => nums!(Day),
|
'e' => nums!(Day),
|
||||||
'f' => num0!(Nanosecond),
|
'f' => num0!(Nanosecond),
|
||||||
'g' => num0!(IsoYearMod100),
|
'g' => num0!(IsoYearMod100),
|
||||||
'h' => fix!(ShortMonthName),
|
|
||||||
'j' => num0!(Ordinal),
|
'j' => num0!(Ordinal),
|
||||||
'k' => nums!(Hour),
|
'k' => nums!(Hour),
|
||||||
'l' => nums!(Hour12),
|
'l' => nums!(Hour12),
|
||||||
|
@ -305,7 +304,7 @@ impl<'a> Iterator for StrftimeItems<'a> {
|
||||||
Some(c) if c.is_whitespace() => {
|
Some(c) if c.is_whitespace() => {
|
||||||
// `%` is not a whitespace, so `c != '%'` is redundant
|
// `%` is not a whitespace, so `c != '%'` is redundant
|
||||||
let nextspec = self.remainder.find(|c: char| !c.is_whitespace())
|
let nextspec = self.remainder.find(|c: char| !c.is_whitespace())
|
||||||
.unwrap_or(self.remainder.len());
|
.unwrap_or_else(|| self.remainder.len());
|
||||||
assert!(nextspec > 0);
|
assert!(nextspec > 0);
|
||||||
let item = sp!(&self.remainder[..nextspec]);
|
let item = sp!(&self.remainder[..nextspec]);
|
||||||
self.remainder = &self.remainder[nextspec..];
|
self.remainder = &self.remainder[nextspec..];
|
||||||
|
@ -315,7 +314,7 @@ impl<'a> Iterator for StrftimeItems<'a> {
|
||||||
// the next item is literal
|
// the next item is literal
|
||||||
_ => {
|
_ => {
|
||||||
let nextspec = self.remainder.find(|c: char| c.is_whitespace() || c == '%')
|
let nextspec = self.remainder.find(|c: char| c.is_whitespace() || c == '%')
|
||||||
.unwrap_or(self.remainder.len());
|
.unwrap_or_else(|| self.remainder.len());
|
||||||
assert!(nextspec > 0);
|
assert!(nextspec > 0);
|
||||||
let item = lit!(&self.remainder[..nextspec]);
|
let item = lit!(&self.remainder[..nextspec]);
|
||||||
self.remainder = &self.remainder[nextspec..];
|
self.remainder = &self.remainder[nextspec..];
|
||||||
|
|
|
@ -359,6 +359,7 @@
|
||||||
|
|
||||||
#![cfg_attr(bench, feature(test))] // lib stability features as per RFC #507
|
#![cfg_attr(bench, feature(test))] // lib stability features as per RFC #507
|
||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
|
#![deny(missing_debug_implementations)]
|
||||||
|
|
||||||
extern crate time as oldtime;
|
extern crate time as oldtime;
|
||||||
extern crate num;
|
extern crate num;
|
||||||
|
@ -374,7 +375,7 @@ pub use oldtime::Duration;
|
||||||
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
|
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
|
||||||
pub use date::{Date, MIN_DATE, MAX_DATE};
|
pub use date::{Date, MIN_DATE, MAX_DATE};
|
||||||
pub use datetime::DateTime;
|
pub use datetime::DateTime;
|
||||||
#[cfg(feature = "rustc-serialize")] pub use datetime::TsSeconds;
|
#[cfg(feature = "rustc-serialize")] pub use datetime::rustc_serialize::TsSeconds;
|
||||||
pub use format::{ParseError, ParseResult};
|
pub use format::{ParseError, ParseResult};
|
||||||
|
|
||||||
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
|
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
|
||||||
|
@ -410,7 +411,10 @@ pub mod naive {
|
||||||
pub use self::date::{NaiveDate, MIN_DATE, MAX_DATE};
|
pub use self::date::{NaiveDate, MIN_DATE, MAX_DATE};
|
||||||
pub use self::isoweek::IsoWeek;
|
pub use self::isoweek::IsoWeek;
|
||||||
pub use self::time::NaiveTime;
|
pub use self::time::NaiveTime;
|
||||||
pub use self::datetime::{NaiveDateTime, TsSeconds};
|
pub use self::datetime::NaiveDateTime;
|
||||||
|
#[cfg(feature = "rustc-serialize")]
|
||||||
|
pub use self::datetime::rustc_serialize::TsSeconds;
|
||||||
|
|
||||||
|
|
||||||
/// Serialization/Deserialization of naive types in alternate formats
|
/// Serialization/Deserialization of naive types in alternate formats
|
||||||
///
|
///
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
//! ISO 8601 calendar date without timezone.
|
//! ISO 8601 calendar date without timezone.
|
||||||
|
|
||||||
use std::{str, fmt, hash};
|
use std::{str, fmt};
|
||||||
use std::ops::{Add, Sub, AddAssign, SubAssign};
|
use std::ops::{Add, Sub, AddAssign, SubAssign};
|
||||||
use num::traits::ToPrimitive;
|
use num::traits::ToPrimitive;
|
||||||
use oldtime::Duration as OldDuration;
|
use oldtime::Duration as OldDuration;
|
||||||
|
@ -93,7 +93,7 @@ const MAX_BITS: usize = 44;
|
||||||
/// The year number is same to that of the [calendar date](#calendar-date).
|
/// The year number is same to that of the [calendar date](#calendar-date).
|
||||||
///
|
///
|
||||||
/// This is currently the internal format of Chrono's date types.
|
/// This is currently the internal format of Chrono's date types.
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
|
||||||
pub struct NaiveDate {
|
pub struct NaiveDate {
|
||||||
ymdf: DateImpl, // (year << 13) | of
|
ymdf: DateImpl, // (year << 13) | of
|
||||||
}
|
}
|
||||||
|
@ -1302,14 +1302,6 @@ impl Datelike for NaiveDate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `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,
|
/// An addition of `Duration` to `NaiveDate` discards the fractional days,
|
||||||
/// rounding to the closest integral number of days towards `Duration::zero()`.
|
/// rounding to the closest integral number of days towards `Duration::zero()`.
|
||||||
///
|
///
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
//! ISO 8601 date and time without timezone.
|
//! ISO 8601 date and time without timezone.
|
||||||
|
|
||||||
use std::{str, fmt, hash};
|
use std::{str, fmt, hash};
|
||||||
use std::ops::{Add, Sub, Deref, AddAssign, SubAssign};
|
use std::ops::{Add, Sub, AddAssign, SubAssign};
|
||||||
use num::traits::ToPrimitive;
|
use num::traits::ToPrimitive;
|
||||||
use oldtime::Duration as OldDuration;
|
use oldtime::Duration as OldDuration;
|
||||||
|
|
||||||
|
@ -53,24 +53,6 @@ pub struct NaiveDateTime {
|
||||||
time: NaiveTime,
|
time: NaiveTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A DateTime that can be deserialized from a seconds-based timestamp
|
|
||||||
pub struct TsSeconds(NaiveDateTime);
|
|
||||||
|
|
||||||
impl From<TsSeconds> for NaiveDateTime {
|
|
||||||
/// Pull the internal NaiveDateTime out
|
|
||||||
fn from(obj: TsSeconds) -> NaiveDateTime {
|
|
||||||
obj.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for TsSeconds {
|
|
||||||
type Target = NaiveDateTime;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NaiveDateTime {
|
impl NaiveDateTime {
|
||||||
/// Makes a new `NaiveDateTime` from date and time components.
|
/// Makes a new `NaiveDateTime` from date and time components.
|
||||||
/// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
|
/// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
|
||||||
|
@ -153,7 +135,7 @@ impl NaiveDateTime {
|
||||||
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
|
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
|
||||||
let (days, secs) = div_mod_floor(secs, 86400);
|
let (days, secs) = div_mod_floor(secs, 86400);
|
||||||
let date = days.to_i32().and_then(|days| days.checked_add(719163))
|
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));
|
.and_then(NaiveDate::from_num_days_from_ce_opt);
|
||||||
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
|
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
|
||||||
match (date, time) {
|
match (date, time) {
|
||||||
(Some(date), Some(time)) => Some(NaiveDateTime { date: date, time: time }),
|
(Some(date), Some(time)) => Some(NaiveDateTime { date: date, time: time }),
|
||||||
|
@ -1121,6 +1103,7 @@ impl Timelike for NaiveDateTime {
|
||||||
///
|
///
|
||||||
/// Practically this also takes account of fractional seconds, so it is not recommended.
|
/// 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.)
|
/// (For the obvious reason this also distinguishes leap seconds from non-leap seconds.)
|
||||||
|
#[cfg_attr(feature = "cargo-clippy", allow(derive_hash_xor_eq))]
|
||||||
impl hash::Hash for NaiveDateTime {
|
impl hash::Hash for NaiveDateTime {
|
||||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
||||||
self.date.hash(state);
|
self.date.hash(state);
|
||||||
|
@ -1459,7 +1442,7 @@ fn test_decodable_json<F, E>(from_str: F)
|
||||||
|
|
||||||
#[cfg(all(test, feature = "rustc-serialize"))]
|
#[cfg(all(test, feature = "rustc-serialize"))]
|
||||||
fn test_decodable_json_timestamp<F, E>(from_str: F)
|
fn test_decodable_json_timestamp<F, E>(from_str: F)
|
||||||
where F: Fn(&str) -> Result<TsSeconds, E>, E: ::std::fmt::Debug
|
where F: Fn(&str) -> Result<rustc_serialize::TsSeconds, E>, E: ::std::fmt::Debug
|
||||||
{
|
{
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
*from_str("0").unwrap(),
|
*from_str("0").unwrap(),
|
||||||
|
@ -1474,8 +1457,9 @@ fn test_decodable_json_timestamp<F, E>(from_str: F)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
#[cfg(feature = "rustc-serialize")]
|
||||||
mod rustc_serialize {
|
pub mod rustc_serialize {
|
||||||
use super::{NaiveDateTime, TsSeconds};
|
use std::ops::Deref;
|
||||||
|
use super::NaiveDateTime;
|
||||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||||
|
|
||||||
impl Encodable for NaiveDateTime {
|
impl Encodable for NaiveDateTime {
|
||||||
|
@ -1490,6 +1474,25 @@ mod rustc_serialize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A `DateTime` that can be deserialized from a seconds-based timestamp
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TsSeconds(NaiveDateTime);
|
||||||
|
|
||||||
|
impl From<TsSeconds> for NaiveDateTime {
|
||||||
|
/// Pull the internal NaiveDateTime out
|
||||||
|
fn from(obj: TsSeconds) -> NaiveDateTime {
|
||||||
|
obj.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for TsSeconds {
|
||||||
|
type Target = NaiveDateTime;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Decodable for TsSeconds {
|
impl Decodable for TsSeconds {
|
||||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds, D::Error> {
|
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds, D::Error> {
|
||||||
Ok(TsSeconds(
|
Ok(TsSeconds(
|
||||||
|
@ -1518,14 +1521,14 @@ mod rustc_serialize {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tools to help serializing/deserializing NaiveDateTimes
|
/// Tools to help serializing/deserializing `NaiveDateTime`s
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
pub mod serde {
|
pub mod serde {
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use super::{NaiveDateTime};
|
use super::{NaiveDateTime};
|
||||||
use serdelib::{ser, de};
|
use serdelib::{ser, de};
|
||||||
|
|
||||||
/// Serialize a NaiveDateTime as an RFC 3339 string
|
/// Serialize a `NaiveDateTime` as an RFC 3339 string
|
||||||
///
|
///
|
||||||
/// See [the `serde` module](./serde/index.html) for alternate
|
/// See [the `serde` module](./serde/index.html) for alternate
|
||||||
/// serialization formats.
|
/// serialization formats.
|
||||||
|
@ -1613,7 +1616,7 @@ pub mod serde {
|
||||||
|
|
||||||
use NaiveDateTime;
|
use NaiveDateTime;
|
||||||
|
|
||||||
/// Deserialize a DateTime from a seconds timestamp
|
/// Deserialize a `DateTime` from a seconds timestamp
|
||||||
///
|
///
|
||||||
/// Intended for use with `serde`s `deserialize_with` attribute.
|
/// Intended for use with `serde`s `deserialize_with` attribute.
|
||||||
///
|
///
|
||||||
|
|
|
@ -518,6 +518,7 @@ impl NaiveTime {
|
||||||
/// (from_hms(20, 4, 5), -86400));
|
/// (from_hms(20, 4, 5), -86400));
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~~
|
/// ~~~~
|
||||||
|
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
|
||||||
pub fn overflowing_add_signed(&self, mut rhs: OldDuration) -> (NaiveTime, i64) {
|
pub fn overflowing_add_signed(&self, mut rhs: OldDuration) -> (NaiveTime, i64) {
|
||||||
let mut secs = self.secs;
|
let mut secs = self.secs;
|
||||||
let mut frac = self.frac;
|
let mut frac = self.frac;
|
||||||
|
@ -545,13 +546,13 @@ impl NaiveTime {
|
||||||
|
|
||||||
let rhssecs = rhs.num_seconds();
|
let rhssecs = rhs.num_seconds();
|
||||||
let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap();
|
let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap();
|
||||||
debug_assert!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac) == rhs);
|
debug_assert_eq!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac), rhs);
|
||||||
let rhssecsinday = rhssecs % 86400;
|
let rhssecsinday = rhssecs % 86400;
|
||||||
let mut morerhssecs = rhssecs - rhssecsinday;
|
let mut morerhssecs = rhssecs - rhssecsinday;
|
||||||
let rhssecs = rhssecsinday as i32;
|
let rhssecs = rhssecsinday as i32;
|
||||||
let rhsfrac = rhsfrac as i32;
|
let rhsfrac = rhsfrac as i32;
|
||||||
debug_assert!(-86400 < rhssecs && rhssecs < 86400);
|
debug_assert!(-86400 < rhssecs && rhssecs < 86400);
|
||||||
debug_assert!(morerhssecs % 86400 == 0);
|
debug_assert_eq!(morerhssecs % 86400, 0);
|
||||||
debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000);
|
debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000);
|
||||||
|
|
||||||
let mut secs = secs as i32 + rhssecs;
|
let mut secs = secs as i32 + rhssecs;
|
||||||
|
@ -980,6 +981,7 @@ impl Timelike for NaiveTime {
|
||||||
///
|
///
|
||||||
/// Practically this also takes account of fractional seconds, so it is not recommended.
|
/// 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.)
|
/// (For the obvious reason this also distinguishes leap seconds from non-leap seconds.)
|
||||||
|
#[cfg_attr(feature = "cargo-clippy", allow(derive_hash_xor_eq))]
|
||||||
impl hash::Hash for NaiveTime {
|
impl hash::Hash for NaiveTime {
|
||||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
||||||
self.secs.hash(state);
|
self.secs.hash(state);
|
||||||
|
|
|
@ -77,7 +77,7 @@ fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> oldtime::Timespec {
|
||||||
/// let dt: DateTime<Local> = Local::now();
|
/// let dt: DateTime<Local> = Local::now();
|
||||||
/// let dt: DateTime<Local> = Local.timestamp(0, 0);
|
/// let dt: DateTime<Local> = Local.timestamp(0, 0);
|
||||||
/// ~~~~
|
/// ~~~~
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct Local;
|
pub struct Local;
|
||||||
|
|
||||||
impl Local {
|
impl Local {
|
||||||
|
|
Loading…
Reference in New Issue