Connecting Tech Pros Worldwide Forums | Help | Site Map

Display dates between range in perl

Newbie
 
Join Date: Oct 2007
Location: Chennai
Posts: 4
#1: Oct 17 '07
Hi ,

i want a sample perl code to display all the dates between a date range.
say, the start_date=18/03/2007 and end_date=18/04/2007.
here i want to print all the dates beween 18/03/2007 & 18/04/2007 in perl.

Plesae help at the earliest.

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#2: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by aravicha

Hi ,

i want a sample perl code to display all the dates between a date range.
say, the start_date=18/03/2007 and end_date=18/04/2007.
here i want to print all the dates beween 18/03/2007 & 18/04/2007 in perl.

Plesae help at the earliest.

One of the easiest ways that I know of to compare two dates is to convert them into epoch seconds ( the number of seconds since January 1st, 1970, when Unix was born).

You can check out how to do this by reading up on the DateTime::Format::Epoch module.

Regards,

Jeff
Newbie
 
Join Date: Oct 2007
Location: Chennai
Posts: 4
#3: Oct 17 '07

re: Display dates between range in perl


jeff,
i need to diaply all the dates between the date range in the same format in perl.
could you plesae gimme the code here. i need it urgent.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#4: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by aravicha

jeff,
i need to diaply all the dates between the date range in the same format in perl.
could you plesae gimme the code here. i need it urgent.

I completely sympathize with your urgency, but please know that this is not a code writing service, and your emergencies are not ours. Everyone here volunteer their time. This being a learning site, we require that you do the legwork and we will help you if you get stuck.

Go ahead and try some code and see if you can get it to work. Doing the conversion and comparison is pretty easy.

Regards,

Jeff
Newbie
 
Join Date: Oct 2007
Location: Chennai
Posts: 4
#5: Oct 17 '07

re: Display dates between range in perl


i got the below error when i try to run the sample scrpt
Can't locate DateTime/Format/Epoch.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4-solaris-64i
nt /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1/sun4-solaris-64int /usr/perl5/site_perl/5.6.1 /u
sr/perl5/site_perl /usr/perl5/vendor_perl/5.6.1/sun4-solaris-64int /usr/perl5/vendor_perl/5.6.1 /us
r/perl5/vendor_perl .) at d.pl line 2.
BEGIN failed--compilation aborted at d.pl line 2.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#6: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by aravicha

i got the below error when i try to run the sample scrpt
Can't locate DateTime/Format/Epoch.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4-solaris-64i
nt /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1/sun4-solaris-64int /usr/perl5/site_perl/5.6.1 /u
sr/perl5/site_perl /usr/perl5/vendor_perl/5.6.1/sun4-solaris-64int /usr/perl5/vendor_perl/5.6.1 /us
r/perl5/vendor_perl .) at d.pl line 2.
BEGIN failed--compilation aborted at d.pl line 2.

In order to use the module, you are going to have to install it on your system as it is not part of the default Perl install.

You will want to do something like the following:

Expand|Select|Wrap|Line Numbers
  1. perl -MCPAN -e 'install DateTime::Format::Epoch'
  2.  
That will install the module if you already have the CPAN interface setup. If not, you may have to back out, setup the CPAN interface, install the Bundle::CPAN module, and then install this one.

Regards,

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#7: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by aravicha

jeff,
i need to diaply all the dates between the date range in the same format in perl.
could you plesae gimme the code here. i need it urgent.


Why is it urgent?
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#8: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by KevinADC

Why is it urgent?

I don't think it matters. Especially since his sense of urgency does not translate to us. :-)
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#9: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by numberwhun

I don't think it matters. Especially since his sense of urgency does not translate to us. :-)

I would like to read the explanation why this is urgent to the OP.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#10: Oct 17 '07

re: Display dates between range in perl


Quote:

Originally Posted by KevinADC

I would like to read the explanation why this is urgent to the OP.

LOL!!! See, now your just pickin'!!
Newbie
 
Join Date: Oct 2007
Posts: 7
#11: Oct 18 '07

re: Display dates between range in perl


Hi,

Use the below script !!!!!!
Expand|Select|Wrap|Line Numbers
  1. # =======================
  2. #!/usr/bin/perl
  3.  
  4. #
  5. # ==> One limitation: This script is not tested for dates before 1st Jan 1970 <==
  6. #
  7. my( $fromdate, $todate , $fromep, $toep) ;
  8.  
  9. if ($#ARGV < 1)
  10.    { print STDERR "Script $0 must get 2 input parameters\n"; &usage ; exit(1) ;}
  11.  
  12. $fromdate = $ARGV[0] ;
  13. $todate   = $ARGV[1] ;
  14.  
  15. if ( $fromdate > $todate )
  16. {
  17.    print STDERR "From date must be less than or equal to To-date. Both dates must be in YYYYMMDD format\n"; &usage; exit(1); }
  18.  
  19. $_ = $fromdate ;
  20. if (!/([0-9]){8}/)
  21. {  print STDERR "Check if From-date is in YYYYMMDD format\n"; &usage; exit(1); }
  22.  
  23. $_ = $todate ;
  24. if (!/([0-9]){8}/)
  25. {  print STDERR "Check if To-date is in YYYYMMDD format\n"; &usage; exit(1); }
  26.  
  27. if ( $fromdate > 20381231 || $todate > 20381231 )
  28. { print STDERR "\n\nThis script will not work for dates greater than 31st December 2038\nSorry about that, please use alternative logic\n\n" ; exit(1);}
  29.  
  30. if ( $fromdate < 19700101 || $todate < 19700101 )
  31. { print STDERR "\n\nThis script will not work for dates before 1st January 1970\nSorry about that, please use alternative logic\n\n" ; exit(1);}
  32.  
  33. &showDaysBetween ( $fromdate , $todate ) ;
  34.  
  35.  
  36. sub showDaysBetween {
  37.     my( $fromdate, $todate , $fromep, $toep, $currdate, $currep) ;
  38.     ( $fromdate, $todate) = @_ ;
  39.  
  40.     $fromep = &getEpochForGivenDate( "$fromdate" ) ;
  41.     # print "Epoch for $fromdate is $fromep\n" ;
  42.     $currdate = $fromdate ;
  43.     $currep = $fromep ;
  44.  
  45.     $toep = &getEpochForGivenDate( "$todate" ) ;
  46.     # print "Epoch for $todate is $toep\n" ;
  47.  
  48.     print "Day = $currdate\n" ;
  49.     while ( $currdate != $todate )
  50.         {
  51.            $currep += 86400 ;
  52.            $currdate = &getYYYYMMDDforEpoch( $currep ) ;
  53.            print "Day = $currdate\n" ;
  54.         }
  55. }
  56.  
  57. ## Get the epoch value for given day. Current system date is the reference for this.
  58. ## We can get the epoch for current date-time using the time() function, the by adding or substracting 86400 seconds
  59. ## (i.e. seconds in one day) we will find out the epoch number for any given day
  60. sub getEpochForGivenDate {
  61.     my ($indate, $today, $day, $month, $year, $nowtime, $newday, $newtime) ;
  62.     ($indate) = @_ ; ## Input date must be in YYYYMMDD format
  63.  
  64.     $nowtime = time ;
  65.     ($day, $month, $year) = (localtime($nowtime))[3,4,5] ;
  66.     $year = $year + 1900 ; $month ++ ;
  67.     $today="$year$month$day" ;
  68.  
  69.     if ( $indate == $today )
  70.          { retuen $nowtime ; }
  71.     elsif ( $indate lt $today )
  72.          {
  73.            $newtime = $nowtime - 86400 ;
  74.            $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  75.            print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  76.            while ( $newday != $indate )
  77.                  {
  78.                    $newtime = $newtime - 86400 ;
  79.                    $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  80.                    print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  81.                  }
  82.            return $newtime ;
  83.          }
  84.     else
  85.          {
  86.            $newtime = $nowtime + 86400 ;
  87.            $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  88.            print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  89.            while ( $newday != $indate )
  90.                  {
  91.                    $newtime = $newtime + 86400 ;
  92.                    $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  93.                    print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  94.                  }
  95.            return $newtime ;
  96.          }
  97. }
  98.  
  99. sub usage {
  100.     print STDERR "\nUsage : $0 <fromdate in YYYYMMDD> <todate in YYYYMMDD>\n\n( Purpose of this script is to display days between 2 given dates )\n\n" ;
  101. }
  102.  
  103. sub getYYYYMMDDforEpoch {
  104.  
  105.    my $stamp ;
  106.   ($stamp) = @_ ;
  107.   ## Given a epoch number, using localtime function, compose the date string in YYYYMMDD
  108.   return ( (localtime($stamp))[5] + 1900 ) . ( (length(( (localtime($stamp))[4] + 1 )) < 2)? "0".( (localtime($stamp))[4] + 1 ) : ( (localtime($stamp))[4] + 1 ) ) . ( ( length((localtime($stamp))[3]) < 2)? "0".(localtime($stamp))[3] : (localtime($stamp))[3] );
  109.  
  110. }
  111.  
  112. # ========================================
  113.  
Cheers
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#12: Oct 18 '07

re: Display dates between range in perl


cheguvera -

You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.

Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there.

In addition, you can also add the language to your code tags. Here is an example:

[code=perl]
<some code>
[/code]

Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN.

- Moderator
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#13: Oct 18 '07

re: Display dates between range in perl


cheguvera -

This is a learning forum and not a code writing service. We would rather a person code their solutions and learn from that than provide them code and have them learn nothing.

We have pushed him in the right direction and were waiting for an explanation of his urgency. Now, we will probably not get that. But, in return, you can now support any questions he has on the code you have provided.

Regards,

Jeff
Reply


Similar Perl bytes