localtime(time())

M

Martin Trautmann

Hi all,

what's wrong about my interpretation of localtime?

perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
my $date =($year+1900)."-".
sprintf("%02d", $mon) ."-".
sprintf("%02d", $mday);
print $date;'

.... returns 2007-04-22 instead of 2007-05-22.

Do I need to increment the month count since Janury is counted as zero?

Thanks,
Martin
 
M

Martien verbruggen

Hi all,

what's wrong about my interpretation of localtime?

perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
my $date =($year+1900)."-".
sprintf("%02d", $mon) ."-".
sprintf("%02d", $mday);
print $date;'

... returns 2007-04-22 instead of 2007-05-22.

Do I need to increment the month count since Janury is counted as zero?

The documentation for localtime states:

$ perldoc -f localtime

[SNIP]
$mday is the day of the
month, and $mon is the month itself, in the range 0..11 with 0
indicating January and 11 indicating December.
[SNIP]

I believe the answer to your question is in that bit.

Did you know about perldoc? If not, perldoc -f reads the perlfunc
documentation, and displays that part of that documentation file that
pertains to the function you give it as an argument. You can, of course,
simply read perlfuncdirectly. It's a lot faster than posting to Usenet.

It's interesting that the variable list you use is identical to that
from the documentation.

Martien
 
T

Tony Curtis

Martin said:
Hi all,

what's wrong about my interpretation of localtime?

perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
my $date =($year+1900)."-".
sprintf("%02d", $mon) ."-".
sprintf("%02d", $mday);
print $date;'

... returns 2007-04-22 instead of 2007-05-22.

Do I need to increment the month count since Janury is counted as zero?

Well, you've answered your own question...but simpler is:

use POSIX qw( strftime );

print strftime '%Y-%m-%d', localtime();

hth
t
 
M

Martin Trautmann

Did you know about perldoc? If not, perldoc -f reads the perlfunc
documentation, and displays that part of that documentation file that
pertains to the function you give it as an argument. You can, of course,
simply read perlfuncdirectly. It's a lot faster than posting to Usenet.

I ftp'ed the file to a cgi place where I found the bug later on - thus I
did not have access to perldoc.
It's interesting that the variable list you use is identical to that
from the documentation.

It was taken from the documentation before...
.... where I just had expected a different meaning. $years was ok
(counting from 1900), while I was surprised by $mon, which was $mon--
instead - good for array processing, but not what it looked like.

Yes, shame on me for not reading the docu...

Fortunately, I got extra feedback, showing the even better solution,

Thanks,
Martin
 
U

Uri Guttman

MT> I ftp'ed the file to a cgi place where I found the bug later on - thus I
MT> did not have access to perldoc.

ever heard of perldoc.perl.org? or installing perl on your desktop so
you can have perldoc locally? the excuse "i didn't have access to
perldoc" is weaker than your dog eating your computer.

uri
 
D

Dr.Ruud

Martin Trautmann schreef:
my $date =($year+1900)."-".
sprintf("%02d", $mon) ."-".
sprintf("%02d", $mday);
print $date;'

my $date = sprintf( q[%s-%02d-%02d],
$y + 1900,
$m + 1,
$d,
);
print $date;
 
B

Brad Baxter

Martin Trautmann schreef:
my $date =($year+1900)."-".
sprintf("%02d", $mon) ."-".
sprintf("%02d", $mday);
print $date;'

my $date = sprintf( q[%s-%02d-%02d],
$y + 1900,
$m + 1,
$d,
);
print $date;

I've used a variant of the following for years.

#!/usr/local/bin/perl -l
use warnings;
use strict;

sub now {
for( $_[0] ) {
defined and /(.*?)yyyy(.*?)mm(.*?)dd(.*)/i and
return sprintf( "$1%04d$2%02d$3%02d$4",
sub { ( $_[5] + 1900, $_[4] + 1, $_[3] )
}->( localtime ) );
}
scalar localtime;
}

print now;
print now 'yyyy-mm-dd';
print now 'YYYY/MM/DD';
print now 'Year: yyyy, Month: mm, Day: dd'

__END__
Wed May 23 10:06:26 2007
2007-05-23
2007/05/23
Year: 2007, Month: 05, Day: 23
 
U

Uri Guttman

BB> I've used a variant of the following for years.

BB> sub now {
BB> for( $_[0] ) {
BB> defined and /(.*?)yyyy(.*?)mm(.*?)dd(.*)/i and
BB> return sprintf( "$1%04d$2%02d$3%02d$4",
BB> sub { ( $_[5] + 1900, $_[4] + 1, $_[3] )
BB> }->( localtime ) );
BB> }
BB> scalar localtime;
BB> }

bletcherific!!

what if you want a different format? days of week? use POSIX::strftime
and you get all you do there and much more.

uri
 
G

Gunnar Hjalmarsson

Tony said:
Well, you've answered your own question...but simpler is:

use POSIX qw( strftime );

print strftime '%Y-%m-%d', localtime();

I'm not sure that using POSIX is simpler, since at least I would need to
look up the applicable conversion specifiers.

I would do:

my ($d, $m, $y) = ( localtime )[3..5];
printf '%d-%02d-%02d', $y+1900 , $m+1, $d;
 
T

Tony Curtis

Gunnar said:
Tony said:
Well, you've answered your own question...but simpler is:

use POSIX qw( strftime );

print strftime '%Y-%m-%d', localtime();

I'm not sure that using POSIX is simpler, since at least I would need to
look up the applicable conversion specifiers.

I would do:

my ($d, $m, $y) = ( localtime )[3..5];
printf '%d-%02d-%02d', $y+1900 , $m+1, $d;

"Simpler" of course depends on the metric(s) you use to measure complexity,
but I would definitely argue that strftime() is certainly much easier to
read,
especially *for other people*, and thus produces "simpler" code.

It helps you solve a problem instead of coding a problem.

hth
t
 
G

Gunnar Hjalmarsson

Tony said:
Gunnar said:
Tony said:
Well, you've answered your own question...but simpler is:

use POSIX qw( strftime );

print strftime '%Y-%m-%d', localtime();

I'm not sure that using POSIX is simpler, since at least I would need
to look up the applicable conversion specifiers.

I would do:

my ($d, $m, $y) = ( localtime )[3..5];
printf '%d-%02d-%02d', $y+1900 , $m+1, $d;

"Simpler" of course depends on the metric(s) you use to measure complexity,
but I would definitely argue that strftime() is certainly much easier to
read,
especially *for other people*, and thus produces "simpler" code.

Working with localtime() and (s)printf() is simple enough even for
beginner level Perl programmers. Whoelse would you be coding for? ;-)
 
P

Peter J. Holzer

%Y, %m and %d are mnemonic enough that you probably only have to look
%them up once (although you might confuse %m and %M). Some others are
very obscure though.

I would do:

my ($d, $m, $y) = ( localtime )[3..5];
printf '%d-%02d-%02d', $y+1900 , $m+1, $d;

So you need to remember the order of fields in the list returned by
localtime (quick, which field is day of the week?), that you have to add
1 to the month and 1900 to the year. Plus it's two lines instead of one.

Working with localtime() and (s)printf() is simple enough even for
beginner level Perl programmers. Whoelse would you be coding for? ;-)

Judging from the number of programs which thought that the year after
1999 would be 19100 localtime() was obviously not simple enough for many
programmers.

hp
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top