mirror of
https://github.com/dart-lang/sdk
synced 2024-10-02 23:39:19 +00:00
Add DateTime.timestamp()
constructor for "now as UTC".
Change-Id: I497f335b1f1eb0f691c2d44557c50f0f55358426 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292003 Reviewed-by: Slava Egorov <vegorov@google.com> Reviewed-by: Aske Simon Christensen <askesc@google.com> Auto-Submit: Lasse Nielsen <lrn@google.com> Reviewed-by: Nate Bosch <nbosch@google.com> Commit-Queue: Lasse Nielsen <lrn@google.com> Reviewed-by: Stephen Adams <sra@google.com>
This commit is contained in:
parent
0196a5b296
commit
f0e099ec5b
|
@ -30,6 +30,7 @@
|
|||
|
||||
#### `dart:core`
|
||||
- Added `bool.parse` and `bool.tryParse` static methods.
|
||||
- Added `DateTime.timestamp()` constructor to get current time as UTC.
|
||||
|
||||
- **Breaking change** [#49529][]:
|
||||
- Removed the deprecated `List` constructor, as it wasn't null safe.
|
||||
|
|
|
@ -373,6 +373,11 @@ class DateTime {
|
|||
: isUtc = false,
|
||||
_value = Primitives.dateNow();
|
||||
|
||||
@patch
|
||||
DateTime._nowUtc()
|
||||
: isUtc = true,
|
||||
_value = Primitives.dateNow();
|
||||
|
||||
/// Rounds the given [microsecond] to the nearest milliseconds value.
|
||||
///
|
||||
/// For example, invoked with argument `2600` returns `3`.
|
||||
|
|
|
@ -280,6 +280,11 @@ class DateTime {
|
|||
: isUtc = false,
|
||||
_value = Primitives.dateNow();
|
||||
|
||||
@patch
|
||||
DateTime._nowUtc()
|
||||
: isUtc = true,
|
||||
_value = Primitives.dateNow();
|
||||
|
||||
/// Rounds the given [microsecond] to the nearest milliseconds value.
|
||||
///
|
||||
/// For example, invoked with argument `2600` returns `3`.
|
||||
|
|
|
@ -66,6 +66,11 @@ class DateTime {
|
|||
: isUtc = false,
|
||||
_value = _getCurrentMicros();
|
||||
|
||||
@patch
|
||||
DateTime._nowUtc()
|
||||
: isUtc = true,
|
||||
_value = _getCurrentMicros();
|
||||
|
||||
@patch
|
||||
String get timeZoneName {
|
||||
if (isUtc) return "UTC";
|
||||
|
|
|
@ -225,6 +225,14 @@ class DateTime implements Comparable<DateTime> {
|
|||
/// ```
|
||||
DateTime.now() : this._now();
|
||||
|
||||
/// Constructs a [DateTime] with the current UTC date and time.
|
||||
///
|
||||
///
|
||||
/// ```dart
|
||||
/// final mark = DateTime.timestamp();
|
||||
/// ```
|
||||
DateTime.timestamp() : this._nowUtc();
|
||||
|
||||
/// Constructs a new [DateTime] instance based on [formattedString].
|
||||
///
|
||||
/// Throws a [FormatException] if the input string cannot be parsed.
|
||||
|
|
|
@ -30,6 +30,45 @@ void testNow() {
|
|||
Expect.isFalse(t1.isUtc);
|
||||
}
|
||||
|
||||
void testTimestamp() {
|
||||
// Assume `DateTime.now` works.
|
||||
const int N = 1000000;
|
||||
var nowBefore = DateTime.now();
|
||||
var timestamp = DateTime.timestamp();
|
||||
var iterations = 0;
|
||||
while (true) {
|
||||
// Test that `timestamp` is using the same "current time" as `now`.
|
||||
Expect.isTrue(timestamp.isUtc, "Is UTC");
|
||||
Expect.isTrue(
|
||||
nowBefore.microsecondsSinceEpoch <= timestamp.microsecondsSinceEpoch,
|
||||
"After an earlier now");
|
||||
|
||||
var laterNow = DateTime.now();
|
||||
Expect.isTrue(
|
||||
timestamp.microsecondsSinceEpoch <= laterNow.microsecondsSinceEpoch,
|
||||
"Before a later now");
|
||||
|
||||
var newTimestamp = DateTime.timestamp();
|
||||
|
||||
// Succeed if time has also progressed.
|
||||
if (timestamp.microsecondsSinceEpoch <
|
||||
newTimestamp.microsecondsSinceEpoch) {
|
||||
break;
|
||||
}
|
||||
// Otherwise emit diagnostics occasionally, until the test times out
|
||||
// or succeeds. Which should be in at most a few milliseconds if all
|
||||
// is well.
|
||||
if (++iterations >= N) {
|
||||
print("testTimestamp: No DateTime.timestamp() progress in $N loops."
|
||||
"Time: $timestamp");
|
||||
iterations = 0;
|
||||
}
|
||||
|
||||
nowBefore = laterNow;
|
||||
timestamp = newTimestamp;
|
||||
}
|
||||
}
|
||||
|
||||
void testMillisecondsSinceEpoch() {
|
||||
var dt1 = new DateTime.now();
|
||||
var millisecondsSinceEpoch = dt1.millisecondsSinceEpoch;
|
||||
|
@ -1218,6 +1257,7 @@ void testRegression46966() {
|
|||
|
||||
void main() {
|
||||
testNow();
|
||||
testTimestamp();
|
||||
testMillisecondsSinceEpoch();
|
||||
testMicrosecondsSinceEpoch();
|
||||
testConstructors();
|
||||
|
|
Loading…
Reference in a new issue