• _stranger_@lemmy.world
    link
    fedilink
    arrow-up
    29
    ·
    edit-2
    2 days ago

    Ok, everyone who’s ever had to use datetime hates it, but not because it’s insufficient, but because international date/time is such a nightmare that the library must be complicated enough to support all the edge cases I’m convinced that library has a function for traveling trough time.

    For years I’ve wrapped datetime with custom functions that do exactly and only what I want to mitigate its all-plumbing-zero-porcelain approach to the problem.

      • Alaknár@sopuli.xyz
        link
        fedilink
        English
        arrow-up
        1
        ·
        12 hours ago

        This is exactly why I love PowerShell.

        Need the [DateTime] object from 10 years ago? No biggie, just chuck (Get-Date).AddYears(-10) down your console.

        Need it in a specific timezone? That one’s trickier, but since PowerShell can do .Net, run this:

        $TargetDateTime = (Get-Date).AddYears(-10)
        $TargetTimeZone = "India Standard Time"
        $tz = [TimeZoneInfo]::FindSystemTimeZoneById($TargetTimeZone)
        $utcOffset = $tz.GetUtcOffset($TargetDateTime)
        [DateTimeOffset]::new($TargetDateTime.Ticks, $utcOffset)
        

        And you get a DateTimeOffset object, which is this beauty:

        DateTime           : 25/08/2015 23:15:14
        UtcDateTime        : 25/08/2015 17:45:14
        LocalDateTime      : 25/08/2015 19:45:14
        Date               : 25/08/2015 00:00:00
        Day                : 25
        DayOfWeek          : Tuesday
        DayOfYear          : 237
        Hour               : 23
        Millisecond        : 421
        Microsecond        : 428
        Nanosecond         : 600
        Minute             : 15
        Month              : 8
        Offset             : 05:30:00
        TotalOffsetMinutes : 330
        Second             : 14
        Ticks              : 635761413144214286
        UtcTicks           : 635761215144214286
        TimeOfDay          : 23:15:14.4214286
        Year               : 2015
        

        DateTime is the time in your target timezone, UtcDateTime is, well, the UTC time, and LocalDateTime is the time on host you ran the commands on.

    • ripcord@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      edit-2
      2 days ago

      Complicated or not, the interfaces suck. And dont have to. And that’s the problem.