This is a simple class for representing duration objects. These objects are used whenever you do date math with DateTimeX::Lite.pm.

See the How Date Math is Done section of the DateTimeX::Lite.pm documentation for more details. The short course: One cannot in general convert between seconds, minutes, days, and months, so this class will never do so. Instead, create the duration with the desired units to begin with, for example by calling the appropriate subtraction/delta method on a DateTimeX::Lite.pm object.

Like DateTimeX::Lite itself, DateTimeX::Lite::Duration returns the object from mutator methods in order to make method chaining possible.

DateTimeX::Lite::Duration has the following methods:

new( ... )

This method takes the parameters "years", "months", "weeks", "days", "hours", "minutes", "seconds", "nanoseconds", and "end_of_month". All of these except "end_of_month" are numbers. If any of the numbers are negative, the entire duration is negative.

All of the numbers must be integers.

Internally, years as just treated as 12 months. Similarly, weeks are treated as 7 days, and hours are converted to minutes. Seconds and nanoseconds are both treated separately.

The "end_of_month" parameter must be either "wrap", "limit", or "preserve". This parameter specifies how date math that crosses the end of a month is handled.

In "wrap" mode, adding months or years that result in days beyond the end of the new month will roll over into the following month. For instance, adding one year to Feb 29 will result in Mar 1.

If you specify "end_of_month" mode as "limit", the end of the month is never crossed. Thus, adding one year to Feb 29, 2000 will result in Feb 28, 2001. If you were to then add three more years this will result in Feb 28, 2004.

If you specify "end_of_month" mode as "preserve", the same calculation is done as for "limit" except that if the original date is at the end of the month the new date will also be. For instance, adding one month to Feb 29, 2000 will result in Mar 31, 2000.

For positive durations, the "end_of_month" parameter defaults to wrap. For negative durations, the default is "limit". This should match how most people "intuitively" expect datetime math to work.

clone

Returns a new object with the same properties as the object on which this method was called.

in_units( ... )

Returns the length of the duration in the units (any of those that can be passed to new) given as arguments. All lengths are integral, but may be negative. Smaller units are computed from what remains after taking away the larger units given, so for example:

These methods provide the information DateTimeX::Lite.pm needs for doing date math. The numbers returned may be positive or negative.

deltas

Returns a hash with the keys "months", "days", "minutes", "seconds", and "nanoseconds", containing all the delta information for the object.

is_positive, is_zero, is_negative

Indicates whether or not the duration is positive, zero, or negative.

If the duration contains both positive and negative units, then it will return false for all of these methods.

is_wrap_mode, is_limit_mode, is_preserve_mode

Indicates what mode is used for end of month wrapping.

end_of_month_mode

Returns one of "wrap", "limit", or "preserve".

calendar_duration

Returns a new object with the same calendar delta (months and days only) and end of month mode as the current object.

clock_duration

Returns a new object with the same clock deltas (minutes, seconds, and nanoseconds) and end of month mode as the current object.

inverse

Returns a new object with the same deltas as the current object, but multiple by -1. The end of month mode for the new object will be the default end of month mode, which depends on whether the new duration is positive or negative.

This is a class method that can be used to compare or sort durations. Comparison is done by adding each duration to the specified DateTimeX::Lite.pm object and comparing the resulting datetimes. This is necessary because without a base, many durations are not comparable. For example, 1 month may otr may not be longer than 29 days, depending on what datetime it is added to.

If no base datetime is given, then the result of DateTimeX::Lite->now is used instead. Using this default will give non-repeatable results if used to compare two duration objects containing different units. It will also give non-repeatable results if the durations contain multiple types of units, such as months and days.

However, if you know that both objects only consist of one type of unit (months or days or hours, etc.), and each duration contains the same type of unit, then the results of the comparison will be repeatable.

years, months, weeks, days, hours, minutes, seconds, nanoseconds

These methods return numbers indicating how many of the given unit the object represents, after having done a conversion to any larger units. For example, days are first converted to weeks, and then the remainder is returned. These numbers are always positive.