Display dates between range in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aravicha
    New Member
    • Oct 2007
    • 4

    Display dates between range in perl

    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
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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::Forma t::Epoch module.

    Regards,

    Jeff

    Comment

    • aravicha
      New Member
      • Oct 2007
      • 4

      #3
      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.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • aravicha
          New Member
          • Oct 2007
          • 4

          #5
          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.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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:

            Code:
            perl -MCPAN -e 'install DateTime::Format::Epoch'
            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

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              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?

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                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. :-)

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  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.

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Originally posted by KevinADC
                    I would like to read the explanation why this is urgent to the OP.
                    LOL!!! See, now your just pickin'!!

                    Comment

                    • cheguvera
                      New Member
                      • Oct 2007
                      • 7

                      #11
                      Hi,

                      Use the below script !!!!!!
                      [code=perl]
                      # =============== ========
                      #!/usr/bin/perl

                      #
                      # ==> One limitation: This script is not tested for dates before 1st Jan 1970 <==
                      #
                      my( $fromdate, $todate , $fromep, $toep) ;

                      if ($#ARGV < 1)
                      { print STDERR "Script $0 must get 2 input parameters\n"; &usage ; exit(1) ;}

                      $fromdate = $ARGV[0] ;
                      $todate = $ARGV[1] ;

                      if ( $fromdate > $todate )
                      {
                      print STDERR "From date must be less than or equal to To-date. Both dates must be in YYYYMMDD format\n"; &usage; exit(1); }

                      $_ = $fromdate ;
                      if (!/([0-9]){8}/)
                      { print STDERR "Check if From-date is in YYYYMMDD format\n"; &usage; exit(1); }

                      $_ = $todate ;
                      if (!/([0-9]){8}/)
                      { print STDERR "Check if To-date is in YYYYMMDD format\n"; &usage; exit(1); }

                      if ( $fromdate > 20381231 || $todate > 20381231 )
                      { 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);}

                      if ( $fromdate < 19700101 || $todate < 19700101 )
                      { 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);}

                      &showDaysBetwee n ( $fromdate , $todate ) ;


                      sub showDaysBetween {
                      my( $fromdate, $todate , $fromep, $toep, $currdate, $currep) ;
                      ( $fromdate, $todate) = @_ ;

                      $fromep = &getEpochForGiv enDate( "$fromdate" ) ;
                      # print "Epoch for $fromdate is $fromep\n" ;
                      $currdate = $fromdate ;
                      $currep = $fromep ;

                      $toep = &getEpochForGiv enDate( "$todate" ) ;
                      # print "Epoch for $todate is $toep\n" ;

                      print "Day = $currdate\n" ;
                      while ( $currdate != $todate )
                      {
                      $currep += 86400 ;
                      $currdate = &getYYYYMMDDfor Epoch( $currep ) ;
                      print "Day = $currdate\n" ;
                      }
                      }

                      ## Get the epoch value for given day. Current system date is the reference for this.
                      ## We can get the epoch for current date-time using the time() function, the by adding or substracting 86400 seconds
                      ## (i.e. seconds in one day) we will find out the epoch number for any given day
                      sub getEpochForGive nDate {
                      my ($indate, $today, $day, $month, $year, $nowtime, $newday, $newtime) ;
                      ($indate) = @_ ; ## Input date must be in YYYYMMDD format

                      $nowtime = time ;
                      ($day, $month, $year) = (localtime($now time))[3,4,5] ;
                      $year = $year + 1900 ; $month ++ ;
                      $today="$year$m onth$day" ;

                      if ( $indate == $today )
                      { retuen $nowtime ; }
                      elsif ( $indate lt $today )
                      {
                      $newtime = $nowtime - 86400 ;
                      $newday = &getYYYYMMDDfor Epoch( $newtime ) ;
                      print "Indate=$in date Newday=$newday\ n" if ($ENV{DETAIL_DE BUG} eq "1") ;
                      while ( $newday != $indate )
                      {
                      $newtime = $newtime - 86400 ;
                      $newday = &getYYYYMMDDfor Epoch( $newtime ) ;
                      print "Indate=$in date Newday=$newday\ n" if ($ENV{DETAIL_DE BUG} eq "1") ;
                      }
                      return $newtime ;
                      }
                      else
                      {
                      $newtime = $nowtime + 86400 ;
                      $newday = &getYYYYMMDDfor Epoch( $newtime ) ;
                      print "Indate=$in date Newday=$newday\ n" if ($ENV{DETAIL_DE BUG} eq "1") ;
                      while ( $newday != $indate )
                      {
                      $newtime = $newtime + 86400 ;
                      $newday = &getYYYYMMDDfor Epoch( $newtime ) ;
                      print "Indate=$in date Newday=$newday\ n" if ($ENV{DETAIL_DE BUG} eq "1") ;
                      }
                      return $newtime ;
                      }
                      }

                      sub usage {
                      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" ;
                      }

                      sub getYYYYMMDDforE poch {

                      my $stamp ;
                      ($stamp) = @_ ;
                      ## Given a epoch number, using localtime function, compose the date string in YYYYMMDD
                      return ( (localtime($sta mp))[5] + 1900 ) . ( (length(( (localtime($sta mp))[4] + 1 )) < 2)? "0".( (localtime($sta mp))[4] + 1 ) : ( (localtime($sta mp))[4] + 1 ) ) . ( ( length((localti me($stamp))[3]) < 2)? "0".(localtime( $stamp))[3] : (localtime($sta mp))[3] );

                      }

                      # =============== =============== ==========
                      [/code]

                      Cheers
                      Last edited by numberwhun; Oct 18 '07, 03:16 PM. Reason: add code tags

                      Comment

                      • numberwhun
                        Recognized Expert Moderator Specialist
                        • May 2007
                        • 3467

                        #12
                        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 &#91;code] and end with &#91;/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:

                        &#91;code=pe rl]
                        <some code>
                        &#91;/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

                        Comment

                        • numberwhun
                          Recognized Expert Moderator Specialist
                          • May 2007
                          • 3467

                          #13
                          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

                          Comment

                          Working...