Skip to content

Commit f153a1c

Browse files
committed
v0.3 backports
1 parent c4619ea commit f153a1c

12 files changed

+304
-171
lines changed

CHANGELOG.md

+19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ Versioning].
77

88
---
99

10+
## 0.2.23 [2020-11-17]
11+
12+
## Compatibility notes
13+
14+
Due to #293, any method that requires knowledge of the local offset will now
15+
_fail_ on Linux. For `try_` methods, this means returning an error. For others,
16+
it means assuming UTC.
17+
18+
### Deprecated
19+
20+
- `UtcOffset::timestamp` (moved to `UtcOffset::unix_timestamp`)
21+
- `UtcOffset::timestamp_nanos` (moved to `UtcOffset::unix_timestamp_nanos`)
22+
- `date` (moved to `macros::date`)
23+
- `time` (moved to `macros::time`)
24+
- `offset` (moved to `macros::offset`)
25+
- `OffsetDateTime::now_local` (assumes UTC if unable to be determined)
26+
- `UtcOffset::local_offset_at` (assumes UTC if unable to be determined)
27+
- `UtcOffset::current_local_offset` (assumes UTC if unable to be determined)
28+
1029
## 0.2.22 [2020-09-25]
1130

1231
### Fixed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "time"
3-
version = "0.2.22"
3+
version = "0.2.23"
44
authors = ["Jacob Pratt <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/time-rs/time"

src/format/format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use alloc::{borrow::ToOwned, string::String};
99
#[cfg_attr(__time_02_supports_non_exhaustive, non_exhaustive)]
1010
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1111
pub enum Format {
12+
#[cfg_attr(__time_02_docs, doc(alias = "ISO8601"))]
1213
Rfc3339,
1314
Custom(String),
1415
#[cfg(not(__time_02_supports_non_exhaustive))]

src/lib.rs

+90-67
Original file line numberDiff line numberDiff line change
@@ -311,90 +311,42 @@ mod weekday;
311311

312312
pub use date::Date;
313313
pub use duration::Duration;
314+
pub use error::Error;
315+
#[deprecated(
316+
since = "0.2.23",
317+
note = "Errors have been moved to the `error` module."
318+
)]
314319
pub use error::{
315-
ComponentRange as ComponentRangeError, ConversionRange as ConversionRangeError, Error,
320+
ComponentRange as ComponentRangeError, ConversionRange as ConversionRangeError,
316321
IndeterminateOffset as IndeterminateOffsetError, Parse as ParseError,
317322
};
323+
#[deprecated(
324+
since = "0.2.23",
325+
note = "Extension traits have been moved to the `ext` module."
326+
)]
318327
pub use ext::{NumericalDuration, NumericalStdDuration, NumericalStdDurationShort};
319328
pub(crate) use format::DeferredFormat;
320329
pub use format::Format;
321330
use format::ParseResult;
322331
#[cfg(feature = "std")]
323332
pub use instant::Instant;
333+
#[deprecated(
334+
since = "0.2.23",
335+
note = "Macros have been moved to the `macros` module."
336+
)]
337+
pub use macros::{date, offset, time};
324338
pub use offset_date_time::OffsetDateTime;
325339
pub use primitive_date_time::PrimitiveDateTime;
326340
#[allow(deprecated)]
327341
pub use sign::Sign;
328342
#[allow(unused_imports)]
329343
use standback::prelude::*;
330-
/// Construct a [`Date`](crate::Date) with a statically known value.
331-
///
332-
/// The resulting expression can be used in `const` or `static` declarations.
333-
///
334-
/// Three formats are supported: year-week-weekday, year-ordinal, and
335-
/// year-month-day.
336-
///
337-
/// ```rust
338-
/// # use time::{Date, date, Weekday::*};
339-
/// # fn main() -> time::Result<()> {
340-
/// assert_eq!(date!(2020-W01-3), Date::try_from_iso_ywd(2020, 1, Wednesday)?);
341-
/// assert_eq!(date!(2020-001), Date::try_from_yo(2020, 1)?);
342-
/// assert_eq!(date!(2020-01-01), Date::try_from_ymd(2020, 1, 1)?);
343-
/// # Ok(())
344-
/// # }
345-
/// ```
346-
pub use time_macros::date;
347-
/// Construct a [`UtcOffset`](crate::UtcOffset) with a statically known value.
348-
///
349-
/// The resulting expression can be used in `const` or `static` declarations.
350-
///
351-
/// A sign and the hour must be provided; minutes and seconds default to zero.
352-
/// `UTC` (both uppercase and lowercase) is also allowed.
353-
///
354-
/// ```rust
355-
/// # use time::{offset, UtcOffset};
356-
/// assert_eq!(offset!(UTC), UtcOffset::hours(0));
357-
/// assert_eq!(offset!(utc), UtcOffset::hours(0));
358-
/// assert_eq!(offset!(+0), UtcOffset::hours(0));
359-
/// assert_eq!(offset!(+1), UtcOffset::hours(1));
360-
/// assert_eq!(offset!(-1), UtcOffset::hours(-1));
361-
/// assert_eq!(offset!(+1:30), UtcOffset::minutes(90));
362-
/// assert_eq!(offset!(-1:30), UtcOffset::minutes(-90));
363-
/// assert_eq!(offset!(+1:30:59), UtcOffset::seconds(5459));
364-
/// assert_eq!(offset!(-1:30:59), UtcOffset::seconds(-5459));
365-
/// assert_eq!(offset!(+23:59:59), UtcOffset::seconds(86_399));
366-
/// assert_eq!(offset!(-23:59:59), UtcOffset::seconds(-86_399));
367-
/// ```
368-
pub use time_macros::offset;
369-
/// Construct a [`Time`](crate::Time) with a statically known value.
370-
///
371-
/// The resulting expression can be used in `const` or `static` declarations.
372-
///
373-
/// Hours and minutes must be provided, while seconds defaults to zero. AM/PM is
374-
/// allowed (either uppercase or lowercase). Any number of subsecond digits may
375-
/// be provided (though any past nine will be discarded).
376-
///
377-
/// All components are validated at compile-time. An error will be raised if any
378-
/// value is invalid.
379-
///
380-
/// ```rust
381-
/// # use time::{Time, time};
382-
/// # fn main() -> time::Result<()> {
383-
/// assert_eq!(time!(0:00), Time::try_from_hms(0, 0, 0)?);
384-
/// assert_eq!(time!(1:02:03), Time::try_from_hms(1, 2, 3)?);
385-
/// assert_eq!(time!(1:02:03.004_005_006), Time::try_from_hms_nano(1, 2, 3, 4_005_006)?);
386-
/// assert_eq!(time!(12:00 am), Time::try_from_hms(0, 0, 0)?);
387-
/// assert_eq!(time!(1:02:03 am), Time::try_from_hms(1, 2, 3)?);
388-
/// assert_eq!(time!(1:02:03.004_005_006 am), Time::try_from_hms_nano(1, 2, 3, 4_005_006)?);
389-
/// assert_eq!(time!(12:00 pm), Time::try_from_hms(12, 0, 0)?);
390-
/// assert_eq!(time!(1:02:03 pm), Time::try_from_hms(13, 2, 3)?);
391-
/// assert_eq!(time!(1:02:03.004_005_006 pm), Time::try_from_hms_nano(13, 2, 3, 4_005_006)?);
392-
/// # Ok(())
393-
/// # }
394-
/// ```
395-
pub use time_macros::time;
396344
pub use time_mod::Time;
397345
pub use utc_offset::UtcOffset;
346+
#[deprecated(
347+
since = "0.2.23",
348+
note = "This function has been moved to the `util` module."
349+
)]
398350
pub use util::{days_in_year, is_leap_year, validate_format_string, weeks_in_year};
399351
pub use weekday::Weekday;
400352

@@ -473,6 +425,77 @@ pub fn parse<T: private::Parsable>(s: impl AsRef<str>, format: impl AsRef<str>)
473425
private::Parsable::parse(s, format)
474426
}
475427

428+
/// Macros to statically construct values that are known to be valid.
429+
pub mod macros {
430+
431+
/// Construct a [`Date`](crate::Date) with a statically known value.
432+
///
433+
/// The resulting expression can be used in `const` or `static` declarations.
434+
///
435+
/// Three formats are supported: year-week-weekday, year-ordinal, and
436+
/// year-month-day.
437+
///
438+
/// ```rust
439+
/// # use time::{Date, macros::date, Weekday::*};
440+
/// # fn main() -> time::Result<()> {
441+
/// assert_eq!(date!(2020-W01-3), Date::try_from_iso_ywd(2020, 1, Wednesday)?);
442+
/// assert_eq!(date!(2020-001), Date::try_from_yo(2020, 1)?);
443+
/// assert_eq!(date!(2020-01-01), Date::try_from_ymd(2020, 1, 1)?);
444+
/// # Ok(())
445+
/// # }
446+
/// ```
447+
pub use time_macros::date;
448+
/// Construct a [`UtcOffset`](crate::UtcOffset) with a statically known value.
449+
///
450+
/// The resulting expression can be used in `const` or `static` declarations.
451+
///
452+
/// A sign and the hour must be provided; minutes and seconds default to zero.
453+
/// `UTC` (both uppercase and lowercase) is also allowed.
454+
///
455+
/// ```rust
456+
/// # use time::{macros::offset, UtcOffset};
457+
/// assert_eq!(offset!(UTC), UtcOffset::hours(0));
458+
/// assert_eq!(offset!(utc), UtcOffset::hours(0));
459+
/// assert_eq!(offset!(+0), UtcOffset::hours(0));
460+
/// assert_eq!(offset!(+1), UtcOffset::hours(1));
461+
/// assert_eq!(offset!(-1), UtcOffset::hours(-1));
462+
/// assert_eq!(offset!(+1:30), UtcOffset::minutes(90));
463+
/// assert_eq!(offset!(-1:30), UtcOffset::minutes(-90));
464+
/// assert_eq!(offset!(+1:30:59), UtcOffset::seconds(5459));
465+
/// assert_eq!(offset!(-1:30:59), UtcOffset::seconds(-5459));
466+
/// assert_eq!(offset!(+23:59:59), UtcOffset::seconds(86_399));
467+
/// assert_eq!(offset!(-23:59:59), UtcOffset::seconds(-86_399));
468+
/// ```
469+
pub use time_macros::offset;
470+
/// Construct a [`Time`](crate::Time) with a statically known value.
471+
///
472+
/// The resulting expression can be used in `const` or `static` declarations.
473+
///
474+
/// Hours and minutes must be provided, while seconds defaults to zero. AM/PM is
475+
/// allowed (either uppercase or lowercase). Any number of subsecond digits may
476+
/// be provided (though any past nine will be discarded).
477+
///
478+
/// All components are validated at compile-time. An error will be raised if any
479+
/// value is invalid.
480+
///
481+
/// ```rust
482+
/// # use time::{Time, macros::time};
483+
/// # fn main() -> time::Result<()> {
484+
/// assert_eq!(time!(0:00), Time::try_from_hms(0, 0, 0)?);
485+
/// assert_eq!(time!(1:02:03), Time::try_from_hms(1, 2, 3)?);
486+
/// assert_eq!(time!(1:02:03.004_005_006), Time::try_from_hms_nano(1, 2, 3, 4_005_006)?);
487+
/// assert_eq!(time!(12:00 am), Time::try_from_hms(0, 0, 0)?);
488+
/// assert_eq!(time!(1:02:03 am), Time::try_from_hms(1, 2, 3)?);
489+
/// assert_eq!(time!(1:02:03.004_005_006 am), Time::try_from_hms_nano(1, 2, 3, 4_005_006)?);
490+
/// assert_eq!(time!(12:00 pm), Time::try_from_hms(12, 0, 0)?);
491+
/// assert_eq!(time!(1:02:03 pm), Time::try_from_hms(13, 2, 3)?);
492+
/// assert_eq!(time!(1:02:03.004_005_006 pm), Time::try_from_hms_nano(13, 2, 3, 4_005_006)?);
493+
/// # Ok(())
494+
/// # }
495+
/// ```
496+
pub use time_macros::time;
497+
}
498+
476499
// For some back-compatibility, we're also implementing some deprecated types
477500
// and methods. They will be removed completely in 0.3.
478501

src/offset_date_time.rs

+72-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,17 @@ impl OffsetDateTime {
113113
/// local offset.
114114
///
115115
/// ```rust
116+
/// # #![allow(deprecated)]
116117
/// # use time::OffsetDateTime;
117118
/// assert!(OffsetDateTime::now_local().year() >= 2019);
118119
/// ```
119120
#[cfg(feature = "std")]
120121
#[cfg_attr(__time_02_docs, doc(cfg(feature = "std")))]
122+
#[deprecated(
123+
since = "0.2.23",
124+
note = "UTC is returned if the local offset cannot be determined"
125+
)]
126+
#[allow(deprecated)]
121127
pub fn now_local() -> Self {
122128
let t = Self::now_utc();
123129
t.to_offset(UtcOffset::local_offset_at(t))
@@ -129,7 +135,10 @@ impl OffsetDateTime {
129135
///
130136
/// ```rust
131137
/// # use time::OffsetDateTime;
132-
/// assert!(OffsetDateTime::try_now_local().is_ok());
138+
/// let now = OffsetDateTime::try_now_local();
139+
/// # if false {
140+
/// assert!(now.is_ok());
141+
/// # }
133142
/// ```
134143
#[cfg(feature = "std")]
135144
#[cfg_attr(__time_02_docs, doc(cfg(feature = "std")))]
@@ -276,6 +285,31 @@ impl OffsetDateTime {
276285
/// date!(1970-01-01)
277286
/// .midnight()
278287
/// .assume_utc()
288+
/// .unix_timestamp(),
289+
/// 0,
290+
/// );
291+
/// assert_eq!(
292+
/// date!(1970-01-01)
293+
/// .midnight()
294+
/// .assume_utc()
295+
/// .to_offset(offset!(-1))
296+
/// .unix_timestamp(),
297+
/// 0,
298+
/// );
299+
/// ```
300+
pub fn unix_timestamp(self) -> i64 {
301+
(self - Self::unix_epoch()).whole_seconds()
302+
}
303+
304+
/// Get the [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time).
305+
///
306+
/// ```rust
307+
/// # #![allow(deprecated)]
308+
/// # use time::{date, offset};
309+
/// assert_eq!(
310+
/// date!(1970-01-01)
311+
/// .midnight()
312+
/// .assume_utc()
279313
/// .timestamp(),
280314
/// 0,
281315
/// );
@@ -288,8 +322,12 @@ impl OffsetDateTime {
288322
/// 0,
289323
/// );
290324
/// ```
325+
#[deprecated(
326+
since = "0.2.23",
327+
note = "Use `OffsetDateTime::unix_timestamp` instead"
328+
)]
291329
pub fn timestamp(self) -> i64 {
292-
(self - Self::unix_epoch()).whole_seconds()
330+
self.unix_timestamp()
293331
}
294332

295333
/// Get the Unix timestamp in nanoseconds.
@@ -300,22 +338,51 @@ impl OffsetDateTime {
300338
/// date!(1970-01-01)
301339
/// .midnight()
302340
/// .assume_utc()
303-
/// .timestamp_nanos(),
341+
/// .unix_timestamp_nanos(),
304342
/// 0,
305343
/// );
306344
/// assert_eq!(
307345
/// date!(1970-01-01)
308346
/// .with_time(time!(1:00))
309347
/// .assume_utc()
310348
/// .to_offset(offset!(-1))
311-
/// .timestamp_nanos(),
349+
/// .unix_timestamp_nanos(),
312350
/// 3_600_000_000_000,
313351
/// );
314352
/// ```
315-
pub fn timestamp_nanos(self) -> i128 {
353+
pub fn unix_timestamp_nanos(self) -> i128 {
316354
(self - Self::unix_epoch()).whole_nanoseconds()
317355
}
318356

357+
/// Get the Unix timestamp in nanoseconds.
358+
///
359+
/// ```rust
360+
/// # #![allow(deprecated)]
361+
/// use time::{date, offset, time};
362+
/// assert_eq!(
363+
/// date!(1970-01-01)
364+
/// .midnight()
365+
/// .assume_utc()
366+
/// .unix_timestamp_nanos(),
367+
/// 0,
368+
/// );
369+
/// assert_eq!(
370+
/// date!(1970-01-01)
371+
/// .with_time(time!(1:00))
372+
/// .assume_utc()
373+
/// .to_offset(offset!(-1))
374+
/// .unix_timestamp_nanos(),
375+
/// 3_600_000_000_000,
376+
/// );
377+
/// ```
378+
#[deprecated(
379+
since = "0.2.23",
380+
note = "Use `OffsetDateTime::unix_timestamp_nanos` instead"
381+
)]
382+
pub fn timestamp_nanos(self) -> i128 {
383+
self.unix_timestamp_nanos()
384+
}
385+
319386
/// Get the `Date` in the stored offset.
320387
///
321388
/// ```rust

src/primitive_date_time.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,11 @@ impl PrimitiveDateTime {
425425
/// # #![allow(deprecated)]
426426
/// # use time::{date, offset};
427427
/// assert_eq!(
428-
/// date!(2019-01-01).midnight().using_offset(offset!(UTC)).timestamp(),
428+
/// date!(2019-01-01).midnight().using_offset(offset!(UTC)).unix_timestamp(),
429429
/// 1_546_300_800,
430430
/// );
431431
/// assert_eq!(
432-
/// date!(2019-01-01).midnight().using_offset(offset!(-1)).timestamp(),
432+
/// date!(2019-01-01).midnight().using_offset(offset!(-1)).unix_timestamp(),
433433
/// 1_546_300_800,
434434
/// );
435435
/// ```
@@ -451,11 +451,11 @@ impl PrimitiveDateTime {
451451
/// ```rust
452452
/// # use time::{date, offset};
453453
/// assert_eq!(
454-
/// date!(2019-01-01).midnight().assume_offset(offset!(UTC)).timestamp(),
454+
/// date!(2019-01-01).midnight().assume_offset(offset!(UTC)).unix_timestamp(),
455455
/// 1_546_300_800,
456456
/// );
457457
/// assert_eq!(
458-
/// date!(2019-01-01).midnight().assume_offset(offset!(-1)).timestamp(),
458+
/// date!(2019-01-01).midnight().assume_offset(offset!(-1)).unix_timestamp(),
459459
/// 1_546_304_400,
460460
/// );
461461
/// ```
@@ -469,7 +469,7 @@ impl PrimitiveDateTime {
469469
/// ```rust
470470
/// # use time::date;
471471
/// assert_eq!(
472-
/// date!(2019-01-01).midnight().assume_utc().timestamp(),
472+
/// date!(2019-01-01).midnight().assume_utc().unix_timestamp(),
473473
/// 1_546_300_800,
474474
/// );
475475
/// ```

0 commit comments

Comments
 (0)