Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

help on getting date

by Anonymous Monk
on Jun 02, 2003 at 16:14 UTC ( #262412=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone , Is there a way to get the system date using perl in the following format mm-dd-yyyy , and then is there a way to compare the dates to see which is greater or lesser then a given date which is also in mm-dd-yyyy format Thank you for your help

Comment on help on getting date
Re: help on getting date
by hardburn (Abbot) on Jun 02, 2003 at 16:17 UTC

    You can get dates using localtime.

    Comparing dates is trickier. Dig through the Date:: and DateTime:: modules. The DateTime:: namespace is for a new project trying to unify Perl's mess of Date and Time modules into a single, consistant API, so look there first.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: help on getting date
by halley (Prior) on Jun 02, 2003 at 16:20 UTC

    Personally, if sorting or comparing human-readable dates is important, I'd recommend YYYY-MM-DD format. It naturally sorts, and reduces ordering confusion about 3 June or 6 January. Bigger units to the left.

    As mentioned, localtime() will return the individual components for you. Just remember to add 1 to the month and 1900 to the year.

    If your data is already IN the poor MM-DD-YYYY format, then you'll need the help of one of the Date::* modules to convert or compute back to a useful scalar unit like epoch time, Julian dates, or something. Keep dates numeric internally, and provide formatted equivalents only for user presentation.

    --
    [ e d @ h a l l e y . c c ]

      First off I want to emphasize that you're right on all the really important points. The nit I will pick is that "need" is kind of a strong word, given that there's a pretty simple way to convert MM-DD-YYYY to YYYY-MM-DD format, even if it's not widely capable like a Date::* module:

      # assumes you have 0-padded dates, but if you don't you can change # the \d\d's to \d\d?'s , and do an appropriate sprintf. my @ymd = map { my ($m, $d, $y) = $_ =~ /(\d\d)-(\d\d)-(\d{4})/; "$y- +$m-$d"; } @dmy;

      HTH

      If not P, what? Q maybe?
      "Sidney Morgenbesser"

        why bother with the temporary variables, especially inside map? Heck, you'd also screen out a small subset of invalid data.
        print  map {$_ =~ /(\d\d)-(\d\d)-(\d{4})/ && "$3-$1-$2"; } ("01-02-1999");
        of course, using this kind of code, you'd need to make sure that your dataset's valid -- you won't get Date::Manip's heavy duty "Am I valid datetime or not" routines.

Re: help on getting date
by Mr. Muskrat (Abbot) on Jun 02, 2003 at 16:20 UTC

    Take a look at Date::Calc for working with dates. The documentation for it has a recipe for comparing dates.

    As far as formatting to mm-dd-yy, you can use printf or sprintf to do that depending on what you want to do with it once it is formatted.

•Re: help on getting date
by merlyn (Sage) on Jun 02, 2003 at 16:29 UTC

      The comment is totally inappropriate. I had considered making it myself.

      Cheers,
      Ovid

      New address of my CGI Course.
      Silence is Evil (feel free to copy and distribute widely - note copyright text)

        That makes three of us.. :) And since there are obviously quite a few who thought the same I say we leave it here so the rest of us can move on. (I'm sure merlyn will be glad to stand-in for us in taking the heat for actually making the comment.. ;-) SCNR..)

        Makeshifts last the longest.

Re: help on getting date
by fglock (Vicar) on Jun 02, 2003 at 17:00 UTC
    my $dt = DateTime->now; print "system date ", $dt->strftime( "%m-%d-%Y" ); my $compare = $dt <=> $other_date;

    Note: $other_date must be a DateTime object

Re: help on getting date
by fglock (Vicar) on Jun 02, 2003 at 17:26 UTC

    You can use DateTime::Format::Strptime

    use DateTime::Format::Strptime; my $pat = DateTime::Format::Strptime->new( pattern => '%m-%d-%Y' ); my $dt1 = $pat->parse_datetime( "06-07-2003" ); my $dt2 = $pat->parse_datetime( "07-06-2003" ); print $dt1 < $dt2 ? "smaller" : "equal or bigger";
Re: help on getting date
by crouchingpenguin (Priest) on Jun 02, 2003 at 18:23 UTC

    1. Is there a way to get the system date using perl in the following format mm-dd-yyyy?

    Yes, using POSIX::strftime:

    print POSIX::strftime("%m-%d-%Y",localtime()),"\n";

    2. is there a way to compare the dates to see which is greater or lesser then a given date which is in mm-dd-yyyy format?

    Yes, using POSIX::mktime() and optionally POSIX::difftime():

    my $start_date_string = '05-03-2003'; my @date_parts = split('-',$start_date_string); # build time my $then_time = POSIX::mktime( 0,0,0, $date_parts[1], ($date_parts[0] -1), ($date_parts[2] - 1900) ); ### or could be mktime() from another split string here my $now_time = time(); #could also use POSIX::difftime: #"the time difference (in seconds) between two times #(as returned by 'time()')" if( $then_time < $now_time ){ print POSIX::ctime($then_time) . " was before " . POSIX::ctime($now_time) . "\n"; }else{ print POSIX::ctime($then_time) . " was after " . POSIX::ctime($now_time) . "\n"; }

    All that fun and POSIX comes with core perl.

    Update:Oops, thanks rob_au for catching my typo.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
      Excellent post. Just one point which I would pick up on, is that values for the month, weekday and day of the year passed to POSIX::mktime begin at zero. As such, the determination of $then_time in your post should alternatively read:

      my $then_time = POSIX::mktime ( 0, # Seconds 0, # Minutes 0, # Hours $date_parts[1], # Day (of month) $date_parts[0] - 1, # Month $date_parts[2] - 1900 # Year );

      This behaviour is documented on the POSIX module man page.

       

      perl -le 'print+unpack"N",pack"B32","00000000000000000000001001100100"'

Re: help on getting date
by data64 (Chaplain) on Jun 03, 2003 at 03:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://262412]
Approved by hardburn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-09-20 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls