tags:

views:

105

answers:

3

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

+1  A: 

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

Space
+6  A: 

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);
Nic Gibson
Why didn't you link directly to Date::Calc http://search.cpan.org/perldoc/Date::Calc or http://search.cpan.org/perldoc?Date::Calc ?
Brad Gilbert
Because I wanted to point at the module not the docs?
Nic Gibson
+2  A: 

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
Oesor