Search code examples
datetimetimezoneraku

Why DateTime.now and Date.today.DateTime have different timezone?


[23] > my $now = DateTime.now
2024-09-29T14:59:10.178051+08:00
[24] > my $eight-hours-later = Date.today.DateTime.later(:8hours)
2024-09-29T08:00:00Z

[25] > $now - $eight-hours-later
-3649.82194842
[26] > $now.timezone
28800
[27] > $eight-hours-later.timezone
0
[28] > Date.today.DateTime.timezone
0
[29] > $now.WHAT
(DateTime)
[30] $eight-hours-later.WHAT
(DateTime)

In the above REPL, $now and $eight-hours-later both are DateTime, but why $now has timezone of 28800 and $eight-hours-later has timezone of 0, result in different timezone.

My Raku version is:

Welcome to Rakudo v2024.05.1.
Implementing the Raku Programming Language v6.d.
Built on MoarVM version 2024.05

Solution

  • A very good question: and I'd say this is LTA behaviour.

    Therefore I've just added a :timezone argument to the Date.DateTime coercer, that defaults to $*TZ

    Since this is a breaking change, this new behaviour is only available in 6.e and higher (and only in the upcoming 2024.10 release).

    % raku -e 'use v6.d; say Date.today.DateTime'                 
    2024-09-29T00:00:00Z
    % raku -e 'use v6.e.PREVIEW; say Date.today.DateTime'
    2024-09-29T00:00:00+02:00
    

    As a temporary workaround, you can use the in-timezone method with $*TZ as the argument.

    % raku -e 'say Date.today.DateTime.in-timezone($*TZ)'
    2024-09-29T02:00:00+02:00