Solved

date format  in perl

Posted on 2011-09-30
7
269 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search in text file in column and compare 4 47
rename outfile before writing 2 72
Using Perl to parse rows 7 95
Migrate OTRS to newest version. 1 335
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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 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