jonny
jonny

Reputation: 736

How can I get UTC equivalents of Today_and_Now() and Today() in Perl?

How can I get UTC equivalents of Today_and_Now() and Today() call results? Can I convert them back to local time?

Upvotes: 1

Views: 2193

Answers (3)

Oesor
Oesor

Reputation: 6642

I've turned into more of a fan of DateTime each time I use it.

my $local_tz = DateTime::TimeZone->new( name=> 'local' );
my $now = DateTime->now( time_zone => $local_tz );

$now->set_time_zone('UTC');
say $now->hour();  ### UTC

$now->set_time_zone($local_tz);
say $now->hour();  ### Back to local time

Upvotes: 2

Nic Gibson
Nic Gibson

Reputation: 7143

If you are using Today_and_Now() I assume that you're using Date::Calc. So, if you read the docs you will find that you can pass a parameter to both of those that indicates that gmtime() should be used as input rather than localtime(). Simply pass any true value to the these functions.

   my ($year,$month,$day) = Today(1);
   my ($year,$month, $day, $hours, $mins, $secs) = Today_and_Now(1);

Upvotes: 6

Space
Space

Reputation: 7259

Have a look at "Programmatic Time Usage With Perl". This will explain you the brief summary of tips for using time functions in Perl.

Other then this you can also have a look at some perl modules i.e. Time::UTC::Now and DateTime::TimeZone

Upvotes: 1

Related Questions