Commit Graph

62 Commits

Author SHA1 Message Date
Waldir Pimenta 176dfeff87 Reword 'same to' --> 'same as' 2019-12-29 22:27:59 +00:00
Michal Srb 05acc869b9 Accept Borrow<Item> as items
The parse::parse and format::format functions accepted Iterator of owned
Items. While it is sometimes convenient to pass in the owned values,
neither of the functions really need to own them, so references would
be enough. The Borrow trait allows us to pass in Iterator over values,
references, boxes, etc.

According to RFC 1105 this is a minor change, because it shouldn't break
any existing code. And chrono is in pre-1.0 version anyway.

This allows us to remove multiple cloned() calls which speeds up parsing
and formating:

 name                                                 control ns/iter  remove-cloned ns/iter  diff ns/iter   diff %  speedup
 datetime::tests::bench_datetime_from_str             712              582                            -130  -18.26%   x 1.22
 datetime::tests::bench_datetime_parse_from_rfc2822   252              244                              -8   -3.17%   x 1.03
 datetime::tests::bench_datetime_parse_from_rfc3339   242              239                              -3   -1.24%   x 1.01
2019-11-23 23:34:59 +01:00
Christopher Serr e62a054cd9 Introduce an `alloc` feature 2019-09-09 13:51:10 +02:00
Christopher Serr 9dc91f78ed Implement Support for no_std
This adds a new `std` feature to chrono that is enabled by default. By
deactivating this feature via `default-features = false` you can now use
chrono in applications that don't use the standard library. The `serde`
feature is supported as well.

Resolves #336
2019-09-07 12:12:49 +02:00
Toby Dimmick d5d5fd92b7 impl Sub<T> for <T> for Date/Time types 2018-04-03 13:42:03 +01:00
David Kellum 9742ecccc5 Allow (clippy) needless_pass_by_value on signed_duration_since
Current clippy is probably correct, but its a breaking change that
isn't appropriate now. Add allow's to get the build working again.

Also these Date(Time)::signed_duration_since cases appear to match
there `Naive` counterparts, where clippy isn't complaining. If its
fixed in the future, should probably be changed across the board, not
just here.
2018-01-17 11:49:06 -08:00
Brandon W Maister c63a012253 Fix Clippy lints: needless_lifetimes 2017-07-09 15:10:26 -05:00
Kang Seonghoon 7b9b0c4437
Renamed `chrono::UTC` to `chrono::Utc`.
Fixes #148.
2017-06-22 00:33:17 +09:00
Kang Seonghoon 9768b289f0
New type: `chrono::naive::IsoWeek`.
This removes `Datelike::isoweekdate` in favor of `Datelike::iso_week`.
The original `isoweekdate` was not named in accordance with the style
guide and also used to return the day of the week which is already
provided by `Datelike::weekday`. The new design should be more
reasonable.

Note that we initially do not implement any public constructor for
`IsoWeek`. That is, the only legitimate way to get a new `IsoWeek` is
from `Datelike::iso_week`. This sidesteps the issue of boundary values
(for example the year number in the maximal date will overflow in
the week date) while giving the same power as the original API.

Partially accounts for #139. We may add additional week types
as necessary---this is the beginning.
2017-06-22 00:21:24 +09:00
Kang Seonghoon 42a7c8e589
Fixed more documentation links.
Linkchecker recognizes the distinction between internal and external
links (which are not checked by default), and considers URLs which
does not have the starting URL base as a prefix internal...

This commit has been verified against a proper set of options to
Linkchecker, but there are several false positives (for our purposes)
which would make the automated checking not as effective. </rant>
2017-06-21 21:58:42 +09:00
Kang Seonghoon c06bc01f0b
Flattened intermediate implementation modules.
There used to be multiple modules like `chrono::datetime` which only
provide a single type `DateTime`. In retrospect, this module structure
never reflected how people use those types; with the release of 0.3.0
`chrono::prelude` is a preferred way to glob-import types, and due to
reexports `chrono::DateTime` and likes are also common enough.

Therefore this commit removes those implementation modules and
flattens the module structure. Specifically:

    Before                              After
    ----------------------------------  ----------------------------
    chrono:📅:Date                  chrono::Date
    chrono:📅:MIN                   chrono::MIN_DATE
    chrono:📅:MAX                   chrono::MAX_DATE
    chrono::datetime::DateTime          chrono::DateTime
    chrono::datetime::TsSeconds         chrono::TsSeconds
    chrono::datetime::serde::*          chrono::serde::*
    chrono::naive::time::NaiveTime      chrono::naive::NaiveTime
    chrono::naive:📅:NaiveDate      chrono::naive::NaiveDate
    chrono::naive:📅:MIN            chrono::naive::MIN_DATE
    chrono::naive:📅:MAX            chrono::naive::MAX_DATE
    chrono::naive::datetime::NaiveDateTime
                                        chrono::naive::NaiveDateTime
    chrono::naive::datetime::TsSeconds  chrono::naive::TsSeconds
    chrono::naive::datetime::serde::*   chrono::naive::serde::*
    chrono::offset::utc::UTC            chrono::offset::UTC
    chrono::offset::fixed::FixedOffset  chrono::offset::FixedOffset
    chrono::offset::local::Local        chrono::offset::Local
    chrono::format::parsed::Parsed      chrono::format::Parsed

All internal documentation links have been updated (phew!) and
verified with LinkChecker [1]. Probably we can automate this check
in the future.

[1] https://wummel.github.io/linkchecker/

Closes #161. Compared to the original proposal, `chrono::naive` is
retained as we had `TsSeconds` types duplicated for `NaiveDateTime`
and `DateTime` (legitimately).
2017-06-21 14:03:49 +09:00
Ben Boeckel ede06d6beb date: mention that `NaiveDate` is better for serialization
Fixes #92.
2017-05-05 19:02:42 +09:00
Kang Seonghoon e9e7bdd99c
Removed older aliases (previously marked as deprecated).
The intention was to add newer methods using `std::time::Duration`
to the older names, but it will break the API compatibility anyway.
Better to completely remove them right now.
2017-02-07 04:51:08 +09:00
Kang Seonghoon 7ea1ce5080
`FixedOffset` is now the official "fixed offset value" type.
This may sound strange, but the final type for the offset "value" was
originally `time::Duration` (returned by `Offset::local_minus_utc`).
This caused a lot of problems becaus adding `Duration` fully interacts
with leap seconds and `Duration` itself is somewhat deprecated.

This commit entirely replaces this role of `Duration` with
`FixedOffset`. So if we had `Offset` and `Duration` to represent
the "storage" offset type and the offset "value" in the past,
we now have `Offset` and `FixedOffset`. Storage-to-value conversion is
called to "fix" the offset---an apt term for the type.

The list of actual changes:

- The time zone offset is now restricted to UTC-23:59:59 through
  UTC+23:59:59, and no subsecond value is allowed. As described above,
  `FixedOffset` is now fully used for this purpose.

- One can now add and subtract `FixedOffset` to/from timelike values.
  Replaces a temporary `chrono::offset::add_with_leapsecond` function.
  Datelike & non-timelike values are never affected by the offset.

- UTC and local views to `Date<Tz>` are now identical. We keep
  relevant methods for the consistency right now.

- `chrono::format::format` now receives `FixedOffset` in place of
  `(Old)Duration`.

- `Offset` now has a `fix` method to resolve, or to "fix" the
  "storage" offset (`Offset`) to the offset "value" (`FixedOffset`).

- `FixedOffset::{local_minus_utc, utc_minus_local}` methods are added.
  They no longer depend on `Duration` as well.
2017-02-07 03:43:59 +09:00
Kang Seonghoon c118a3985f
Serialization cleanup for 0.3.
- Rustc-serialize now uses the same serialization format as Serde.
  This also means that the older format (naturally derived from
  the internals) is no longer supported.

- Serialization support only existed for rustc-serialize has been
  (temporarily) removed. This affects `Date<Tz>` and all individual
  time zone types. This does *not* affect `DateTime<Tz>` as it has
  individual support per `Tz`.

  Please note that this is considered a temporary solution to avoid
  stabilizing diverging implementations. Their implementations will
  likely be reintroduced later.
2017-02-07 03:08:01 +09:00
Kang Seonghoon c63ef14734
`time::Duration` is no longer the sole duration type described.
Due to the backward compatibility we won't be going to remove support
for `time::Duration` in 0.3, and the initial 0.3.0 release won't have
proper `std::time::Duration` support (haven't finalized the logics).
However we will reserve proper names and signatures for the upcoming
`std::time::Duration` support---the "older" duration type will be
referred as "signed" in the names.

- Added a `chrono::prelude` module. This does not have the (old)
  `Duration` type reexported, so the documentation has now correctly
  replaced all occurrences of `chrono::Duration`. The existing
  `chrono::Duration` reexport itself remains for the compatibility.

- Avoided using a plain `Duration` type in the signature, to avoid
  any ambiguity.

- Renamed `checked_{add,sub}` to `checked_{add,sub}_signed`.

- Subtraction operator between two instants has been removed and
  replaced with `signed_duration_since`. This follows the naming
  chosen by `std::time::SystemTime` etc., and the version for newer
  `std::time::Duration` will be named to `duration_since`.
2017-02-06 09:39:32 +09:00
Kang Seonghoon de4df91421
Removed all remaining mentions of rust-chrono (very old name). 2017-02-06 06:15:57 +09:00
Kang Seonghoon ad6253f653 Proper (de)serialization format handling.
For a while Chrono's serialization support was barely working,
i.e. usable but never been safe. While it wouldn't cause any memory
unsafety, attacker can fabricate an input that will make most users
confused (e.g. seemingly same Date which doesn't compare equally).
This commit will properly error for those cases.

It was also problematic that the generated rustc-serialize format is
very inefficient, especially for JSON. Due to the backward
compatibillity this commit does NOT fix them (likely to be in 0.3),
but this does try to define the exact format and define tons of
tests to detect any change to the serialization.

There are several remaining problems in the serialization format;
the serde implementation seems good, but it is unable to distinguish
some cases of leap seconds (practically won't matter, but still).
The rustc-serialize implementation would require a massive redesign.
For now, I postpone those issues to 0.3 (what a convenient excuse).

Fixes #42.
2016-08-04 03:22:12 +09:00
Kang Seonghoon 4025d617f5 Fixed #61.
Technically it was a problem of `DateTime::date` with a lax test.
Documented the intention and guarantees on `Date` as a result.
2016-02-05 01:23:46 +09:00
Kang Seonghoon 24bc15fdd3 more documentation for `NaiveTime`; some terminology updates. 2015-09-12 02:41:38 +09:00
Kang Seonghoon acf4eab102 more documentation, finishing examples for chrono::naive::date. 2015-09-06 21:30:09 +09:00
Jisoo Park b5281af9f3 Add optional rustc-serialize support 2015-04-28 18:42:18 +09:00
Kang Seonghoon 2be6e14446 Fixes #27.
This is due to somewhat ambiguous semantics of `Date`. It cannot
really constructed without an intermediate `DateTime` much like
the removed `Time`, but it is much more useful than `Time` so
we need some reasonable meaning to it. This commit clarifies
that meaning and corrects some problems around it:

- The date itself is timezone-agnostic unless the timezone itself
  has an offset equal to or greater than one day. In all current
  time zones, the date conversion should be a no-op.

- The date may be attached some offset; that offset should have
  been occurred within the corresponding day in either the local
  time or the UTC.

- `TimeZone` is free to assign the offset within this constraint.
  For convenience, the current `Local` time zone assumes the local
  midnight or the UTC midnight.
2015-03-03 02:37:20 +09:00
Kang Seonghoon 2dbc11dcb1 0.2.3: Fixed a couple of outstanding bugs.
- `DateTime<Tz>` and `Date<Tz>` is now `Copy`/`Send` when
  `Tz::Offset` is `Copy`/`Send`. The implementations for them were
  mistakenly omitted. Fixes #25.

- `Local::from_utc_datetime` didn't set a correct offset.
  The tests for `Local` were lacking. Fixes #26.
2015-02-27 13:08:20 +09:00
Kang Seonghoon c11b6deb5a 0.2.1: language changes.
- `std::hash` has been renewed.

- `DelayedFormat` no longer has a redundant lifetime.
2015-02-21 18:15:11 +09:00
Kang Seonghoon f39b13a14c removed timezone-aware `Time` type.
`Time` with an associated time zone is in principle possible, but
in practice it can only meaningfully constructed from an existing
`DateTime`. this makes it hard to implement other operations
natural to `NaiveTime` for `Time` (e.g. `with_*` methods), so
we simply let it go.

migration path: if you *do* happen to use `Time`, don't panic!
every operation possible to `Time` is much more possible to
`NaiveTime`. if you have to deal with a local time, first combine
it with a `NaiveDate`, convert it via `TimeZone::from_local_datetime`
then extract `Time` part again.
2015-02-19 04:17:27 +09:00
Kang Seonghoon 664c4d0191 merged the new offset design branch. 2015-02-19 01:48:29 +09:00
Kang Seonghoon a4f5d19d24 mass renaming from offset/state to timezone/offset. 2015-02-19 00:30:13 +09:00
Kang Seonghoon e43cb62f10 initial implementation of `Offset` redesign (#11).
- We have splitted `Offset` into `Offset` and `OffsetState` (name
  changes in consideration). The former is used to construct and convert
  local or UTC date, and the latter is used to store the UTC offset
  inside constructed values. Some offsets are their own states as well.

- This uses lots of associated types which implementation is still in
  flux. Currently it crashes with debuginfo enabled. We've temporarily
  disabled debuginfo from `Cargo.toml`.

- This technically allows a conversion to the local time, but not yet
  tested.
2015-02-19 00:25:04 +09:00
Kang Seonghoon c7f132cca2 added `format_with_items` methods to every types. 2015-02-14 12:34:31 +09:00
Kang Seonghoon 3d00a0fd5a added `checked_{add,sub}` methods to `[Naive]Date[Time]` types.
- Existing `+` and `-` operators use them, and properly panics with
  a correct error message on overflow/underflow.
2015-02-04 16:19:54 +09:00
Kang Seonghoon 43ee68b522 new formatter design!
- Format string is internally represented as a series of formatting
  items. Items can be directly given to now-public `format::format`
  function as well.

- Format string parser is separated to `format::strftime` module.
  This is to allow for potentional alternative formatting syntaxes.

- `DelayedFormat` now receives an iterator for formatting items.
2015-02-04 16:19:54 +09:00
Kang Seonghoon cf5e2f322f 0.1.15: language changes.
- `std::fmt::Show` is now `std::fmt::Debug`.
- `std::fmt::String` is now `std::fmt::Display`.
2015-01-24 17:45:12 +09:00
Kang Seonghoon ca84749869 0.1.13: language changes and fmt::String supports.
- Every type modulo `LocalResult` and `Offset` now implements
  `std::fmt::String`, so `.to_string()` can be used.
  The exact format has been changed for better looking output.

- `std::fmt::Show` are intended for "stricter" output,
  which mostly means the strict ISO 8601 format.
  `DelayedFormat` also implements this, but only for inspection.

- `Offset` should implement `Show` but can omit `String`.
  The old `name` method is merged into `String` implementations.
2015-01-10 03:27:24 +09:00
Kang Seonghoon d79c460fc4 0.1.12: language changes, removed ops for Duration in the lhs.
- Feature flags used are all accepted.
- Orphan check workaround is no longer required.
- Impl reachability rules prevent the addition of `Duration + others`,
  as `Duration` is not implemented in this crate. Removed the impl;
  use `others + Duration` as a workaround.
2015-01-08 02:08:41 +09:00
Kang Seonghoon 883b656d49 0.1.10: language changes.
- `std::str::SendStr` is now `std::string::CowString<'static>`.
2015-01-06 10:44:08 +09:00
Kang Seonghoon e2ddee2f76 0.1.9: language changes.
- `Add` and `Sub` switches to associated types.
2015-01-05 18:31:15 +09:00
Kang Seonghoon cde432ed8e bumped copyright years. 2015-01-04 15:08:25 +09:00
Ken Tossell 3303127415 Fixed build against rust nightly.
Added 'use' statements for cmd::Ordering, ops::{Add, Sub}, borrow::IntoCow, num::ToPrimitive

Changed deriving -> derive. Changed [x, ..N] -> [x; N].

Made format(f: &fmt::Formatter) return fmt::Result instead of
IoResult. Had to raise generic errors in a couple of cases where
IoResult's fields were being set (but these errors were being thrown
out anyway).

Temporarily set #![feature(old_orphan_check)] because

Thanks for @eddyb for explaining the format() situation.
2015-01-03 17:45:07 -05:00
Ken Tossell 221e58eca6 Removed reflexive type specifier from Date<Off>'s Eq implementation 2015-01-01 17:18:37 -05:00
Kang Seonghoon e982cd1851 0.1.5: language changes.
- Add and Sub requires a value instead of a reference.
- Tuple indexing is now ungated.

Fixes #15.
2014-12-17 10:29:48 +09:00
Kang Seonghoon 407ad383c5 fixed an edge case with date-changing offsets. 2014-12-13 21:53:53 +09:00
klutzy 37d85ffd3b Update for upstream changes 2014-12-13 20:29:06 +09:00
Kang Seonghoon 920a681d54 better documentation, some additional APIs.
- `Date/Time - Duration` is now supported. (duh!)
- `with_offset` methods have been added.
- `LocalResult` now implements common traits.
- `LocalResult` has several methods to propagate errors.
  this makes the initialization from untrusted sources easier
  (`off.ymd_opt(y,m,d).and_hms_opt(h,n,s).single()`).
2014-11-28 23:53:22 +09:00
Kang Seonghoon 86665d0a7f 0.1.2: no more `num` dependency, multidispatched addition.
- `num` dependency is gone. It was only used for floored division
  and it is not hard to copy only that portion from num.
- `Duration + Date` (or so) was blocked by rust-lang/rust#7590,
  which has been subsequently fixed.
- Removed unused `unsafe` checks.
2014-11-24 14:52:07 +09:00
Kang Seonghoon ec6c874042 language changes: namespaced enums, no more std::num::{Zero,Bounded}. 2014-11-19 10:59:32 +09:00
Eunchong Yu 850e1d8654 Follow rustc master (1add4dedc 2014-10-10) 2014-10-12 00:56:49 +09:00
Kang Seonghoon 80ed400689 added missing `.offset()` methods; UTC/FixedOffset now implement Eq. 2014-08-29 18:14:08 +09:00
Kang Seonghoon f1931ad21e added `chrono::format` module and `format` methods.
this is a saner replacement for `time::strftime`; it does not allocate
the additional memory.
2014-08-01 04:08:19 +09:00
Kang Seonghoon 95f5c0c095 splitted naive types into their own modules. 2014-07-29 16:17:26 +09:00