Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

date format  in perl

Posted on 2011-09-30
7
Medium Priority
?
283 Views
Last Modified: 2012-05-12
Hi,
I am searching for data based on date. so what format do I need to use for the below example

Wed Sep  7 02:33:14 EDT 2011

$date= ???
0
Comment
Question by:new_perl_user
7 Comments
 
LVL 7

Accepted Solution

by:
whosbetterthanme earned 2000 total points
ID: 36893861
my $date = scalar localtime(time());

# this will be close.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36893968
Nice one!
I've been using Perl for 15 years and it's the first time I run into this wicked trick.
Thank you for sharing whosbetterthanme.
You are starting to deserve your nickname... :-)
0
 
LVL 85

Expert Comment

by:ozo
ID: 36894832
since assigning to a scalar implies scalar context, you don't need the explicit scalar function,
and since localtime defaults to using time, you don't need the explicit time function either.

to get exactly the format you requested, you can
use POSIX qw(strftime);  
my $date = strftime "%a %b %e %H:%M:%S %Z %Y", localtime;  
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:parparov
ID: 36894836
You can do without time() since it's default argument of localtime.
However the asker seems to be looking for a date that is not now.

Then my $date = scalar localtime($timestamp_you_have); will do the trick.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 36894869
Slightly shorter than ozo's version

use POSIX qw(strftime);  
my $date = strftime "%a %b %e %T %Z %Y", localtime;  

Open in new window



jermonee, are you being serious?  In 15 years, you've never used
$date=localtime();

Open in new window

0
 
LVL 10

Expert Comment

by:jeromee
ID: 36894913
Nope.
Always used strftime or sprintf.
Like Larry Wall would say: http://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it
0
 
LVL 7

Expert Comment

by:whosbetterthanme
ID: 36894921
Well, even though new perl user hasn't responded yet, I suppose it's a matter of old school and using what works!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question