From 68424cca43f2b3aa397ed11231b1c5071460e922 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Fri, 24 Jan 2025 19:15:59 +0900 Subject: [PATCH 1/4] feat: Add `DateTimeSecondsSinceEpoch` --- sdk/lib/core/date_time.dart | 14 +++++++++++++ .../date_time_seconds_since_epoch_test.dart | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/corelib/date_time_seconds_since_epoch_test.dart diff --git a/sdk/lib/core/date_time.dart b/sdk/lib/core/date_time.dart index ffa349fa61a8..10eb2fbcd062 100644 --- a/sdk/lib/core/date_time.dart +++ b/sdk/lib/core/date_time.dart @@ -1007,3 +1007,17 @@ extension DateTimeCopyWith on DateTime { ); } } + +/// Adds methods for seconds since epoch to [DateTime] objects. +@Since("3.8") +extension DateTimeSecondsSinceEpoch on DateTime { + /// Returns the number of seconds since the Unix epoch (1970-01-01 00:00:00 UTC). + int get secondsSinceEpoch { + return this.millisecondsSinceEpoch ~/ 1000; + } + + /// Creates a [DateTime] instance from seconds since epoch. + static DateTime fromSecondsSinceEpoch(int seconds) { + return DateTime.fromMillisecondsSinceEpoch(seconds * 1000); + } +} diff --git a/tests/corelib/date_time_seconds_since_epoch_test.dart b/tests/corelib/date_time_seconds_since_epoch_test.dart new file mode 100644 index 000000000000..fd08fa387605 --- /dev/null +++ b/tests/corelib/date_time_seconds_since_epoch_test.dart @@ -0,0 +1,20 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "package:expect/expect.dart"; + +// Dart test program for DateTime.secondsSinceEpoch. + +testSecondsSinceEpoch(DateTime original, int expectedSeconds) { + final result = original.secondsSinceEpoch; + Expect.equals(expectedSeconds, result); +} + +void main() { + final epoch = DateTime.utc(1970, 1, 1); + final oneHourLater = DateTime.utc(1970, 1, 1, 1); // 3600 seconds since epoch + + testSecondsSinceEpoch(epoch, 0); + testSecondsSinceEpoch(oneHourLater, 3600); +} From dd2c05012d851dc4418c8963503eef005f23eca1 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Thu, 20 Feb 2025 18:07:30 +0900 Subject: [PATCH 2/4] refactor: Refactor docstring and 1000 to Duration.millisecondsPerSecond --- sdk/lib/core/date_time.dart | 12 +++++++++--- .../corelib/date_time_seconds_since_epoch_test.dart | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/sdk/lib/core/date_time.dart b/sdk/lib/core/date_time.dart index 10eb2fbcd062..a9b3826023d6 100644 --- a/sdk/lib/core/date_time.dart +++ b/sdk/lib/core/date_time.dart @@ -1011,13 +1011,19 @@ extension DateTimeCopyWith on DateTime { /// Adds methods for seconds since epoch to [DateTime] objects. @Since("3.8") extension DateTimeSecondsSinceEpoch on DateTime { - /// Returns the number of seconds since the Unix epoch (1970-01-01 00:00:00 UTC). + /// This value is independent of the time zone. + /// + /// The value is at most 8,640,000,000,000 seconds (100,000,000 days) + /// from the Unix epoch. + /// In other words: `secondsSinceEpoch.abs() <= 8640000000000`. int get secondsSinceEpoch { - return this.millisecondsSinceEpoch ~/ 1000; + return this.millisecondsSinceEpoch ~/ Duration.millisecondsPerSecond; } /// Creates a [DateTime] instance from seconds since epoch. static DateTime fromSecondsSinceEpoch(int seconds) { - return DateTime.fromMillisecondsSinceEpoch(seconds * 1000); + return DateTime.fromMillisecondsSinceEpoch( + seconds * Duration.millisecondsPerSecond, + ); } } diff --git a/tests/corelib/date_time_seconds_since_epoch_test.dart b/tests/corelib/date_time_seconds_since_epoch_test.dart index fd08fa387605..b124a45f220c 100644 --- a/tests/corelib/date_time_seconds_since_epoch_test.dart +++ b/tests/corelib/date_time_seconds_since_epoch_test.dart @@ -1,10 +1,10 @@ -// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. import "package:expect/expect.dart"; -// Dart test program for DateTime.secondsSinceEpoch. +/// Tests `DateTime.secondsSinceEpoch`. testSecondsSinceEpoch(DateTime original, int expectedSeconds) { final result = original.secondsSinceEpoch; From 95e9a8b81ac437804c4f9c3ae38e8427ebc7a67a Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Mon, 31 Mar 2025 15:28:42 +0900 Subject: [PATCH 3/4] feat: Add `DateTime.fromSecondsSinceEpoch` constructor and `secondsSinceEpoch` getter --- sdk/lib/core/date_time.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sdk/lib/core/date_time.dart b/sdk/lib/core/date_time.dart index a9b3826023d6..9132173a809c 100644 --- a/sdk/lib/core/date_time.dart +++ b/sdk/lib/core/date_time.dart @@ -399,6 +399,24 @@ class DateTime implements Comparable { static const int _maxMicrosecondsSinceEpoch = _maxMillisecondsSinceEpoch * Duration.microsecondsPerMillisecond; + /// Constructs a new [DateTime] instance + /// with the given [secondsSinceEpoch]. + /// + /// /// If [isUtc] is false then the date is in the local time zone. + /// + /// The constructed [DateTime] represents + /// 1970-01-01T00:00:00Z + [secondsSinceEpoch] seconds in the given + /// time zone (local or UTC). + /// ```dart + /// final newYearsDay = + /// DateTime.fromSecondsSinceEpoch(1641031200, isUtc:true); + /// print(newYearsDay); // 2022-01-01 10:00:00.000Z + /// ``` + external DateTime.fromSecondsSinceEpoch( + int secondsSinceEpoch, { + bool isUtc = false, + }); + /// Constructs a new [DateTime] instance /// with the given [millisecondsSinceEpoch]. /// @@ -788,6 +806,16 @@ class DateTime implements Comparable { bool isUtc, ); + /// The number of seconds since + /// the "Unix epoch" 1970-01-01T00:00:00Z (UTC). + /// + /// This value is independent of the time zone. + /// + /// This value is at most + /// 86,400,000,000s. (100,000,000 days) from the Unix epoch. + /// In other words: `secondsSinceEpoch.abs() <= 86400000000`. + external int get secondsSinceEpoch; + /// The number of milliseconds since /// the "Unix epoch" 1970-01-01T00:00:00Z (UTC). /// From 626a138564facd780e1ec9193b7ee4d9f4a63778 Mon Sep 17 00:00:00 2001 From: Jiun Kim Date: Thu, 3 Apr 2025 17:01:56 +0900 Subject: [PATCH 4/4] refactor: Remove extension DateTimeSecondsSinceEpoch --- sdk/lib/core/date_time.dart | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/sdk/lib/core/date_time.dart b/sdk/lib/core/date_time.dart index 9132173a809c..7af07027d705 100644 --- a/sdk/lib/core/date_time.dart +++ b/sdk/lib/core/date_time.dart @@ -412,6 +412,7 @@ class DateTime implements Comparable { /// DateTime.fromSecondsSinceEpoch(1641031200, isUtc:true); /// print(newYearsDay); // 2022-01-01 10:00:00.000Z /// ``` + @Since("3.8") external DateTime.fromSecondsSinceEpoch( int secondsSinceEpoch, { bool isUtc = false, @@ -814,6 +815,7 @@ class DateTime implements Comparable { /// This value is at most /// 86,400,000,000s. (100,000,000 days) from the Unix epoch. /// In other words: `secondsSinceEpoch.abs() <= 86400000000`. + @Since("3.8") external int get secondsSinceEpoch; /// The number of milliseconds since @@ -1035,23 +1037,3 @@ extension DateTimeCopyWith on DateTime { ); } } - -/// Adds methods for seconds since epoch to [DateTime] objects. -@Since("3.8") -extension DateTimeSecondsSinceEpoch on DateTime { - /// This value is independent of the time zone. - /// - /// The value is at most 8,640,000,000,000 seconds (100,000,000 days) - /// from the Unix epoch. - /// In other words: `secondsSinceEpoch.abs() <= 8640000000000`. - int get secondsSinceEpoch { - return this.millisecondsSinceEpoch ~/ Duration.millisecondsPerSecond; - } - - /// Creates a [DateTime] instance from seconds since epoch. - static DateTime fromSecondsSinceEpoch(int seconds) { - return DateTime.fromMillisecondsSinceEpoch( - seconds * Duration.millisecondsPerSecond, - ); - } -}