⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 duration.pm

📁 1. 记录每个帖子的访问人情况
💻 PM
📖 第 1 页 / 共 2 页
字号:
sub _multiply_overload{    my $self = shift;    my $new = $self->clone;    return $new->multiply(@_);}sub _compare_overload{    die "DateTime::Duration does not overload comparison.  See the documentation on the compare() method for details.";}1;__END__=head1 NAMEDateTime::Duration - Duration objects for date math=head1 SYNOPSIS  use DateTime::Duration;  $d = DateTime::Duration->new( years   => 3,                                months  => 5,                                weeks   => 1,                                days    => 1,                                hours   => 6,                                minutes => 15,                                seconds => 45,                                 nanoseconds => 12000 );  # Convert to different units  $d->in_units('days', 'hours', 'seconds');  # Human-readable accessors, always positive  $d->years;  $d->months;  $d->weeks;  $d->days;  $d->hours;  $d->minutes;  $d->seconds;  $d->nanoseconds;  if ( $d->is_positive ) { ... }  if ( $d->is_zero )     { ... }  if ( $d->is_negative ) { ... }  # The important parts for date math  $d->delta_months  $d->delta_days  $d->delta_minutes  $d->delta_seconds  $d->delta_nanoseconds  my %deltas = $d->deltas  $d->is_wrap_mode  $d->is_limit_mode  $d->is_preserve_mode  # Multiple all deltas by -1  my $opposite = $d->inverse;  my $bigger  = $dur1 + $dur2;  my $smaller = $dur1 - $dur2; # the result could be negative  my $bigger  = $dur1 * 3;  my $base_dt = DateTime->new( year => 2000 );  my @sorted =      sort { DateTime::Duration->compare( $a, $b, $base_dt ) } @durations;=head1 DESCRIPTIONThis is a simple class for representing duration objects.  Theseobjects are used whenever you do date math with DateTime.pm.See the L<How Date Math is Done|DateTime/"How Date Math is Done">section of the DateTime.pm documentation for more details.  The shortcourse:  One cannot in general convert between seconds, minutes, days,and months, so this class will never do so.  Instead, create theduration with the desired units to begin with, for example by callingthe appropriate subtraction/delta method on a C<DateTime.pm> object.=head1 METHODSLike C<DateTime> itself, C<DateTime::Duration> returns the object frommutator methods in order to make method chaining possible.C<DateTime::Duration> has the following methods:=over 4=item * new( ... )This method takes the parameters "years", "months", "weeks", "days","hours", "minutes", "seconds", "nanoseconds", and "end_of_month".  Allof these except "end_of_month" are numbers.  If any of the numbers arenegative, the entire duration is negative.Internally, years as just treated as 12 months.  Similarly, weeks aretreated as 7 days, and hours are converted to minutes.  Seconds andnanoseconds are both treated separately.The "end_of_month" parameter must be either "wrap", "limit", or"preserve".  These specify how changes across the end of a month arehandled.In "wrap" mode, adding months or years that result in days beyond theend of the new month will roll over into the following month.  Forinstance, adding one year to Feb 29 will result in Mar 1.If you specify "end_of_month" mode as "limit", the end of the monthis never crossed.  Thus, adding one year to Feb 29, 2000 will resultin Feb 28, 2001.  However, adding three more years will result in Feb28, 2004, not Feb 29.If you specify "end_of_month" mode as "preserve", the same calculationis done as for "limit" except that if the original date is at the endof the month the new date will also be.  For instance, adding onemonth 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 howmost people "intuitively" expect datetime math to work.=item * cloneReturns a new object with the same properties as the object on whichthis method was called.=item * in_units( ... )Returns the length of the duration in the units (any of those that canbe passed to L<new>) given as arguments.  All lengths are integral,but may be negative.  Smaller units are computed from what remainsafter taking away the larger units given, so for example:  my $dur = DateTime::Duration->new( years => 1, months => 15 );  $dur->in_units( 'years' );            # 2  $dur->in_units( 'months' );           # 27  $dur->in_units( 'years', 'months' );  # (2, 3)Note that the numbers returned by this method may not match the valuesgiven to the constructor.In list context, in_units returns the lengths in the order of the unitsgiven.  In scalar context, it returns the length in the first unit (butstill computes in terms of all given units).If you need more flexibility in presenting information aboutdurations, please take a look a C<DateTime::Format::Duration>.=item * years, months, weeks, days, hours, minutes, seconds, nanosecondsThese methods return numbers indicating how many of the given unit theobject represents, after having taken away larger units.  Thesenumbers are always positive.  So days is equivalent to C<abs(in_units( 'days', 'weeks' ) )>.=item * delta_months, delta_days, delta_minutes, delta_seconds, delta_nanosecondsThese methods provide the same information as those above, but in away suitable for doing date math.  The numbers returned may bepositive or negative.  So delta_days is equivalent toC<in_units('days')>.=item * deltasReturns a hash with the keys "months", "days", "minutes", "seconds",and "nanoseconds", containing all the delta information for theobject.=item * is_positive, is_zero, is_negativeIndicates whether or not the duration is positive, zero, or negative.If the duration contains both positive and negative units, then itwill return false for B<all> of these methods.=item * is_wrap_mode, is_limit_mode, is_preserve_modeIndicates what mode is used for end of month wrapping.=item * inverseReturns a new object with the same deltas as the current object, butmultiple by -1.  The end of month mode for the new object will be thedefault end of month mode, which depends on whether the new durationis positive or negative.=item * add_duration( $duration_object ), subtract_duration( $duration_object )Adds or subtracts one duration from another.=item * add( ... ), subtract( ... )Syntactic sugar for addition and subtraction.  The parameters given tothese methods are used to create a new object, which is then passed toC<add_duration()> or C<subtract_duration()>, as appropriate.=item * multiply( $number )Multiplies each unit in the by the specified number.=item * DateTime::Duration->compare( $duration1, $duration2, $base_datetime )This is a class method that can be used to compare or sort durations.Comparison is done by adding each duration to the specifiedC<DateTime.pm> object and comparing the resulting datetimes.  This isnecessary because without a base, many durations are not comparable.For example, 1 month may otr may not be longer than 29 days, dependingon what datetime it is added to.If no base datetime is given, then the result of C<< DateTime->now >>is used instead.  Using this default will give non-repeatable resultsif used to compare two duration objects containing different units.It will also give non-repeatable results if the durations containmultiple types of units, such as months and days.However, if you know that both objects only contain the same units,and just a single I<type>, then the results of the comparison will berepeatable.=back=head2 OverloadingThis class overload addition, subtraction, and mutiplication.Comparison is B<not> overloaded.  If you attempt to compare durationsusing C<< <=> >> or C<cmp>, then an exception will be thrown!  Use theC<compare()> class method instead.=head1 SUPPORTSupport for this module is provided via the datetime@perl.org emaillist.  See http://lists.perl.org/ for more details.=head1 AUTHORDave Rolsky <autarch@urth.org>However, please see the CREDITS file for more details on who I reallystole all the code from.=head1 COPYRIGHTCopyright (c) 2003 David Rolsky.  All rights reserved.  This programis free software; you can redistribute it and/or modify it under thesame terms as Perl itself.Portions of the code in this distribution are derived from otherworks.  Please see the CREDITS file for more details.The full text of the license can be found in the LICENSE file includedwith this module.=head1 SEE ALSOdatetime@perl.org mailing listhttp://datetime.perl.org/=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -