Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] DATE problem

Expand Messages
  • Charles K. Clarkson
    ... In perl, dates are kept in a serial integer. Its value is equal to the number seconds since a particular date called the Epoch. All perl functions use the
    Message 1 of 7 , Dec 4, 2002
    • 0 Attachment
      Katus Waldemar [waldemar.katus@...] scribbled:

      : [I] have a problem with date. For example:
      :
      : my $DATE_ = system("date +%y%m%d");
      : print DATE_
      :
      : and I return: 021204
      :
      : It's OK , but I want return 021203.
      : That is $DATE_ - 1, but it's not simple.....
      : (for example: today is 020301;
      : 020301 - 1 = 020228 or 020229:))
      :
      : Please write me how I can writ in PERL (or shell)
      : function, which return date - 1.

      In perl, dates are kept in a serial integer.
      Its value is equal to the number seconds since a
      particular date called the Epoch. All perl
      functions use the same Epoch. Perl has a function
      which translates the serial number to a format
      human beings find easier to understand.
      Some modules which come standard with perl
      have additional formatting abilities. If you need
      a lot of date functions you'll want to look at
      Date::Calc.

      If you look in 'perlfunc' (part of the
      standard perl documentation), you'll find the
      'localtime' function. It is used to go from the
      serial number mentioned above to the various
      human readable items. The POSIX function strftime
      takes the same arguments to display date and time
      in user defined formats.

      To convert a date to a serial takes a little
      more work. The date format differs by locale,
      application, and other factors. 120103 may be the
      twelfth day of January 2003, it may be the first
      day of December 2003, or even the third day of
      January 1912.

      Because of this, date conversion using a
      single function can return erroneous results. The
      best method is for you, the programmer, to handle
      the conversion. One of the easiest functions to
      convert dates from human readable to serial is in
      a module included with the standard perl
      distribution called Time::Local. It provides a
      function named 'timelocal'.

      For this example we'll use 'timelocal' to
      convert 021204 to a serial, we'll subtract one
      day (24 hours X 60 minutes X 60 seconds), and
      we'll use strftime from the POSIX module to
      convert the date back to the "%y%m%d" format.

      We can use 'substr' or a regex to convert
      the date. I like regexes better, but 'substr'
      is faster. As you can see we must make some
      assumptions about the four digit year.

      my $date = '021204';
      my ( $year, $month, $day ) = $date =~ /(\d\d)(\d\d)(\d\d)/;

      # adjust year to 4 digits
      $year += $year > 49 ? 1900 : 2000;

      # adjust month to 0 .. 11 range
      --$month;


      Convert it all to a serial using timelocal().

      use Time::Local 'timelocal';

      # timelocal expects ( $sec, $min, $hours, $mday, $mon, $year );

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


      Subtract one day.

      my $one_day = 24 * 60 * 60;

      $date_serial -= $one_day;


      Convert it back to the format we want using
      'strftime' from the POSIX module.

      use POSIX 'strftime';

      print strftime( '%y%m%d', localtime $date_serial );


      Putting it all together (with some added
      subroutines):

      use POSIX 'strftime';
      use Time::Local 'timelocal';

      my $date = '021204';
      print "$date\n";

      my $one_day = 24 * 60 * 60;
      my $serial = date_to_serial( $date );
      print serial_to_date( $serial - $one_day, '%y%m%d' );


      sub date_to_serial {
      local $_ = shift;
      my ( $year, $month, $day ) = /(\d\d)(\d\d)(\d\d)/;

      # adjust year to 4 digits
      $year += $year > 49 ? 1900 : 2000;

      # adjust month to 0 .. 11 range
      --$month;

      # timelocal expects ( $sec, $min, $hours, $mday, $mon, $year );
      return timelocal( 0, 0, 0, $day, $month, $year );
      }

      sub serial_to_date {
      my( $serial, $format ) = @_;
      return strftime( $format, localtime $serial );
      }


      HTH,

      Charles K. Clarkson
      --
      Head Bottle Washer,
      Clarkson Energy Homes, Inc.
      Mobile Home Specialists
      254 968-8328
    • Hans Ginzel
      ... perldoc -f time; perldoc -f localtime; print join , localtime(time - 24*60*60); `date -d day ago ` H.
      Message 2 of 7 , Dec 4, 2002
      • 0 Attachment
        On Wed, Dec 04, 2002 at 11:49:19AM +0100, Katus Waldemar wrote:
        > Please write me how I can writ in PERL (or shell) function, which return
        > date - 1.

        perldoc -f time; perldoc -f localtime;

        print join " ", localtime(time - 24*60*60);

        `date -d "day ago"`

        H.
      • Katus Waldemar
        I wrote in this way: #!/usr/bin/perl my $date_time=time; $date_time=$date_time-86400; ($day, $month, $year)=(localtime($date_time))[3,4,5]; $day_ok= 0$day ;
        Message 3 of 7 , Dec 5, 2002
        • 0 Attachment
          I wrote in this way:

          #!/usr/bin/perl

          my $date_time=time;
          $date_time=$date_time-86400;
          ($day, $month, $year)=(localtime($date_time))[3,4,5];

          $day_ok="0$day";
          $year_o=substr($year, 1, 2);
          $year_ok="20$year_o";
          $month_ok=$month+1;

          $date_all="$day_ok$month_ok$year_ok\n" if $day <= 9;
          $date_all="$day$month_ok$year_ok\n" if $day > 9;

          A have additional question: How I can join two or more strings, for example
          $a="mather";
          $b="father";
          c$=$a + $b; ?????????????





          -----Original Message-----
          From: Charles K. Clarkson [mailto:cclarkson@...]
          Sent: Wednesday, December 04, 2002 7:15 PM
          To: perl-beginner@yahoogroups.com
          Subject: RE: [PBML] DATE problem


          Katus Waldemar [waldemar.katus@...] scribbled:

          : [I] have a problem with date. For example:
          :
          : my $DATE_ = system("date +%y%m%d");
          : print DATE_
          :
          : and I return: 021204
          :
          : It's OK , but I want return 021203.
          : That is $DATE_ - 1, but it's not simple.....
          : (for example: today is 020301;
          : 020301 - 1 = 020228 or 020229:))
          :
          : Please write me how I can writ in PERL (or shell)
          : function, which return date - 1.

          In perl, dates are kept in a serial integer.
          Its value is equal to the number seconds since a
          particular date called the Epoch. All perl
          functions use the same Epoch. Perl has a function
          which translates the serial number to a format
          human beings find easier to understand.
          Some modules which come standard with perl
          have additional formatting abilities. If you need
          a lot of date functions you'll want to look at
          Date::Calc.

          If you look in 'perlfunc' (part of the
          standard perl documentation), you'll find the
          'localtime' function. It is used to go from the
          serial number mentioned above to the various
          human readable items. The POSIX function strftime
          takes the same arguments to display date and time
          in user defined formats.

          To convert a date to a serial takes a little
          more work. The date format differs by locale,
          application, and other factors. 120103 may be the
          twelfth day of January 2003, it may be the first
          day of December 2003, or even the third day of
          January 1912.

          Because of this, date conversion using a
          single function can return erroneous results. The
          best method is for you, the programmer, to handle
          the conversion. One of the easiest functions to
          convert dates from human readable to serial is in
          a module included with the standard perl
          distribution called Time::Local. It provides a
          function named 'timelocal'.

          For this example we'll use 'timelocal' to
          convert 021204 to a serial, we'll subtract one
          day (24 hours X 60 minutes X 60 seconds), and
          we'll use strftime from the POSIX module to
          convert the date back to the "%y%m%d" format.

          We can use 'substr' or a regex to convert
          the date. I like regexes better, but 'substr'
          is faster. As you can see we must make some
          assumptions about the four digit year.

          my $date = '021204';
          my ( $year, $month, $day ) = $date =~ /(\d\d)(\d\d)(\d\d)/;

          # adjust year to 4 digits
          $year += $year > 49 ? 1900 : 2000;

          # adjust month to 0 .. 11 range
          --$month;


          Convert it all to a serial using timelocal().

          use Time::Local 'timelocal';

          # timelocal expects ( $sec, $min, $hours, $mday, $mon, $year );

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


          Subtract one day.

          my $one_day = 24 * 60 * 60;

          $date_serial -= $one_day;


          Convert it back to the format we want using
          'strftime' from the POSIX module.

          use POSIX 'strftime';

          print strftime( '%y%m%d', localtime $date_serial );


          Putting it all together (with some added
          subroutines):

          use POSIX 'strftime';
          use Time::Local 'timelocal';

          my $date = '021204';
          print "$date\n";

          my $one_day = 24 * 60 * 60;
          my $serial = date_to_serial( $date );
          print serial_to_date( $serial - $one_day, '%y%m%d' );


          sub date_to_serial {
          local $_ = shift;
          my ( $year, $month, $day ) = /(\d\d)(\d\d)(\d\d)/;

          # adjust year to 4 digits
          $year += $year > 49 ? 1900 : 2000;

          # adjust month to 0 .. 11 range
          --$month;

          # timelocal expects ( $sec, $min, $hours, $mday, $mon, $year );
          return timelocal( 0, 0, 0, $day, $month, $year );
          }

          sub serial_to_date {
          my( $serial, $format ) = @_;
          return strftime( $format, localtime $serial );
          }


          HTH,

          Charles K. Clarkson
          --
          Head Bottle Washer,
          Clarkson Energy Homes, Inc.
          Mobile Home Specialists
          254 968-8328









          Yahoo! Groups Sponsor

          ADVERTISEMENT

          <http://rd.yahoo.com/M=237459.2675695.4055211.2225243/D=egroupweb/S=17050069
          51:HM/A=1267611/R=0/*http://ad.doubleclick.net/jump/N2524.Yahoo/B1071650;sz=
          300x250;ord=1039025721016962?>

          <http://us.adserver.yahoo.com/l?M=237459.2675695.4055211.2225243/D=egroupmai
          l/S=:HM/A=1267611/rand=371293329>

          Unsubscribing info is here:
          http://help.yahoo.com/help/us/groups/groups-32.html
          <http://help.yahoo.com/help/us/groups/groups-32.html>

          Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
          <http://docs.yahoo.com/info/terms/> .
        • Greg
          ... example ... This is one of the most basic things in Perl: Two ways (at least) - 1. $c = $a$b ; 2. $c = $a . $b; Greg bmwclub.org.uk uksites4all.co.uk
          Message 4 of 7 , Dec 5, 2002
          • 0 Attachment
            --- In perl-beginner@y..., Katus Waldemar <waldemar.katus@b...> wrote:
            > A have additional question: How I can join two or more strings, for
            example
            > $a="mather";
            > $b="father";
            > c$=$a + $b; ?????????????

            This is one of the most basic things in Perl:

            Two ways (at least) -

            1. $c = "$a$b";

            2. $c = $a . $b;

            Greg
            bmwclub.org.uk
            uksites4all.co.uk
          • Charles K. Clarkson
            Katus Waldemar [waldemar.katus@bzwbk.pl] ... Have a look at the sprintf function. my( $day, $month, $year ) = ( localtime $date_time )[3,4,5]; my $date_all =
            Message 5 of 7 , Dec 5, 2002
            • 0 Attachment
              Katus Waldemar [waldemar.katus@...]
              :
              : #!/usr/bin/perl
              :
              : my $date_time=time;
              : $date_time=$date_time-86400;
              : ($day, $month, $year)=(localtime($date_time))[3,4,5];
              :
              : $day_ok="0$day";
              : $year_o=substr($year, 1, 2);
              : $year_ok="20$year_o";
              : $month_ok=$month+1;
              :
              : $date_all="$day_ok$month_ok$year_ok\n" if $day <= 9;
              : $date_all="$day$month_ok$year_ok\n" if $day > 9;

              Have a look at the sprintf function.

              my( $day, $month, $year ) = ( localtime $date_time )[3,4,5];

              my $date_all = sprintf "%02d%02d%04d", $day, $month + 1, $year + 1900;

              HTH,

              Charles K. Clarkson
              --
              Head Bottle Washer,
              Clarkson Energy Homes, Inc.
              Mobile Home Specialists
              254 968-8328
            • Mystik Gotan
              I would suggest concentating: $c = $a . $b ; $c .= $a$b ; Both should work. ... Bob Erinkveld (Webmaster Insane Hosts) www.insane-hosts.net MSN:
              Message 6 of 7 , Dec 5, 2002
              • 0 Attachment
                I would suggest concentating:

                $c = "$a . $b";
                $c .= "$a$b";

                Both should work.



                --------------
                Bob Erinkveld (Webmaster Insane Hosts)
                www.insane-hosts.net
                MSN: gotan2k3@...





                >From: Katus Waldemar <waldemar.katus@...>
                >Reply-To: perl-beginner@yahoogroups.com
                >To: "'perl-beginner@yahoogroups.com'" <perl-beginner@yahoogroups.com>
                >Subject: RE: [PBML] DATE problem
                >Date: Thu, 5 Dec 2002 09:03:05 +0100
                >
                >I wrote in this way:
                >
                >#!/usr/bin/perl
                >
                >my $date_time=time;
                >$date_time=$date_time-86400;
                >($day, $month, $year)=(localtime($date_time))[3,4,5];
                >
                >$day_ok="0$day";
                >$year_o=substr($year, 1, 2);
                >$year_ok="20$year_o";
                >$month_ok=$month+1;
                >
                >$date_all="$day_ok$month_ok$year_ok\n" if $day <= 9;
                >$date_all="$day$month_ok$year_ok\n" if $day > 9;
                >
                >A have additional question: How I can join two or more strings, for example
                >$a="mather";
                >$b="father";
                >c$=$a + $b; ?????????????
                >
                >
                >
                >
                >
                >-----Original Message-----
                >From: Charles K. Clarkson [mailto:cclarkson@...]
                >Sent: Wednesday, December 04, 2002 7:15 PM
                >To: perl-beginner@yahoogroups.com
                >Subject: RE: [PBML] DATE problem
                >
                >
                >Katus Waldemar [waldemar.katus@...] scribbled:
                >
                >: [I] have a problem with date. For example:
                >:
                >: my $DATE_ = system("date +%y%m%d");
                >: print DATE_
                >:
                >: and I return: 021204
                >:
                >: It's OK , but I want return 021203.
                >: That is $DATE_ - 1, but it's not simple.....
                >: (for example: today is 020301;
                >: 020301 - 1 = 020228 or 020229:))
                >:
                >: Please write me how I can writ in PERL (or shell)
                >: function, which return date - 1.
                >
                > In perl, dates are kept in a serial integer.
                >Its value is equal to the number seconds since a
                >particular date called the Epoch. All perl
                >functions use the same Epoch. Perl has a function
                >which translates the serial number to a format
                >human beings find easier to understand.
                > Some modules which come standard with perl
                >have additional formatting abilities. If you need
                >a lot of date functions you'll want to look at
                >Date::Calc.
                >
                > If you look in 'perlfunc' (part of the
                >standard perl documentation), you'll find the
                >'localtime' function. It is used to go from the
                >serial number mentioned above to the various
                >human readable items. The POSIX function strftime
                >takes the same arguments to display date and time
                >in user defined formats.
                >
                > To convert a date to a serial takes a little
                >more work. The date format differs by locale,
                >application, and other factors. 120103 may be the
                >twelfth day of January 2003, it may be the first
                >day of December 2003, or even the third day of
                >January 1912.
                >
                > Because of this, date conversion using a
                >single function can return erroneous results. The
                >best method is for you, the programmer, to handle
                >the conversion. One of the easiest functions to
                >convert dates from human readable to serial is in
                >a module included with the standard perl
                >distribution called Time::Local. It provides a
                >function named 'timelocal'.
                >
                > For this example we'll use 'timelocal' to
                >convert 021204 to a serial, we'll subtract one
                >day (24 hours X 60 minutes X 60 seconds), and
                >we'll use strftime from the POSIX module to
                >convert the date back to the "%y%m%d" format.
                >
                > We can use 'substr' or a regex to convert
                >the date. I like regexes better, but 'substr'
                >is faster. As you can see we must make some
                >assumptions about the four digit year.
                >
                >my $date = '021204';
                >my ( $year, $month, $day ) = $date =~ /(\d\d)(\d\d)(\d\d)/;
                >
                ># adjust year to 4 digits
                >$year += $year > 49 ? 1900 : 2000;
                >
                ># adjust month to 0 .. 11 range
                >--$month;
                >
                >
                > Convert it all to a serial using timelocal().
                >
                >use Time::Local 'timelocal';
                >
                ># timelocal expects ( $sec, $min, $hours, $mday, $mon, $year );
                >
                >my $date_serial = timelocal( 0, 0, 0, $day, $month, $year );
                >
                >
                > Subtract one day.
                >
                >my $one_day = 24 * 60 * 60;
                >
                >$date_serial -= $one_day;
                >
                >
                > Convert it back to the format we want using
                >'strftime' from the POSIX module.
                >
                >use POSIX 'strftime';
                >
                >print strftime( '%y%m%d', localtime $date_serial );
                >
                >
                > Putting it all together (with some added
                >subroutines):
                >
                >use POSIX 'strftime';
                >use Time::Local 'timelocal';
                >
                >my $date = '021204';
                >print "$date\n";
                >
                >my $one_day = 24 * 60 * 60;
                >my $serial = date_to_serial( $date );
                >print serial_to_date( $serial - $one_day, '%y%m%d' );
                >
                >
                >sub date_to_serial {
                > local $_ = shift;
                > my ( $year, $month, $day ) = /(\d\d)(\d\d)(\d\d)/;
                >
                > # adjust year to 4 digits
                > $year += $year > 49 ? 1900 : 2000;
                >
                > # adjust month to 0 .. 11 range
                > --$month;
                >
                > # timelocal expects ( $sec, $min, $hours, $mday, $mon, $year );
                > return timelocal( 0, 0, 0, $day, $month, $year );
                >}
                >
                >sub serial_to_date {
                > my( $serial, $format ) = @_;
                > return strftime( $format, localtime $serial );
                >}
                >
                >
                >HTH,
                >
                >Charles K. Clarkson
                >--
                >Head Bottle Washer,
                >Clarkson Energy Homes, Inc.
                >Mobile Home Specialists
                >254 968-8328
                >
                >
                >
                >
                >
                >
                >
                >
                >
                >Yahoo! Groups Sponsor
                >
                >ADVERTISEMENT
                >
                ><http://rd.yahoo.com/M=237459.2675695.4055211.2225243/D=egroupweb/S=17050069
                >51:HM/A=1267611/R=0/*http://ad.doubleclick.net/jump/N2524.Yahoo/B1071650;sz=
                >300x250;ord=1039025721016962?>
                >
                ><http://us.adserver.yahoo.com/l?M=237459.2675695.4055211.2225243/D=egroupmai
                >l/S=:HM/A=1267611/rand=371293329>
                >
                >Unsubscribing info is here:
                >http://help.yahoo.com/help/us/groups/groups-32.html
                ><http://help.yahoo.com/help/us/groups/groups-32.html>
                >
                >Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
                ><http://docs.yahoo.com/info/terms/> .
                >
                >
                >
                >
                >Unsubscribing info is here:
                >http://help.yahoo.com/help/us/groups/groups-32.html
                >
                >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/


                _________________________________________________________________
                Ontvang je Hotmail & Messenger berichten op je mobiele telefoon met Hotmail
                SMS http://www.msn.nl/jumppage/
              Your message has been successfully submitted and would be delivered to recipients shortly.