An offshoot of http://stackoverflow.com/questions/172110/how-do-i-elegantly-print-the-date-in-rfc822-format-in-perl, but Windows specific.
On windows:
C:\> perl -MPOSIX print strftime('%z', localtime()),"\n";
Yields:
Central Daylight Time
I was expecting:
-0500
As anyone would on a Linux system. How can I get the "-0500" on Windows?
UPDATE:
Is this really terrible to do in its place? (Assuming I'm not allowed to install DateTime or package it in any way)
C:\> perl -MPOSIX
sub tzoffset {
my $t = time();
my $utc = mktime(gmtime($t));
my $local = mktime(localtime($t));
return ($local - $utc);
}
sub zformat {
my ($tzoffset) = @_;
my $z = '';
if ($tzoffset < 0) {
$z .= '-';
$tzoffset *= -1;
}
my $hours = floor($tzoffset / 60 / 60);
my $minutes = $tzoffset - $hours * 60 * 60;
$z .= sprintf('%02d%02d', $hours, $minutes);
return $z;
}
print zformat(tzoffset()),"\n";
The problem I've noticed is that this returns -0600
vs -0500
(which I'd expect), but my guess is that is due to DST calculations or something? I'm mainly looking for a decent approximation, but I can't figure out why mktime()
is playing with DST?
UPDATE:
Found out that tzoffset()
can be much more "stable" in terms of DST if you just manually force DST off.
sub tzoffset {
my $t = time();
my $utc = mktime(gmtime($t));
my @tmlocal = localtime($t);
$tmlocal[8] = 0; # force dst off, timezone specific
my $local = mktime(@tmlocal);
return ($local - $utc);
}
This way, no matter if you're DST or not, it'll always return -0500
which is what you want from %z
.