Solved

date format  in perl

Posted on 2011-09-30
7
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Accepted Solution

by:
whosbetterthanme earned 500 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 84

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

635 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