user1289
user1289

Reputation: 1321

tzset + setting TZ env variable doesn't work under windows

I'm trying to get current NY time. This code works under Linux, but gives local time under windows.

use POSIX qw(tzset);

sub is_time
{
    $ENV{TZ} = 'America/New_York';
    tzset();

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
    if (($hour > 9 && $hour < 14) || ($hour == 9 && $min > 30))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

What am I doing wrong? Is there an alternative way to do this in Windows?

Upvotes: 0

Views: 1022

Answers (2)

ysth
ysth

Reputation: 98398

Windows doesn't use Olson timezones.

Try $ENV{TZ} = 'EST5EDT';

For example,

use feature qw( say );
use POSIX qw( tzset );
for my $tz (qw( EST5EDT PST8PDT )) {
   local $ENV{TZ} = $tz;
   tzset();
   say("$tz: ", scalar(localtime));
}

Output:

EST5EDT: Tue Aug 22 22:14:04 2017
PST8PDT: Tue Aug 22 19:14:04 2017

Upvotes: 0

ikegami
ikegami

Reputation: 386501

use DateTime qw( );

my $dt = DateTime->now( time_zone => 'America/New_York' );
return $dt->hour > 9 && $dt->hour < 14 || $dt->hour == 9 && $dt->minute > 30;

Upvotes: 1

Related Questions