Click here to get back home

How to print formatted (!) the current timestamp?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How to print formatted (!) the current timestamp? Thomas Blabb 05-04-2008
Posted by Gunnar Hjalmarsson on May 4, 2008, 6:00 pm
Please log in for more thread options
Keith Keller wrote:
>> Keith Keller wrote:
>>> 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

Posted by Ben Morrow on May 4, 2008, 2:29 pm
Please log in for more thread options

> > 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);
>
<snip>
> 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".
[ben@morrow.me.uk]

Posted by Uri Guttman on May 4, 2008, 2:38 pm
Please log in for more thread options


>> 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 ------ uri@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 ---------

Similar ThreadsPosted
print all current variables May 2, 2005, 8:23 pm
Returning to beginning of current line with print()? October 18, 2006, 11:51 pm
Formatted printing June 14, 2005, 11:12 am
Reading a ctime formatted date April 24, 2007, 1:46 pm
strings with formatted characters in %ARGV June 25, 2007, 12:33 pm
How to force formatted date (month) language ? July 7, 2006, 6:02 pm
reading in a nested formatted data structure with quirks June 28, 2006, 6:15 pm
(",) Print This! Press [Ctrl][P] Keys To Print... >> http://www.phpbbserver.com/phpbb/viewtopic.php?t=2&mforum=anysubjectchat < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < May 30, 2005, 2:05 pm
Preserve Timestamp January 21, 2005, 1:34 pm
FAQ 5.23: How do I get a file's timestamp in perl? December 19, 2004, 12:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap