Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Which Module for Dates?

by jrefior (Novice)
on Aug 25, 2014 at 03:53 UTC ( #1098484=perlquestion: print w/replies, xml ) Need Help??
jrefior has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am writing a Finance::StockAccount set of modules to analyze the past transactions in a personal stock account. I hope to distribute this on CPAN. Dates are incidental to this: I need to be able to import transactions with dates and compare dates to sort chronologically. I want to make it easy to use my modules, including when importing dates or setting a date using a string.

There are plenty of CPAN modules related to dates, including parsing, comparing, and formatting for display. So there is no reason for me to reinvent the wheel here. So far, I am thinking of using DateTime, the choice appears to be simple and defensible, as it seems so standard, is so robust, and there is so much built around it. DateTime::Format::CLDR and DateTime::Format::Flexible look like relatively standard and easy ways to import dates from strings. So then some of my classes will have a "date" property like date => $dt. Others will have methods that sort transactions by date using the DateTime->compare method.

That's all fine for me. But is that fine for the people who might use my module? Is there a standard or recommended way to handle dates in CPAN modules? Is a dependency on DateTime okay? DateTime has a lot of dependencies itself. I see Time::Moment is lighter weight, should I worry about performance? Any other thoughts/suggestions?

Thanks very much for your help.


Replies are listed 'Best First'.
Re: Which Module for Dates?
by ikegami (Pope) on Aug 25, 2014 at 04:09 UTC

    Since you're just sorting them, a module is just going to slow you down a lot. How are the dates stored?

    Seconds since epoch? Just sort numerically.

    Using one time zone? Your goal is to rearrange into a lexically sortable format (e.g. %Y-%m-%d). This might require modules depending on the specifics on the format. Once you have that, you can sort lexically.

    Using different time zones? Your goal is to convert them into a single time zone (e.g. UTC) or seconds since epoch. This might require modules depending on the specifics on the format. Once you have that, see above.

Re: Which Module for Dates?
by CountZero (Bishop) on Aug 25, 2014 at 06:29 UTC
    DateTime is a heavyweight module, but unless I hit some memory or speed problems, I always use it. Once you have your datetimee field parsed into a DateTime-object, you can do so much with it. In the beginning it may seem overkill, but as your application grows and grows, it is likely you will hit some limits with other datetime representations and then you will have to go back and refactor everything.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Which Module for Dates?
by tobyink (Abbot) on Aug 25, 2014 at 14:54 UTC

    DateTime is probably the slowest date/time handling modules on CPAN, but it also provides incredibly correct date/time handling. It can convert between timezones flawlessly; it handles those crazy leap-seconds we have from time to time; with the right plugins it can parse from and serialize into pretty much any format you need.

    Apart from its speed, there are plenty of other things to dislike about it too: the objects are mutable which can allow for spooky action at a distance unless you remember to call clone all over the place, and it feels weird that from_epoch dies when called with a single parameter. But if you need correct date/time handling with support for timezones, it's the only game in town, so get with it! (That said, there is a DateTime subclass on CPAN called DateTime::Moonpig which cleans up some of DateTime's API warts.)

    If all your times are in a single time zone, and you don't need to be pedantically accurate with your calculations (taking into account leap seconds, daylight savings, etc) then a lighter weight module like Time::Moment might be suitable.

Re: Which Module for Dates?
by trippledubs (Hermit) on Aug 27, 2014 at 14:36 UTC
    Time::Piece, now part of Perl core. Unless you need times past 2038 and you use 32 bit perl. In that case use something else.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1098484]
Approved by kevbot
Front-paged by kevbot
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-03-19 18:54 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.