Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,171 @@
package Time::Moment::Adjusters;
use strict;
use warnings;
use Carp qw[];
BEGIN {
our $VERSION = '0.44';
our @EXPORT_OK = qw[ NextDayOfWeek
NextOrSameDayOfWeek
PreviousDayOfWeek
PreviousOrSameDayOfWeek
FirstDayOfWeekInMonth
LastDayOfWeekInMonth
NthDayOfWeekInMonth
WesternEasterSunday
OrthodoxEasterSunday
NearestMinuteInterval ];
our %EXPORT_TAGS = (
all => [ @EXPORT_OK ],
);
require Exporter;
*import = \&Exporter::import;
}
sub NextDayOfWeek {
@_ == 1 or Carp::croak(q<Usage: NextDayOfWeek(day)>);
my ($day) = @_;
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
return sub {
my ($tm) = @_;
return $tm->plus_days(($day - $tm->day_of_week + 6) % 7 + 1);
};
}
sub NextOrSameDayOfWeek {
@_ == 1 or Carp::croak(q<Usage: NextOrSameDayOfWeek(day)>);
my ($day) = @_;
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
return sub {
my ($tm) = @_;
return $tm->plus_days(($day - $tm->day_of_week) % 7);
};
}
sub PreviousDayOfWeek {
@_ == 1 or Carp::croak(q<Usage: PreviousDayOfWeek(day)>);
my ($day) = @_;
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
return sub {
my ($tm) = @_;
return $tm->minus_days(($tm->day_of_week - $day + 6) % 7 + 1);
};
}
sub PreviousOrSameDayOfWeek {
@_ == 1 or Carp::croak(q<Usage: PreviousOrSameDayOfWeek(day)>);
my ($day) = @_;
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
return sub {
my ($tm) = @_;
return $tm->minus_days(($tm->day_of_week - $day) % 7);
};
}
sub FirstDayOfWeekInMonth {
@_ == 1 or Carp::croak(q<Usage: FirstDayOfWeekInMonth(day)>);
my ($day) = @_;
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
return sub {
my ($tm) = @_;
$tm = $tm->with_day_of_month(1);
return $tm->plus_days(($day - $tm->day_of_week) % 7);
};
}
sub LastDayOfWeekInMonth {
@_ == 1 or Carp::croak(q<Usage: LastDayOfWeekInMonth(day)>);
my ($day) = @_;
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
return sub {
my ($tm) = @_;
$tm = $tm->at_last_day_of_month;
return $tm->minus_days(($tm->day_of_week - $day) % 7);
};
}
sub NthDayOfWeekInMonth {
@_ == 2 or Carp::croak(q<Usage: NthDayOfWeekInMonth(ordinal, day)>);
my ($ordinal, $day) = @_;
($ordinal >= -4 && $ordinal <= 4 && $ordinal != 0)
or Carp::croak(q<Parameter 'ordinal' is out of the range [-4, -1] u [1, 4]>);
($day >= 1 && $day <= 7)
or Carp::croak(q<Parameter 'day' is out of the range [1, 7]>);
if ($ordinal > 0) {
my $days = 7 * --$ordinal;
return sub {
my ($tm) = @_;
$tm = $tm->with_day_of_month(1);
return $tm->plus_days($days + ($day - $tm->day_of_week) % 7);
};
}
else {
my $days = 7 * ++$ordinal;
return sub {
my ($tm) = @_;
$tm = $tm->at_last_day_of_month;
return $tm->plus_days($days - ($tm->day_of_week - $day) % 7);
};
}
}
sub WesternEasterSunday {
@_ == 0 or Carp::croak(q<Usage: WesternEasterSunday()>);
return sub {
my ($tm) = @_;
return $tm->with_rdn(Time::Moment::Internal::western_easter_sunday($tm->year));
};
}
sub OrthodoxEasterSunday {
@_ == 0 or Carp::croak(q<Usage: OrthodoxEasterSunday()>);
return sub {
my ($tm) = @_;
return $tm->with_rdn(Time::Moment::Internal::orthodox_easter_sunday($tm->year));
};
}
sub NearestMinuteInterval {
@_ == 1 or Carp::croak(q<Usage: NearestMinuteInterval(interval)>);
my ($interval) = @_;
($interval >= 1 && $interval <= 1440)
or Carp::croak(q<Parameter 'interval' is out of the range [1, 1440]>);
my $msec = $interval * 60 * 1000;
my $mid = int(($msec + 1) / 2);
return sub {
my ($tm) = @_;
my $msod = $msec * int(($tm->millisecond_of_day + $mid) / $msec);
return $tm->with_millisecond_of_day($msod);
};
}
1;

View File

@@ -0,0 +1,143 @@
=encoding utf-8
=head1 NAME
Time::Moment::Adjusters - Adjusters for Time::Moment
=head1 SYNOPSIS
$adjuster = NextDayOfWeek($day);
$adjuster = NextOrSameDayOfWeek($day);
$adjuster = PreviousDayOfWeek($day);
$adjuster = PreviousOrSameDayOfWeek($day);
$adjuster = FirstDayOfWeekInMonth($day);
$adjuster = LastDayOfWeekInMonth($day);
$adjuster = NthDayOfWeekInMonth($ordinal, $day);
$adjuster = WesternEasterSunday();
$adjuster = OrthodoxEasterSunday();
$adjuster = NearestMinuteInterval($interval);
=head1 DESCRIPTION
C<Time::Moment::Adjusters> provides adjusters. An adjuster is a CODE reference
invoked with an instance of Time::Moment and is expected to return an instance
of Time::Moment.
=head1 FUNCTIONS
=head2 NextDayOfWeek
$adjuster = NextDayOfWeek($day);
The C<$adjuster> adjusts the date to the next occurrence of the given I<day>
of the week [1=Monday, 7=Sunday] that is after the date.
=head2 NextOrSameDayOfWeek
$adjuster = NextOrSameDayOfWeek($day);
The C<$adjuster> adjusts the date to the next occurrence of the given I<day>
of the week [1=Monday, 7=Sunday]. If the date already falls on the given
I<day> of the week it's unaltered.
=head2 PreviousDayOfWeek
$adjuster = PreviousDayOfWeek($day);
The C<$adjuster> adjusts the date to the previous occurrence of the given
I<day> of the week [1=Monday, 7=Sunday] that is before the date.
=head2 PreviousOrSameDayOfWeek
$adjuster = PreviousOrSameDayOfWeek($day);
The C<$adjuster> adjusts the date to the previous occurrence of the given
I<day> of the week [1=Monday, 7=Sunday]. If the date already falls on the
given I<day> of the week it's unaltered.
=head2 FirstDayOfWeekInMonth
$adjuster = FirstDayOfWeekInMonth($day);
The C<$adjuster> adjusts the date to the first occurrence of the given
I<day> of the week [1=Monday, 7=Sunday] within the month.
=head2 LastDayOfWeekInMonth
$adjuster = LastDayOfWeekInMonth($day);
The C<$adjuster> adjusts the date to the last occurrence of the given
I<day> of the week [1=Monday, 7=Sunday] within the month.
=head2 NthDayOfWeekInMonth
$adjuster = NthDayOfWeekInMonth($ordinal, $day);
The C<$adjuster> adjusts the date to the given I<ordinal> I<day> of
the week within the month.
B<Parameters:>
=over 4
=item ordinal
The I<ordinal> of the week within the month [-4, -1] [1, 4].
=item day
The I<day> of the week [1=Monday, 7=Sunday].
=back
=head2 WesternEasterSunday
$adjuster = WesternEasterSunday();
The C<$adjuster> adjusts the date to the Western Easter Sunday. The Western
computus is based on the Gregorian calendar.
=head2 OrthodoxEasterSunday
$adjuster = OrthodoxEasterSunday();
The C<$adjuster> adjusts the date to the Orthodox Easter Sunday. The Orthodox
computus is based on the Julian calendar with the Julian date converted to
the equivalent Gregorian date.
=head2 NearestMinuteInterval
$adjuster = NearestMinuteInterval($interval);
The C<$adjuster> adjusts the time of day to the nearest minute of the given
minute I<interval> [1, 1440].
Given an minute interval of C<30>:
T10:14:59 => T10:00:00
T10:15:00 => T10:30:00
T10:29:59 => T10:30:00
T23:55:00 => T00:00:00 (midnight of the following day)
=head1 EXPORTS
None by default. All functions can be exported using the C<:all> tag or
individually.
=head1 AUTHOR
Christian Hansen C<chansen@cpan.org>
=head1 COPYRIGHT
Copyright 2015-2017 by Christian Hansen.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.