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

Re: Getting the date in the round-about way

Expand Messages
  • Damien Carbery
    ... You say you want the date in variables. Here are two ideas: put date/month/year in 3 variables like at the end of your post or in one variable. I m only
    Message 1 of 5 , Aug 1, 2003
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "gaijin_punch"
      <gaijin_punch@y...> wrote:
      > Been a while. I have been using my Perl, just haven't been doing too
      > much out of my realm, so I've been doing little other than lurking here.
      >
      > Anyways, for the questions:
      >
      > 1: All of the date/time examples (at least that I noticed) in the Perl
      > Cookbook deal with using printf to print out the time/date. What I
      > need, is the date in numbers (today would be 08012003) and I need
      > to use them as variables.
      >
      > What I'm doing is probably the really long way, but basically running
      > the system "date" command, then using
      > regexes to get the day/month/year. On Linux, the month prints out as
      > "Jan, Feb, etc.", so I have a %months hash where each month has its
      > corresponding number value. Finally, I pad a 0 to the day and month
      > if the value is less than 10.
      >
      > So if someone took a peak at my script would they laugh hyseterically?
      > Finally, is it possible to store different values in different
      > variables with one regex? Before, I was able to get the day, month,
      > and year w/ one line:
      >
      > #when output from system('date') is in $_
      > ( $day, $month, $year ) = ( split / /, $_)[2,1,5];
      >
      > When the date changed (IE - the day or month went above or below 10)
      > this didn't work. With the regexes, I'm running 3 different ones.
      > Wondering if a one-liner would work, since they're all taken from the
      > same thing.
      >
      > $day =~ s/.+?\s+.+?\s+([0-9]{1,2})\s.*/$1/i;
      > $month =~ s/.+?\s([a-z]{3})\s.*/$1/i;
      > $year =~ s/.+?\s([0-9]{4})/$1/i;
      >
      > Thanks

      You say you want the date in variables. Here are two ideas: put
      date/month/year in 3 variables like at the end of your post or in one
      variable.
      I'm only using internal perl functions so this will work on all
      operating systems and does not invoke the shell so will be quicker
      than when you use the external 'date' command.

      For the first option (3 variables) you can modify the date and month
      to prefix with a '0' when you really need it. Don't convert the
      numbers to strings until absolutely necessary, even though perl (I
      believe) will convert between numbers and strings silently and as
      necessary.

      #!/usr/bin/perl -w

      use strict;

      # Two ideas: Put data in 3 variables or in one variable.
      my ( $date, $month, $year ) = ( localtime() )[ 3, 4, 5 ];
      # Convert to normal ranges (1-12 for month, 4 digit year).
      $month++;
      $year += 1900;
      print "localtime: $date, $month, $year\n";

      # Put in a list so month and year can be modified before
      # becoming a string.
      my @lt = localtime();
      my $DateString = sprintf( "%02d%02d%04d", $lt[3], $lt[4]+1, $lt[5]+1900 );
      print "DateString = $DateString\n";
    • Jeff 'japhy' Pinyan
      ... Well, you get your data from the localtime() function, whether you re printing it or not. ... Bad idea. Getting the current date and time should not be
      Message 2 of 5 , Aug 2, 2003
      • 0 Attachment
        On Aug 1, gaijin_punch said:

        >1: All of the date/time examples (at least that I noticed) in the Perl
        >Cookbook deal with using printf to print out the time/date. What I
        >need, is the date in numbers (today would be 08012003) and I need to
        >use them as variables.

        Well, you get your data from the localtime() function, whether you're
        printing it or not.

        >What I'm doing is probably the really long way, but basically running the
        >system "date" command, then using regexes to get the day/month/year. On
        >Linux, the month prints out as "Jan, Feb, etc.", so I have a %months hash
        >where each month has its corresponding number value. Finally, I pad a 0
        >to the day and month if the value is less than 10.

        Bad idea. Getting the current date and time should not be difficult. You
        should not have to pull teeth.

        And I HIGHLY doubt you are "running the system 'data' command" and getting
        its output in $_. system() doesn't RETURN output.

        >So if someone took a peak at my script would they laugh hyseterically?
        >Finally, is it possible to store different values in different variables
        >with one regex? Before, I was able to get the day, month, and year w/
        >one line:

        >( $day, $month, $year ) = ( split / /, $_)[2,1,5];

        That splits on a SINGLE space. Use the magical argument of ' ' to split,
        and you'll see different results:

        ($d, $m, $y) = (split ' ', $_)[2,1,5];
        ($d, $m, $y) = (split ' ')[2,1,5]; # same thing
        ($d, $m, $y) = (split)[2,1,5]; # same thing

        But I digress. Don't run a system command to get the date and/or time.
        Use the functions Perl gave you.

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

        For more, read:

        perldoc -f localtime

        --
        Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
        RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
        <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
        [ I'm looking for programming work. If you like my work, let me know. ]
      • gaijin_punch
        Hey guys: Thanks for the input. There was many references to localtime in the book, but I guess I overlooked a few things. The way I did it was mainly b/c I
        Message 3 of 5 , Aug 4, 2003
        • 0 Attachment
          Hey guys:

          Thanks for the input. There was many references to localtime in the
          book, but I guess I overlooked a few things. The way I did it was
          mainly b/c I used to do something similar w/ bash, and I knew it'd
          work, so didnt' bother w/ the other ways. :)

          However, I've duly noted for future reference.

          Ciao

          > And I HIGHLY doubt you are "running the system 'data' command" and
          getting
          > its output in $_. system() doesn't RETURN output.

          open ( DATE, "date|") or die "Can't run date command $!";
          while ( <DATE> ) {
          chomp;
          print "$_\n";
          }

          yields:
          Tue Aug 5 09:11:15 JST 2003
          Don't know what else to tell ya. :)
        • Jeff 'japhy' Pinyan
          ... You made it sound like you were doing: $date = system date ; which does NOT return the information into $date that you expect. I see you re using open(),
          Message 4 of 5 , Aug 4, 2003
          • 0 Attachment
            On Aug 5, gaijin_punch said:

            >Thanks for the input. There was many references to localtime in the
            >book, but I guess I overlooked a few things. The way I did it was
            >mainly b/c I used to do something similar w/ bash, and I knew it'd
            >work, so didnt' bother w/ the other ways. :)

            >> And I HIGHLY doubt you are "running the system 'data' command" and
            >getting
            >> its output in $_. system() doesn't RETURN output.
            >
            >open ( DATE, "date|") or die "Can't run date command $!";
            >while ( <DATE> ) {
            > chomp;
            > print "$_\n";
            >}
            >
            >yields:
            >Tue Aug 5 09:11:15 JST 2003
            >Don't know what else to tell ya. :)

            You made it sound like you were doing:

            $date = system 'date';

            which does NOT return the information into $date that you expect. I see
            you're using open(), which will return the stuff you want, but it's still
            inferior and not portable; localtime() is the way to go.

            --
            Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
            RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
            <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
            [ I'm looking for programming work. If you like my work, let me know. ]
          Your message has been successfully submitted and would be delivered to recipients shortly.