/usr/lib/perl5/Date/Simple/NoXS.pm is in libdate-simple-perl 3.03.03-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | # Date::Simple::NoXS - used internally by Date::Simple.
use strict;
package Date::Simple;
sub _ymd {
my ( $class, @args ) = @_;
my $days = ymd_to_days(@args);
return unless defined($days);
return ( bless \$days, $class );
}
sub _d8 {
my ( $o, $d8 ) = @_;
my @ymd = $d8 =~ m/^(\d{4})(\d\d)(\d\d)$/ or return undef;
return $o->_ymd(@ymd);
}
# Precise integer arithmetic functions unfortunately missing from
# Perl's core:
sub _divmod {
my ( $quot, $int );
$quot = $_[0] / $_[1];
$int = int($quot);
$int -= 1 if $int > $quot;
$_[0] %= $_[1];
return $int;
}
sub _div {
my ( $quot, $int );
$quot = $_[0] / $_[1];
$int = int($quot);
return $int - 1 if $int > $quot;
return $int;
}
sub leap_year {
my $y = shift;
return ( ( $y % 4 == 0 ) and ( $y % 400 == 0 or $y % 100 != 0 ) ) || 0;
}
my @days_in_month = (
[ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
[ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
);
sub days_in_month ($$) {
my ( $y, $m ) = @_;
return $days_in_month[ leap_year($y) ][$m];
}
sub validate ($$$) {
my ( $y, $m, $d ) = @_;
# any +ve integral year is valid
return 0 if $y != abs int $y;
return 0 unless 1 <= $m and $m <= 12;
return 0 unless 1 <= $d and $d <= $days_in_month[ leap_year($y) ][$m];
return 1;
}
# Given a year, month, and day, return the canonical day number.
# That is the number of days since 1 January 1970, negative if earlier.
sub ymd_to_days {
my ( $Y, $M, $D ) = @_;
my ( $days, $x );
if ( $M < 1
|| $M > 12
|| $D < 1
|| ( $D > 28 && $D > days_in_month( $Y, $M ) ) ) {
return undef;
}
$days = $D +
( undef, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333 )[$M];
$days += 365 * ( $Y - 1970 );
$x = ( $M <= 2 ? $Y - 1 : $Y );
$days += _div( ( $x - 1968 ), 4 );
$days -= _div( ( $x - 1900 ), 100 );
$days += _div( ( $x - 1600 ), 400 );
return $days;
}
sub days_since_1970 { ${ $_[0] } }
# Given a canonical day number (days since 1 Jan 1970), return the
# year, month, and day.
sub days_to_ymd {
my ($days) = @_;
my ( $year, $mnum, $mday, $tmp );
# Shift frame of reference from 1 Jan 1970 to (the imaginary) 1 Mar 0AD.
$tmp = $days + 719468;
# Do the math.
$year = 400 * _divmod( $tmp, 146097 );
if ( $tmp == 146096 ) {
# Handle 29 Feb 2000, 2400, ...
$year += 400;
$mnum = 2;
$mday = 29;
}
else {
$year += 100 * _divmod( $tmp, 36524 );
$year += 4 * _divmod( $tmp, 1461 );
if ( $tmp == 1460 ) {
$year += 4;
$mnum = 2;
$mday = 29;
}
else {
$year += _divmod( $tmp, 365 );
$mnum = _divmod( $tmp, 31 );
$mday = $tmp + ( 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5 )[$mnum];
$tmp = ( 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28 )[$mnum];
if ( $mday > $tmp ) {
$mday -= $tmp;
$mnum += 1;
}
if ( $mnum > 9 ) {
$mnum -= 9;
$year += 1;
}
else {
$mnum += 3;
}
}
}
return ( $year, $mnum, $mday );
}
sub as_ymd { return days_to_ymd( ${ $_[0] } ); }
sub as_d8 { return sprintf( "%04d%02d%02d", &as_ymd ); }
sub as_iso { return sprintf( "%04d-%02d-%02d", &as_ymd ); }
sub year { return (&as_ymd)[0]; }
sub month { return (&as_ymd)[1]; }
sub day { return (&as_ymd)[2]; }
sub day_of_week {
return ( ( ${ $_[0] } + 4 ) % 7 );
}
#------------------------------------------------------------------------------
# the following methods are called by the overloaded operators, so they should
# not normally be called directly.
#------------------------------------------------------------------------------
sub _add {
my ( $date, $diff ) = @_;
if ( $diff !~ /^-?\d+$/ ) {
Carp::croak("Date interval must be an integer");
}
my $new_date = bless( \( $$date + $diff ), ref($date) );
$new_date->default_format( $date->default_format );
return $new_date;
}
sub _subtract {
my ( $left, $right, $reverse ) = @_;
my $new_date;
if ($reverse) {
Carp::croak("Can't subtract a date from a non-date");
}
if ( ref($right) eq '' && $right =~ /^-?\d+$/ ) {
$new_date = bless( \( $$left - $right ), ref($left) );
$new_date->default_format( $left->default_format );
return $new_date;
}
return ( $$left - $$right );
}
sub _compare {
my ( $left, $right, $reverse ) = @_;
$right = $left->new($right) || _inval( $left, $right );
return ( $reverse ? $$right <=> $$left : $$left <=> $$right );
}
sub _eq {
my ( $left, $right ) = @_;
return ( ( $right = $left->_new($right) ) && $$right == $$left );
}
sub _ne {
return ( !&_eq );
}
1;
=head1 NAME
Date::Simple::NoXS - Pure Perl support for Date::Simple.
=head1 SYNOPSIS
use Date::Simple;
=head1 DESCRIPTION
Used internally by Date::Simple.
=head1 SEE ALSO
L<Date::Simple>.
=cut
|