Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Comparing DateTime objects in different timezones

by thanos1983 (Parson)
on Nov 01, 2017 at 08:18 UTC ( #1202499=note: print w/replies, xml ) Need Help??

in reply to Comparing DateTime objects in different timezones

Hello Cody Fendant,

Fellow Monks have provided you with the answer to your question. I would like to add another option here with one of my favorite modules regarding time manipulations Date::Manip.

Small sample of code with different timezone(s) and time subtraction. You can apply the time subtraction based on your time of receiving the email etc...

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $tz = new Date::Manip::TZ; my $dateLocal = ParseDate('now'); say $dateLocal; # From timeZone To timeZone my $dateTimeZone = Date_ConvTZ($dateLocal,"GMT","CST"); my $unixLocal = UnixDate($dateLocal,'%Y-%m-%d-%H-%M-%S'); say 'Local Timezone: ' . $unixLocal; my $unixTimeZone = UnixDate($dateTimeZone,'%Y-%m-%d-%H-%M-%S'); say 'Different Timezone: ' . $unixTimeZone; my $date1 = ParseDate($dateLocal); my $date2 = ParseDate($dateTimeZone); my $delta = DateCalc($date1, $date2, 1); say 'Difference between Timezones: ' . $delta; # Change Format my $str = UnixDate($dateLocal,"It is now %T on %b %e, %Y."); say $str; my $str_second = UnixDate($dateLocal,"It is now %T on %B %e, %Y."); say $str_second; my $str_decimal = UnixDate($dateLocal,"It is now decimal %T on %B %d, +%Y."); say $str_decimal; # Add more time or subtract or do what ever you want with time my $deltastr = "12 hours ago"; my $date_past = DateCalc($dateLocal,$deltastr); # say $date; my $str_past = UnixDate($date_past,"It was 12 hours ago %T on %B %d, % +Y."); say $str_past; __END__ $ perl 2017110109:11:31 Local Timezone: 2017-11-01-09-11-31 Different Timezone: 2017-11-01-17-11-31 Difference between Timezones: 0:0:0:0:8:0:0 It is now 09:11:31 on Nov 1, 2017. It is now 09:11:31 on November 1, 2017. It is now decimal 09:11:31 on November 01, 2017. It was 12 hours ago 21:11:31 on October 31, 2017.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202499]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2022-08-11 08:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found