DateTime-Astro

 view release on metacpan or  search on metacpan

lib/DateTime/Astro.pm  view on Meta::CPAN


    my $backend = 'XS';

    # XXX forcibly set explicit_xs so that PP won't be loaded unless
    # explicitly called
    my $explicit_xs = 0;
    if (exists $ENV{PERL_DATETIME_ASTRO_BACKEND} && 
        $ENV{PERL_DATETIME_ASTRO_BACKEND} eq 'XS') {
        $explicit_xs = 1;
    } elsif ($ENV{PERL_DATETIME_ASTRO_BACKEND}) {
        $backend = $ENV{PERL_DATETIME_ASTRO_BACKEND};
    }
        
    my $loaded;
    my @errors;
    if ($backend ne 'PP') {
        eval {
            XSLoader::load __PACKAGE__, $VERSION;
            require DateTime::AstroXS;
            $loaded = 'XS';
        };
        if (my $e = $@) {
            push @errors, "Failed to load XS backend: $e";
        }
    }

    if (! $loaded && ! $explicit_xs) {
        eval {
            require DateTime::AstroPP;
            $loaded = 'PP';
        };
        if (my $e = $@) {
            push @errors, "Failed to load PP backend: $e";
        }
    }

    if (! $loaded ) {
        die("DateTime::Astro: Failed to load backend implementations. Can't proceed\n" . join("\n", @errors));
    }

    eval "sub BACKEND() { '$loaded' }";
}

sub moment {
    my $dt = shift;
    Carp::croak("moment called with invalid value: " . (defined $dt ? $dt : "(undef)")) unless ref $dt eq 'DateTime';
    my ($rd, $seconds) = $dt->utc_rd_values;
    return $rd + ($seconds / 86400);
}

1;

__END__

=head1 NAME

DateTime::Astro - Functions For Astromical Calendars

=head1 DESCRIPTION

DateTime::Astro implements functions used in astronomical calendars, such
as calculation of lunar longitudea and solar longitude.

This module is best used in environments where a C compiler and the MPFR arbitrary precision math library is installed. It can fallback to using Math::BigInt, but that would pretty much render it useless because of its speed and loss of accuracy that...

=head1 DISCLAIMER

This module works, but there are several caveats you should be aware of:

=head2 MPFR Is Required / PurePerl Version Not Functional

There /is/ a HALF BAKED Pure Perl implmentation bundled with this distribution, but at this point please consider it UNUSABLE. This sort of calculation requires the speed and efficiency of a C library anyway.

As such, you HAVE to have MPFR installed correctly in your system. Please consult your local package manager, or http://mpfr.org

Patches to make the pure perl version work better is always welcome.

=head2 17 solar terms are still off by ~ 5 minutes

I've tried very hard to correctly calculate the solar term dates with this
module, but I still get 17 instances in about 130 years worth of solar terms,
where the dates are off by an average of about 5 minutes -- and these usually 
fall at right about midnight, causing day-based comparisons to be off by 1.

I'm sure there's something that's causing a round off somwhere. If you're up
to it, please see xt/101_solar_terms.t and see if you can fix it for me!

=head1 FUNCTIONS

=head2 BACKEND()

Returns 'XS' or 'PP', noting the current backend.

=head2 dt_from_moment($moment)

Given a moment (days since rd + fractional seconds), returns a DateTime object in UTC

=head2 dynamical_moment($moment)

Computes the moment value from given moemnt, taking into account the ephemeris correction.

=head2 dynamical_moment_from_dt($dt)

Computes the moment value from a DateTime object, taking into account the ephemeris correction.

=head2 ephemeris_correction($moment)

Computes the ephemeris correction on a given moment

=head2 gregorian_components_from_rd($rd_days)

Computes year, month, date from RD value

=head2 gregorian_year_from_rd($rd_days)

Computes year from RD value

=head2 julian_centuries($dt)

Computes the julian centuries for given DateTime object



( run in 1.818 second using v1.01-cache-2.11-cpan-39bf76dae61 )