Skip to content

[WIP] feat: Add DateTimeSecondsSinceEpoch #59975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sdk/lib/core/date_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,25 @@ class DateTime implements Comparable<DateTime> {
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
/// ```
@Since("3.8")
external DateTime.fromSecondsSinceEpoch(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see some logics about max value and validation for milliseconds and microseconds from epoch but nothing for seconds since epoch, is that expected ?

int secondsSinceEpoch, {
bool isUtc = false,
});

/// Constructs a new [DateTime] instance
/// with the given [millisecondsSinceEpoch].
///
Expand Down Expand Up @@ -788,6 +807,17 @@ class DateTime implements Comparable<DateTime> {
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`.
@Since("3.8")
external int get secondsSinceEpoch;

/// The number of milliseconds since
/// the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
///
Expand Down
20 changes: 20 additions & 0 deletions tests/corelib/date_time_seconds_since_epoch_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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";

/// Tests `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);
}