Categories

Posts in this category

Sun, 11 Apr 2010

A Perl 6 Date Module


Permanent link

The Perl 6 specification talks, among other things, about built-in date/time classes. After much discussions back and forth it is now (and is likely staying that way) a subset of the excellent Perl 5 module DateTime.

The discussions on the perl6-language mailing list showed that Dave Rolsky, author of DateTime, wants us to learn from the shortcomings of DateTime, in particular having a date-only class looks like a good idea to him.

That encouraged me to implement such a module. There's also prior art for that on CPAN: Date::Simple.

It's really simple, as the name says: You can construct a Date::Simple object either from year, month, day or from a string in the form YYYY-MM-DD, subtracting two dates yields an integer, adding or subtracting an integer to a date gives another date, you can ++ or -- date objects to get the next/previous day, and you can query date objects for year, month, day and day of week.

With some good literature at hand (and the Date::Simple test suite for inspiration) that's rather straight forward to implement, so that's what I did. I called it Date, and the code is on github.

It works to the extent possible by current Rakudo: all the object oriented features work, overloading operators doesn't quite work yet. Still the logic for the operators is already there, and once the bug in Rakudo is fixed, the Date module will be nearly fully implemented.

I enjoyed doing some solid work in this area of continued bikeshedding, and I'll wait and see if people want such a class in Perl 6 or not.

[/perl-6] Permanent link

Comments / Trackbacks:

Trackback URL: /blog-en/perl-6/date-module.trackback

Chris Dolan wrote

Good enough?
This concerns me. Java learned the hard way that having "good enough" built-in date support is permanently bad because there will be a whole class of bugs that plagues the language forever. The Joda-Time project (that hopefully bring sane date math to Java 1.7) is an example of immutable dates/times/periods/durations done right.

One thing about Perl5's implementation is that the builtin time support was so limited that nobody sane actually tried to use it for real date/time work.

But then, you've got code and I just have words. :-)

Moritz wrote

Good enough?
What gives you the impression that Date is "good enough" and not "good"?

It is meant to be "good", in the sense that for the type of calculations it does it is much easier to use than the full DateTime, which is still available if Date is not enough.

Klamsi wrote


DateTime is a really good module. The big Problem for me to use it in a webserver context ist, that it is REALLY slow.

To query the database server with "SELECT $date - INTERVAL 1 DAY" for example is even much more performant than to calc it per DateTime.

Klamsi wrote


For interest I testet

SELECT BENCHMARK(1000000, "2010-01-01 00:00:00" - INTERVAL cast(rand()*50 as unsigned) DAY);

1 row in set (1.08 sec)

In Perl using DateTime I think it never would finish.

wrote


Write a comment

The comments on this blog post have been disabled; the comment form below will not work.

 
Name:
URL: [http://www.example.com/] (optional)
Title: (optional)
Comments:
Save my Name and URL/Email for next time