469,300 Members | 2,312 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

Module for calculating previous day's date

25
Hi,

Is there any module which checks for the previous day's date. For eg I run the program which gives me the current date and I have to check another log file to see if it contains the timestamp as the previous day's date. If it is present, I have to print that particular line to the screen.

So please let me know if there is a module available to check for the previous day date.

Thanks,
Sangith
Jun 19 '07 #1
6 5011
miller
1,089 Expert 1GB
You'll have to calculate it. Look into this module for date manipulation routines:

cpan Date::Calc

- Miller
Jun 19 '07 #2
sangith
25
Thanks for your reply. I will look into the document that you mentioned.

Thanks,
Sangith
Jun 19 '07 #3
KevinADC
4,059 Expert 2GB
if you can get the date/time in seconds from the epoch calculating yesterdays date is very easy:

$yesterday = time - 86400;
print scalar $yesterday;
Jun 19 '07 #4
alcazar
10
You can even use localtime as below with some simple manipulations as below.

Expand|Select|Wrap|Line Numbers
  1. $yesterday_date=get_yesterday();
  2. print "$yesterday_date";
  3.  
  4. sub get_yesterday
  5. {
  6. my ($csec,$cmin,$chr,$cmday,$cmon,$cyear,$cwday,$cyday) = localtime(time) ;
  7.  
  8.   $cmday-=1;
  9.   if(length($cmday) == 1) { $cmday = '0' . $cmday }
  10.  
  11.   $cmon+=1;
  12.   if(length($cmon) == 1) { $cmon = '0' . $cmon }
  13.  
  14.   $cyear+=1900;
  15.  
  16.   my $cDate = "$cmday/$cmon/$cyear";
  17.  
  18. return $cDate;
  19. }
NOTE:localtime function would return the time which is set on the local macine/web server on which the perl script is executed.

cheers,
Alcazar


Thanks for your reply. I will look into the document that you mentioned.

Thanks,
Sangith
Jun 20 '07 #5
KevinADC
4,059 Expert 2GB
alcazar,

the better way is to subtract a days worth of seconds from time:

Expand|Select|Wrap|Line Numbers
  1. my ($csec,$cmin,$chr,$cmday,$cmon,$cyear,$cwday,$cyday) = localtime(time-86400) ;
now you accurately have yesterdays date.

Your suggested way will not always work properly. For example, if the day of the month is 1 (one), you will end up with zero after subtracting one day. There is no such day in a month as zero. Also, the month will not roll back properly either.

Kevin
Jun 20 '07 #6
miller
1,089 Expert 1GB
You can even use localtime as below with some simple manipulations as below.
Yes, as Kevin said, rolling your own data calculation routines is a waste of time. Either do calculations in time since epoch, or use the Date::Calc module to manipulate based individual date parts. There is no reason to do it otherwise, and plenty of reasons not to.

- Miller
Jun 20 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Anthony Robinson | last post: by
1 post views Thread by Shaun Thornburgh | last post: by
2 posts views Thread by Gustavo G. Rondina | last post: by
8 posts views Thread by =?Utf-8?B?QWw=?= | last post: by
4 posts views Thread by gimme_this_gimme_that | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.