Discussion:
How to print formatted (!) the current timestamp?
(too old to reply)
Thomas Blabb
2008-05-04 09:09:46 UTC
Permalink
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
Owen
2008-05-04 10:03:51 UTC
Permalink
Post by Thomas Blabb
I want to print the current time(stamp) in formatted style.
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
Keith Keller
2008-05-04 18:06:10 UTC
Permalink
Post by Thomas Blabb
I want to print the current time(stamp) in formatted style.
print STDOUT "Now=%Y%m%d-%H%M\n", localtime(time);
Did you read perldoc -f localtime?

If you are happy with localtime's formatting, simply print it as a
scalar:

print scalar localtime();

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).

--keith
--
kkeller-***@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
Gunnar Hjalmarsson
2008-05-04 18:22:35 UTC
Permalink
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?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Keith Keller
2008-05-04 20:54:35 UTC
Permalink
Post by Gunnar Hjalmarsson
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?
Was I ranting? I thought I was just being complete. Thanks for the
pointer to POSIX::strftime; I seldom use the POSIX:: modules.

--keith
--
kkeller-***@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
Gunnar Hjalmarsson
2008-05-04 22:00:58 UTC
Permalink
Post by Keith Keller
Post by Gunnar Hjalmarsson
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?
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.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Ben Morrow
2008-05-04 18:29:02 UTC
Permalink
Post by Keith Keller
Post by Thomas Blabb
I want to print the current time(stamp) in formatted style.
print STDOUT "Now=%Y%m%d-%H%M\n", localtime(time);
<snip>
Post by Keith Keller
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
--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[***@morrow.me.uk]
Uri Guttman
2008-05-04 18:38:52 UTC
Permalink
Post by Thomas Blabb
print STDOUT "Now=%Y%m%d-%H%M\n", localtime(time);
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
--
Uri Guttman ------ ***@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Continue reading on narkive:
Loading...