Time::Period

davorg on 2002-12-29T19:57:42

I've been dabbling with overload.pm over the last week or so. I've never really looked at it in much depth before, but I'm scheduled to give a talk on to london.pm next month and that's always a good excuse to learn something new.

I was looking for a good (but slightly unusual) example for the constant overloading feature of overload.pm when I came up with the concept of Time::Period.

Basically, it allows you to do date calculations in a pretty easy to understand way. You can simply do something like this:

use Time::Period ':constants';

$one_month = '1m';
$now = time;
$then = $now + $one_month;
print scalar localtime $then;
Of course you can make that simpler with code like:
$wake = time + '1y + 1d'; # a year and a day
print "The princess will wake up at ",
  scalar localtime $wake;

It's on its way to CPAN, but in the meantime you can get it from http://dave.org.uk/modules/.

Oh, and if Trelane is reading... that's the topic for my talk at the next tech meet.


Wheel, reinvented

Matts on 2002-12-29T23:07:28

Check out Time::Piece, which ships with Time::Seconds. Also check out Date::ICal, which ships with Date::ICal::Duration.

All achieve the same thing.

Re:Wheel, reinvented

vek on 2002-12-30T01:57:32

I seem to recall that both Date::Calc and Date::Manip might also do the same thing. Could be wrong though as it's been a while since I've looked...

Re:Wheel, reinvented

davorg on 2002-12-30T06:46:19

Oh, I know about Time::Seconds (I'm a big advocate of using Time::Piece). This isn't my attempt to replace it, just something that I was playing around with that I thought I'd share.

There are differences between the two module tho'. Time::Seconds uses exported symbolic constansts where Time::Period uses objects. Also adding one year or one month to a date using Time::Seconds will add a fixed number of seconds whereas Time::Period will really add one year or one month.

Didn't know about Date::ICal::Duration, I'll check it out.

Re:Wheel, reinvented

Matts on 2002-12-30T09:40:39

Yep, this is a rather fundamental flaw in Time::Seconds (though there will always be issues with adding real durations too - like what to do adding a month at March 31st). I have been thinking seriously about re-writing Time::Piece entirely in terms of Date::ICal to try and fix this flaw.

1m

bart on 2002-12-29T23:33:31

Is that one month, or one minute?

Re:1m

davorg on 2002-12-30T06:49:47

Yeah, it's really unhelpful that "month" and "minute" both beign with "m" :(

To get round that I arbitrarily decided that lower case letters represent units of time that are a day or bigger and uppercase letters represent untis of time that are smaller than a day. Another way of putting it is that lower case letters represent dates and upper case letters represent times.

So '1m' is one month. '1M' is one minute.

Thanks, minor niggles

2shortplanks on 2002-12-29T23:42:14

Cool, now I don't have to chase you about what you're talking about, only how long.

Niggles on the module:

  • print Time::Period->new("1y") - Time::Period->new("1S") returns "1y, 1S" which is obviously wrong.
  • print Time::Duration->new("24H")*2 does not DWIM, it complains about *, but I see that's on your TODO
  • I can't understand what the codes doing very well, you've not written a single comment ;-)

Re:Thanks, minor niggles

davorg on 2002-12-30T06:54:59

Cool, now I don't have to chase you about what you're talking about, only how long.

Can I have half an hour?

  • print Time::Period->new("1y") - Time::Period->new("1S") returns "1y, 1S" which is obviously wrong.
  • print Time::Duration->new("24H")*2 does not DWIM, it complains about *, but I see that's on your TODO

Addition is the only operation that is currently documented. Therefore it's the only one you should expect to work as documented :)

  • I can't understand what the codes doing very well, you've not written a single comment ;-)

If I commented the code, then no-one would have any reason to come to my talk :)