DateTime-Calendar-Pataphysical

 view release on metacpan or  search on metacpan

lib/DateTime/Calendar/Pataphysical.pm  view on Meta::CPAN

sub utc_rd_values {
    my $self = shift;

    return if $self->is_imaginary;

    my ($year, $month, $day) = @{$self}{qw/year month day/};
    $year++ if $year < 0;

    my $cyear = $year;
    $cyear++ if $month > 6;
    $day++ if $month > 11;

    my $rd = 683984 +                   # 7 September 1873 = 28 Phalle '0'
             ($year-1) * 365 +          # normal years: 365 real days
             _floor( .25 * $cyear) -    # leap years
             _floor(($cyear+72)/100) +  # century years
             _floor(($cyear+272)/400 ) +
             + ($month - 1) * 28 + $day;
    return ($rd, $self->{rd_secs}, $self->{rd_nano});
}

sub utc_rd_as_seconds {
    my $self = shift;
    my ($rd_days, $rd_secs, $rd_nano) = $self->utc_rd_values;

    if (defined $rd_days) {
        return $rd_days*24*60*60 + $rd_secs + $rd_nano * 1e-9;
    } else {
        return undef;
    }
}

sub from_object {
    my $class = shift;
    my %p = validate( @_,
                      { object => { type => OBJECT,
                                    can => 'utc_rd_values',
                                  },
                        locale => { type => SCALAR | OBJECT,
                                      default => $class->DefaultLocale },
                      },
                       );

    $p{object} = $p{object}->clone->set_time_zone( 'floating' )
                                if $p{object}->can( 'set_time_zone' );

    my ( $rd_days, $rd_secs, $rd_nano ) = $p{object}->utc_rd_values;

    my ($y, $m, $d) = $class->_rd2ymd( $rd_days );

    return $class->new( year => $y, month => $m, day => $d,
                        rd_secs => $rd_secs||0, rd_nano => $rd_nano||0,
                        locale => $p{locale} );
}

sub _rd2ymd {
    my ($class, $rd) = @_;

    # Algorithm similar to the one on
    # http://home.capecod.net/~pbaum/date/injdalg2.htm
    # for the gregorian calendar

    # Number of days since 1 Pedale 127 (day after first extra leap day) =
    # 24-02-2000
    $rd -= 730173;

    my $a = _floor(($rd-0.25)/(100*365.2425));
    my $b = $rd - 0.25 + $a - _floor($a/4);
    my $y = _floor($b/365.25);
    my $d = $rd + $a - _floor($a/4) - _floor(365.25*$y);

    my $m;
    if ($d < 5*28 + 1) {        # Before 29 Gidouille
        $m = _floor(($d-1)/28);
        $d -= $m * 28;
    } elsif ($d == 5*28 + 1) {  # 29 Gidouille
        $m = 4;
        $d = 29;
    } elsif ($d < 366) {        # Before 29 Gueules
        $m = _floor(($d-2)/28);
        $d -= $m*28 + 1;
    } else {                    # 29 Gueules (leap day)
        $m = 12;
        $d = 29;
    }

    $y += 127;
    $m += 7;
    if ($m > 13) {
        $m -= 13;
        $y ++;
    }

    # There is no year 0
    if ($y <= 0) {
        $y--;
    }

    return $y, $m, $d;
}

sub from_epoch {
    my $class = shift;
    my %p = validate( @_,
                      { epoch => { type => SCALAR },
                        locale => { type => SCALAR | OBJECT,
                                      default => $class->DefaultLocale },
                      }
                    );

    my $rd = int($p{epoch}/(24*60*60) + 719163);

    my ($y, $m, $d) = $class->_rd2ymd( $rd );

    return $class->new( year => $y, month => $m, day => $d,
                        locale => $p{locale} );
}

sub now { shift->from_epoch( epoch => (scalar time), @_ ) }

sub _add_overload {

lib/DateTime/Calendar/Pataphysical.pm  view on Meta::CPAN

2 Ubu ès Liens
4 St Pissembock, oncle
4 St Pissedoux, caporal des hommes libres
4 St Panurge, moraliste
4 St Glé, neurologue-aliéniste
4 St Pistolet à Merdre, jubilaire
4 Nativité de St Bruggle
v Le soleil solide froid

3 St Chibre, planton
4 Ste Ruth, zélatrice
4 St Zebb, passe-partout
4 St Mnester, confesseur
2 Assomption de Ste Messaline
v Penis Angelicus
4 St Patrobas, pompier
3 Ste Léda, ajusteuse
4 St Godemiché, économe
4 Ste Nitouche, orante
4 Ste Lèchefrite, botteuse
4 Ste Andouille, amphibologue
4 Ste Bitre, ouvreuse et St Étalon, couvreur
3 Bataille de Morsang
3 Mort de Dionysos, surhomme
4 Nativité de St Vibescu, pohète et Commémoration de Ste Cuculine d'Ancône
4 Ste Gallinacée, cocotte
4 St Lingam, bouche-trou
4 St Prélote, capucin
4 St Pie VIII, navigant
3 St Erbrand, polytechnicien
2 Ste Dragonne, pyrophage
4 St Lazare, gare
4 Ste Orchidée, aumonière
4 Nativité apparente d'Artaud le Momo
4 Disparition de l'Ancien Breughel, incendiaire
4 St Priape, franc-tireur
3 Transfixion de Ste Messaline
v Le Termès
EOF

1;

__END__

=for Pod::Coverage::TrustPod
     DefaultLocale
     day_0
     day_of_month_0
     day_of_week_0
     day_of_year_0
     locale
     mday_0
     mon
     mon_0
     month_0

=encoding utf-8

=head1 NAME

DateTime::Calendar::Pataphysical - Dates in the Pataphysical calendar

=head1 VERSION

version 0.07

=head1 SYNOPSIS

  use DateTime::Calendar::Pataphysical;

  $dt = DateTime::Calendar::Pataphysical->new( year  => 1752,
                                               month => 10,
                                               day   => 4 );

=head1 DESCRIPTION

DateTime::Calendar::Pataphysical is the implementation of the
Pataphysical calendar. Each year in this calendar contains 13 months of
29 days. This regularity makes this a convenient alternative for the
irregular Gregorian calendar.

This module is designed to be easy to use in combination with
L<DateTime>. Most of its methods correspond to a L<DateTime> method of the
same name.

=head1 CLASS METHODS

=head2 new

    my $dt = DateTime::Calendar::Pataphysical-new(
        year  => $year_in_the_pataphysical_era,
        month => $pataphysical_month_number,
        day   => $pataphysical_day_number,
    );

This class method accepts parameters for each date and time component:
C<year>, C<month>, C<day>.  Additionally, it accepts a C<locale>
parameter.

The C<rd_secs> parameter is also accepted. This parameter is only useful
in conversions to other calendars; this calendar does not use its value.

=head2 from_epoch

    my $dt = DateTime::Calendar::Pataphysical->from_epoch( epoch => $epoch, ... );

This class method can be used to construct a new object from an epoch
time instead of components. Just as with the L<new> constructor, it
accepts a C<locale> parameter.

=head2 now

    my $dt = DateTime::Calendar::Pataphysical->now;

This class method is equivalent to calling C<from_epoch()> with the
value returned from Perl's L<time|perlfunc/time> function.

=head2 from_object

    my $dt = DateTime::Calendar::Pataphysical->from_object( object => $object, ... );

This class method can be used to construct a new object from
any object that implements the L<utc_rd_values> method.  All
L<DateTime::Calendar> modules must implement this method in order to
provide cross-calendar compatibility.  This method accepts a
C<locale> parameter.

The time part of C<$object> is stored, and will only be used if the created
object is converted to another calendar. Only the date part of C<$object>
is used to calculate the pataphysical date. This calculation is based on
the local time and date of C<$object>.

=head2 last_day_of_month

    my $dt = DateTime::Calendar::Pataphysical->last_day_of_month( ... );

This constructor takes the same arguments as can be given to the
L<now> method, except for C<day>.  Additionally, both C<year> and
C<month> are required.

=head1 METHODS

=head2 clone

    my $clone = $dt->clone;

This object method returns a replica of the given object.

=head2 year

Returns the year.

=head2 month

Returns the month of the year, from C<1 .. 13>.

=head2 month_name

Returns the name of the current month.

=head2 day_of_month

=head2 day

=head2 mday

Returns the day of the month, from C<1 .. 29>.

=head2 day_of_week

=head2 wday

=head2 dow

Returns the day of the week as a number, from C<1 .. 7>, with C<1> being
Sunday and C<7> being Saturday. Returns C<undef> if the day is a "hunyadi".

=head2 day_name

Returns the name of the current day of the week.

=head2 day_of_year

=head2 doy

Returns the day of the year.

=head2 ymd

=head2 mdy

=head2 dmy

     my $string = $dt->ymd( $optional_separator );

Each method returns the year, month, and day, in the order indicated
by the method name.  Years are zero-padded to three digits.  Months and
days are 0-padded to two digits.

By default, the values are separated by a dash (C<->), but this can be
overridden by passing a value to the method.

=head2 date

Alias for L<ymd>.

=head2 datetime

Equivalent to

    $dt->ymd('-') . 'EP'

=head2 is_leap_year

This method returns a true or false indicating whether or not the
L<DateTime> object is in a leap year.

=head2 week

    my ( $week_year, $week_number ) = $dt->week;

Returns information about the calendar week which contains this
L<DateTime> object. The values returned by this method are also available
separately through the L<week_year> and L<week_number> methods.

=head2 week_year

Returns the year of the week. In the Pataphysical calendar, this is
equal to the year of the date, as all weeks fall in one year only.

=head2 week_number

Returns the week of the year, from C<1 .. 53>.

The 29th of each month falls outside of any week; C<week_number> returns
C<undef> for these dates.

=head2 utc_rd_values

Returns the current UTC Rata Die days and seconds as a two element
list.  This exists primarily to allow other calendar modules to create
objects based on the values provided by this object.

=head2 utc_rd_as_seconds

Returns the current UTC Rata Die days and seconds purely as seconds.
This is useful when you need a single number to represent a date.

=head2 strftime

    my $string = $dt->strftime( $format, ... );

This method implements functionality similar to the C<strftime()>
method in C.  However, if given multiple format strings, then it will
return multiple elements, one for each format string.

See L<DateTime> for a list of all possible format specifiers. This
module implements all specifiers related to dates. There is one
additional specifier: C<%*> represents the feast of that date.

=head2 feast

Returns the feast or vacuation of the given date.

=head2 type_of_feast

Returns the type of feast or vacuation.

  '*' means Fête Suprème Première première
  '1' means Fête Suprème Première seconde
  '2' means Fête Suprème Seconde
  '3' means Fête Suprème Tierce
  '4' means Fête Suprème Quarte
  'v' means Vacuation

=head2 is_imaginary

Returns true or false indicating whether the L<DateTime> object represents an
imaginary date.

=head2 set

This method can be used to change the local components of a date time,
or its locale.  This method accepts any parameter allowed by L<new>.

=head2 truncate

    $dt->truncate( to => ... );

This method allows you to reset some of the local time components in
the object to their C<zero> values.  The C<to> parameter is used to
specify which values to truncate, and it may be one of C<year>,
C<month>, or C<day>.

=head2 add_duration

    $dt->add_duration( $duration_object );

This method adds a C<DateTime::Duration> to the current L<DateTime>.
See the L<DateTime::Duration> documentation for more details.



( run in 0.645 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )