CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Calculate previous day given a fixed date

 



cuboidgraphix
User

Feb 23, 2010, 6:29 PM

Post #1 of 11 (4867 views)
Calculate previous day given a fixed date Can't Post

Hi guys,
I had posted earlier in the beginners forum, but I guess this is more complex since I got no response.

I am given a fixed date.

$date = '2010-01-01' // or whatever date given to the variable.

I need to calculate the previous date/day. eg: $yesterday = '2009-12-31' .... is there an easy way to do this through perl?

Thanks in advance for any assistance rendered.


cuboidgraphix
User

Feb 23, 2010, 9:18 PM

Post #2 of 11 (4853 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

Anyone ... Please? I was thinking that maybe it might be possible through epoch time? Maybe it's wishful thinking? I've tried several ways, but I'm just not too good at perl.


FishMonger
Veteran / Moderator

Feb 24, 2010, 5:06 AM

Post #3 of 11 (4846 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

CPAN is your friend. A 30 second search would have given you multiple options, such as:

Date::Calc - Gregorian calendar date calculations
http://search.cpan.org/~stbey/Date-Calc-6.3/lib/Date/Calc.pod

Date::Parse - Parse date strings into time values
http://search.cpan.org/~gbarr/TimeDate-1.20/lib/Date/Parse.pm

Date::Manip - date manipulation routines
http://search.cpan.org/~sbeck/Date-Manip-5.55/lib/Date/Manip.pod


cuboidgraphix
User

Feb 24, 2010, 6:30 AM

Post #4 of 11 (4842 views)
Re: [FishMonger] Calculate previous day given a fixed date [In reply to] Can't Post

Thanks fishmonger,
I did look at a couple of those but I don't have the module loaded and I don't have the rights to install it. I did try working with epoch time and so far it works fine. The only problem I have is that I want to insert the output as a variable instead of using the printf.

Any insight on how I can do this?


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Time::Local;

my $date = '2010-01-01';

my $year = substr $date, 0, 4;
my $month = substr $date, 5, 2;
my $day = substr $date, 8, 2;

my $tday = timelocal(0,0,0,$day,$month,$year);

my $yday = $tday - 2716143;

my($sec,$min,$hour);
($sec,$min,$hour,$day,$month,$year) = localtime($yday);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

my $ydate = printf("%02d-%02d-%02d", $year, $month, $day);

#print $ydate;


at this point I have print $ydate commented out, but it still keeps printing it because of the printf.


cuboidgraphix
User

Feb 24, 2010, 6:53 AM

Post #5 of 11 (4840 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

Nevermind.

I guess I was totally mind blocked last night/early this morning.

This is my resulting script.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Time::Local;

my $date = '2010-01-01';

my $year = substr $date, 0, 4;
my $month = substr $date, 5, 2;
my $day = substr $date, 8, 2;

my $tday = timelocal(0,0,0,$day,$month,$year);

my $yday = $tday - 2716143;

my($sec,$min,$hour);
($sec,$min,$hour,$day,$month,$year) = localtime($yday);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

my $ydate = $year."-".$month."-".$day."\n";

print $ydate;


Thanks fish!!! :)


FishMonger
Veteran / Moderator

Feb 24, 2010, 7:07 AM

Post #6 of 11 (4838 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

2716143
How did you come up with that number? That's 31.4368 days.

You can reduce this section to 1 line by using the strftime function in the POSIX module.

Code
my($sec,$min,$hour);  
($sec,$min,$hour,$day,$month,$year) = localtime($yday);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

my $ydate = $year."-".$month."-".$day."\n";



Code
my $ydate = strftime("%Y-%m-%d\n", localtime($yday) );



cuboidgraphix
User

Feb 24, 2010, 7:12 AM

Post #7 of 11 (4837 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

OK... I'm back. :)

Apparently there is a problem with my script when $date = '2010-01-29';

I get this..

Day '29' out of range 1..28 at ./testdate.pl line 13

Any idea why it happens and how to fix it?

Thanks


FishMonger
Veteran / Moderator

Feb 24, 2010, 7:44 AM

Post #8 of 11 (4837 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

Did you read the documentation for localtime or Time::Local?


Quote
It is worth drawing particular attention to the expected ranges for the values provided. The value for the day of the month is the actual day (ie 1..31), while the month is the number of months since January (0..11).


Month '01' is February and Feb 2010 does not have 29 days.


cuboidgraphix
User

Feb 24, 2010, 8:29 AM

Post #9 of 11 (4831 views)
Re: [FishMonger] Calculate previous day given a fixed date [In reply to] Can't Post

Yes FishMonger... :) I fixed it. :) This is the code.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Time::Local;

my $date = '2010-01-29';

my $year = substr $date, 0, 4;
my $month = substr $date, 5, 2;
my $day = substr $date, 8, 2;

my $tday = timelocal(0,0,0,$day,$month-1,$year);

my $yday = $tday - 86400;

my($sec,$min,$hour);
($sec,$min,$hour,$day,$month,$year) = localtime($yday);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

my $ydate = $year."-".$month."-".$day;

print $ydate;


However, I am trying to figure out how to change my output's date format. I get the output as '2010-1-28'.

I need the format to be '2010-01-28' ie: yyyy-mm-dd.

Any insight on how to achieve this?


cuboidgraphix
User

Feb 24, 2010, 8:38 AM

Post #10 of 11 (4830 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post

OK nevermind,

My internet is so slow.. I didn't see your previous posts.

Your script did get me the right format i was looking for.

Thanks! :D


rovf
Veteran

Feb 26, 2010, 6:36 AM

Post #11 of 11 (4816 views)
Re: [cuboidgraphix] Calculate previous day given a fixed date [In reply to] Can't Post


In Reply To
I don't have the module loaded and I don't have the rights to install it.


You don't need any special rights to install a module. If you can create a file on your computer, you can install a Perl module. Just install it into your $HOME.

Ronald

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives