Module for calculating previous day's date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sangith
    New Member
    • Jun 2007
    • 25

    Module for calculating previous day's date

    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
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    You'll have to calculate it. Look into this module for date manipulation routines:

    cpan Date::Calc

    - Miller

    Comment

    • sangith
      New Member
      • Jun 2007
      • 25

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

      Thanks,
      Sangith

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        if you can get the date/time in seconds from the epoch calculating yesterdays date is very easy:

        $yesterday = time - 86400;
        print scalar $yesterday;

        Comment

        • alcazar
          New Member
          • Jun 2007
          • 10

          #5
          You can even use localtime as below with some simple manipulations as below.

          Code:
          $yesterday_date=get_yesterday();
          print "$yesterday_date";
          
          sub get_yesterday
          {
          my ($csec,$cmin,$chr,$cmday,$cmon,$cyear,$cwday,$cyday) = localtime(time) ;
          
            $cmday-=1;
            if(length($cmday) == 1) { $cmday = '0' . $cmday }
          
            $cmon+=1;
            if(length($cmon) == 1) { $cmon = '0' . $cmon }
          
            $cyear+=1900;
          
            my $cDate = "$cmday/$cmon/$cyear";
          
          return $cDate;
          }
          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


          Originally posted by sangith
          Thanks for your reply. I will look into the document that you mentioned.

          Thanks,
          Sangith

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            alcazar,

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

            Code:
            my ($csec,$cmin,$chr,$cmday,$cmon,$cyear,$cwday,$cyday) = localtime([B]time-86400[/B]) ;
            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

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              Originally posted by alcazar
              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

              Comment

              Working...