Problems dealing with dates in Windows

Discussion in 'Perl Misc' started by GGelz, Feb 2, 2007.

  1. GGelz

    GGelz Guest

    Hi all,

    I'm having quite a bit of trouble simply getting a date using perl on
    a winXP system.

    First of all, if I do something like:

    $date = `date /T`;

    I'll get an error saying:

    date: invalid date '/T'

    on the console.

    Now, I was able to work around this by just doing a:

    $date = localtime(time);

    printing $date gives

    "Fri Feb 2 12:23:11 2007"

    now, if I split it using

    ($day, $month, $monthday, $time, $year) = split(/\s/, $date);

    and then print out all the values, this is what I get:

    $day = Fri
    $month = Feb
    $monthday =
    $time = 2
    $year = 12:23:11

    I have no idea what is happening between "Feb" and "2" but it is
    driving me crazy.

    There has to be an easier way to handle dates in Windows using perl.
    Will someone clue me in, please?

    Thank you!
    GGelz, Feb 2, 2007
    #1
    1. Advertising

  2. GGelz

    John Bokma Guest

    "GGelz" <> wrote:

    > Hi all,
    >
    > I'm having quite a bit of trouble simply getting a date using perl on
    > a winXP system.
    >
    > First of all, if I do something like:
    >
    > $date = `date /T`;
    >
    > I'll get an error saying:
    >
    > date: invalid date '/T'
    >
    > on the console.


    Weird:

    perl -e "my $date = `date /T`; print $date"
    Fri 02/02/2007

    > Now, I was able to work around this by just doing a:
    >
    > $date = localtime(time);
    >
    > printing $date gives
    >
    > "Fri Feb 2 12:23:11 2007"
    >
    > now, if I split it using
    >
    > ($day, $month, $monthday, $time, $year) = split(/\s/, $date);


    It really helps to read the documentation of new stuff you're about to
    use.

    > and then print out all the values, this is what I get:
    >
    > $day = Fri
    > $month = Feb
    > $monthday =
    > $time = 2
    > $year = 12:23:11
    >
    > I have no idea what is happening between "Feb" and "2" but it is
    > driving me crazy.
    >
    > There has to be an easier way to handle dates in Windows using perl.
    > Will someone clue me in, please?


    perldoc -f split (also, count the number of spaces between Feb and 2)
    Moreover, I strongly suggest to study perldoc -f localtime

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Feb 2, 2007
    #2
    1. Advertising

  3. GGelz

    Paul Lalli Guest

    On Feb 2, 12:40 pm, "GGelz" <> wrote:
    > I'm having quite a bit of trouble simply getting a date using perl on
    > a winXP system.
    >
    > First of all, if I do something like:
    >
    > $date = `date /T`;
    >
    > I'll get an error saying:
    >
    > date: invalid date '/T'
    >
    > on the console.


    On what OS version? On my WinXP, I get:
    C:\>perl -le"print `date /T`"
    02/02/2007


    > Now, I was able to work around this by just doing a:
    >
    > $date = localtime(time);


    Which is, of course, what you should have been doing in the first
    place.

    > printing $date gives
    >
    > "Fri Feb 2 12:23:11 2007"
    >
    > now, if I split it using
    >
    > ($day, $month, $monthday, $time, $year) = split(/\s/, $date);
    >
    > and then print out all the values, this is what I get:
    >
    > $day = Fri
    > $month = Feb
    > $monthday =
    > $time = 2
    > $year = 12:23:11
    >
    > I have no idea what is happening between "Feb" and "2"


    There are two spaces between Feb and 2, but you're only telling Perl
    to split on a single space. Therefore, there is one empty field
    between the two delimiters. Just split on one-or-more spaces instead:
    spilt /\s+/, $date;

    > There has to be an easier way to handle dates in Windows using perl.
    > Will someone clue me in, please?


    "handle dates" is a nonsensical term. The easier and more correct way
    to do whatever it is you want to do depends on whatever it is you want
    to do. What values are you hoping to obtain? How do you want to use
    them? The easiest way to capture numerical values for each component
    of the date/time is to use localtime in a list context:

    my ($sec, $min, $hour, $day, $month, $year) = localtime;

    But make sure you read `perldoc -f localtime` so you know what $month
    and $year actually are.

    Paul Lalli
    Paul Lalli, Feb 2, 2007
    #3
  4. "GGelz" typed:
    > $date = localtime(time);
    > printing $date gives
    > "Fri Feb 2 12:23:11 2007"
    > now, if I split it using
    > ($day, $month, $monthday, $time, $year) = split(/\s/, $date);


    You need '\s+' in there to cause split() to split on any number of
    consecuteive whitespaces.

    > There has to be an easier way to handle dates in Windows using perl.
    > Will someone clue me in, please?


    Take a look at `perldoc -f localtime`.

    --
    Ayaz Ahmed Khan

    A witty saying proves nothing, but saying something pointless gets
    people's attention.
    Ayaz Ahmed Khan, Feb 2, 2007
    #4
  5. GGelz

    John Bokma Guest

    John Bokma, Feb 2, 2007
    #5
  6. GGelz

    Mirco Wahab Guest

    Purl Gurl wrote:
    > GGelz wrote:
    >> First of all, if I do something like:
    >> $date = `date /T`;
    >> I'll get an error saying:
    >> date: invalid date '/T'
    >> on the console.

    >
    > Your comments present problems.
    >
    > These results you report are consistent for older
    > DOS 6 / 7 typical for Windows 98 and Windows ME.


    You are on the wrong track. I can reproduce his
    error message "under WinXP" without problems here:

    Variant 1 win32/putty.exe remote)

    [here]> perl -e '$date=`date /T`; print qq{$date $] on $^O\n}'

    date: invalid date `/T'
    5.008008 on linux

    Variant 2 - cygwin/bash local, in a DOS-Box)

    [there]> perl -e '$date=`date /T`; print qq{$date $] on $^O\n}'

    date: invalid date `/T
    5.008007 on cygwin


    You see, I can reproduce the message "under XP"
    in many cases ;-) XP sux! pearl sux!

    Regards

    Mirco
    Mirco Wahab, Feb 2, 2007
    #6
  7. GGelz

    Mirco Wahab Guest

    Purl Gurl wrote:
    > Mirco Wahab wrote:
    >> You are on the wrong track. I can reproduce his
    >> error message "under WinXP" without problems here:

    >
    >> 5.008008 on linux
    >> 5.008007 on cygwin

    >
    > No, you are on the wrong track.
    > Linux is not Windows XP. Cygwin is not Windows XP.
    > Neither are a DOS 6 / 7 command line.
    >
    > Others present precisely the same results as mine,
    > using Windows XP. Why have you singled me for
    > response but not others reporting precisely
    > the same results I report?


    Other would have probably laughed at it
    and then added "yea, he's *using* Windows XP
    somehow at the time, but surely not for Perl ..."

    But you wouldn't. So you got singled out
    for that this time ;-)

    Regards

    Mirco
    Mirco Wahab, Feb 2, 2007
    #7
  8. GGelz

    John Bokma Guest

    Glenn Jackman <> wrote:

    > At 2007-02-02 01:42PM, "John Bokma" wrote:
    >> "Paul Lalli" <> wrote:
    >>
    >> > On what OS version? On my WinXP, I get:
    >> > C:\>perl -le"print `date /T`"
    >> > 02/02/2007

    >>
    >> No Fri?

    >
    > Depends on your regional settings. I use:
    > C:\>date /t
    > 2007-02-02


    Yes, I was guessing that :-D.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Feb 2, 2007
    #8
  9. GGelz

    Dr.Ruud Guest

    Ayaz Ahmed Khan schreef:
    > "GGelz":


    >> $date = localtime(time);
    >> printing $date gives
    >> "Fri Feb 2 12:23:11 2007"
    >> now, if I split it using
    >> ($day, $month, $monthday, $time, $year) = split(/\s/, $date);

    >
    > You need '\s+' in there to cause split() to split on any number of
    > consecuteive whitespaces.


    Often the special ' ' does even more what one expects, because it skips
    leading whitespace too, see `perldoc -f split`.

    $ perl -wle '@x = split /\s+/, q{ a b c }; print scalar @x'
    4

    $ perl -wle '@x = split q{ }, q{ a b c }; print scalar @x'
    3

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Feb 3, 2007
    #9
  10. "Dr.Ruud" typed:
    > Ayaz Ahmed Khan schreef:
    >> You need '\s+' in there to cause split() to split on any number of
    >> consecuteive whitespaces.

    > Often the special ' ' does even more what one expects, because it skips
    > leading whitespace too, see `perldoc -f split`.
    >
    > $ perl -wle '@x = split /\s+/, q{ a b c }; print scalar @x'
    > 4
    > $ perl -wle '@x = split q{ }, q{ a b c }; print scalar @x'
    > 3


    Didn't know that. Thanks.

    --
    Ayaz Ahmed Khan

    A witty saying proves nothing, but saying something pointless gets
    people's attention.
    Ayaz Ahmed Khan, Feb 3, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    840
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    175
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    194
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    176
    Eric I.
    Jun 8, 2008
  5. Stuart Clarke

    Dealing with dates & timezones

    Stuart Clarke, Aug 20, 2010, in forum: Ruby
    Replies:
    4
    Views:
    115
    Stuart Clarke
    Aug 20, 2010
Loading...

Share This Page