From 4f9ccfb4645a9a747b109211b0264b28cc641c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 03:24:04 +0300 Subject: [PATCH 01/12] time units draft --- src/libcore/time.rs | 32 ++++++++++++++++++++++++++++++++ src/libstd/time.rs | 1 + 2 files changed, 33 insertions(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 54973b7b7783a..bbc2f43b36dfa 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -31,6 +31,29 @@ const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; const MICROS_PER_SEC: u64 = 1_000_000; +/// 1 nanosecond `Duration` +#[unstable(feature = "time_units", issue = "0")] +const NS: Duration = Duration::from_nanos(1); +/// 1 microsecond `Duration` +#[unstable(feature = "time_units", issue = "0")] +const US: Duration = Duration::from_micros(1); +#[unstable(feature = "time_units", issue = "0")] +/// 1 millisecond `Duration` +#[unstable(feature = "time_units", issue = "0")] +const MS: Duration = Duration::from_millis(1); +/// 1 second `Duration` +#[unstable(feature = "time_units", issue = "0")] +const S: Duration = Duration::from_secs(1); +/// 1 minute `Duration` +#[unstable(feature = "time_units", issue = "0")] +const M: Duration = Duration::from_secs(60); +/// 1 hour `Duration` +#[unstable(feature = "time_units", issue = "0")] +const H: Duration = Duration::from_secs(60*60); +/// 1 day `Duration` +#[unstable(feature = "time_units", issue = "0")] +const D: Duration = Duration::from_secs(24*60*60); + /// A `Duration` type to represent a span of time, typically used for system /// timeouts. /// @@ -501,6 +524,15 @@ impl Mul for Duration { } } +#[unstable(feature = "time_units", issue = "0")] +impl Mul for u32 { + type Output = Duration; + + fn mul(self, rhs: Duration) -> Duration { + rhs.checked_mul(self).expect("overflow when multiplying scalar by duration") + } +} + #[stable(feature = "time_augmented_assignment", since = "1.9.0")] impl MulAssign for Duration { fn mul_assign(&mut self, rhs: u32) { diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 90ab349159915..f2308bebd0d06 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -30,6 +30,7 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; +pub use core::time::{NS, US, MS, S, M, H, D}; /// A measurement of a monotonically nondecreasing clock. /// Opaque and useful only with `Duration`. From 2f90e91e31227e22c6e2b48905803a13f1a6776f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 12:30:31 +0300 Subject: [PATCH 02/12] review update --- src/libcore/time.rs | 10 ---------- src/libstd/time.rs | 1 + 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index bbc2f43b36dfa..7621f7948bd6a 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -44,15 +44,6 @@ const MS: Duration = Duration::from_millis(1); /// 1 second `Duration` #[unstable(feature = "time_units", issue = "0")] const S: Duration = Duration::from_secs(1); -/// 1 minute `Duration` -#[unstable(feature = "time_units", issue = "0")] -const M: Duration = Duration::from_secs(60); -/// 1 hour `Duration` -#[unstable(feature = "time_units", issue = "0")] -const H: Duration = Duration::from_secs(60*60); -/// 1 day `Duration` -#[unstable(feature = "time_units", issue = "0")] -const D: Duration = Duration::from_secs(24*60*60); /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -524,7 +515,6 @@ impl Mul for Duration { } } -#[unstable(feature = "time_units", issue = "0")] impl Mul for u32 { type Output = Duration; diff --git a/src/libstd/time.rs b/src/libstd/time.rs index f2308bebd0d06..50f2181ac9c92 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -30,6 +30,7 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; +#[unstable(feature = "time_units", issue = "0")] pub use core::time::{NS, US, MS, S, M, H, D}; /// A measurement of a monotonically nondecreasing clock. From d230ef76b24750bb5e44b62dd733ff14f05645ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 12:48:14 +0300 Subject: [PATCH 03/12] remove excessive unstable attribute --- src/libcore/time.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 7621f7948bd6a..d59b29ebc8b0b 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -37,7 +37,6 @@ const NS: Duration = Duration::from_nanos(1); /// 1 microsecond `Duration` #[unstable(feature = "time_units", issue = "0")] const US: Duration = Duration::from_micros(1); -#[unstable(feature = "time_units", issue = "0")] /// 1 millisecond `Duration` #[unstable(feature = "time_units", issue = "0")] const MS: Duration = Duration::from_millis(1); From 96aa20892522f5fcdaea54b48c7433bd4b9eb16b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 13:05:47 +0300 Subject: [PATCH 04/12] add pub --- src/libcore/time.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index d59b29ebc8b0b..2f4d98a35f04e 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -33,16 +33,16 @@ const MICROS_PER_SEC: u64 = 1_000_000; /// 1 nanosecond `Duration` #[unstable(feature = "time_units", issue = "0")] -const NS: Duration = Duration::from_nanos(1); +pub const NS: Duration = Duration::from_nanos(1); /// 1 microsecond `Duration` #[unstable(feature = "time_units", issue = "0")] -const US: Duration = Duration::from_micros(1); +pub const US: Duration = Duration::from_micros(1); /// 1 millisecond `Duration` #[unstable(feature = "time_units", issue = "0")] -const MS: Duration = Duration::from_millis(1); +pub const MS: Duration = Duration::from_millis(1); /// 1 second `Duration` #[unstable(feature = "time_units", issue = "0")] -const S: Duration = Duration::from_secs(1); +pub const S: Duration = Duration::from_secs(1); /// A `Duration` type to represent a span of time, typically used for system /// timeouts. From ef92df436404c16ce9718586cfeac99786a031ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 13:14:55 +0300 Subject: [PATCH 05/12] return unstable attribute to impl Mul --- src/libcore/time.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 2f4d98a35f04e..a444cb2276743 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -514,6 +514,7 @@ impl Mul for Duration { } } +#[unstable(feature = "time_units", issue = "0")] impl Mul for u32 { type Output = Duration; From 5d6ab9f6bf3f4df3d3d2808c17f3623543654a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 13:26:20 +0300 Subject: [PATCH 06/12] removed import of deleted consts --- src/libstd/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 50f2181ac9c92..cc2cd1568d618 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -31,7 +31,7 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; #[unstable(feature = "time_units", issue = "0")] -pub use core::time::{NS, US, MS, S, M, H, D}; +pub use core::time::{NS, US, MS, S}; /// A measurement of a monotonically nondecreasing clock. /// Opaque and useful only with `Duration`. From e78120d9bf62dc6b709e317e7ff4382a07798e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 13:56:59 +0300 Subject: [PATCH 07/12] add #![feature(time_units)] to std and core --- src/libcore/lib.rs | 1 + src/libstd/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index bbe6ae8619fec..5dcfd2c9f773b 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -122,6 +122,7 @@ #![feature(const_slice_len)] #![feature(const_str_as_bytes)] #![feature(const_str_len)] +#![feature(time_units)] #[prelude_import] #[allow(unused)] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index fec14b8d67d35..ddc10d43c5471 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -324,6 +324,7 @@ #![feature(float_internals)] #![feature(panic_info_message)] #![feature(panic_implementation)] +#![feature(time_units)] #![default_lib_allocator] From 9f9520d408ea4e092c71fa875a380c084dcdb460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 15:37:05 +0300 Subject: [PATCH 08/12] use full spelling instead of abbreviation --- src/libcore/time.rs | 11 +++++++---- src/libstd/time.rs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index a444cb2276743..4988e662b7b7e 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -33,16 +33,19 @@ const MICROS_PER_SEC: u64 = 1_000_000; /// 1 nanosecond `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const NS: Duration = Duration::from_nanos(1); +pub const NANOSECOND: Duration = Duration::from_nanos(1); /// 1 microsecond `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const US: Duration = Duration::from_micros(1); +pub const MICROSECOND: Duration = Duration::from_micros(1); /// 1 millisecond `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const MS: Duration = Duration::from_millis(1); +pub const MILLISECOND: Duration = Duration::from_millis(1); /// 1 second `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const S: Duration = Duration::from_secs(1); +pub const SECOND: Duration = Duration::from_secs(1); +/// 1 minute `Duration` +#[unstable(feature = "time_units", issue = "0")] +pub const MINUTE: Duration = Duration::from_secs(60); /// A `Duration` type to represent a span of time, typically used for system /// timeouts. diff --git a/src/libstd/time.rs b/src/libstd/time.rs index cc2cd1568d618..922ede8669ffc 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -31,7 +31,7 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; #[unstable(feature = "time_units", issue = "0")] -pub use core::time::{NS, US, MS, S}; +pub use core::time::{NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE}; /// A measurement of a monotonically nondecreasing clock. /// Opaque and useful only with `Duration`. From f5db99d7c8c116a428606e482a6257c2779741d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 17:34:27 +0300 Subject: [PATCH 09/12] mark impl Mul stable and remove MINUTE --- src/libcore/time.rs | 5 +---- src/libstd/time.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 4988e662b7b7e..31947cc8236e7 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -43,9 +43,6 @@ pub const MILLISECOND: Duration = Duration::from_millis(1); /// 1 second `Duration` #[unstable(feature = "time_units", issue = "0")] pub const SECOND: Duration = Duration::from_secs(1); -/// 1 minute `Duration` -#[unstable(feature = "time_units", issue = "0")] -pub const MINUTE: Duration = Duration::from_secs(60); /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -517,7 +514,7 @@ impl Mul for Duration { } } -#[unstable(feature = "time_units", issue = "0")] +#[stable(feature = "time_units", since = "1.29.0")] impl Mul for u32 { type Output = Duration; diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 922ede8669ffc..6d11f1beeaca3 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -31,7 +31,7 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; #[unstable(feature = "time_units", issue = "0")] -pub use core::time::{NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE}; +pub use core::time::{NANOSECOND, MICROSECOND, MILLISECOND, SECOND}; /// A measurement of a monotonically nondecreasing clock. /// Opaque and useful only with `Duration`. From 9e5daa56cd6800117d69e0d6123f53be6ffb6e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 20 Jul 2018 18:05:13 +0300 Subject: [PATCH 10/12] fix stability attribute --- src/libcore/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 31947cc8236e7..1edc65dd91673 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -514,7 +514,7 @@ impl Mul for Duration { } } -#[stable(feature = "time_units", since = "1.29.0")] +#[stable(feature = "u32_duration_mul", since = "1.29.0")] impl Mul for u32 { type Output = Duration; From 2732503718d0f6d1f1f32e83a59325dcc6fe6dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 21 Jul 2018 17:23:01 +0300 Subject: [PATCH 11/12] construct constants directly without using `from_*` methods --- src/libcore/time.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 1edc65dd91673..d3ffc0f600c4c 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -33,16 +33,16 @@ const MICROS_PER_SEC: u64 = 1_000_000; /// 1 nanosecond `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const NANOSECOND: Duration = Duration::from_nanos(1); +pub const NANOSECOND: Duration = Duration { secs: 0, nanos: 1 }; /// 1 microsecond `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const MICROSECOND: Duration = Duration::from_micros(1); +pub const MICROSECOND: Duration = Duration { secs: 0, nanos: NANOS_PER_MICRO }; /// 1 millisecond `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const MILLISECOND: Duration = Duration::from_millis(1); +pub const MILLISECOND: Duration = Duration { secs: 0, nanos: NANOS_PER_MILLI }; /// 1 second `Duration` #[unstable(feature = "time_units", issue = "0")] -pub const SECOND: Duration = Duration::from_secs(1); +pub const SECOND: Duration = Duration { secs: 1, nanos: 0 }; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. From 5d9b925315e87c39247cd65af3d3ff21367f960c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 21 Jul 2018 21:56:46 +0300 Subject: [PATCH 12/12] return minute, hour and day constants, improve documentation --- src/libcore/time.rs | 24 ++++++++++++++++++++---- src/libstd/time.rs | 4 +++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index d3ffc0f600c4c..c26a3225ad4ba 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -31,18 +31,34 @@ const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; const MICROS_PER_SEC: u64 = 1_000_000; -/// 1 nanosecond `Duration` +/// 1 nanosecond `Duration`. #[unstable(feature = "time_units", issue = "0")] pub const NANOSECOND: Duration = Duration { secs: 0, nanos: 1 }; -/// 1 microsecond `Duration` +/// 1 microsecond `Duration`. #[unstable(feature = "time_units", issue = "0")] pub const MICROSECOND: Duration = Duration { secs: 0, nanos: NANOS_PER_MICRO }; -/// 1 millisecond `Duration` +/// 1 millisecond `Duration`. #[unstable(feature = "time_units", issue = "0")] pub const MILLISECOND: Duration = Duration { secs: 0, nanos: NANOS_PER_MILLI }; -/// 1 second `Duration` +/// 1 second `Duration`. #[unstable(feature = "time_units", issue = "0")] pub const SECOND: Duration = Duration { secs: 1, nanos: 0 }; +/// 1 minute `Duration` (60 seconds). +/// +/// Note that it's different from UTC minute, which can be 59-61 seconds long. +#[unstable(feature = "time_units", issue = "0")] +pub const MINUTE: Duration = Duration { secs: 60, nanos: 0 }; +/// 1 hour `Duration` (60*60 = 3 600 seconds). +/// +/// Note that it's different from UTC hour, which can be 3559-3661 seconds long. +#[unstable(feature = "time_units", issue = "0")] +pub const HOUR: Duration = Duration { secs: 3_600, nanos: 0 }; +/// 1 day `Duration` (24*60*60 = 86 400 seconds). +/// +/// Note that it's different from UTC day, length of which can vary from plus +/// minus second (leap seconds) and up to plus minus an hour (Daylight Save Time). +#[unstable(feature = "time_units", issue = "0")] +pub const DAY: Duration = Duration { secs: 86_400, nanos: 0 }; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 6d11f1beeaca3..c9299f5c83191 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -31,7 +31,9 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; #[unstable(feature = "time_units", issue = "0")] -pub use core::time::{NANOSECOND, MICROSECOND, MILLISECOND, SECOND}; +pub use core::time::{ + NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DAY, +}; /// A measurement of a monotonically nondecreasing clock. /// Opaque and useful only with `Duration`.