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

Hi Monks!
I am trying to get the time zone dynamically for a script I am working on, and I cant figured it out what is the best way to do it. I've have got this far but having problems getting the result on the format I need, may be someone here will have a better way or can help me format what I have to the way I am trying. Thanks!
#!/usr/bin/perl use strict; use Time::Zone; use Date::Format; use Date::Parse; my $TZ = 'America/New_York'; my $now = scalar localtime time() + tz_offset($TZ); my $today = time2str( "%m-%d-%Y \@ %H:%M:%S", $now); # I am trying to format the result like this = 09-30-2010 @ 21:45:06 print "Today: $today\n";

Thanks for the help!

Replies are listed 'Best First'.
Re: Dynamic Time Zone Help
by ikegami (Patriarch) on Oct 01, 2010 at 03:41 UTC
    use DateTime qw( ); print DateTime->now(time_zone => 'America/New_York') ->strftime("Today: %m-%d-%Y \@ %H:%M:%S\n");
    Today: 09-30-2010 @ 23:40:33

    DateTime

Re: Dynamic Time Zone Help
by suhailck (Friar) on Oct 01, 2010 at 02:50 UTC
    An alternative
    #!/usr/bin/perl use strict; use Date::Format; $ENV{'TZ'} = 'America/New_York'; my $today = time2str( "%m-%d-%Y \@ %H:%M:%S", time); # I am trying to format the result like this = 09-30-2010 @ 21:45:06 print "Today: $today\n";
      Doesn't work for me. Threaded build, Windows.
Re: Dynamic Time Zone Help
by ambrus (Abbot) on Oct 01, 2010 at 19:02 UTC
    use Date::Manip::Date; my $d = Date::Manip::Date->new("now"); $d->convert("America/New_York"); print $d->printf("Today: %m-%d-%Y \@ %H:%M:%S\n");