Skip to content

Add DateTime.fromSecondsSinceEpoch and int get secondsSinceEpoch #59961

Open
@KKimj

Description

@KKimj

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-coretype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions