Open
Description
Proposal to Add Features to the DateTime
Class
-
DateTime.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
final DateTime now = DateTime.now();
// Verbose, often takes up 2 lines due to 80-column formatting.
final int seconds = now.millisecondsSinceEpoch ~/ 1000;
TO-BE
final DateTime now = DateTime.now();
final int seconds = now.secondsSinceEpoch;
Thanks!
I’d love to hear your thoughts—feel free to drop a comment!