talexb has asked for the wisdom of the Perl Monks concerning the following question:

A month ago I set up a new server with Ubuntu 22 for a client, replacing an old one that had Ubuntu 18, which I upgraded to Ubuntu 20. The server has about tqo dozen scripts that I've developed and supported for the last six years. He's very happy with the setup.

However, I've been having some challenges with time zones with the new server. I did specify the Eastern (America/Toronto) time zone during installation, and this appeared to work (when logged in as a regular user), until I noticed that cron jobs were running at UTC time. Eventually I figured out I had to set the system time zone (see this post on Mastodon) using timedatectl set-timezone America/Toronto. There's still one script that was mis-behaving, though. The log file showed that the time period is was fetching transactions was correct ..

2024/06/03 07:02:02 INFO : Starting date is 2024-05-27 00:00:00 EDT 2024/06/03 07:02:02 INFO : Ending date is 2024-05-28 00:00:00 EDT
But the epoch values that were being used
..created[gte]=1716782400&created[lt]=1716868800
were UTC. ?!??!?

Naturally, I went back to the documentation at DateTime and saw that for the epoch method, it returns ".. the UTC epoch value for the datetime object." This is confusing to me, since I'm expecting an epoch value in the selected time zone. Those epoch values are four hours later than what I want, and this is messing things up for my client -- this code has worked fine for ~6 years, but is now broken.

I'm not asking for a fix (I know what I need to do), I'm just asking whether some concept was adjusted somewhere. Thanks.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: DateTime's epoch method returns UTC value?
by NERDVANA (Priest) on Jun 03, 2024 at 20:32 UTC
    There's no such thing as a non-UTC epoch number. At one moment in time, the epoch will be identical all around the world, always. (give or take the way different systems handle leap seconds)

    For normal date handling, you start with an epoch, create a DateTime from that epoch, then set_time_zone to get a localized rendering of the time. DateTime objects start on the "floating" time zone, so sometimes you need to first set_time_zone("UTC") then set_time_zone($TZ).

    BTW, the DateTime module respects the $ENV{TZ} rather than the symlink in your filesystem, so make sure that is set correctly wherever the perl script runs.

      Thanks -- it looks like I've sorted this out. The original script ran without any timezone specification, so it was 'floating' .. and that worked fine. My mistake was trying to set a timezone at the new installation.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.