Skip to content

Commit

Permalink
0.1.5: language changes.
Browse files Browse the repository at this point in the history
- Add and Sub requires a value instead of a reference.
- Tuple indexing is now ungated.

Fixes #15.
  • Loading branch information
lifthrasiir committed Dec 17, 2014
1 parent 0d8f836 commit e982cd1
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 47 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "chrono"
version = "0.1.4"
version = "0.1.5"
authors = ["Kang Seonghoon <public+rust@mearie.org>"]

description = "Date and time library for Rust"
homepage = "https://github.com/lifthrasiir/rust-chrono"
documentation = "https://lifthrasiir.github.io/rust-chrono/chrono/"
repository = "https://github.com/lifthrasiir/rust-chrono"
keywords = ["date", "time", "calendar"]
readme = "README.md"
license = "MIT/Apache-2.0"

Expand Down
12 changes: 5 additions & 7 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,23 @@ impl<Off:Offset> hash::Hash for Date<Off> {
}

impl<Off:Offset> Add<Duration,Date<Off>> for Date<Off> {
fn add(&self, rhs: &Duration) -> Date<Off> {
Date { date: self.date + *rhs, offset: self.offset.clone() }
fn add(self, rhs: Duration) -> Date<Off> {
Date { date: self.date + rhs, offset: self.offset }
}
}

impl<Off:Offset> Add<Date<Off>,Date<Off>> for Duration {
#[inline]
fn add(&self, rhs: &Date<Off>) -> Date<Off> { rhs.add(self) }
fn add(self, rhs: Date<Off>) -> Date<Off> { rhs.add(self) }
}

impl<Off:Offset, Off2:Offset> Sub<Date<Off2>,Duration> for Date<Off> {
fn sub(&self, rhs: &Date<Off2>) -> Duration {
self.date - rhs.date
}
fn sub(self, rhs: Date<Off2>) -> Duration { self.date - rhs.date }
}

impl<Off:Offset> Sub<Duration,Date<Off>> for Date<Off> {
#[inline]
fn sub(&self, rhs: &Duration) -> Date<Off> { self.add(&-*rhs) }
fn sub(self, rhs: Duration) -> Date<Off> { self.add(-rhs) }
}

impl<Off:Offset> fmt::Show for Date<Off> {
Expand Down
12 changes: 5 additions & 7 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,23 @@ impl<Off:Offset> hash::Hash for DateTime<Off> {
}

impl<Off:Offset> Add<Duration,DateTime<Off>> for DateTime<Off> {
fn add(&self, rhs: &Duration) -> DateTime<Off> {
DateTime { datetime: self.datetime + *rhs, offset: self.offset.clone() }
fn add(self, rhs: Duration) -> DateTime<Off> {
DateTime { datetime: self.datetime + rhs, offset: self.offset }
}
}

impl<Off:Offset> Add<DateTime<Off>,DateTime<Off>> for Duration {
#[inline]
fn add(&self, rhs: &DateTime<Off>) -> DateTime<Off> { rhs.add(self) }
fn add(self, rhs: DateTime<Off>) -> DateTime<Off> { rhs.add(self) }
}

impl<Off:Offset, Off2:Offset> Sub<DateTime<Off2>,Duration> for DateTime<Off> {
fn sub(&self, rhs: &DateTime<Off2>) -> Duration {
self.datetime - rhs.datetime
}
fn sub(self, rhs: DateTime<Off2>) -> Duration { self.datetime - rhs.datetime }
}

impl<Off:Offset> Sub<Duration,DateTime<Off>> for DateTime<Off> {
#[inline]
fn sub(&self, rhs: &Duration) -> DateTime<Off> { self.add(&-*rhs) }
fn sub(self, rhs: Duration) -> DateTime<Off> { self.add(-rhs) }
}

impl<Off:Offset> fmt::Show for DateTime<Off> {
Expand Down
14 changes: 7 additions & 7 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ fn format(w: &mut Writer, date: Option<&NaiveDate>, time: Option<&NaiveTime>,
(Some('Y'), Some(d), _, _) => try!(write!(w, "{}", d.year())),
(Some('C'), Some(d), _, _) => try!(write!(w, "{:02}", d.year() / 100)),
(Some('y'), Some(d), _, _) => try!(write!(w, "{:02}", d.year() % 100)),
(Some('G'), Some(d), _, _) => try!(write!(w, "{:04}", d.isoweekdate().val0())),
(Some('g'), Some(d), _, _) => try!(write!(w, "{:02}", d.isoweekdate().val0() % 100)),
(Some('G'), Some(d), _, _) => try!(write!(w, "{:04}", d.isoweekdate().0)),
(Some('g'), Some(d), _, _) => try!(write!(w, "{:02}", d.isoweekdate().0 % 100)),

// month
(Some('m'), Some(d), _, _) => try!(write!(w, "{:02}", d.month())),
Expand All @@ -73,7 +73,7 @@ fn format(w: &mut Writer, date: Option<&NaiveDate>, time: Option<&NaiveTime>,
(Some('W'), Some(d), _, _) =>
try!(write!(w, "{:02}", (d.ordinal() - d.weekday().num_days_from_monday()
+ 7) / 7)),
(Some('V'), Some(d), _, _) => try!(write!(w, "{:02}", d.isoweekdate().val1())),
(Some('V'), Some(d), _, _) => try!(write!(w, "{:02}", d.isoweekdate().1)),

// day of week
(Some('a'), Some(d), _, _) =>
Expand All @@ -98,12 +98,12 @@ fn format(w: &mut Writer, date: Option<&NaiveDate>, time: Option<&NaiveTime>,
// hour
(Some('H'), _, Some(t), _) => try!(write!(w, "{:02}", t.hour())),
(Some('k'), _, Some(t), _) => try!(write!(w, "{:2}", t.hour())),
(Some('I'), _, Some(t), _) => try!(write!(w, "{:02}", t.hour12().val1())),
(Some('l'), _, Some(t), _) => try!(write!(w, "{:2}", t.hour12().val1())),
(Some('I'), _, Some(t), _) => try!(write!(w, "{:02}", t.hour12().1)),
(Some('l'), _, Some(t), _) => try!(write!(w, "{:2}", t.hour12().1)),
(Some('P'), _, Some(t), _) =>
try!(write!(w, "{}", if t.hour12().val0() {"pm"} else {"am"})),
try!(write!(w, "{}", if t.hour12().0 {"pm"} else {"am"})),
(Some('p'), _, Some(t), _) =>
try!(write!(w, "{}", if t.hour12().val0() {"PM"} else {"AM"})),
try!(write!(w, "{}", if t.hour12().0 {"PM"} else {"AM"})),

// minute
(Some('M'), _, Some(t), _) => try!(write!(w, "{:02}", t.minute())),
Expand Down
8 changes: 4 additions & 4 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl Datelike for NaiveDate {
}

impl Add<Duration,NaiveDate> for NaiveDate {
fn add(&self, rhs: &Duration) -> NaiveDate {
fn add(self, rhs: Duration) -> NaiveDate {
// TODO overflow currently fails

let year = self.year();
Expand All @@ -397,11 +397,11 @@ impl Add<Duration,NaiveDate> for NaiveDate {

impl Add<NaiveDate,NaiveDate> for Duration {
#[inline]
fn add(&self, rhs: &NaiveDate) -> NaiveDate { rhs.add(self) }
fn add(self, rhs: NaiveDate) -> NaiveDate { rhs.add(self) }
}

impl Sub<NaiveDate,Duration> for NaiveDate {
fn sub(&self, rhs: &NaiveDate) -> Duration {
fn sub(self, rhs: NaiveDate) -> Duration {
let year1 = self.year();
let year2 = rhs.year();
let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400);
Expand All @@ -414,7 +414,7 @@ impl Sub<NaiveDate,Duration> for NaiveDate {

impl Sub<Duration,NaiveDate> for NaiveDate {
#[inline]
fn sub(&self, rhs: &Duration) -> NaiveDate { self.add(&-*rhs) }
fn sub(self, rhs: Duration) -> NaiveDate { self.add(-rhs) }
}

impl fmt::Show for NaiveDate {
Expand Down
12 changes: 6 additions & 6 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ impl Timelike for NaiveDateTime {
}

impl Add<Duration,NaiveDateTime> for NaiveDateTime {
fn add(&self, rhs: &Duration) -> NaiveDateTime {
fn add(self, rhs: Duration) -> NaiveDateTime {
// Duration does not directly give its parts, so we need some additional calculations.
let days = rhs.num_days();
let nanos = (*rhs - Duration::days(days)).num_nanoseconds().unwrap();
debug_assert!(Duration::days(days) + Duration::nanoseconds(nanos) == *rhs);
let nanos = (rhs - Duration::days(days)).num_nanoseconds().unwrap();
debug_assert!(Duration::days(days) + Duration::nanoseconds(nanos) == rhs);
debug_assert!(-86400_000_000_000 < nanos && nanos < 86400_000_000_000);

let mut date = self.date + Duration::days(days);
Expand All @@ -185,18 +185,18 @@ impl Add<Duration,NaiveDateTime> for NaiveDateTime {

impl Add<NaiveDateTime,NaiveDateTime> for Duration {
#[inline]
fn add(&self, rhs: &NaiveDateTime) -> NaiveDateTime { rhs.add(self) }
fn add(self, rhs: NaiveDateTime) -> NaiveDateTime { rhs.add(self) }
}

impl Sub<NaiveDateTime,Duration> for NaiveDateTime {
fn sub(&self, rhs: &NaiveDateTime) -> Duration {
fn sub(self, rhs: NaiveDateTime) -> Duration {
(self.date - rhs.date) + (self.time - rhs.time)
}
}

impl Sub<Duration,NaiveDateTime> for NaiveDateTime {
#[inline]
fn sub(&self, rhs: &Duration) -> NaiveDateTime { self.add(&-*rhs) }
fn sub(self, rhs: Duration) -> NaiveDateTime { self.add(-rhs) }
}

impl fmt::Show for NaiveDateTime {
Expand Down
16 changes: 8 additions & 8 deletions src/naive/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ impl NaiveTime {
}

impl Timelike for NaiveTime {
#[inline] fn hour(&self) -> u32 { self.hms().val0() }
#[inline] fn minute(&self) -> u32 { self.hms().val1() }
#[inline] fn second(&self) -> u32 { self.hms().val2() }
#[inline] fn hour(&self) -> u32 { self.hms().0 }
#[inline] fn minute(&self) -> u32 { self.hms().1 }
#[inline] fn second(&self) -> u32 { self.hms().2 }
#[inline] fn nanosecond(&self) -> u32 { self.frac }

#[inline]
Expand Down Expand Up @@ -172,10 +172,10 @@ impl Timelike for NaiveTime {
}

impl Add<Duration,NaiveTime> for NaiveTime {
fn add(&self, rhs: &Duration) -> NaiveTime {
fn add(self, rhs: Duration) -> NaiveTime {
// there is no direct interface in `Duration` to get only the nanosecond part,
// so we need to do the additional calculation here.
let rhs2 = *rhs - Duration::seconds(rhs.num_seconds());
let rhs2 = rhs - Duration::seconds(rhs.num_seconds());
let mut secs = self.secs + (rhs.num_seconds() % 86400 + 86400) as u32;
let mut nanos = self.frac + rhs2.num_nanoseconds().unwrap() as u32;

Expand All @@ -192,11 +192,11 @@ impl Add<Duration,NaiveTime> for NaiveTime {

impl Add<NaiveTime,NaiveTime> for Duration {
#[inline]
fn add(&self, rhs: &NaiveTime) -> NaiveTime { rhs.add(self) }
fn add(self, rhs: NaiveTime) -> NaiveTime { rhs.add(self) }
}

impl Sub<NaiveTime,Duration> for NaiveTime {
fn sub(&self, rhs: &NaiveTime) -> Duration {
fn sub(self, rhs: NaiveTime) -> Duration {
// the number of whole non-leap seconds
let secs = self.secs as i64 - rhs.secs as i64 - 1;

Expand All @@ -214,7 +214,7 @@ impl Sub<NaiveTime,Duration> for NaiveTime {

impl Sub<Duration,NaiveTime> for NaiveTime {
#[inline]
fn sub(&self, rhs: &Duration) -> NaiveTime { self.add(&-*rhs) }
fn sub(self, rhs: Duration) -> NaiveTime { self.add(-rhs) }
}

impl fmt::Show for NaiveTime {
Expand Down
12 changes: 5 additions & 7 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,23 @@ impl<Off:Offset> hash::Hash for Time<Off> {
}

impl<Off:Offset> Add<Duration,Time<Off>> for Time<Off> {
fn add(&self, rhs: &Duration) -> Time<Off> {
Time { time: self.time + *rhs, offset: self.offset.clone() }
fn add(self, rhs: Duration) -> Time<Off> {
Time { time: self.time + rhs, offset: self.offset }
}
}

impl<Off:Offset> Add<Time<Off>,Time<Off>> for Duration {
#[inline]
fn add(&self, rhs: &Time<Off>) -> Time<Off> { rhs.add(self) }
fn add(self, rhs: Time<Off>) -> Time<Off> { rhs.add(self) }
}

impl<Off:Offset, Off2:Offset> Sub<Time<Off2>,Duration> for Time<Off> {
fn sub(&self, rhs: &Time<Off2>) -> Duration {
self.time - rhs.time
}
fn sub(self, rhs: Time<Off2>) -> Duration { self.time - rhs.time }
}

impl<Off:Offset> Sub<Duration,Time<Off>> for Time<Off> {
#[inline]
fn sub(&self, rhs: &Duration) -> Time<Off> { self.add(&-*rhs) }
fn sub(self, rhs: Duration) -> Time<Off> { self.add(-rhs) }
}

impl<Off:Offset> fmt::Show for Time<Off> {
Expand Down

0 comments on commit e982cd1

Please sign in to comment.