in reply to Re^2: Parsing/regex question
in thread Parsing/regex question

Don't, it's wrong.

86400 seconds sooner/later can be the same day.
86400 seconds sooner/later can be two days earlier/later.

use DateTime qw( ); my $today = DateTime->today( time_zone => 'local' ); my $tomorrow = $today->add ( days => 1 ); my $yesterday = $today->subtract( days => 1 ); print "Today is: ", $today ->strftime('%m/%d/%Y'), "\n"; print "Tomorrow is: ", $tomorrow ->strftime('%m/%d/%Y'), "\n"; print "Yesterday is: ", $yesterday->strftime('%m/%d/%Y'), "\n";

Update: Added solution.
Update: Fixed spelling of time_zone. (underscore was missing)

Replies are listed 'Best First'.
Re^4: Parsing/regex question
by toolic (Bishop) on Jul 06, 2009 at 20:21 UTC
    In which case, we can use a CPAN module, such as Date::Simple:
    use Date::Simple ('date', 'today'); my $date = today(); print "Today is: " , $date, "\n"; print "Tomorrow is: " , $date + 1, "\n"; print "Yesterday is: " , $date - 1, "\n";

    Update: oops... didn't see your updated code

Re^4: Parsing/regex question
by vxp (Pilgrim) on Jul 07, 2009 at 18:02 UTC
    cool! doing the following now:
    $today_tmp = DateTime->now(); $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); $today = $today_tmp->mdy('/'); $tomorrow = $tomorrow_tmp->mdy('/'); $yesterday = $yesterday_tmp->mdy('/'); print "Today: $today -=- "; print "Tomorrow: $tomorrow -=- "; print "Yesterday: $yesterday -=- ";

    Works perfectly!

      No, it doesn't. I thought you wanted the local date, not the date halfway around the world. If so, you introduced a bug by removing time_zone => 'local'. (Yeah, I had it misspelled.)

      I'm also curious as to why you changed from today to now.

        I actually do want the date half way around the world :) The 'local' was taken out because the script is still a work-in-progress, so to speak.

        I've got the following now:

        ... my $tz_ny = "America/New_York"; my $tz_ln = "Europe/London"; my $tz_tk = "Asia/Tokyo"; my $tz_hk = "Asia/Hong_Kong"; my $tz_se = "Asia/Seoul"; my $tz_mo = "Europe/Moscow"; ... if ($region =~ /ny/) { $today_tmp = DateTime->now( time_zone => $tz_ny); print "it is $today_tmp now in NY\n"; $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); } elsif ($region =~ /ln/) { $today_tmp = DateTime->now( time_zone => $tz_ln); print "it is $today_tmp now in LN\n"; $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); } elsif ($region =~ /tk/) { $today_tmp = DateTime->now( time_zone => $tz_tk); print "it is $today_tmp now in TK\n"; $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); } elsif ($region =~ /hk/) { $today_tmp = DateTime->now( time_zone => $tz_hk); print "it is $today_tmp now in HK\n"; $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); } elsif ($region =~ /se/) { $today_tmp = DateTime->now( time_zone => $tz_se); print "it is $today_tmp now in SE\n"; $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); } elsif ($region =~ /mo/) { $today_tmp = DateTime->now( time_zone => $tz_mo); print "it is $today_tmp now in MO\n"; $tomorrow_tmp = DateTime->now()->add( days => 1 ); $yesterday_tmp = DateTime->now()->subtract( days => 1 ); } $today = $today_tmp->mdy('/'); $tomorrow = $tomorrow_tmp->mdy('/'); $yesterday = $yesterday_tmp->mdy('/'); print "Today ($region): $today -=- "; print "Tomorrow ($region): $tomorrow -=- "; print "Yesterday ($region): $yesterday -=- "; $next_business_day = `$holiday_script "$today" $region`; chomp($next_business_day); print "Next business day: $next_business_day\n"; $today_local_tmp = DateTime->now( time_zone => 'local'); $tomorrow_local_tmp = DateTime->now()->add( days => 1 ); $yesterday_local_tmp = DateTime->now()->subtract( days => 1 ); $today_local = $today_local_tmp->mdy('/'); $tomorrow_local = $tomorrow_local_tmp->mdy('/'); $yesterday_local = $yesterday_local_tmp->mdy('/'); print "Today (local (NY) ): $today_local -=- "; print "Tomorrow (local (NY) ): $tomorrow_local -=- "; print "Yesterday (local (NY) ): $yesterday_local -=- "; $next_business_day_local = `$holiday_script "$today" ny`; chomp($next_business_day_local); print "Next business day (local (NY) ): $next_business_day_local\n"; ...

        So in essence I have 2 variables - one for whatever timezone I'm interested in at the moment, and another one that always contains local time.

        As for now and today - was just playing around and replaced it, then never switched it back :)