-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add DateTime.fromSecondsSinceEpoch
and int get secondsSinceEpoch
#59961
Comments
Summary: User proposes adding |
Adding constructors should be non-breaking, but adding a getter to an interface is breaking. I don't think the benefit would be worth that. |
I guess it could be argued that this field could be added using extension like done with: |
I created this PR to propose a concept for the I welcome your thoughts and feedback! |
Not seeing the big need for this. It is just If we do decide to add something, why just seconds? Not sure I want all of them. |
Thank you for your comments. I agree with your opinions and concerns. Here’s my perspective: 1. Seconds Since Epoch is a De Facto StandardUnix time (= epoch time) is defined as The number of Seconds that have elapsed since the epoch and is widely used across various systems and APIs. Here are some examples:
2. Why Focus on Seconds (and Not Minutes/Hours)?
3. ConclusionThe absence of // Inconsistent, repetitive patterns
final int seconds1 = now.millisecondsSinceEpoch ~/ 1000;
final int seconds2 = now.millisecondsSinceEpoch ~/ Duration.millisecondsPerSecond;
final int seconds3 = (now.millisecondsSinceEpoch / 1000).floor();
final int seconds4 = (now.millisecondsSinceEpoch ~/ 1000).toInt();
// Seconds to Milliseconds is vise versa This enhancement will:
Thank you for considering this proposal! I look forward to your thoughts. 🚀 |
Proposal to Add Features to the
DateTime
ClassDateTime.fromSecondsSinceEpoch
:A constructor that creates a DateTime instance from seconds since the epoch.
int get secondsSinceEpoch
:A getter that retrieves the number of seconds since the epoch from a DateTime instance.
Using
dateTime.millisecondsSinceEpoch ~/ 1000
for conversions is often verbose.Adding these features would simplify the code and enhance the dev-experience.
It would be useful when only seconds are needed from DateTime, ultimately reducing overhead from unnecessary operations with milliseconds.
AS-IS
TO-BE
Thanks!
I’d love to hear your thoughts—feel free to drop a comment!
The text was updated successfully, but these errors were encountered: