CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Working with Dates



Feb 25, 2014, 12:13 PM

Post #1 of 5 (3070 views)
Working with Dates Can't Post

Though Iím beginning to feel more in the intermediate range, this question is in an area I havenít touched yet, working with dates.

I managed to open my first Access database via ODBC and read in a table record that included a date. The date came back as 2011-07-09 00:00:00 which is the SQL yyyy-mm-dd hh:mm:ss format.

When I ask Yahoo finance for historical stock prices from Feb 7 2013 to Mar 25 2013, the months are counted from zero so the URL would be:

and the dates come back as Feb 21, 2013 with an alpha month and a comma.

What is the standard Perl format?

What is the best way to convert everything into that format?

How do I do date math? If I want to know the date that was 3 months before March 10, 2012, it should be December 10, 2011 where January and December have 31 days and February has 29 because it is a leap year. Same is true if I want the date 1 year ago, you subtract 366, not 365, and also consider that 14 days before crosses that leap year boundary! Is there a built in function that takes all of that into consideration?

Veteran / Moderator

Feb 25, 2014, 2:53 PM

Post #2 of 5 (3058 views)
Re: [PapaGeek] Working with Dates [In reply to] Can't Post

Don't ever try to calculate dates yourself, it is an real mess. Perhaps one of the most complicated problems of applied IT (as opposed to theoretical CS), together with rounding issues.

There are a number of Perl functions and modules to deal with dates and times. First some general pointers.

Starting point: the time internal function of Perl returns the number of seconds elapsed since "the Epoch", usually Jan. 1, 1970.

This number of seconds can be transformed into a date and time using the gmtime and localtime Perl internal functions. The Time::gmtime and Time::localtime add some further funtionnalities.

Then, you also have the Time::timegm and Time::Local which do conversions the other way around.

The POSIX module has also a number of possibilities, including the POSIX::strptime functions.

Now, for the precise problems that you discuss, you really have to look at the Date::Calc module, which offers more than 65 date calculation functions abiding to all conventions of the Gregorian Calendar (the calendar used in all Western countries and a large part of the rest of the world), including leap years and so on.

Some other modules that might have some interest for you:

There are many other modules related to times and dates on the CPAN, but I would think that those I mentioned are the main ones, especially Date::Calc for the precise problem you are mentionning.

You'll find many other date and time modules, including Julian calendar (the predecessor of the Gregorian one, associated with Julius Caesar), Maya calendar, various religious calendars (Christian, Jewish, Muslim, etc.), and so on, and, just as an anectotical point, a friend of mine is even maintaining a French Revolutionary Calendar module, a calendar that was used in France only for about only ten years (about 1793 to 1802) -- but the months of Brumaire (see e.g. and Thermidor, just to name two of them, might ring a bell to you if you have any interest in this historical period.


Mar 1, 2014, 2:28 PM

Post #3 of 5 (3004 views)
Re: [PapaGeek] Working with Dates [In reply to] Can't Post

I just wanted to mention another module to add to those that Laurent has already provided.

Time::Piece lets you convert to and from date formats with ease via the strptime and strftime methods.

use Time::Piece; 

my $input_date = 'Feb 21, 2013';

my $date_object = Time::Piece->strptime( $input_date, "%b %d, %Y" );

my $output_date = $date_object->strftime( "%c" );

print "input date => $input_date\n";
print "output date => $output_date\n";

input date  => Feb 21, 2013 
output date => Thu Feb 21 00:00:00 2013



Mar 1, 2014, 4:03 PM

Post #4 of 5 (2995 views)
Re: [Zhris] Working with Dates [In reply to] Can't Post

Thanks for the reply, but this brings up another question. As I read the web, itís about 50 50 on who says use Time and who says use DateTime. Which is the more standard package in Perl?

Iíve started using DateTime, but I can easily change if Time is more the standard. My code is designed to look at the best percentage returns on various investment over a few standard time frames. Using DateTime this is what I have so far:

 sub updatePrices 
my $dt = shift;
my $dt1w = $dt->clone->subtract(days => 7);
my $dt1m = $dt->clone->subtract(months => 1);
my $dt3m = $dt->clone->subtract(months => 3);
my $dt6m = $dt->clone->subtract(months => 6);
my $dt1y = $dt->clone->subtract(years => 1);
my $dt3y = $dt->clone->subtract(years => 3);
my $dt5y = $dt->clone->subtract(years => 5);
. . .

Veteran / Moderator

Mar 2, 2014, 3:57 AM

Post #5 of 5 (2979 views)
Re: [PapaGeek] Working with Dates [In reply to] Can't Post

Don't worry, just use the module that is fit to your needs.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives