ado
ado

Reputation: 1471

Time in Perl with the following format: 20130527

I'm looking for a not-so-long way to produce the following in Perl:

A portion of code that gets the current time (month, day and year). For example, if we are in year 2013, May 27th, the output should be 20130527

I now that if we use "localtime", like

$date_str = localtime;

The output is in the format of: Wed Jul 18 07:24:05 2001 Is there other special variable that can be useful?

Upvotes: 0

Views: 646

Answers (4)

Suvasish Sarker
Suvasish Sarker

Reputation: 435

You can do it like below..

use POSIX;
my $today = POSIX::strftime('%Y%m%d', localtime);
print "$today\n";

Output: 20130527

Upvotes: 1

David W.
David W.

Reputation: 107040

In Perl, time is stored is as the numbers of seconds since The Epoch. The Epoch is usually January 1, 1970.

This makes time good for sorting, and calculating future days. If you need to know what the day will be 30 days from now, you can add 2,592,000 to that (the number of seconds in 30 days).

Perl has a standard function called time that returns the number of seconds since The Epoch. You can then use localtime or gmtime to translate this into an array of time elements (Day, year, hours, etc.).

Perl, for some strange reason, never had an internal function to take an array of time elements and convert it back into the number of seconds since The Epoch. However, Perl always had a standard module that did this with the functions timelocal and timegm. In the Pre-5.0 Perl days, you would require "timelocal.pl";. In Perl 5.0, you now use Time::Local;.

The converting time back and forth between arrays of time elements and the seconds since The Epoch can be a bit of a pain with the localtime, gmtime, timelocal, and timegm functions, and in Perl 5.10, two new modules Time::Piece and Time::Seconds were added. These two modules allow you to format the time using the strptime and strftime functions built into Time::Piece.

If you have Perl 5.10.0 or greater, you have it easy:

use Time::Piece;

my $time = localtime; #$time is a Time::Piece object

# $time_string is in YYYYMMDD format
my $time_string = sprintf "%04d%02d%02d%", $time->year, $time->month, $time->day;

# Another way to do the above using the strftime function:
my $time_string = $time->strftime("%Y%m%d");

However your program should be using the number of seconds since The Epoch as the internal time inside your program. You should store the time in this format. You should calculate the time in this format, and you should pass the time in this format for all functions.

This is because other Perl modules expect the time in this format, and more importantly, other Perl developers who will be looking at and maintaining your code expect the time in this format.

#Converting the time from YYYYMMDD to seconds since the epoch
my $time = Time::Piece->strptime($YYYYDDMM_time, '%Y%m%d');
my $time_since_epoch = $time->epoch;

#Converting time since the epoch to YYYYMMDD
my $time = localtime($time_since_epoch);
my $YYYYMMDD_time = $time->strftime('%Y%m%d');

Upvotes: 5

Tom Williams
Tom Williams

Reputation: 378

Or, use localtime in list context:

($d,$m,$y) = (localtime)[3,4,5];
say sprintf("%4d%02d%02d", $y+1900, $m+1, $d);

Upvotes: 4

friedo
friedo

Reputation: 66957

You can use strftime from the standard POSIX module:

use POSIX 'strftime';
say strftime "%Y%m%d", localtime;

Output:

20130526

Upvotes: 7

Related Questions