SBECK-github / Date-Manip

Other
10 stars 11 forks source link

D::M timezoneinfo uses unreliable symlink filename #21

Closed sni closed 5 years ago

sni commented 6 years ago

Having a docker container which mounts host /etc/localtime into the container /etc/localtime to sync timezone information. Now docker follows the symlink and mounts the hosts localtime (which is CET) onto /usr/share/zoneinfo/UTC ending in a zoneinfo file named UTC but containing CET data.

Linux date behaves correctly and tells the correct time, date and zone.

%> date +"%s - %Y-%m-%d %H:%M:%S %z"
1529498671 - 2018-06-20 14:44:31 +0200

Using D::M tells me:

%> DATE_MANIP_DEBUG=1 perl -MDate::Manip -e 'print UnixDate("now", "%s - %Y-%m-%d %H:%M:%S %z"),"\n";'
*** DEBUG *** METHOD: main [TZ] undef
*** DEBUG *** METHOD: env [zone,TZ] undef
*** DEBUG *** METHOD: file [/etc/TIMEZONE] not found
*** DEBUG *** METHOD: file [/etc/timezone] not found
*** DEBUG *** METHOD: file [/etc/sysconfig/clock] not found
*** DEBUG *** METHOD: file [/etc/default/init] not found
*** DEBUG *** METHOD: tzdata [/etc/localtime /usr/share/zoneinfo] UTC
*** DEBUG *** METHOD: main [TZ] undef
*** DEBUG *** METHOD: env [zone,TZ] undef
*** DEBUG *** METHOD: file [/etc/TIMEZONE] not found
*** DEBUG *** METHOD: file [/etc/timezone] not found
*** DEBUG *** METHOD: file [/etc/sysconfig/clock] not found
*** DEBUG *** METHOD: file [/etc/default/init] not found
*** DEBUG *** METHOD: tzdata [/etc/localtime /usr/share/zoneinfo] UTC
1529505873 - 2018-06-20 14:44:33 +0000

So the time is correct, but the unix timestamp and zone is wrong. I guess looking at the filename instead of the file content is for performance reasons. But on the other hand, those zonefiles are pretty small, so it shouldn't have much of an impact.

Uncommenting this line helps: https://github.com/SBECK-github/Date-Manip/blame/master/lib/Date/Manip/TZ.pm#L108 Since timezone is then correctly detected by date +%Z command.

Other workaround would be to set $ENV{'TZ'} = date +%Z;

SBECK-github commented 5 years ago

I've reordered the TZ detection checks (moved the zoneinfo check down) so the date commands will run first.

I'm not so familiar with docker (becoming more so... but not there yet), so I may reevaluate this later, but for now it seems like a safe change.

Sorry for the slow response... I've been extremely occupied with some other things and have not spent much time on Date::Manip development the last 6 months.