How to print formatted (!) the current timestamp?

T

Thomas Blabb

I want to print the current time(stamp) in formatted style.
The following does not work:

print STDOUT "Now=%Y%m%d-%H%M\n", localtime(time);

What is wrong?

How else can I print out a formatted timestamp?

Tom
 
O

Owen

I want to print the current time(stamp) in formatted style.
The following does not work:

print STDOUT "Now=%Y%m%d-%H%M\n", localtime(time);

What is wrong?

How else can I print out a formatted timestamp?

Tom

Try

use POSIX qw(strftime);
print strftime "%Y%m%d-%H%M\n", localtime;


Owen
 
G

Gunnar Hjalmarsson

Keith said:
perl date formatting has nothing to do with UNIX date; %Y for example
is the hash named Y, and bears no relation at all to your call to
localtime. Read perldoc -f sprintf for valid formats (%Y is
certainly not one).

Wouldn't it have been better to point the OP to POSIX::strftime() and
drop that rant?
 
B

Ben Morrow

If you want to use a print-equivalent statement to do your formatting,
you need to use printf. Also, perl date formatting has nothing to do
with UNIX date; %Y for example is the hash named Y, and bears no
relation at all to your call to localtime. Read perldoc -f sprintf for
valid formats (%Y is certainly not one).

....however it is a valid format for strftime, which I suspect is what
the OP wanted.

use POSIX qw/strftime/;

print STDOUT strftime "Now=%Y%m%d-%H%M\n", localtime;

Ben
 
U

Uri Guttman

KK> If you want to use a print-equivalent statement to do your formatting,
KK> you need to use printf. Also, perl date formatting has nothing to do
KK> with UNIX date; %Y for example is the hash named Y, and bears no
KK> relation at all to your call to localtime. Read perldoc -f sprintf for
KK> valid formats (%Y is certainly not one).

actually %Y is just the string '%Y' there. hashes don't interpolate in
strings. and yes, the OP is confusing time formats (date and strftime)
with printf formats.

uri
 
G

Gunnar Hjalmarsson

Keith said:
Was I ranting? I thought I was just being complete. Thanks for the
pointer to POSIX::strftime; I seldom use the POSIX:: modules.

The attempt to use the string '%Y%m%d-%H%M' in a date formating context
told us that the OP was groping for strftime(). Considering that Owen
had figured it out several hours before you posted, I found it not very
fruitful to lead the OP away from that date formating method.
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top