Problems dealing with dates in Windows

G

GGelz

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!
 
J

John Bokma

GGelz said:
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
 
P

Paul Lalli

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
 
A

Ayaz Ahmed Khan

"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`.
 
M

Mirco Wahab

Purl said:
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
 
M

Mirco Wahab

Purl said:
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
 
D

Dr.Ruud

Ayaz Ahmed Khan schreef:
"GGelz":

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
 
A

Ayaz Ahmed Khan

"Dr.Ruud" typed:
Ayaz Ahmed Khan schreef:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top