Skip to content

Commit cdb88ad

Browse files
committed
rebase
readme updates fix broken function name remove wayward install-cross
1 parent 84f98e0 commit cdb88ad

File tree

19 files changed

+1618
-1449
lines changed

19 files changed

+1618
-1449
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ appveyor = { repository = "chronotope/chrono" }
2020
name = "chrono"
2121

2222
[features]
23-
default = ["clock", "std", "oldtime", "wasmbind"]
23+
default = ["clock", "std", "wasmbind"]
2424
alloc = []
2525
libc = []
2626
std = []
2727
clock = ["std", "winapi", "iana-time-zone"]
28-
oldtime = ["time"]
2928
wasmbind = ["wasm-bindgen", "js-sys"]
3029
unstable-locales = ["pure-rust-locales", "alloc"]
3130
__internal_bench = ["criterion"]
3231
__doctest = []
3332

3433
[dependencies]
35-
time = { version = "0.1.43", optional = true }
3634
num-integer = { version = "0.1.36", default-features = false }
3735
num-traits = { version = "0.2", default-features = false }
3836
rustc-serialize = { version = "0.3.20", optional = true }

README.md

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
[gitter-image]: https://badges.gitter.im/chrono-rs/chrono.svg
1616
[gitter]: https://gitter.im/chrono-rs/chrono
1717

18-
It aims to be a feature-complete superset of
19-
the [time](https://github.com/rust-lang-deprecated/time) library.
20-
In particular,
21-
2218
* Chrono strictly adheres to ISO 8601.
2319
* Chrono is timezone-aware by default, with separate timezone-naive types.
2420
* Chrono is space-optimal and (while not being the primary goal) reasonably efficient.
@@ -74,32 +70,17 @@ See the [cargo docs][] for examples of specifying features.
7470

7571
## Overview
7672

77-
### Duration
73+
### TimeDelta
7874

79-
Chrono currently uses its own [`Duration`] type to represent the magnitude
80-
of a time span. Since this has the same name as the newer, standard type for
81-
duration, the reference will refer this type as `OldDuration`.
75+
Chrono currently uses its own [`TimeDelta`] type to represent the magnitude
76+
of a time span. This is seperate from the [`Duration`] type in the `std`/`core` library as it
77+
supports negative and positive durations. This type can be converted losslessly from [`Duration`]
78+
but when converting into a [`Duration`] you must take the absolute value.
8279

83-
Note that this is an "accurate" duration represented as seconds and
80+
Note that this is an "accurate" TimeDelta represented as seconds and
8481
nanoseconds and does not represent "nominal" components such as days or
85-
months.
86-
87-
When the `oldtime` feature is enabled, [`Duration`] is an alias for the
88-
[`time::Duration`](https://docs.rs/time/0.1.40/time/struct.Duration.html)
89-
type from v0.1 of the time crate. time v0.1 is deprecated, so new code
90-
should disable the `oldtime` feature and use the `chrono::Duration` type
91-
instead. The `oldtime` feature is enabled by default for backwards
92-
compatibility, but future versions of Chrono are likely to remove the
93-
feature entirely.
94-
95-
Chrono does not yet natively support
96-
the standard [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) type,
97-
but it will be supported in the future.
98-
Meanwhile you can convert between two types with
99-
[`Duration::from_std`](https://docs.rs/time/0.1.40/time/struct.Duration.html#method.from_std)
100-
and
101-
[`Duration::to_std`](https://docs.rs/time/0.1.40/time/struct.Duration.html#method.to_std)
102-
methods.
82+
months. These are supported via the [`NaiveDate::succ`] and [`NaiveDate::pred`] functions
83+
as well as the [`Months`] data type.
10384

10485
### Date and Time
10586

@@ -184,7 +165,7 @@ The following illustrates most supported operations to the date and time:
184165

185166
```rust
186167
use chrono::prelude::*;
187-
use chrono::Duration;
168+
use chrono::TimeDelta;
188169

189170
// assume this returned `2014-11-28T21:45:59.324310806+09:00`:
190171
let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806);
@@ -211,11 +192,11 @@ assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November
211192
// arithmetic operations
212193
let dt1 = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
213194
let dt2 = Utc.ymd(2014, 11, 14).and_hms(10, 9, 8);
214-
assert_eq!(dt1.signed_duration_since(dt2), Duration::seconds(-2 * 3600 + 2));
215-
assert_eq!(dt2.signed_duration_since(dt1), Duration::seconds(2 * 3600 - 2));
216-
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) + Duration::seconds(1_000_000_000),
195+
assert_eq!(dt1.signed_signed_duration_since(dt2), TimeDelta::seconds(-2 * 3600 + 2));
196+
assert_eq!(dt2.signed_signed_duration_since(dt1), TimeDelta::seconds(2 * 3600 - 2));
197+
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) + TimeDelta::seconds(1_000_000_000),
217198
Utc.ymd(2001, 9, 9).and_hms(1, 46, 40));
218-
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_000),
199+
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) - TimeDelta::seconds(1_000_000_000),
219200
Utc.ymd(1938, 4, 24).and_hms(22, 13, 20));
220201
```
221202

ci/github.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ source "${BASH_SOURCE[0]%/*}/_shlib.sh"
88
TEST_TZS=(ACST-9:30 EST4 UTC0 Asia/Katmandu)
99
FEATURES=(std serde clock "alloc serde" unstable-locales)
1010
CHECK_FEATURES=(alloc "std unstable-locales" "serde clock" "clock unstable-locales")
11-
RUST_132_FEATURES=(rustc-serialize serde)
11+
RUST_136_FEATURES=(rustc-serialize serde)
1212

1313
main() {
1414
if [[ "$*" =~ "-h" ]]; then
@@ -52,7 +52,7 @@ meaningful in the github actions feature matrix UI.
5252
test_regular UTC0
5353
fi
5454
elif [[ ${RUST_VERSION:-} == 1.38.0 ]]; then
55-
test_132
55+
test_138
5656
else
5757
echo "ERROR: didn't run any tests"
5858
exit 1
@@ -82,9 +82,9 @@ check_combinatoric() {
8282
done
8383
}
8484

85-
test_132() {
85+
test_138() {
8686
runv cargo build --color=always
87-
for feature in "${RUST_132_FEATURES[@]}"; do
87+
for feature in "${RUST_136_FEATURES[@]}"; do
8888
runt cargo build --features "$feature" --color=always
8989
done
9090
}

src/date.rs

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use core::borrow::Borrow;
88
use core::cmp::Ordering;
99
use core::ops::{Add, AddAssign, Sub, SubAssign};
10+
use core::time::Duration;
1011
use core::{fmt, hash};
1112

1213
#[cfg(feature = "rkyv")]
@@ -18,8 +19,8 @@ use crate::format::Locale;
1819
use crate::format::{DelayedFormat, Item, StrftimeItems};
1920
use crate::naive::{IsoWeek, NaiveDate, NaiveTime};
2021
use crate::offset::{TimeZone, Utc};
21-
use crate::oldtime::Duration as OldDuration;
2222
use crate::DateTime;
23+
use crate::TimeDelta;
2324
use crate::{Datelike, Weekday};
2425

2526
/// ISO 8601 calendar date with time zone.
@@ -52,7 +53,7 @@ use crate::{Datelike, Weekday};
5253
///
5354
/// - The date is timezone-agnostic up to one day (i.e. practically always),
5455
/// so the local date and UTC date should be equal for most cases
55-
/// even though the raw calculation between `NaiveDate` and `Duration` may not.
56+
/// even though the raw calculation between `NaiveDate` and `TimeDelta` may not.
5657
#[derive(Clone)]
5758
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
5859
pub struct Date<Tz: TimeZone> {
@@ -236,31 +237,31 @@ impl<Tz: TimeZone> Date<Tz> {
236237
tz.from_utc_date(&self.date)
237238
}
238239

239-
/// Adds given `Duration` to the current date.
240+
/// Adds given `TimeDelta` to the current date.
240241
///
241242
/// Returns `None` when it will result in overflow.
242243
#[inline]
243-
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
244+
pub fn checked_add_signed(self, rhs: TimeDelta) -> Option<Date<Tz>> {
244245
let date = self.date.checked_add_signed(rhs)?;
245246
Some(Date { date, offset: self.offset })
246247
}
247248

248-
/// Subtracts given `Duration` from the current date.
249+
/// Subtracts given `TimeDelta` from the current date.
249250
///
250251
/// Returns `None` when it will result in overflow.
251252
#[inline]
252-
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
253+
pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option<Date<Tz>> {
253254
let date = self.date.checked_sub_signed(rhs)?;
254255
Some(Date { date, offset: self.offset })
255256
}
256257

257258
/// Subtracts another `Date` from the current date.
258-
/// Returns a `Duration` of integral numbers.
259+
/// Returns a `TimeDelta` of integral numbers.
259260
///
260261
/// This does not overflow or underflow at all,
261-
/// as all possible output fits in the range of `Duration`.
262+
/// as all possible output fits in the range of `TimeDelta`.
262263
#[inline]
263-
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: Date<Tz2>) -> OldDuration {
264+
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: Date<Tz2>) -> TimeDelta {
264265
self.date.signed_duration_since(rhs.date)
265266
}
266267

@@ -479,43 +480,80 @@ impl<Tz: TimeZone> hash::Hash for Date<Tz> {
479480
}
480481
}
481482

482-
impl<Tz: TimeZone> Add<OldDuration> for Date<Tz> {
483+
impl<Tz: TimeZone> Add<TimeDelta> for Date<Tz> {
483484
type Output = Date<Tz>;
484485

485486
#[inline]
486-
fn add(self, rhs: OldDuration) -> Date<Tz> {
487-
self.checked_add_signed(rhs).expect("`Date + Duration` overflowed")
487+
fn add(self, rhs: TimeDelta) -> Date<Tz> {
488+
self.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed")
489+
}
490+
}
491+
impl<Tz: TimeZone> Add<Duration> for Date<Tz> {
492+
type Output = Date<Tz>;
493+
494+
#[inline]
495+
fn add(self, rhs: Duration) -> Date<Tz> {
496+
self.checked_add_signed(rhs.into()).expect("`Date + core::time::Duration` overflowed")
488497
}
489498
}
490499

491-
impl<Tz: TimeZone> AddAssign<OldDuration> for Date<Tz> {
500+
impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz> {
492501
#[inline]
493-
fn add_assign(&mut self, rhs: OldDuration) {
494-
self.date = self.date.checked_add_signed(rhs).expect("`Date + Duration` overflowed");
502+
fn add_assign(&mut self, rhs: TimeDelta) {
503+
self.date = self.date.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed");
495504
}
496505
}
497506

498-
impl<Tz: TimeZone> Sub<OldDuration> for Date<Tz> {
507+
impl<Tz: TimeZone> AddAssign<Duration> for Date<Tz> {
508+
#[inline]
509+
fn add_assign(&mut self, rhs: Duration) {
510+
self.date = self
511+
.date
512+
.checked_add_signed(rhs.into())
513+
.expect("`Date + core::time::Duration` overflowed");
514+
}
515+
}
516+
517+
impl<Tz: TimeZone> Sub<TimeDelta> for Date<Tz> {
499518
type Output = Date<Tz>;
500519

501520
#[inline]
502-
fn sub(self, rhs: OldDuration) -> Date<Tz> {
503-
self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed")
521+
fn sub(self, rhs: TimeDelta) -> Date<Tz> {
522+
self.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed")
523+
}
524+
}
525+
526+
impl<Tz: TimeZone> Sub<Duration> for Date<Tz> {
527+
type Output = Date<Tz>;
528+
529+
#[inline]
530+
fn sub(self, rhs: Duration) -> Date<Tz> {
531+
self.checked_sub_signed(rhs.into()).expect("`Date - core::time::Duration` overflowed")
532+
}
533+
}
534+
535+
impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz> {
536+
#[inline]
537+
fn sub_assign(&mut self, rhs: TimeDelta) {
538+
self.date = self.date.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed");
504539
}
505540
}
506541

507-
impl<Tz: TimeZone> SubAssign<OldDuration> for Date<Tz> {
542+
impl<Tz: TimeZone> SubAssign<Duration> for Date<Tz> {
508543
#[inline]
509-
fn sub_assign(&mut self, rhs: OldDuration) {
510-
self.date = self.date.checked_sub_signed(rhs).expect("`Date - Duration` overflowed");
544+
fn sub_assign(&mut self, rhs: Duration) {
545+
self.date = self
546+
.date
547+
.checked_sub_signed(rhs.into())
548+
.expect("`Date - core::time::Duration` overflowed");
511549
}
512550
}
513551

514552
impl<Tz: TimeZone> Sub<Date<Tz>> for Date<Tz> {
515-
type Output = OldDuration;
553+
type Output = TimeDelta;
516554

517555
#[inline]
518-
fn sub(self, rhs: Date<Tz>) -> OldDuration {
556+
fn sub(self, rhs: Date<Tz>) -> TimeDelta {
519557
self.signed_duration_since(rhs)
520558
}
521559
}
@@ -539,7 +577,7 @@ where
539577
mod tests {
540578
use super::Date;
541579

542-
use crate::oldtime::Duration;
580+
use crate::TimeDelta;
543581
use crate::{FixedOffset, NaiveDate, Utc};
544582

545583
#[cfg(feature = "clock")]
@@ -551,15 +589,15 @@ mod tests {
551589
const WEEKS_PER_YEAR: f32 = 52.1775;
552590

553591
// This is always at least one year because 1 year = 52.1775 weeks.
554-
let one_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64);
592+
let one_year_ago = Utc::today() - TimeDelta::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64);
555593
// A bit more than 2 years.
556-
let two_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64);
594+
let two_year_ago = Utc::today() - TimeDelta::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64);
557595

558596
assert_eq!(Utc::today().years_since(one_year_ago), Some(1));
559597
assert_eq!(Utc::today().years_since(two_year_ago), Some(2));
560598

561599
// If the given DateTime is later than now, the function will always return 0.
562-
let future = Utc::today() + Duration::weeks(12);
600+
let future = Utc::today() + TimeDelta::weeks(12);
563601
assert_eq!(Utc::today().years_since(future), None);
564602
}
565603

@@ -569,20 +607,20 @@ mod tests {
569607
let date = Date::<Utc>::from_utc(naivedate, Utc);
570608
let mut date_add = date;
571609

572-
date_add += Duration::days(5);
573-
assert_eq!(date_add, date + Duration::days(5));
610+
date_add += TimeDelta::days(5);
611+
assert_eq!(date_add, date + TimeDelta::days(5));
574612

575613
let timezone = FixedOffset::east(60 * 60);
576614
let date = date.with_timezone(&timezone);
577615
let date_add = date_add.with_timezone(&timezone);
578616

579-
assert_eq!(date_add, date + Duration::days(5));
617+
assert_eq!(date_add, date + TimeDelta::days(5));
580618

581619
let timezone = FixedOffset::west(2 * 60 * 60);
582620
let date = date.with_timezone(&timezone);
583621
let date_add = date_add.with_timezone(&timezone);
584622

585-
assert_eq!(date_add, date + Duration::days(5));
623+
assert_eq!(date_add, date + TimeDelta::days(5));
586624
}
587625

588626
#[test]
@@ -593,8 +631,8 @@ mod tests {
593631
let date = Local.from_utc_date(&naivedate);
594632
let mut date_add = date;
595633

596-
date_add += Duration::days(5);
597-
assert_eq!(date_add, date + Duration::days(5));
634+
date_add += TimeDelta::days(5);
635+
assert_eq!(date_add, date + TimeDelta::days(5));
598636
}
599637

600638
#[test]
@@ -603,20 +641,20 @@ mod tests {
603641
let date = Date::<Utc>::from_utc(naivedate, Utc);
604642
let mut date_sub = date;
605643

606-
date_sub -= Duration::days(5);
607-
assert_eq!(date_sub, date - Duration::days(5));
644+
date_sub -= TimeDelta::days(5);
645+
assert_eq!(date_sub, date - TimeDelta::days(5));
608646

609647
let timezone = FixedOffset::east(60 * 60);
610648
let date = date.with_timezone(&timezone);
611649
let date_sub = date_sub.with_timezone(&timezone);
612650

613-
assert_eq!(date_sub, date - Duration::days(5));
651+
assert_eq!(date_sub, date - TimeDelta::days(5));
614652

615653
let timezone = FixedOffset::west(2 * 60 * 60);
616654
let date = date.with_timezone(&timezone);
617655
let date_sub = date_sub.with_timezone(&timezone);
618656

619-
assert_eq!(date_sub, date - Duration::days(5));
657+
assert_eq!(date_sub, date - TimeDelta::days(5));
620658
}
621659

622660
#[test]
@@ -627,7 +665,7 @@ mod tests {
627665
let date = Local.from_utc_date(&naivedate);
628666
let mut date_sub = date;
629667

630-
date_sub -= Duration::days(5);
631-
assert_eq!(date_sub, date - Duration::days(5));
668+
date_sub -= TimeDelta::days(5);
669+
assert_eq!(date_sub, date - TimeDelta::days(5));
632670
}
633671
}

0 commit comments

Comments
 (0)