$ENV{REMOTE_HOST} = $ENV{REMOTE_HOST) like 192.168.1.45 not defined,
$ENV{REMOTE_ADDR} = 3.95.233.107 and $ENV{SERVER_ADDR} = 67.23.254.53.
$server = devonrover.com and $software = Apache.
Message from (67.23.254.53) to (3.95.233.107) :-   launcher home

Phase of the moon according to local scripts and Astro::MoonPhase

The date is Tuesday the 19th. March 2024 and the time is 5:15:27.
There were 1,710,839,727 seconds since 01-Jan-1970. The local scripts are a translation of the basic code from a book called Celestial Basic by Eric Burgess 1985. The Perl package Astro::MoonPhase which calculates the positions of the sun and the moon as seen from the earth are taken from the book Practical Astronomy by Peter Duffett-Smith 1981.
Phase from Astro::MoonPhase :-

MoonPhase0.32
MoonIllum0.714
MoonAge9.462
MoonDist396,140.287
MoonAng0.503
SunDist148,982,879.641
SunAng0.535

Phase from local scripts :-
the current attributes of the moon
Moon from Wikipedia
This photograph of the moon is taken from Wikipedia.
The crater in the lower left is Tycho named after the Danish astronomer
Tycho Brahe (14 December 1546 - 24 October 1601).
It is a young crater, only about 108 million years old,
and so the dinosaurs must have seen the impact.

documentation  perl GD  commify  launcher   home   guestbook  other ephemerides   lunar calendar   Astro-MoonPhase-0.60   Total eclipse at the new moon  top
T here are 5 scripts :-  moondisplay.cgi, astro2.cgi, moontextimage.cgi, Astro::MoonPhase and Number::Format.

You are visitor number   number of hits   Page updated on June 15th. 2014 and again on January 18th. 2018.

This is astro2.cgi :- top
#!/usr/bin/perlml
# perl -MCPAN -e shell
# cpan> install Astro::MoonPhase
# /usr/local/share/perl/5.14.2
use strict;
use CGI::Carp qw(fatalsToBrowser);
use File::Basename;
print "Content-type: text/html\n\n";
$| = 1;
open (STDERR, ">&STDOUT");
my ( $source1, $source10, $source2, $source20, $source3, $source30, $source4, $source40, $source5, $source50, );
my ( $dainty, $dinty, $tim, $message, $position, $tldname );
my ( $source100, $source200, $source300, $source400, $source500 );
my ( $MoonPhase, $MoonIllum, $MoonIllum1, $MoonAge, $MoonDist, $MoonAng, $SunDist, $SunAng, $server, );
my ( $lib, $title, $title2, $thisfile, $software, $remote_host, $remote_addr, $server_addr );

$thisfile = basename($0);
$server = $ENV{SERVER_NAME}; # like pi3.onwardoverland.com
$position = index($server, ".");
$tldname = substr($server, 0, $position);
$software = $ENV{SERVER_SOFTWARE}; # like Microsoft-IIS/8.0
$source1 = $source100 = "moondisplay.cgi";
# $source2 = $source200 = "moonwrite.cgi";
$source3 = $source300 = "moontextimage.cgi";
# $message = "Message from ($ENV{REMOTE_HOST}) to ($ENV{REMOTE_ADDR}) :-";
# $message = "Message from ($ENV{SERVER_ADDR}) to ($ENV{REMOTE_ADDR}) :-";
if (!defined($remote_host = $ENV{REMOTE_HOST})) {$remote_host = "\$ENV{REMOTE_HOST) like 192.168.1.45 not defined";}
if (!defined($remote_addr = $ENV{REMOTE_ADDR})) {$remote_addr = "\$ENV{REMOTE_ADDR) like 192.168.1.46 not defined";}
if (!defined($server_addr = $ENV{SERVER_ADDR})) {$server_addr = "\$ENV{SERVER_ADDR) like 192.168.1.47 not defined";}
$source40 = "Astro::MoonPhase"; $source50 = "Number::Format";

if (substr($software,0,13) eq "Microsoft-IIS")
{
$source400 = "C:\\devhtml\\lib\\Astro\\MoonPhase.pm";
$source500 = "C:\\devhtml\\lib\\Number\\Format.pm";
$source100 = "C:\\devhtml\\cgi-bin\\moondisplay.cgi";
# $source2 = "C:\\devhtml\\cgi-bin\\moonwrite.cgi";
$source300 = "C:\\devhtml\\cgi-bin\\moontextimage.cgi";
$message = "Message from ($remote_host) to ($remote_addr) :-";
}
elsif (substr($software,0,21) eq "Apache/2.2.11 (Win32)")
{
$source400 = "C:\\devhtml\\lib\\Astro\\MoonPhase.pm";
$source500 = "C:\\devhtml\\lib\\Number\\Format.pm";
$source100 = "C:\\devhtml\\cgi-bin\\moondisplay.cgi";
# $source2 = "C:\\devhtml\\cgi-bin\\moonwrite.cgi";
$source300 = "C:\\devhtml\\cgi-bin\\moontextimage.cgi";
$message = "Message from ($server_addr) to ($remote_addr) :-";
}
elsif ($server eq "onwardoverland.com")
{
use lib '/home/onwardov/public_html/lib/';
$source4 = $source400 = "/home/onwardov/public_html/lib/Astro/MoonPhase.pm";
$source5 = $source500 = "/home/onwardov/public_html/lib/Number/Format.pm";
$message = "Message from ($server_addr) to ($remote_addr) :-";
}
elsif (substr($software,0,6) eq "Apache")
{
use lib '../lib';
$source4 = $source400 = "../lib/Astro/MoonPhase.pm";
$source5 = $source500 = "../lib/Number/Format.pm";
$message = "Message from ($server_addr) to ($remote_addr) :-";
}
else
{
print "unknown \$server/\$software combination $server/$software<BR>\n"; exit;
}
use Astro::MoonPhase;
use Number::Format;
$source4 = "Astro::MoonPhase"; $source5 = "Number::Format";
$source1 = "moondisplay.cgi";
# $source2 = "moonwrite.cgi";
$source3 = "moontextimage.cgi";
$dainty = &dayte; $tim = time;
($MoonPhase, $MoonIllum, $MoonAge, $MoonDist, $MoonAng, $SunDist, $SunAng ) = phase($tim);
$tim = commify2($tim);
$MoonPhase = commify2($MoonPhase); $MoonIllum = commify2($MoonIllum);
$MoonAge = commify2($MoonAge); $MoonDist = commify2($MoonDist);
$MoonAng = commify2($MoonAng); $SunDist = commify2($SunDist); $SunAng = commify2($SunAng);
#if ($MoonPhase > 0.5 ) { $MoonIllum1 = $MoonIllum + 0.001; } else { $MoonIllum1 = $MoonIllum - 0.001; }
#$source10 = "$source1?potm=$MoonIllum&potm1=$MoonIllum1&moonphase=$MoonPhase";
$source10 = "$source1?potm=$MoonIllum&potm1=$MoonIllum&moonphase=$MoonPhase";
$title2 = "Phase of the moon according to local scripts and Astro::MoonPhase";
#$title = "\$MoonIllum = $MoonIllum";
#$title = "$MoonIllum <= \$MoonIllum";
$title = "$MoonIllum $dinty $tldname";
print << "(HEADER)" ;
<HTML><HEAD><TITLE>$title</TITLE>
<meta http-equiv="refresh" content="600">
<link rel="icon" type="vnd.microsoft.icon" href="http://$server/fullmoon4.ico" sizes="16*16">
</HEAD><BODY bgcolor=#CCCCCC><A NAME = top><FONT FACE=ARIAL>

\$ENV{REMOTE_HOST} = $remote_host,<BR>
\$ENV{REMOTE_ADDR} = $remote_addr and \$ENV{SERVER_ADDR} = $server_addr.<BR>
\$server = $server and \$software = $software.

<TABLE><TR><TD>

<TABLE><TR><TD COLSPAN=2>
$message &nbsp; <A HREF=/launcher.html>launcher</A> <A HREF=/index.html>home</A>
<H3>$title2</H3>
$dainty
<BR>There were $tim seconds since <NOBR>01-Jan-1970. </NOBR>
The local scripts are a translation of the basic code from a book called
Celestial Basic by Eric Burgess 1985. The Perl package Astro::MoonPhase
which calculates the positions of the sun and
the moon as seen from the earth are taken from the book Practical Astronomy
by Peter Duffett-Smith 1981.

</TD></TR>
<TR><TD width=300>
<TABLE>
<TR><TD COLSPAN = 2>Phase from Astro::MoonPhase :-<BR><BR></TD></TR>
<TR><TD width=130>
MoonPhase</TD><TD width=110 style='text-align:right;'>$MoonPhase</TD></TR>
<TR><TD>MoonIllum</TD><TD style='text-align:right;'>$MoonIllum</TD></TR>
<TR><TD>MoonAge</TD><TD style='text-align:right;'>$MoonAge</TD></TR>
<TR><TD>MoonDist</TD><TD style='text-align:right;'>$MoonDist</TD></TR>
<TR><TD>MoonAng</TD><TD style='text-align:right;'>$MoonAng</TD></TR>
<TR><TD>SunDist</TD><TD style='text-align:right;'>$SunDist</TD></TR>
<TR><TD>SunAng</TD><TD style='text-align:right;'>$SunAng</TD></TR>
</TABLE>
<TD WIDTH=400 style='text-align:center;'><BR>Phase from local scripts :-<BR>
<IMG SRC = $source3 alt = "the current attributes of the moon"
VALIGN = TOP HSPACE = 20 VSPACE = 10 height=220 width=400>
</TD></TR></TABLE>

</TD><!--TD style='text-align:center;'--><TD>
<A HREF=http://en.wikipedia.org/wiki/Moon>
<IMG SRC = $source10 alt = "Moon from Wikipedia" border=0
VALIGN = TOP HSPACE = 20 VSPACE = 10 height=380 width=400></A><BR>
This photograph of the moon is taken from Wikipedia.<BR>
The crater in the lower left is Tycho named after the Danish astronomer<BR>
<NOBR>Tycho Brahe (14 December 1546 - 24 October 1601).</NOBR><BR>
It is a young crater, only about 108 million years old,<BR>
and so the dinosaurs must have seen the impact.
</TD></TR></TABLE>

</TD></TR></TABLE>
(HEADER)

&credit;
&disp($0);
print "\$source100 = $source100<BR>\n";
&disp($source100);
# print "\$source2 = $source2<BR>\n";
# &disp($source2);
print "\$source300 = $source300<BR>\n";
&disp($source300);
print "\$source400 = $source400<BR>\n";
&disp($source400);
print "\$source500 = $source500<BR>\n";
&disp($source500);
&credit;
print "<BR><BR></BODY></HTML>";

sub disp{
my ($source, @lines, $storey);
$source = $_[0];
open(SOURCE, $source) or die ("Can't open $source: $!");
@lines = <SOURCE>;
close(SOURCE) or die ("Can't close $source: $!");
print "<A NAME = $source><FONT COLOR = red>This is $source :- <FONT COLOR = black><A HREF=#top>top</A><BR>\n";
foreach $storey (@lines){
chop($storey);
$storey =~s/&/&amp;/g;
$storey =~s/</&lt;/g;
$storey =~s/>/&gt;/g;
print "$storey<BR>\n";
}
}
sub commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text }

sub commify2 {
my $text = $_[0];
my $text2 = new Number::Format;
return $text2->format_number($text,3); }

sub dayte {
my (@month,@weekday,$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$modmday,$men);
@month =("January","February","March","April","May","June","July",
"August","September","October","November","December");
@weekday = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$modmday = $mday % 10;
if ($mday > 3 && $mday < 21 ) { $men = "th."; }
elsif ($modmday == 1) { $men = "st."; }
elsif ($modmday == 2) { $men = "nd."; }
elsif ($modmday == 3) { $men = "rd."; }
else { $men = "th.";}
$dinty = sprintf ("%2d:%02d:%02d",$hour, $min, $sec);
#return sprintf ("The date is %s the %d%s %s %d and the time is %2d:%02d:%02d. \n",$weekday[$wday], $mday, $men, $month[$mon], $year+1900,$hour, $min, $sec);
return sprintf ("The date is %s the %d%s %s %d and the time is %s. \n",$weekday[$wday], $mday, $men, $month[$mon], $year+1900, $dinty);
}
sub credit {
print << "(CREDIT)";
<BR>
<A HREF=http://search.cpan.org/~brett/Astro-MoonPhase-0.60/MoonPhase.pm>documentation</A>&nbsp;
<A HREF=https://metacpan.org/pod/distribution/GD/GD.pm>perl GD</A>&nbsp;
<A HREF=http://www.perlmonks.org/?node_id=110137>commify</A>&nbsp;
<A HREF=/launcher.html>launcher</A>&nbsp;&nbsp;
<A HREF=/index.html>home</A>&nbsp;&nbsp;
<a href="#" onkeypress="bar" onclick="MyWindow=window.open('/formost.html?ref=astro','MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no,width=800,height=600'); return false;">guestbook</a>&nbsp;
<A HREF=http://xjubier.free.fr/en/site_pages/astronomy/ephemerides.html>other ephemerides</A>&nbsp;&nbsp;
<A HREF=http://aa.usno.navy.mil/cgi-bin/aa_moonphases.pl?year=2014&ZZZ=END>lunar calendar</A>&nbsp;&nbsp;
<A HREF=http://search.cpan.org/~brett/Astro-MoonPhase-0.60/>Astro-MoonPhase-0.60</A>&nbsp;&nbsp;
<A HREF=http://www.bbc.co.uk/news/world-asia-20307900>Total eclipse at the new moon</A>&nbsp;
<A HREF=#top>top</A>
<BR>
<FONT FACE=ARIAL SIZE=6>T<FONT FACE=ARIAL SIZE=3>
here are 5 scripts :-&nbsp;
<A HREF=#$source100>$source1</A>, <A HREF=#$0>$thisfile</A>, <A HREF=#$source300>$source3</A>, <A HREF=#$source400>$source40</A> and <A HREF=#$source500>$source50</A>.
<BR><BR>
You are visitor number&nbsp;&nbsp;
<IMG width=140 height=27 alt="number of hits"
SRC="/cgi-bin/number9.cgi?style=I&width=7&update=Yes&countfile=jblack_astro.txt">
&nbsp;&nbsp;Page updated on June 15th. 2014 and again on January 18th. 2018.

<BR><BR>
(CREDIT)
}

$source100 = moondisplay.cgi
This is moondisplay.cgi :- top
#!/usr/bin/perlml
use strict;
use GD;
# use GD::Image;
use CGI qw(:standard);
#print "Content-type: text/html\n\n";
#$| = 1;
#open (STDERR, ">&STDOUT");
#use CGI::Carp qw(fatalsToBrowser);
my ($xm, $xm1, $xs, $xs1, $S, $xd, $hp, $sw1, $sw3);
my ($dy, $mn, $yr, $lam, $dpsi, $bet, $p, $q, $x, $y, $f, $r, $thet, $djd);
my ($cd, $lsn, $sd, $i, $k, $md, $ms, $an, $mn1, $yr1, $a, $b, $c, $d);
my ($t, $eps, $epsr, $tpi, $deps, $eps1, $seps, $ceps, $cy, $ty, $sy);
my ($cx, $sx, $tx, $sq, $t2, $ls, $ld, $tls, $tld, $nm, $tnm, $m, $ma);
my ($ea, $SS, $dla, $tnu2, $nu, $a1, $b1, $d1, $e1, $c1, $h1, $dl, $dr, $rsn);
my ($m1, $m2, $m3, $m4, $m5, $m6, $de, $sa, $sb, $sn, $n, $e, $l, $sc, $e2);
my ($g, $w1, $w2, $xd1, $xp, $minmoon);
my ($im, , $im2, $imnew, $white, $black, $red, $blue, $yellow);
my ($PHASE, $PHASE1, $PHASE2, $head, $dayte, $W ,$outfile ,$footnote);
my ($buff1, $buff2, $para, $fromangle, $toangle, $waxwan, $fillpos, $adjust);
my ($POTM, $POTM1, $grey, $width, $height, $MoonPhase);
my ($imold, $whiteold, $blackold, $redold, $blueold, $yellowold,$greyold);
my $pi = 3.1415926535;

print "Content-type: image/gif\n\n";
binmode STDOUT;

if(!defined($POTM = param('potm'))) { $POTM = &CalcyMoon(0); }
if(!defined($POTM1 = param('potm1'))) { $POTM1 = &CalcyMoon(1); }
if(defined($MoonPhase = param('moonphase'))) {
if ($MoonPhase > 0.5 ) { $POTM1 = $POTM + 0.001; } else { $POTM1 = $POTM - 0.001; }
}

($dy, $mn, $yr) = (localtime)[3,4,5];
$yr = $yr + 1900; $mn = $mn + 1;

# my $POTM = &CalcyMoon(0);
# my $POTM1 = &CalcyMoon(1);
$minmoon = 0.010; # no moon if phase of the moon less than this
&moondraw;
syswrite(STDOUT, $buff1);

sub moondraw {
# draw the moon Devendra December 24th 1998 updated April 15th 2014.

$imnew = new GD::Image->new("fullmoon1.gif");
$white = $imnew->colorAllocate(255,255,255);
$black = $imnew->colorAllocate(0,0,0);
$red = $imnew->colorAllocate(255,0,0);
$blue = $imnew->colorAllocate(0,0,255);
$yellow = $imnew->colorAllocate(255,255,0);
$grey = $imnew->colorAllocate(204,204,204); # #CC,#CC, #CC

$imold = new GD::Image(340,380);
$greyold = $imold->colorAllocate(204,204,204); # #CC,#CC, #CC
$whiteold = $imold->colorAllocate(255,255,255);
$blackold = $imold->colorAllocate(0,0,0);
$redold = $imold->colorAllocate(255,0,0);
$blueold = $imold->colorAllocate(0,0,255);
$yellowold = $imold->colorAllocate(255,255,0);

# ($width, $height) = $imnew->getBounds();
# $imnew->transparent($white);
# $imnew->interlaced('true');


$head = "THE STATUS OF THE MOON";
$dayte = 'FOR DAY '.$dy.' IN MONTH '.$mn.' OF THE YEAR '.$yr;

$PHASE1 = 300 - ($POTM * 600);
$PHASE2 = 300 - ((1 - $POTM) * 600);
if (abs($POTM) <= $minmoon) {
$fromangle = 0;
$toangle = 360;
$waxwan = "no moon";
$fillpos = 150;
} else {
if ($POTM > $POTM1) {
$fromangle = 270;
$toangle = 90;
if ($POTM < 0.5) {
$waxwan = "waxing crescent (growing)";
}else{ $waxwan = "waxing gibbous (growing)";}
$fillpos = 1;
} else {
$fromangle = 90;
$toangle = 270;
if ($POTM < 0.5) {
$waxwan = "waning crescent (shrinking)";
}else{ $waxwan = "waning gibbous (shrinking)";}
$fillpos = 299;
}}
if (abs($POTM - 1) <= $minmoon) {
# do nothing
# $im->arc(150,150,300,300,0,360,$yellow);
# $im->fill(150,150,$yellow); # full moon
$waxwan = "full moon";
} else {
if (abs($POTM) <= $minmoon) {
$imnew->arc(150,150,300,300,$fromangle,$toangle,$grey); # new moon - draw grey circle
$imnew->fillToBorder(5,150,$grey,$grey); # and fill it with grey
} else {
$imnew->arc(150,150,300,300,$fromangle,$toangle,$grey); # draw left hand arc
if ($POTM<0.5) {
$imnew->arc(150,150,$PHASE1,300,$fromangle,$toangle,$grey); # draw an ellipse - left hand arc
}
if (abs($POTM-0.5)<0.001) {
$imnew->GD::Image::line(150,0,150,300,$grey); # draw a vertical line
}
if ($POTM>0.5){
$imnew->arc(150,150,$PHASE2,300,$toangle,$fromangle,$grey); #draw an ellipse - right hand arc
}
$imnew->fillToBorder($fillpos,150,$grey,$grey); # in any case fill it with grey
}}
$footnote = 'Phase = '.$POTM." (".$POTM1.") ";
# $im->fill(25,10,$white);
$imold->GD::Image::string (gdLargeFont, 25, 10, $head, $blackold);
$imold->GD::Image::string (gdLargeFont, 25, 25, $dayte, $blackold);
$imold->GD::Image::string (gdLargeFont, 25, 340, $footnote, $blackold);
$imold->GD::Image::string (gdLargeFont, 25, 360, $waxwan, $blackold);

#$image->copyMerge($sourceImage,$dstX,$dstY,$srcX,$srcY,$width,$height,$percent);
$imold->copyMerge($imnew,0,40,0,0,300,300,80);

$buff1 = $imold->gif;
}

sub CalcyMoon {
$adjust = $_[0];
($dy, $mn, $yr) = (localtime)[3,4,5];
$yr = $yr + 1900; $mn = $mn + 1;
&moon($adjust);

&sun($adjust);
$cd = cos($lam - $lsn) * cos($bet);
$d = &acs($cd);
$sd = sin($d);
$i = (.1468 * $sd * (1 - .0549 * sin($md))) / (1 - .0167 * sin($ms));
$i = $pi - $d - &rad($i); $k = (1 + cos($i)) / 2;
return &rou($k, 1000);
}

sub acs {
$W = $_[0];
return 1.570796327 - &asn($W);
}

sub rou {
$x = $_[0];
$y = $_[1];
return int($x * $y + .5) / $y;
}

sub sun {
my $adjust = $_[0];
&julday; $djd = $djd - $adjust;
$t = $djd / 36525; $t2 = $t * $t;
$a = 100.0021359 * $t; $b = 360 * ($a - int($a));
$ls = 279.69668 + (.0003025 * $t2) + $b;
$a = 99.9973604200004 * $t; $b = 360 * ($a - int($a));
$ms = 358.47583 - ((.00015 + (.0000033 * $t)) * $t2) + $b;
$SS = .016751 - (.0000418 * $t) - (1.26E-07 * $t2);
$ma = &rad($ms);
&anomaly;
$a = 62.5520947200002 * $t; $b = 360 * ($a - int($a));
$a1 = &rad(153.23 + $b);
$a = 125.1041894 * $t; $b = 360 * ($a - int($a));
$b1 = &rad(216.57 + $b);
$a = 91.56766028 * $t; $b = 360 * ($a = int($a));
$c1 = &rad(312.69 + $b);
$a = 1236.853095 * $t; $b = 360 * ($a - int($a));
$d1 = &rad(350.74 - .00144 * $t2 + $b);
$e1 = &rad(231.19 + 20.2 * $t);
$a = 183.1353208 * $t; $b = 360 * ($a - int($a));
$h1 = &rad(353.4 + $b);
$dl = .00134 * cos($a1) + .00154 * cos($b1) + .002 * cos($c1);
$dl = $dl + .00179 * sin($d1) + .00178 * sin($e1);
$dr = 5.43E-06 * sin($a1) + 1.575E-05 * sin($b1) + 1.627E-05 * sin($c1);
$dr = $dr + 3.076E-05 * cos($d1) + 9.27E-06 * sin($h1);
$lsn = $nu + &rad($ls - $ms + $dl); $tpi = 6.28318530800002;
$rsn = 1.0000002 * (1 - $SS * cos($ea)) + $dr;
while ($lsn < 0) {
$lsn = $lsn + $tpi;}
while ($lsn > $tpi) {
$lsn = $lsn - $tpi;}
}

sub julday {
{
$mn1 = $mn; $yr1 = $yr; $b = 0;
if ($yr1 < 0) { $yr1 = $yr1 + 1;}
if ($mn < 3) { $mn1 = $mn + 12; $yr1 = $yr1 - 1;}
if (($yr >= 1582) || (($yr == 1582) && ($mn < 10)) || (($yr == 1582) && ($mn == 10) && ($dy < 15))) {
$a = int($yr1 / 100); $b = 2 - $a + int($a / 4);
}
if ($yr1 >= 0) {
$c = int(365.25 * $yr1) - 694025;
} else {
$c = &itg((365.25 * $yr1) - .75) - 694025;
}
$d = int(30.6001 * ($mn1 + 1));
$djd = $b + $c + $d + $dy - 0.5 ;
}}

sub rad {
$W = $_[0];
return .01745329252 * $W;
}

sub moon {
my $adjust = $_[0];
&julday; $djd = $djd - $adjust;
$t = $djd / 36525; $t2 = $t * $t;
$m1 = 27.32158213; $m2 = 365.2596407;
$m3 = 27.55455094; $m4 = 29.5305886800001;
$m5 = 27.21222039; $m6 = 6798.36330700001;
$m1 = $djd / $m1; $m2 = $djd / $m2; $m3 = $djd / $m3;
$m4 = $djd / $m4; $m5 = $djd / $m5; $m6 = $djd / $m6;
$m1 = 360 * ($m1 - int($m1)); $m2 = 360 * ($m2 - int($m2));
$m3 = 360 * ($m3 - int($m3)); $m4 = 360 * ($m4 - int($m4));
$m5 = 360 * ($m5 - int($m5)); $m6 = 360 * ($m6 - int($m6));
$ld = 270.434164 + $m1 - (.001133 - .0000019 * $t) * $t2;
$ms = 358.475833 + $m2 - (.00015 + .0000033 * $t) * $t2;
$md = 296.104608 + $m3 + (.009192 + .0000144 * $t) * $t2;
$de = 350.737486 + $m4 - (.001436 - .0000019 * $t) * $t2;
$f = 11.250889 + $m5 - (.003211 + .0000003 * $t) * $t2;
$n = 259.183275 - $m6 + (.002078 + .000022 * $t) * $t2;
$a = &rad(51.2 + 20.2 * $t); $sa = sin($a);
$sn = sin(&rad($n));
$b = 346.56 + (132.87 - .0091731 * $t) * $t;
$sb = .003964 * sin(&rad($b));
$c = &rad($n + 275.05 - 2.3 * $t); $sc = sin($c);
$ld = $ld + .000233 * $sa + $sb + .001964 * $sn;
$ms = $ms - .001778 * $sa;
$md = $md + .000817 * $sa + $sb + .002541 * $sn;
$f = $f + $sb - .024691 * $sn - .004328 * $sc;
$de = $de + .002011 * $sa + $sb + .001964 * $sn;
$e = 1 - (.002495 + 7.52E-06 * $t) * $t; $e2 = $e * $e;
$ld = &rad($ld); $ms = &rad($ms); $n = &rad($n);
$de = &rad($de); $f = &rad($f); $md = &rad($md);
$l = 6.28875 * sin($md) + 1.27402 * sin(2 * $de - $md) + .658309 * sin(2 * $de);
$l = $l + .213616 * sin(2 * $md) - $e * .185596 * sin($ms) - .114336 * sin(2 * $f);
$l = $l + .058793 * sin(2 * ($de - $md)) + .057212 * $e * sin(2 * $de - $ms - $md);
$l = $l + .05332 * sin(2 * $de + $md) + .045874 * $e * sin(2 * $de - $ms);
$l = $l + .041024 * $e * sin($md - $ms);
$l = $l - .034718 * sin($de) - $e * .030465 * sin($ms + $md);
$l = $l + .015326 * sin(2 * ($de - $f)) - .012528 * sin(2 * $f + $md);
$l = $l - .01098 * sin(2 * $f - $md) + .010674 * sin(4 * $de - $md);
$l = $l + .010034 * sin(3 * $md) + .008548 * sin(4 * $de - 2 * $md);
$l = $l - $e * .00791 * sin($ms - $md + 2 * $de) - $e * .006783 * sin(2 * $de + $ms);
$l = $l + .005162 * sin($md - $de) + $e * .005 * sin($ms + $de);
$l = $l + .003862 * sin(4 * $de) + $e * .004049 * sin($md - $ms + 2 * $de);
$l = $l + .003996 * sin(2 * ($md + $de)) + .003665 * sin(2 * $de - 3 * $md);
$l = $l + $e * .002695 * sin(2 * $md - $ms) + .002602 * sin($md - 2 * ($f + $de));
$l = $l + $e * .002396 * sin(2 * ($de - $md) - $ms) - .002349 * sin($md + $de);
$l = $l + $e2 * .002249 * sin(2 * ($de - $ms)) - $e * .002125 * sin(2 * $md + $ms);
$l = $l - $e2 * .002079 * sin(2 * $ms) + $e2 * .002059 * sin(2 * ($de - $ms) - $md);
$l = $l - .001773 * sin($md + 2 * ($de - $f)) - .001595 * sin(2 * ($f + $de));
$l = $l + $e * .00122 * sin(4 * $de - $ms - $md) - .00111 * sin(2 * ($md + $f));
$l = $l + .000892 * sin($md - 3 * $de);
$l = $l - $e * .000811 * sin($ms + $md + 2 * $de);
$l = $l + $e * .000761 * sin(4 * $de - $ms - 2 * $md);
$l = $l + $e2 * .000704 * sin($md - 2 * ($ms + $de));
$l = $l + $e * .000693 * sin($ms - 2 * ($md - $de));
$l = $l + $e * .000598 * sin(2 * ($de - $f) - $ms) + .00055 * sin($md + 4 * $de);
$l = $l + .000538 * sin(4 * $md) + $e * .000521 * sin(4 * $de - $ms);
$l = $l + .000486 * sin(2 * $md - $de);
$l = $l + $e2 * .000717 * sin($md - 2 * $ms);
$lam = $ld + &rad($l); $tpi = 6.28318530800002;
while ($lam < 0) {
$lam = $lam + $tpi;
}
while ($lam > $tpi) {
$lam = $lam - $tpi;
}
$g = 5.12819 * sin($f) + .280606 * sin($md + $f) + .277693 * sin($md - $f);
$g = $g + .173238 * sin(2 * $de - $f) + .055413 * sin(2 * $de + $f - $md);
$g = $g + .046272 * sin(2 * $de - $f - $md) + .032573 * sin(2 * $de + $f);
$g = $g + .017198 * sin(2 * $md + $f) + .009267 * sin(2 * $de + $md - $f);
$g = $g + .008823 * sin(2 * $md - $f) + $e * .008247 * sin(2 * $de - $ms - $f);
$g = $g + .004323 * sin(2 * ($de - $md) - $f) + .0042 * sin(2 * $de + $f + $md);
$g = $g + $e * .003372 * sin($f - $ms - 2 * $de);
$g = $g + $e * .002472 * sin(2 * $de + $f - $ms - $md);
$g = $g + $e * .002222 * sin(2 * $de + $f - $ms);
$g = $g + $e * .002072 * sin(2 * $de - $f - $ms - $md);
$g = $g + $e * .001877 * sin($f - $ms + $md) + .001828 * sin(4 * $de - $f - $md);
$g = $g - $e * .001803 * sin($f + $ms) - .00175 * sin(3 * $f);
$g = $g + $e * .00157 * sin($md - $ms - $f) - .001487 * sin($f + $de);
$g = $g - $e * .001481 * sin($f + $ms + $md) + $e * .001417 * sin($f - $ms - $md);
$g = $g + $e * .00135 * sin($f - $ms) + .00133 * sin($f - $de);
$g = $g + .001106 * sin($f + 3 * $md) + .00102 * sin(4 * $de - $f);
$g = $g + .000833 * sin($f + 4 * $de - $md) + .000781 * sin($md - 3 * $f);
$g = $g + .00067 * sin($f + 4 * $de - 2 * $md);
$g = $g + .000606 * sin(2 * $de - 3 * $f) + .000597 * sin(2 * ($de + $md) - $f);
$g = $g + $e * .000492 * sin(2 * $de + $md - $ms - $f);
$g = $g + .00045 * sin(2 * ($md - $de) - $f) + .000439 * sin(3 * $md - $f);
$g = $g + .000423 * sin($f + 2 * ($de + $md)) + .000422 * sin(2 * $de - $f - 3 * $md);
$g = $g - $e * .000367 * sin($ms + $f + 2 * $de - $md);
$g = $g - $e * .000353 * sin($ms + $f + 2 * $de) + .000331 * sin($f + 4 * $de);
$g = $g + $e * .000317 * sin(2 * $de + $f - $ms + $md);
$g = $g + $e2 * .000306 * sin(2 * ($de - $ms) - $f) - .000283 * sin($md + 3 * $f);
$w1 = .0004664 * cos($n); $w2 = .0000754 * cos($c);
$bet = &rad($g) * (1 - $w1 - $w2);
$hp = .950724 + .051818 * cos($md) + .009531 * cos(2 * $de - $md);
$hp = $hp + .007843 * cos(2 * $de) + .002824 * cos(2 * $md);
$hp = $hp + .000857 * cos(2 * $de + $md) + $e * .000533 * cos(2 * $de - $ms);
$hp = $hp + $e * .000401 * cos(2 * $de - $md - $ms) + $e * .00032 * cos($md - $ms);
$hp = $hp - .000271 * cos($de) - $e * .000264 * cos($ms + $md);
$hp = $hp - .000198 * cos(2 * $f - $md);
$hp = $hp + .000173 * cos(3 * $md) + .000167 * cos(4 * $de - $md);
$hp = $hp - $e * .000111 * cos($ms) + .000103 * cos(4 * $de - 2 * $md);
$hp = $hp - .000084 * cos(2 * $md - 2 * $de) - $e * .000083 * cos(2 * $de + $ms);
$hp = $hp + .000079 * cos(2 * $de + 2 * $md) + .000072 * cos(4 * $de);
$hp = $hp + $e * .000064 * cos(2 * $de - $ms + $md);
$hp = $hp - $e * .000063 * cos(2 * $de + $ms - $md) + $e * .000041 * cos($ms + $de);
$hp = $hp + $e * .000035 * cos(2 * $md - $ms) - .000033 * cos(3 * $md - 2 * $de);
$hp = $hp - .00003 * cos($md + $de) - .000029 * cos(2 * ($f - $de));
$hp = $hp - $e * .000029 * cos(2 * $md + $ms) + $e2 * .000026 * cos(2 * ($de - $ms));
$hp = $hp - .000023 * cos(2 * ($f - $de) + $md);
$hp = $hp + $e * .000019 * cos(4 * $de - $ms - $md);
$hp = &rad($hp);
}

sub anomaly {
$tpi = 6.283185308;
$m = $ma - $tpi * int($ma / $tpi); $ea = $m;
$dla = $ea - ($SS * sin($ea)) - $m;
while (abs($dla) >= .000001) {
$dla = $dla / (1 - ($SS * cos($ea)));
$ea = $ea - $dla;
$dla = $ea - ($SS * sin($ea)) - $m; # set up for next time around
}

$tnu2 = sqrt((1 + $SS) / (1 - $SS)) * tan($ea / 2);
$nu = 2 * atan2($tnu2, 1);
}

sub tan {
$W = $_[0];
return sin($W)/cos($W);
}


sub asn {
$W = $_[0];
return atan2($W / (sqrt(1.000001 - $W * $W) + 1E-20), 1);
}

$source300 = moontextimage.cgi
This is moontextimage.cgi :- top
#!/usr/bin/perlml
use strict;
#use CGI qw(:standard);
use GD;
my %Degtype = (
"PosNeg" => "-",
"Degr" => 23,
"Min" => 45,
"Sec" => 23
);
my %HMSType = (
"PosNeg" => "+",
"Hour" => 12,
"Min" => 27,
"Sec" => 2
);
my %MoonStatus = (
"Longitude" => {%Degtype},
"Latitude" => {%Degtype},
"Rascend" => {%HMSType},
"Declination" => {%Degtype},
"HParallax" => {%Degtype},
"AngularDim" => {%Degtype},
"EarthMoon" => 12345,
"POTM" => 0.5
);
my @flag = (1 .. 7);
my ($xm, $xm1, $xs, $xs1, $S, $xd, $hp, $sw1, $sw3);
my ($dy, $mn, $yr, $lam, $dpsi, $bet, $p, $q, $x, $y, $f, $r, $thet, $djd);
my ($cd, $lsn, $sd, $i, $k, $md, $ms, $an, $mn1, $yr1, $a, $b, $c, $d);
my ($t, $eps, $epsr, $tpi, $deps, $eps1, $seps, $ceps, $cy, $ty, $sy);
my ($cx, $sx, $tx, $sq, $t2, $ls, $ld, $tls, $tld, $nm, $tnm, $m, $ma);
my ($ea, $SS, $dla, $tnu2, $nu, $a1, $b1, $d1, $e1, $c1, $h1, $dl, $dr, $rsn);
my ($m1, $m2, $m3, $m4, $m5, $m6, $de, $sa, $sb, $sn, $n, $e, $l, $sc, $e2);
my ($g, $w1, $w2, $xd1, $xp);
my ($im, , $im2, $white, $black, $red, $blue, $yellow);
my ($PHASE, $PHASE1, $PHASE2, $head, $dayte, $W ,$outfile ,$footnote);
my ($buff1, $buff2, $para, $fromangle, $toangle, $waxwan, $fillpos, $grey);
my $pi = 3.1415926535;
#if (!defined ($para = param('gifgif'))) {$para = 'textgif';}
print "Content-type: image/gif\n\n";
binmode STDOUT;
&CalcMoon; # this does all the work
&datadisplay; # this displays the text of the current moon attributes as an image (text doesn't work)
syswrite(STDOUT, $buff2);

sub CalcMoon {
($dy, $mn, $yr) = (localtime)[3,4,5];
$yr = $yr + 1900; $mn = $mn + 1;
&moon;
&nutat;
$x = &deg($lam) + $dpsi; $sw1 = 1;
&minsec;
$MoonStatus{"Longitude"}{"PosNeg"} = $S;
$MoonStatus{"Longitude"}{"Degr"} = $xd;
$MoonStatus{"Longitude"}{"Min"} = $xm;
$MoonStatus{"Longitude"}{"Sec"} = $xs;
$x = &deg($bet);
&minsec;
$MoonStatus{"Latitude"}{"PosNeg"} = $S;
$MoonStatus{"Latitude"}{"Degr"} = $xd;
$MoonStatus{"Latitude"}{"Min"} = $xm;
$MoonStatus{"Latitude"}{"Sec"} = $xs;
$x = $lam; $y = $bet; $sw3 = -1;
&eqecl;
$x = &deg($p / 15);
&minsec;
$MoonStatus{'Rascend'}{'PosNeg'} = $S;
$MoonStatus{'Rascend'}{'Hour'} = $xd;
$MoonStatus{'Rascend'}{'Min'} = $xm;
$MoonStatus{'Rascend'}{'Sec'} = $xs;
$x = &deg($q); &minsec;
$MoonStatus{'Declination'}{'PosNeg'} = $S;
$MoonStatus{'Declination'}{'Degr'} = $xd;
$MoonStatus{'Declination'}{'Min'} = $xm;
$MoonStatus{'Declination'}{'Sec'} = $xs;
$x = &deg($hp); &minsec;
$MoonStatus{'HParallax'}{'Degr'} = $xd;
$MoonStatus{'HParallax'}{'Min'} = $xm;
$MoonStatus{'HParallax'}{'Sec'} = $xs;
$r = 6378.14 / sin($hp);
$MoonStatus{'EarthMoon'} = $r;
$f = $r / 384401;
$thet = .5181 / $f;
$x = ($thet); &minsec;
$MoonStatus{'AngularDim'}{'Degr'} = $xd;
$MoonStatus{'AngularDim'}{'Min'} = $xm;
$MoonStatus{'AngularDim'}{'Sec'} = $xs;
&sun;
$cd = cos($lam - $lsn) * cos($bet);
$d = &acs($cd);
$sd = sin($d);
$i = (.1468 * $sd * (1 - .0549 * sin($md))) / (1 - .0167 * sin($ms));
$i = $pi - $d - &rad($i); $k = (1 + cos($i)) / 2;
$MoonStatus{'POTM'} = &rou($k, 1000);
$flag[1] = 0; $flag[5] = 0; $flag[6] = 0;
$flag[7] = 0;
}

sub datadisplay {
$im2 = new GD::Image(400,220);
$grey = $im2->colorAllocate(204,204,204); # background color
$white = $im2->colorAllocate(255,255,255);
$black = $im2->colorAllocate(0,0,0);
$red = $im2->colorAllocate(255,0,0);
$blue = $im2->colorAllocate(0,0,255);
$yellow = $im2->colorAllocate(255,255,0);
$a = "THE STATUS OF THE MOON FOR DAY $dy";
$b = "IN MONTH $mn OF THE YEAR $yr IS:-";
$c = "ATTRIBUTE DEG MIN SEC ";
$im2->GD::Image::string (gdLargeFont, 25, 10, $a, $black);
$im2->GD::Image::string (gdLargeFont, 25, 25, $b, $black);
$im2->GD::Image::string (gdLargeFont, 25, 45, $c, $black);
$a = $MoonStatus{'Longitude'}{'PosNeg'}; $b = $MoonStatus{'Longitude'}{'Degr'}; $c = $MoonStatus{'Longitude'}{'Min'}; $d = $MoonStatus{'Longitude'}{'Sec'};
$e = sprintf("Longitude $a %3d %3d %2.2f", $b, $c, $d);
$im2->GD::Image::string (gdLargeFont, 25, 65, $e, $black);
$a = $MoonStatus{'Latitude'}{'PosNeg'}; $b = $MoonStatus{'Latitude'}{'Degr'}; $c = $MoonStatus{'Latitude'}{'Min'}; $d = $MoonStatus{'Latitude'}{'Sec'};
$e = sprintf("Latitude $a %3d %3d %2.2f", $b, $c, $d);
$im2->GD::Image::string (gdLargeFont, 25, 85, $e, $black);
$a = $MoonStatus{'Rascend'}{'PosNeg'}; $b = $MoonStatus{'Rascend'}{'Hour'}; $c = $MoonStatus{'Rascend'}{'Min'}; $d = $MoonStatus{'Rascend'}{'Sec'};
$e = sprintf("Right Ascension $a %3d %3d %2.2f", $b, $c, $d);
$im2->GD::Image::string (gdLargeFont, 25, 105, $e, $black);
$a = $MoonStatus{'Declination'}{'PosNeg'}; $b = $MoonStatus{'Declination'}{'Degr'}; $c = $MoonStatus{'Declination'}{'Min'}; $d = $MoonStatus{'Declination'}{'Sec'};
$e = sprintf("Declination $a %3d %3d %2.2f", $b, $c, $d);
$im2->GD::Image::string (gdLargeFont, 25, 125, $e, $black);
$a = $MoonStatus{'HParallax'}{'Degr'}; $b = $MoonStatus{'HParallax'}{'Min'}; $c = $MoonStatus{'HParallax'}{'Sec'};
$e = sprintf("Horizontal Parallax %3d %3d %2.2f", $a, $b, $c);
$im2->GD::Image::string (gdLargeFont, 25, 145, $e, $black);
$a = $MoonStatus{'AngularDim'}{'Degr'}; $b = $MoonStatus{'AngularDim'}{'Min'}; $c = $MoonStatus{'AngularDim'}{'Sec'};
$e = sprintf("Angular Dimension %3d %3d %2.2f", $a, $b, $c);
$im2->GD::Image::string (gdLargeFont, 25, 165, $e, $black);
$a = $MoonStatus{'EarthMoon'};
$e = sprintf("Earth Moon Status %10.2f", $a);
$im2->GD::Image::string (gdLargeFont, 25, 185, $e, $black);
$a = $MoonStatus{'POTM'};
$e =sprintf("Phase of the moon %1.3f", $a);
$im2->GD::Image::string (gdLargeFont, 25, 205, $e, $black);
$buff2 = $im2->gif;
}

sub acs {
$W = $_[0];
return 1.570796327 - &asn($W);
}

sub anomaly {
$tpi = 6.283185308;
$m = $ma - $tpi * int($ma / $tpi); $ea = $m;
$dla = $ea - ($SS * sin($ea)) - $m;
while (abs($dla) >= .000001) {
$dla = $dla / (1 - ($SS * cos($ea)));
$ea = $ea - $dla;
$dla = $ea - ($SS * sin($ea)) - $m; # set up for next time around
}

$tnu2 = sqrt((1 + $SS) / (1 - $SS)) * tan($ea / 2);
$nu = 2 * atan2($tnu2, 1);
}

sub tan {
$W = $_[0];
return sin($W)/cos($W);
}

sub asn {
$W = $_[0];
return atan2($W / (sqrt(1.000001 - $W * $W) + 1E-20), 1);
}


sub deg {
$W = $_[0];
return 57.29577951 * $W;
}

sub eqecl {
if ($flag[7] != 1) {
$tpi = 2 * $pi; $flag[7] = 1;
if ($flag[6] == 0) {$deps = 0;}
&obliq;
$eps1 = &rad($eps + $deps);
$seps = sin($eps1); $ceps = cos($eps1);
}
$cy = cos($y); $sy = sin($y);
if (abs($cy) < 1E-20) {$cy = 1E-20;}
$ty = $sy / $cy; $cx = cos($x); $sx = sin($x);
$sq = ($sy * $ceps) - ($cy * $seps * $sx * $sw3);
$q = &asn($sq); $a = ($sx * $ceps) + ($ty * $seps * $sw3);
$p = atan2($a/$cx, 1);
if ($cx < 0) {$p = $p + $pi;}
if ($p > $tpi) {$p = $p - $tpi;}
if ($p < 0) {$p = $p + $tpi;}
}

sub sgn {
$W = $_[0];
if ($W<0) { return -1; }
if ($W==0) { return 0; }
if ($W>0) { return 1;}
}

sub itg {
$W = $_[0];
return int($W) + 1 * &sgn($W) * int((&sgn($W) - 1) / 2);
}

sub julday {
if ($flag[1] != 1) {
$mn1 = $mn; $yr1 = $yr; $flag[1] = 1; $b = 0;
if ($yr1 < 0) { $yr1 = $yr1 + 1;}
if ($mn < 3) { $mn1 = $mn + 12; $yr1 = $yr1 - 1;}
if (($yr >= 1582) || (($yr == 1582) && ($mn < 10)) || (($yr == 1582) && ($mn == 10) && ($dy < 15))) {
$a = int($yr1 / 100); $b = 2 - $a + int($a / 4);
}
if ($yr1 >= 0) {
$c = int(365.25 * $yr1) - 694025;
} else {
$c = &itg((365.25 * $yr1) - .75) - 694025;
}
$d = int(30.6001 * ($mn1 + 1));
$djd = $b + $c + $d + $dy - 0.5 ;
}}

sub minsec {
if ($sw1 == -1) {
$sn = 1;
if (($xd < 0) || ($xm < 0) || ($xs < 0)) { $sn = -1;}
$xd1 = abs($xd); $xm1 = abs($xm); $xs1 = abs($xs);
$x = (((($xs1 / 60) + $xm1) / 60) + $xd1) * $sn;
} else {
$sn = &sgn($x); $xp = abs($x); $xd = int($xp);
$a = ($xp - $xd) * 60; $xm = int($a);
$xs = int((($a - $xm) * 600) + .5) / 10;
if ($sn == -1) {$S = "-";} else {$S = "+";}
}
}

sub moon {
&julday;
$t = $djd / 36525; $t2 = $t * $t;
$m1 = 27.32158213; $m2 = 365.2596407;
$m3 = 27.55455094; $m4 = 29.5305886800001;
$m5 = 27.21222039; $m6 = 6798.36330700001;
$m1 = $djd / $m1; $m2 = $djd / $m2; $m3 = $djd / $m3;
$m4 = $djd / $m4; $m5 = $djd / $m5; $m6 = $djd / $m6;
$m1 = 360 * ($m1 - int($m1)); $m2 = 360 * ($m2 - int($m2));
$m3 = 360 * ($m3 - int($m3)); $m4 = 360 * ($m4 - int($m4));
$m5 = 360 * ($m5 - int($m5)); $m6 = 360 * ($m6 - int($m6));
$ld = 270.434164 + $m1 - (.001133 - .0000019 * $t) * $t2;
$ms = 358.475833 + $m2 - (.00015 + .0000033 * $t) * $t2;
$md = 296.104608 + $m3 + (.009192 + .0000144 * $t) * $t2;
$de = 350.737486 + $m4 - (.001436 - .0000019 * $t) * $t2;
$f = 11.250889 + $m5 - (.003211 + .0000003 * $t) * $t2;
$n = 259.183275 - $m6 + (.002078 + .000022 * $t) * $t2;
$a = &rad(51.2 + 20.2 * $t); $sa = sin($a);
$sn = sin(&rad($n));
$b = 346.56 + (132.87 - .0091731 * $t) * $t;
$sb = .003964 * sin(&rad($b));
$c = &rad($n + 275.05 - 2.3 * $t); $sc = sin($c);
$ld = $ld + .000233 * $sa + $sb + .001964 * $sn;
$ms = $ms - .001778 * $sa;
$md = $md + .000817 * $sa + $sb + .002541 * $sn;
$f = $f + $sb - .024691 * $sn - .004328 * $sc;
$de = $de + .002011 * $sa + $sb + .001964 * $sn;
$e = 1 - (.002495 + 7.52E-06 * $t) * $t; $e2 = $e * $e;
$ld = &rad($ld); $ms = &rad($ms); $n = &rad($n);
$de = &rad($de); $f = &rad($f); $md = &rad($md);
$l = 6.28875 * sin($md) + 1.27402 * sin(2 * $de - $md) + .658309 * sin(2 * $de);
$l = $l + .213616 * sin(2 * $md) - $e * .185596 * sin($ms) - .114336 * sin(2 * $f);
$l = $l + .058793 * sin(2 * ($de - $md)) + .057212 * $e * sin(2 * $de - $ms - $md);
$l = $l + .05332 * sin(2 * $de + $md) + .045874 * $e * sin(2 * $de - $ms);
$l = $l + .041024 * $e * sin($md - $ms);
$l = $l - .034718 * sin($de) - $e * .030465 * sin($ms + $md);
$l = $l + .015326 * sin(2 * ($de - $f)) - .012528 * sin(2 * $f + $md);
$l = $l - .01098 * sin(2 * $f - $md) + .010674 * sin(4 * $de - $md);
$l = $l + .010034 * sin(3 * $md) + .008548 * sin(4 * $de - 2 * $md);
$l = $l - $e * .00791 * sin($ms - $md + 2 * $de) - $e * .006783 * sin(2 * $de + $ms);
$l = $l + .005162 * sin($md - $de) + $e * .005 * sin($ms + $de);
$l = $l + .003862 * sin(4 * $de) + $e * .004049 * sin($md - $ms + 2 * $de);
$l = $l + .003996 * sin(2 * ($md + $de)) + .003665 * sin(2 * $de - 3 * $md);
$l = $l + $e * .002695 * sin(2 * $md - $ms) + .002602 * sin($md - 2 * ($f + $de));
$l = $l + $e * .002396 * sin(2 * ($de - $md) - $ms) - .002349 * sin($md + $de);
$l = $l + $e2 * .002249 * sin(2 * ($de - $ms)) - $e * .002125 * sin(2 * $md + $ms);
$l = $l - $e2 * .002079 * sin(2 * $ms) + $e2 * .002059 * sin(2 * ($de - $ms) - $md);
$l = $l - .001773 * sin($md + 2 * ($de - $f)) - .001595 * sin(2 * ($f + $de));
$l = $l + $e * .00122 * sin(4 * $de - $ms - $md) - .00111 * sin(2 * ($md + $f));
$l = $l + .000892 * sin($md - 3 * $de);
$l = $l - $e * .000811 * sin($ms + $md + 2 * $de);
$l = $l + $e * .000761 * sin(4 * $de - $ms - 2 * $md);
$l = $l + $e2 * .000704 * sin($md - 2 * ($ms + $de));
$l = $l + $e * .000693 * sin($ms - 2 * ($md - $de));
$l = $l + $e * .000598 * sin(2 * ($de - $f) - $ms) + .00055 * sin($md + 4 * $de);
$l = $l + .000538 * sin(4 * $md) + $e * .000521 * sin(4 * $de - $ms);
$l = $l + .000486 * sin(2 * $md - $de);
$l = $l + $e2 * .000717 * sin($md - 2 * $ms);
$lam = $ld + &rad($l); $tpi = 6.28318530800002;
while ($lam < 0) {
$lam = $lam + $tpi;
}
while ($lam > $tpi) {
$lam = $lam - $tpi;
}
$g = 5.12819 * sin($f) + .280606 * sin($md + $f) + .277693 * sin($md - $f);
$g = $g + .173238 * sin(2 * $de - $f) + .055413 * sin(2 * $de + $f - $md);
$g = $g + .046272 * sin(2 * $de - $f - $md) + .032573 * sin(2 * $de + $f);
$g = $g + .017198 * sin(2 * $md + $f) + .009267 * sin(2 * $de + $md - $f);
$g = $g + .008823 * sin(2 * $md - $f) + $e * .008247 * sin(2 * $de - $ms - $f);
$g = $g + .004323 * sin(2 * ($de - $md) - $f) + .0042 * sin(2 * $de + $f + $md);
$g = $g + $e * .003372 * sin($f - $ms - 2 * $de);
$g = $g + $e * .002472 * sin(2 * $de + $f - $ms - $md);
$g = $g + $e * .002222 * sin(2 * $de + $f - $ms);
$g = $g + $e * .002072 * sin(2 * $de - $f - $ms - $md);
$g = $g + $e * .001877 * sin($f - $ms + $md) + .001828 * sin(4 * $de - $f - $md);
$g = $g - $e * .001803 * sin($f + $ms) - .00175 * sin(3 * $f);
$g = $g + $e * .00157 * sin($md - $ms - $f) - .001487 * sin($f + $de);
$g = $g - $e * .001481 * sin($f + $ms + $md) + $e * .001417 * sin($f - $ms - $md);
$g = $g + $e * .00135 * sin($f - $ms) + .00133 * sin($f - $de);
$g = $g + .001106 * sin($f + 3 * $md) + .00102 * sin(4 * $de - $f);
$g = $g + .000833 * sin($f + 4 * $de - $md) + .000781 * sin($md - 3 * $f);
$g = $g + .00067 * sin($f + 4 * $de - 2 * $md);
$g = $g + .000606 * sin(2 * $de - 3 * $f) + .000597 * sin(2 * ($de + $md) - $f);
$g = $g + $e * .000492 * sin(2 * $de + $md - $ms - $f);
$g = $g + .00045 * sin(2 * ($md - $de) - $f) + .000439 * sin(3 * $md - $f);
$g = $g + .000423 * sin($f + 2 * ($de + $md)) + .000422 * sin(2 * $de - $f - 3 * $md);
$g = $g - $e * .000367 * sin($ms + $f + 2 * $de - $md);
$g = $g - $e * .000353 * sin($ms + $f + 2 * $de) + .000331 * sin($f + 4 * $de);
$g = $g + $e * .000317 * sin(2 * $de + $f - $ms + $md);
$g = $g + $e2 * .000306 * sin(2 * ($de - $ms) - $f) - .000283 * sin($md + 3 * $f);
$w1 = .0004664 * cos($n); $w2 = .0000754 * cos($c);
$bet = &rad($g) * (1 - $w1 - $w2);
$hp = .950724 + .051818 * cos($md) + .009531 * cos(2 * $de - $md);
$hp = $hp + .007843 * cos(2 * $de) + .002824 * cos(2 * $md);
$hp = $hp + .000857 * cos(2 * $de + $md) + $e * .000533 * cos(2 * $de - $ms);
$hp = $hp + $e * .000401 * cos(2 * $de - $md - $ms) + $e * .00032 * cos($md - $ms);
$hp = $hp - .000271 * cos($de) - $e * .000264 * cos($ms + $md);
$hp = $hp - .000198 * cos(2 * $f - $md);
$hp = $hp + .000173 * cos(3 * $md) + .000167 * cos(4 * $de - $md);
$hp = $hp - $e * .000111 * cos($ms) + .000103 * cos(4 * $de - 2 * $md);
$hp = $hp - .000084 * cos(2 * $md - 2 * $de) - $e * .000083 * cos(2 * $de + $ms);
$hp = $hp + .000079 * cos(2 * $de + 2 * $md) + .000072 * cos(4 * $de);
$hp = $hp + $e * .000064 * cos(2 * $de - $ms + $md);
$hp = $hp - $e * .000063 * cos(2 * $de + $ms - $md) + $e * .000041 * cos($ms + $de);
$hp = $hp + $e * .000035 * cos(2 * $md - $ms) - .000033 * cos(3 * $md - 2 * $de);
$hp = $hp - .00003 * cos($md + $de) - .000029 * cos(2 * ($f - $de));
$hp = $hp - $e * .000029 * cos(2 * $md + $ms) + $e2 * .000026 * cos(2 * ($de - $ms));
$hp = $hp - .000023 * cos(2 * ($f - $de) + $md);
$hp = $hp + $e * .000019 * cos(4 * $de - $ms - $md);
$hp = &rad($hp);
}

sub nutat {
if ($flag[6] != 1) {
$flag[6] = 1; $flag[7] = 0;
&julday;
$t = $djd / 36525; $t2 = $t * $t;
$a = 100.0021358 * $t; $b = 360 * ($a - int($a));
$ls = 279.697 + .000303 * $t2 + $b;
$a = 1336.855231 * $t; $b = 360 * ($a - int($a));
$ld = 270.434 - .001133 * $t2 + $b;
$a = 99.9973605600003 * $t; $b = 360 * ($a - int($a));
$ms = 358.476 - .00015 * $t2 + $b;
$a = 13255523.59 * $t; $b = 360 * ($a - int($a));
$md = 296.105 + .009192 * $t2 + $b;
$a = 5.372616667 * $t; $b = 360 * ($a - int($a));
$nm = 259.183 + .002078 * $t2 - $b;
$tls = 2 * &rad($ls); $nm = &rad($nm);
$tnm = 2 * &rad($nm); $ms = &rad($ms);
$tld = 2 * &rad($ld); $md = &rad($md);
$dpsi = (-17.2327 - .01737 * $t) * sin($nm);
$dpsi = $dpsi + (-1.2729 - .00013 * $t) * sin($tls) + .2088 * sin($tnm);
$dpsi = $dpsi - .2037 * sin($tld) + (.1261 - .00031 * $t) * sin($ms);
$dpsi = $dpsi + .0675 * sin($md) - (.0497 - .00012 * $t) * sin($tls + $ms);
$dpsi = $dpsi - .0342 * sin($tld - $nm) - .0261 * sin($tld + $md);
$dpsi = $dpsi + .0214 * sin($tls - $ms) - .0149 * sin($tls - $tld + $md);
$dpsi = $dpsi + .0124 * sin($tls - $nm) + .0114 * sin($tld - $md);
$deps = (9.21 + .00091 * $t) * cos($nm) + (.5522 - .00029 * $t) * cos($tls);
$deps = $deps - .0904 * cos($tnm) + .0884 * cos($tld) + .0216 * cos($tls + $ms);
$deps = $deps + .0183 * cos($tld - $nm) + .0113 * cos($tld + $md);
$deps = $deps - .0093 * cos($tls - $ms) - .0066 * cos($tls - $nm);
$dpsi = $dpsi / 3600; $deps = $deps / 3600;
}}

sub obliq {
if ($flag[5] != 1) {
$flag[5] = 1;
&julday;
$t = $djd / 36525;
$c = (((-.00181 * $t) + .0059) * $t + 46.845) * $t;
$eps = 23.45229444 - ($c / 3600);
$epsr = $eps * .01745329252;
}}

sub rad {
$W = $_[0];
return .01745329252 * $W;
}

sub rou {
$x = $_[0];
$y = $_[1];
return int($x * $y + .5) / $y;
}

sub sun {
&julday;
$t = $djd / 36525; $t2 = $t * $t;
$a = 100.0021359 * $t; $b = 360 * ($a - int($a));
$ls = 279.69668 + (.0003025 * $t2) + $b;
$a = 99.9973604200004 * $t; $b = 360 * ($a - int($a));
$ms = 358.47583 - ((.00015 + (.0000033 * $t)) * $t2) + $b;
$SS = .016751 - (.0000418 * $t) - (1.26E-07 * $t2);
$ma = &rad($ms);
&anomaly;
$a = 62.5520947200002 * $t; $b = 360 * ($a - int($a));
$a1 = &rad(153.23 + $b);
$a = 125.1041894 * $t; $b = 360 * ($a - int($a));
$b1 = &rad(216.57 + $b);
$a = 91.56766028 * $t; $b = 360 * ($a = int($a));
$c1 = &rad(312.69 + $b);
$a = 1236.853095 * $t; $b = 360 * ($a - int($a));
$d1 = &rad(350.74 - .00144 * $t2 + $b);
$e1 = &rad(231.19 + 20.2 * $t);
$a = 183.1353208 * $t; $b = 360 * ($a - int($a));
$h1 = &rad(353.4 + $b);
$dl = .00134 * cos($a1) + .00154 * cos($b1) + .002 * cos($c1);
$dl = $dl + .00179 * sin($d1) + .00178 * sin($e1);
$dr = 5.43E-06 * sin($a1) + 1.575E-05 * sin($b1) + 1.627E-05 * sin($c1);
$dr = $dr + 3.076E-05 * cos($d1) + 9.27E-06 * sin($h1);
$lsn = $nu + &rad($ls - $ms + $dl); $tpi = 6.28318530800002;
$rsn = 1.0000002 * (1 - $SS * cos($ea)) + $dr;
while ($lsn < 0) {
$lsn = $lsn + $tpi;}
while ($lsn > $tpi) {
$lsn = $lsn - $tpi;}
}

$source400 = ../lib/Astro/MoonPhase.pm
This is ../lib/Astro/MoonPhase.pm :- top
package Astro::MoonPhase;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(phase phasehunt phaselist);
$VERSION = '0.60';

use vars qw (
$Epoch
$Elonge $Elongp $Eccent $Sunsmax $Sunangsiz
$Mmlong $Mmlongp $Mlnode $Minc $Mecc $Mangsiz $Msmax $Mparallax $Synmonth
$Pi
);

# Astronomical constants.

$Epoch = 2444238.5; # 1980 January 0.0

# Constants defining the Sun's apparent orbit.

$Elonge = 278.833540; # ecliptic longitude of the Sun at epoch 1980.0
$Elongp = 282.596403; # ecliptic longitude of the Sun at perigee
$Eccent = 0.016718; # eccentricity of Earth's orbit
$Sunsmax = 1.495985e8; # semi-major axis of Earth's orbit, km
$Sunangsiz = 0.533128; # sun's angular size, degrees, at semi-major axis distance

# Elements of the Moon's orbit, epoch 1980.0.

$Mmlong = 64.975464; # moon's mean longitude at the epoch
$Mmlongp = 349.383063; # mean longitude of the perigee at the epoch
$Mlnode = 151.950429; # mean longitude of the node at the epoch
$Minc = 5.145396; # inclination of the Moon's orbit
$Mecc = 0.054900; # eccentricity of the Moon's orbit
$Mangsiz = 0.5181; # moon's angular size at distance a from Earth
$Msmax = 384401.0; # semi-major axis of Moon's orbit in km
$Mparallax = 0.9507; # parallax at distance a from Earth
$Synmonth = 29.53058868; # synodic month (new Moon to new Moon)

# Properties of the Earth.

$Pi = 3.14159265358979323846; # assume not near black hole nor in Tennessee

# Handy mathematical functions.

sub sgn { return (($_[0] < 0) ? -1 : ($_[0] > 0 ? 1 : 0)); } # extract sign
sub fixangle { return ($_[0] - 360.0 * (floor($_[0] / 360.0))); } # fix angle
sub torad { return ($_[0] * ($Pi / 180.0)); } # deg->rad
sub todeg { return ($_[0] * (180.0 / $Pi)); } # rad->deg
sub dsin { return (sin(torad($_[0]))); } # sin from deg
sub dcos { return (cos(torad($_[0]))); } # cos from deg

sub tan { return sin($_[0])/cos($_[0]); }
sub asin { return ($_[0]<-1 or $_[0]>1) ? undef : atan2($_[0],sqrt(1-$_[0]*$_[0])); }
sub atan {
if ($_[0]==0) { return 0; }
elsif ($_[0]>0) { return atan2(sqrt(1+$_[0]*$_[0]),sqrt(1+1/($_[0]*$_[0]))); }
else { return -atan2(sqrt(1+$_[0]*$_[0]),sqrt(1+1/($_[0]*$_[0]))); }
}

sub floor {
my $val = shift;
my $neg = $val < 0;
my $asint = int($val);
my $exact = $val == $asint;

return ($exact ? $asint : $neg ? $asint - 1 : $asint);
}



# jtime - convert internal date and time to astronomical Julian
# time (i.e. Julian date plus day fraction)


sub jtime {

my $t = shift;
my ($julian);
$julian = ($t / 86400) + 2440587.5; # (seconds /(seconds per day)) + julian date of epoch
return ($julian);

}


# jdaytosecs - convert Julian date to a UNIX epoch

sub jdaytosecs {
my $jday = shift;
my $stamp;

$stamp = ($jday - 2440587.5)*86400; # (juliandate - jdate of unix epoch)*(seconds per julian day)
return($stamp);

}



# jyear - convert Julian date to year, month, day, which are
# returned via integer pointers to integers
sub jyear {

my ($td, $yy, $mm, $dd) = @_;
my ($z, $f, $a, $alpha, $b, $c, $d, $e);

$td += 0.5; # astronomical to civil
$z = floor($td);
$f = $td - $z;

if ($z < 2299161.0) {
$a = $z;
} else {
$alpha = floor(($z - 1867216.25) / 36524.25);
$a = $z + 1 + $alpha - floor($alpha / 4);
}


$b = $a + 1524;
$c = floor(($b - 122.1) / 365.25);
$d = floor(365.25 * $c);
$e = floor(($b - $d) / 30.6001);

$$dd = $b - $d - floor(30.6001 * $e) + $f;
$$mm = $e < 14 ? $e - 1 : $e - 13;
$$yy = $$mm > 2 ? $c - 4716 : $c - 4715;

}

## meanphase -- Calculates time of the mean new Moon for a given
## base date. This argument K to this function is the
## precomputed synodic month index, given by:
##
## K = (year - 1900) * 12.3685
##
## where year is expressed as a year and fractional year.


sub meanphase {
my ($sdate, $k) = @_;
my ($t, $t2, $t3, $nt1);

## Time in Julian centuries from 1900 January 0.5
$t = ($sdate - 2415020.0) / 36525;
$t2 = $t * $t; ## Square for frequent use
$t3 = $t2 * $t; ## Cube for frequent use

$nt1 = 2415020.75933 + $Synmonth * $k
+ 0.0001178 * $t2
- 0.000000155 * $t3
+ 0.00033 * dsin(166.56 + 132.87 * $t - 0.009173 * $t2);

return ($nt1);
}


# truephase - given a K value used to determine the mean phase of the
# new moon, and a phase selector (0.0, 0.25, 0.5, 0.75),
# obtain the true, corrected phase time

sub truephase {
my ($k, $phase) = @_;
my ($t, $t2, $t3, $pt, $m, $mprime, $f);
my $apcor = 0;

$k += $phase; # add phase to new moon time
$t = $k / 1236.85; # time in Julian centuries from
# 1900 January 0.5
$t2 = $t * $t; # square for frequent use
$t3 = $t2 * $t; # cube for frequent use

# mean time of phase */
$pt = 2415020.75933
+ $Synmonth * $k
+ 0.0001178 * $t2
- 0.000000155 * $t3
+ 0.00033 * dsin(166.56 + 132.87 * $t - 0.009173 * $t2);

# Sun's mean anomaly
$m = 359.2242
+ 29.10535608 * $k
- 0.0000333 * $t2
- 0.00000347 * $t3;

# Moon's mean anomaly
$mprime = 306.0253
+ 385.81691806 * $k
+ 0.0107306 * $t2
+ 0.00001236 * $t3;

# Moon's argument of latitude
$f = 21.2964
+ 390.67050646 * $k
- 0.0016528 * $t2
- 0.00000239 * $t3;

if (($phase < 0.01) || (abs($phase - 0.5) < 0.01)) {
# Corrections for New and Full Moon.

$pt += (0.1734 - 0.000393 * $t) * dsin($m)
+ 0.0021 * dsin(2 * $m)
- 0.4068 * dsin($mprime)
+ 0.0161 * dsin(2 * $mprime)
- 0.0004 * dsin(3 * $mprime)
+ 0.0104 * dsin(2 * $f)
- 0.0051 * dsin($m + $mprime)
- 0.0074 * dsin($m - $mprime)
+ 0.0004 * dsin(2 * $f + $m)
- 0.0004 * dsin(2 * $f - $m)
- 0.0006 * dsin(2 * $f + $mprime)
+ 0.0010 * dsin(2 * $f - $mprime)
+ 0.0005 * dsin($m + 2 * $mprime);
$apcor = 1;
}
elsif ((abs($phase - 0.25) < 0.01 || (abs($phase - 0.75) < 0.01))) {
$pt += (0.1721 - 0.0004 * $t) * dsin($m)
+ 0.0021 * dsin(2 * $m)
- 0.6280 * dsin($mprime)
+ 0.0089 * dsin(2 * $mprime)
- 0.0004 * dsin(3 * $mprime)
+ 0.0079 * dsin(2 * $f)
- 0.0119 * dsin($m + $mprime)
- 0.0047 * dsin($m - $mprime)
+ 0.0003 * dsin(2 * $f + $m)
- 0.0004 * dsin(2 * $f - $m)
- 0.0006 * dsin(2 * $f + $mprime)
+ 0.0021 * dsin(2 * $f - $mprime)
+ 0.0003 * dsin($m + 2 * $mprime)
+ 0.0004 * dsin($m - 2 * $mprime)
- 0.0003 * dsin(2 * $m + $mprime);
if ($phase < 0.5) {
# First quarter correction.
$pt += 0.0028 - 0.0004 * dcos($m) + 0.0003 * dcos($mprime);
}
else {
# Last quarter correction.
$pt += -0.0028 + 0.0004 * dcos($m) - 0.0003 * dcos($mprime);
}
$apcor = 1;
}
if (!$apcor) {
die "truephase() called with invalid phase selector ($phase).\n";
}
return ($pt);
}

# phasehunt - find time of phases of the moon which surround the current
# date. Five phases are found, starting and ending with the
# new moons which bound the current lunation

sub phasehunt {
my $sdate = jtime(shift || time());
my ($adate, $k1, $k2, $nt1, $nt2);
my ($yy, $mm, $dd);

$adate = $sdate - 45;

jyear($adate, \$yy, \$mm, \$dd);
$k1 = floor(($yy + (($mm - 1) * (1.0 / 12.0)) - 1900) * 12.3685);

$adate = $nt1 = meanphase($adate, $k1);

while (1) {
$adate += $Synmonth;
$k2 = $k1 + 1;
$nt2 = meanphase($adate, $k2);
if (($nt1 <= $sdate) && ($nt2 > $sdate)) {
last;
}
$nt1 = $nt2;
$k1 = $k2;

}



return (
jdaytosecs(truephase($k1, 0.0)),
jdaytosecs(truephase($k1, 0.25)),
jdaytosecs(truephase($k1, 0.5)),
jdaytosecs(truephase($k1, 0.75)),
jdaytosecs(truephase($k2, 0.0))
);
}



# phaselist - find time of phases of the moon between two dates
# times (in & out) are seconds_since_1970

sub phaselist
{
my ($sdate, $edate) = map { jtime($_) } @_;

my (@phases, $d, $k, $yy, $mm);

jyear($sdate, \$yy, \$mm, \$d);
$k = floor(($yy + (($mm - 1) * (1.0 / 12.0)) - 1900) * 12.3685) - 2;

while (1) {
++$k;
for my $phase (0.0, 0.25, 0.5, 0.75) {
$d = truephase($k, $phase);

return @phases if $d >= $edate;

if ($d >= $sdate) {
push @phases, int(4 * $phase) unless @phases;
push @phases, jdaytosecs($d);
} # end if date should be listed
} # end for each $phase
} # end while 1
} # end phaselist



# kepler - solve the equation of Kepler

sub kepler {
my ($m, $ecc) = @_;
my ($e, $delta);
my $EPSILON = 1e-6;

$m = torad($m);
$e = $m;
do {
$delta = $e - $ecc * sin($e) - $m;
$e -= $delta / (1 - $ecc * cos($e));
} while (abs($delta) > $EPSILON);
return ($e);
}



# phase - calculate phase of moon as a fraction:
#
# The argument is the time for which the phase is requested,
# expressed as a Julian date and fraction. Returns the terminator
# phase angle as a percentage of a full circle (i.e., 0 to 1),
# and stores into pointer arguments the illuminated fraction of
# the Moon's disc, the Moon's age in days and fraction, the
# distance of the Moon from the centre of the Earth, and the
# angular diameter subtended by the Moon as seen by an observer
# at the centre of the Earth.

sub phase {
my $pdate = jtime(shift || time());

my $pphase; # illuminated fraction
my $mage; # age of moon in days
my $dist; # distance in kilometres
my $angdia; # angular diameter in degrees
my $sudist; # distance to Sun
my $suangdia; # sun's angular diameter

my ($Day, $N, $M, $Ec, $Lambdasun, $ml, $MM, $MN, $Ev, $Ae, $A3, $MmP,
$mEc, $A4, $lP, $V, $lPP, $NP, $y, $x, $Lambdamoon, $BetaM,
$MoonAge, $MoonPhase,
$MoonDist, $MoonDFrac, $MoonAng, $MoonPar,
$F, $SunDist, $SunAng,
$mpfrac);

# Calculation of the Sun's position.

$Day = $pdate - $Epoch; # date within epoch
$N = fixangle((360 / 365.2422) * $Day); # mean anomaly of the Sun
$M = fixangle($N + $Elonge - $Elongp); # convert from perigee
# co-ordinates to epoch 1980.0
$Ec = kepler($M, $Eccent); # solve equation of Kepler
$Ec = sqrt((1 + $Eccent) / (1 - $Eccent)) * tan($Ec / 2);
$Ec = 2 * todeg(atan($Ec)); # true anomaly
$Lambdasun = fixangle($Ec + $Elongp); # Sun's geocentric ecliptic
# longitude
# Orbital distance factor.
$F = ((1 + $Eccent * cos(torad($Ec))) / (1 - $Eccent * $Eccent));
$SunDist = $Sunsmax / $F; # distance to Sun in km
$SunAng = $F * $Sunangsiz; # Sun's angular size in degrees


# Calculation of the Moon's position.

# Moon's mean longitude.
$ml = fixangle(13.1763966 * $Day + $Mmlong);

# Moon's mean anomaly.
$MM = fixangle($ml - 0.1114041 * $Day - $Mmlongp);

# Moon's ascending node mean longitude.
$MN = fixangle($Mlnode - 0.0529539 * $Day);

# Evection.
$Ev = 1.2739 * sin(torad(2 * ($ml - $Lambdasun) - $MM));

# Annual equation.
$Ae = 0.1858 * sin(torad($M));

# Correction term.
$A3 = 0.37 * sin(torad($M));

# Corrected anomaly.
$MmP = $MM + $Ev - $Ae - $A3;

# Correction for the equation of the centre.
$mEc = 6.2886 * sin(torad($MmP));

# Another correction term.
$A4 = 0.214 * sin(torad(2 * $MmP));

# Corrected longitude.
$lP = $ml + $Ev + $mEc - $Ae + $A4;

# Variation.
$V = 0.6583 * sin(torad(2 * ($lP - $Lambdasun)));

# True longitude.
$lPP = $lP + $V;

# Corrected longitude of the node.
$NP = $MN - 0.16 * sin(torad($M));

# Y inclination coordinate.
$y = sin(torad($lPP - $NP)) * cos(torad($Minc));

# X inclination coordinate.
$x = cos(torad($lPP - $NP));

# Ecliptic longitude.
$Lambdamoon = todeg(atan2($y, $x));
$Lambdamoon += $NP;

# Ecliptic latitude.
$BetaM = todeg(asin(sin(torad($lPP - $NP)) * sin(torad($Minc))));

# Calculation of the phase of the Moon.

# Age of the Moon in degrees.
$MoonAge = $lPP - $Lambdasun;

# Phase of the Moon.
$MoonPhase = (1 - cos(torad($MoonAge))) / 2;

# Calculate distance of moon from the centre of the Earth.

$MoonDist = ($Msmax * (1 - $Mecc * $Mecc)) /
(1 + $Mecc * cos(torad($MmP + $mEc)));

# Calculate Moon's angular diameter.

$MoonDFrac = $MoonDist / $Msmax;
$MoonAng = $Mangsiz / $MoonDFrac;

# Calculate Moon's parallax.

$MoonPar = $Mparallax / $MoonDFrac;

$pphase = $MoonPhase;
$mage = $Synmonth * (fixangle($MoonAge) / 360.0);
$dist = $MoonDist;
$angdia = $MoonAng;
$sudist = $SunDist;
$suangdia = $SunAng;
$mpfrac = fixangle($MoonAge) / 360.0;
return wantarray ? ( $mpfrac, $pphase, $mage, $dist, $angdia, $sudist,$suangdia ) : $mpfrac;
}

1;
__END__

=head1 NAME

Astro::MoonPhase - Information about the phase of the Moon

=head1 SYNOPSIS

use Astro::MoonPhase;

( $MoonPhase,
$MoonIllum,
$MoonAge,
$MoonDist,
$MoonAng,
$SunDist,
$SunAng ) = phase($seconds_since_1970);

@phases = phasehunt($seconds_since_1970);

($phase, @times) = phaselist($start, $stop);

=head1 DESCRIPTION

MoonPhase calculates information about the phase of the moon
at a given time.

=head1 FUNCTIONS

=head2 phase()

( $MoonPhase,
$MoonIllum,
$MoonAge,
$MoonDist,
$MoonAng,
$SunDist,
$SunAng ) = phase($seconds_since_1970);

$MoonPhase = phase($seconds_since_1970);

The argument is the time for which the phase is requested,
expressed as a time returned by the C<time> function. If C<$seconds_since_1970>
is omitted, it does C<phase(time)>.

Return value in scalar context is $MoonPhase,
the terminator phase angle as a percentage of a full circle (i.e., 0 to 1).

=over 4

=item B<Return values in array context:>

=item $MoonPhase:

the terminator phase angle as a percentage of a full circle (i.e., 0 to 1)

=item $MoonIllum:

the illuminated fraction of the Moon's disc

=item $MoonAge:

the Moon's age in days and fraction

=item $MoonDist:

the distance of the Moon from the centre of the Earth

=item $MoonAng:

the angular diameter subtended by the Moon as seen by
an observer at the centre of the Earth.

=item $SunDist:

the distance from the Sun in km

=item $SunAng:

the angular size of Sun in degrees

=back

Example:

( $MoonPhase,
$MoonIllum,
$MoonAge,
$MoonDist,
$MoonAng,
$SunDist,
$SunAng ) = phase();

print "MoonPhase = $MoonPhase\n";
print "MoonIllum = $MoonIllum\n";
print "MoonAge = $MoonAge\n";
print "MoonDist = $MoonDist\n";
print "MoonAng = $MoonAng\n";
print "SunDist = $SunDist\n";
print "SunAng = $SunAng\n";

could print something like this:

MoonPhase = 0.598939375319023
MoonIllum = 0.906458030827876
MoonAge = 17.6870323368022
MoonDist = 372479.357420033
MoonAng = 0.534682403555093
SunDist = 152078368.820205
SunAng = 0.524434538105092

=head2 phasehunt()

@phases = phasehunt($seconds_since_1970);

Finds time of phases of the moon which surround the given
date. Five phases are found, starting and ending with the
new moons which bound the current lunation.

The argument is the time, expressed as a time returned
by the C<time> function. If C<$seconds_since_1970>
is omitted, it does C<phasehunt(time)>.

Example:

@phases = phasehunt();
print "New Moon = ", scalar(localtime($phases[0])), "\n";
print "First quarter = ", scalar(localtime($phases[1])), "\n";
print "Full moon = ", scalar(localtime($phases[2])), "\n";
print "Last quarter = ", scalar(localtime($phases[3])), "\n";
print "New Moon = ", scalar(localtime($phases[4])), "\n";

could print something like this:

New Moon = Wed Jun 24 06:51:47 1998
First quarter = Wed Jul 1 21:42:19 1998
Full moon = Thu Jul 9 19:02:47 1998
Last quarter = Thu Jul 16 18:15:18 1998
New Moon = Thu Jul 23 16:45:01 1998

=head2 phaselist()

($phase, @times) = phaselist($start, $stop);

Finds times of all phases of the moon which occur on or after
C<$start> but before C<$stop>. Both the arguments and the return
values are expressed as seconds since 1970 (like the C<time> function
returns).

C<$phase> is an integer indicating the phase of the moon at
C<$times[0]>, as shown in this table:

0 New Moon
1 First quarter
2 Full Moon
3 Last quarter

The remaining values in C<@times> indicate subsequent phases of the
moon (in ascending order by time). If there are no phases of the moon
between C<$start> and C<$stop>, C<phaselist> returns the empty list.

Example:

@name = ("New Moon", "First quarter", "Full moon", "Last quarter");
($phase, @times) = phaselist($start, $stop);

while (@times) {
printf "%-14s= %s\n", $name[$phase], scalar localtime shift @times;
$phase = ($phase + 1) % 4;
}

could produce the same output as the C<phasehunt> example above (given
the appropriate start & stop times).

=head1 ABOUT THE ALGORITHMS

The algorithms used in this program to calculate the positions of Sun and
Moon as seen from the Earth are given in the book I<Practical Astronomy
With Your Calculator> by B<Peter Duffett-Smith, Second Edition,
Cambridge University Press, 1981>. Ignore the word "Calculator" in the
title; this is an essential reference if you're interested in
developing software which calculates planetary positions, orbits,
eclipses, and the like. If you're interested in pursuing such
programming, you should also obtain:

I<Astronomical Formulae for Calculators> by B<Jean Meeus, Third Edition,
Willmann-Bell, 1985>. A must-have.

I<Planetary Programs and Tables from -4000 to +2800> by B<Pierre
Bretagnon and Jean-Louis Simon, Willmann-Bell, 1986>. If you want the
utmost (outside of JPL) accuracy for the planets, it's here.

I<Celestial BASIC> by B<Eric Burgess, Revised Edition, Sybex, 1985>. Very
cookbook oriented, and many of the algorithms are hard to dig out of
the turgid BASIC code, but you'll probably want it anyway.

Many of these references can be obtained from Willmann-Bell, P.O. Box
35025, Richmond, VA 23235, USA. Phone: (804) 320-7016. In addition
to their own publications, they stock most of the standard references
for mathematical and positional astronomy.

=head1 LICENCE

This program is in the public domain: "Do what thou wilt shall be the
whole of the law".

=head1 AUTHORS

The moontool.c Release 2.0:

A Moon for the Sun
Designed and implemented by John Walker in December 1987,
revised and updated in February of 1988.

Initial Perl transcription:

Raino Pikkarainen, 1998
raino.pikkarainen@saunalahti.fi

The moontool.c Release 2.4:

Major enhancements by Ron Hitchens, 1989

Revisions:

Brett Hamilton http://simple.be/
Bug fix, 2003
Second transcription and bugfixes, 2004

Christopher J. Madsen http://www.cjmweb.net/
Added phaselist function, March 2007
$source500 = ../lib/Number/Format.pm
This is ../lib/Number/Format.pm :- top
package Number::Format;

# Minimum version is 5.8.0. May work on earlier versions, but not
# supported on any version older than 5.8.
require 5.008;

use strict;
use warnings;

=head1 NAME

Number::Format - Perl extension for formatting numbers

=head1 SYNOPSIS

use Number::Format;
my $x = new Number::Format %args;
$formatted = $x->round($number, $precision);
$formatted = $x->format_number($number, $precision, $trailing_zeroes);
$formatted = $x->format_negative($number, $picture);
$formatted = $x->format_picture($number, $picture);
$formatted = $x->format_price($number, $precision, $symbol);
$formatted = $x->format_bytes($number, $precision);
$number = $x->unformat_number($formatted);

use Number::Format qw(:subs);
$formatted = round($number, $precision);
$formatted = format_number($number, $precision, $trailing_zeroes);
$formatted = format_negative($number, $picture);
$formatted = format_picture($number, $picture);
$formatted = format_price($number, $precision, $symbol);
$formatted = format_bytes($number, $precision);
$number = unformat_number($formatted);

=head1 REQUIRES

Perl, version 5.8 or higher.

POSIX.pm to determine locale settings.

Carp.pm is used for some error reporting.

=head1 DESCRIPTION

These functions provide an easy means of formatting numbers in a
manner suitable for displaying to the user.

There are two ways to use this package. One is to declare an object
of type Number::Format, which you can think of as a formatting engine.
The various functions defined here are provided as object methods.
The constructor C<new()> can be used to set the parameters of the
formatting engine. Valid parameters are:

THOUSANDS_SEP - character inserted between groups of 3 digits
DECIMAL_POINT - character separating integer and fractional parts
MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price
MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price
INT_CURR_SYMBOL - character(s) denoting currency (see format_price())
DECIMAL_DIGITS - number of digits to the right of dec point (def 2)
DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal
NEG_FORMAT - format to display negative numbers (def ``-x'')
KILO_SUFFIX - suffix to add when format_bytes formats kilobytes (trad)
MEGA_SUFFIX - " " " " " " megabytes (trad)
GIGA_SUFFIX - " " " " " " gigabytes (trad)
KIBI_SUFFIX - suffix to add when format_bytes formats kibibytes (iec)
MEBI_SUFFIX - " " " " " " mebibytes (iec)
GIBI_SUFFIX - " " " " " " gibibytes (iec)

They may be specified in upper or lower case, with or without a
leading hyphen ( - ).

If C<THOUSANDS_SEP> is set to the empty string, format_number will not
insert any separators.

The defaults for C<THOUSANDS_SEP>, C<DECIMAL_POINT>,
C<MON_THOUSANDS_SEP>, C<MON_DECIMAL_POINT>, and C<INT_CURR_SYMBOL>
come from the POSIX locale information (see L<perllocale>). If your
POSIX locale does not provide C<MON_THOUSANDS_SEP> and/or
C<MON_DECIMAL_POINT> fields, then the C<THOUSANDS_SEP> and/or
C<DECIMAL_POINT> values are used for those parameters. Formerly,
POSIX was optional but this caused problems in some cases, so it is
now required. If this causes you hardship, please contact the author
of this package at <SwPrAwM@cpan.org> (remove "SPAM" to get correct
email address) for help.

If any of the above parameters are not specified when you invoke
C<new()>, then the values are taken from package global variables of
the same name (e.g. C<$DECIMAL_POINT> is the default for the
C<DECIMAL_POINT> parameter). If you use the C<:vars> keyword on your
C<use Number::Format> line (see non-object-oriented example below) you
will import those variables into your namesapce and can assign values
as if they were your own local variables. The default values for all
the parameters are:

THOUSANDS_SEP = ','
DECIMAL_POINT = '.'
MON_THOUSANDS_SEP = ','
MON_DECIMAL_POINT = '.'
INT_CURR_SYMBOL = 'USD'
DECIMAL_DIGITS = 2
DECIMAL_FILL = 0
NEG_FORMAT = '-x'
KILO_SUFFIX = 'K'
MEGA_SUFFIX = 'M'
GIGA_SUFFIX = 'G'
KIBI_SUFFIX = 'KiB'
MEBI_SUFFIX = 'MiB'
GIBI_SUFFIX = 'GiB'

Note however that when you first call one of the functions in this
module I<without> using the object-oriented interface, further setting
of those global variables will have no effect on non-OO calls. It is
recommended that you use the object-oriented interface instead for
fewer headaches and a cleaner design.

The C<DECIMAL_FILL> and C<DECIMAL_DIGITS> values are not set by the
Locale system, but are definable by the user. They affect the output
of C<format_number()>. Setting C<DECIMAL_DIGITS> is like giving that
value as the C<$precision> argument to that function. Setting
C<DECIMAL_FILL> to a true value causes C<format_number()> to append
zeroes to the right of the decimal digits until the length is the
specified number of digits.

C<NEG_FORMAT> is only used by C<format_negative()> and is a string
containing the letter 'x', where that letter will be replaced by a
positive representation of the number being passed to that function.
C<format_number()> and C<format_price()> utilize this feature by
calling C<format_negative()> if the number was less than 0.

C<KILO_SUFFIX>, C<MEGA_SUFFIX>, and C<GIGA_SUFFIX> are used by
C<format_bytes()> when the value is over 1024, 1024*1024, or
1024*1024*1024, respectively. The default values are "K", "M", and
"G". These apply in the default "traditional" mode only. Note: TERA
or higher are not implemented because of integer overflows on 32-bit
systems.

C<KIBI_SUFFIX>, C<MEBI_SUFFIX>, and C<GIBI_SUFFIX> are used by
C<format_bytes()> when the value is over 1024, 1024*1024, or
1024*1024*1024, respectively. The default values are "KiB", "MiB",
and "GiB". These apply in the "iso60027"" mode only. Note: TEBI or
higher are not implemented because of integer overflows on 32-bit
systems.

The only restrictions on C<DECIMAL_POINT> and C<THOUSANDS_SEP> are that
they must not be digits, must not be identical, and must each be one
character. There are no restrictions on C<INT_CURR_SYMBOL>.

For example, a German user might include this in their code:

use Number::Format;
my $de = new Number::Format(-thousands_sep => '.',
-decimal_point => ',',
-int_curr_symbol => 'DEM');
my $formatted = $de->format_number($number);

Or, if you prefer not to use the object oriented interface, you can do
this instead:

use Number::Format qw(:subs :vars);
$THOUSANDS_SEP = '.';
$DECIMAL_POINT = ',';
$INT_CURR_SYMBOL = 'DEM';
my $formatted = format_number($number);

=head1 EXPORTS

Nothing is exported by default. To export the functions or the global
variables defined herein, specify the function name(s) on the import
list of the C<use Number::Format> statement. To export all functions
defined herein, use the special tag C<:subs>. To export the
variables, use the special tag C<:vars>; to export both subs and vars
you can use the tag C<:all>.

=cut

###---------------------------------------------------------------------

use strict;
use Exporter;
use Carp;
use POSIX;
use base qw(Exporter);

our @EXPORT_SUBS =
qw( format_number format_negative format_picture
format_price format_bytes round unformat_number );

our @EXPORT_LC_NUMERIC =
qw( $DECIMAL_POINT $THOUSANDS_SEP $GROUPING );

our @EXPORT_LC_MONETARY =
qw( $INT_CURR_SYMBOL $CURRENCY_SYMBOL $MON_DECIMAL_POINT
$MON_THOUSANDS_SEP $MON_GROUPING $POSITIVE_SIGN $NEGATIVE_SIGN
$INT_FRAC_DIGITS $FRAC_DIGITS $P_CS_PRECEDES $P_SEP_BY_SPACE
$N_CS_PRECEDES $N_SEP_BY_SPACE $P_SIGN_POSN $N_SIGN_POSN );

our @EXPORT_OTHER =
qw( $DECIMAL_DIGITS $DECIMAL_FILL $NEG_FORMAT
$KILO_SUFFIX $MEGA_SUFFIX $GIGA_SUFFIX
$KIBI_SUFFIX $MEBI_SUFFIX $GIBI_SUFFIX );

our @EXPORT_VARS = ( @EXPORT_LC_NUMERIC, @EXPORT_LC_MONETARY, @EXPORT_OTHER );
our @EXPORT_ALL = ( @EXPORT_SUBS, @EXPORT_VARS );

our @EXPORT_OK = ( @EXPORT_ALL );

our %EXPORT_TAGS = ( subs => \@EXPORT_SUBS,
vars => \@EXPORT_VARS,
lc_numeric_vars => \@EXPORT_LC_NUMERIC,
lc_monetary_vars => \@EXPORT_LC_MONETARY,
other_vars => \@EXPORT_OTHER,
all => \@EXPORT_ALL );

our $VERSION = '1.73';

# Refer to http://www.opengroup.org/onlinepubs/007908775/xbd/locale.html
# for more details about the POSIX variables

# Locale variables provided by POSIX for numbers (LC_NUMERIC)
our $DECIMAL_POINT = '.'; # decimal point symbol for numbers
our $THOUSANDS_SEP = ','; # thousands separator for numbers
our $GROUPING = undef;# grouping rules for thousands (UNSUPPORTED)

# Locale variables provided by POSIX for currency (LC_MONETARY)
our $INT_CURR_SYMBOL = 'USD';# intl currency symbol
our $CURRENCY_SYMBOL = '$'; # domestic currency symbol
our $MON_DECIMAL_POINT = '.'; # decimal point symbol for monetary values
our $MON_THOUSANDS_SEP = ','; # thousands separator for monetary values
our $MON_GROUPING = undef;# like 'grouping' for monetary (UNSUPPORTED)
our $POSITIVE_SIGN = ''; # string to add for non-negative monetary
our $NEGATIVE_SIGN = '-'; # string to add for negative monetary
our $INT_FRAC_DIGITS = 2; # digits to right of decimal for intl currency
our $FRAC_DIGITS = 2; # digits to right of decimal for currency
our $P_CS_PRECEDES = 1; # curr sym precedes(1) or follows(0) positive
our $P_SEP_BY_SPACE = 1; # add space to positive; 0, 1, or 2
our $N_CS_PRECEDES = 1; # curr sym precedes(1) or follows(0) negative
our $N_SEP_BY_SPACE = 1; # add space to negative; 0, 1, or 2
our $P_SIGN_POSN = 1; # sign rules for positive: 0-4
our $N_SIGN_POSN = 1; # sign rules for negative: 0-4

# The following are specific to Number::Format
our $DECIMAL_DIGITS = 2;
our $DECIMAL_FILL = 0;
our $NEG_FORMAT = '-x';
our $KILO_SUFFIX = 'K';
our $MEGA_SUFFIX = 'M';
our $GIGA_SUFFIX = 'G';
our $KIBI_SUFFIX = 'KiB';
our $MEBI_SUFFIX = 'MiB';
our $GIBI_SUFFIX = 'GiB';

our $DEFAULT_LOCALE = { (
# LC_NUMERIC
decimal_point => $DECIMAL_POINT,
thousands_sep => $THOUSANDS_SEP,
grouping => $GROUPING,

# LC_MONETARY
int_curr_symbol => $INT_CURR_SYMBOL,
currency_symbol => $CURRENCY_SYMBOL,
mon_decimal_point => $MON_DECIMAL_POINT,
mon_thousands_sep => $MON_THOUSANDS_SEP,
mon_grouping => $MON_GROUPING,
positive_sign => $POSITIVE_SIGN,
negative_sign => $NEGATIVE_SIGN,
int_frac_digits => $INT_FRAC_DIGITS,
frac_digits => $FRAC_DIGITS,
p_cs_precedes => $P_CS_PRECEDES,
p_sep_by_space => $P_SEP_BY_SPACE,
n_cs_precedes => $N_CS_PRECEDES,
n_sep_by_space => $N_SEP_BY_SPACE,
p_sign_posn => $P_SIGN_POSN,
n_sign_posn => $N_SIGN_POSN,

# The following are specific to Number::Format
decimal_digits => $DECIMAL_DIGITS,
decimal_fill => $DECIMAL_FILL,
neg_format => $NEG_FORMAT,
kilo_suffix => $KILO_SUFFIX,
mega_suffix => $MEGA_SUFFIX,
giga_suffix => $GIGA_SUFFIX,
kibi_suffix => $KIBI_SUFFIX,
mebi_suffix => $MEBI_SUFFIX,
gibi_suffix => $GIBI_SUFFIX,
) };

#
# Largest integer a 32-bit Perl can handle is based on the mantissa
# size of a double float, which is up to 53 bits. While we may be
# able to support larger values on 64-bit systems, some Perl integer
# operations on 64-bit integer systems still use the 53-bit-mantissa
# double floats. To be safe, we cap at 2**53; use Math::BigFloat
# instead for larger numbers.
#
use constant MAX_INT => 2**53;

###---------------------------------------------------------------------

# INTERNAL FUNCTIONS

# These functions (with names beginning with '_' are for internal use
# only. There is no guarantee that they will remain the same from one
# version to the next!

##----------------------------------------------------------------------

# _get_self creates an instance of Number::Format with the default
# values for the configuration parameters, if the first element of
# @_ is not already an object.

my $DefaultObject;
sub _get_self
{
# Not calling $_[0]->isa because that may result in unblessed
# reference error
unless (ref $_[0] && UNIVERSAL::isa($_[0], "Number::Format"))
{
$DefaultObject ||= new Number::Format();
unshift (@_, $DefaultObject);
}
@_;
}

##----------------------------------------------------------------------

# _check_seps is used to validate that the thousands_sep,
# decimal_point, mon_thousands_sep and mon_decimal_point variables
# have acceptable values. For internal use only.

sub _check_seps
{
my ($self) = @_;
croak "Not an object" unless ref $self;
foreach my $prefix ("", "mon_")
{
croak "${prefix}thousands_sep is undefined"
unless defined $self->{"${prefix}thousands_sep"};
croak "${prefix}thousands_sep may not be numeric"
if $self->{"${prefix}thousands_sep"} =~ /\d/;
croak "${prefix}decimal_point may not be numeric"
if $self->{"${prefix}decimal_point"} =~ /\d/;
croak("${prefix}thousands_sep and ".
"${prefix}decimal_point may not be equal")
if $self->{"${prefix}decimal_point"} eq
$self->{"${prefix}thousands_sep"};
}
}

##----------------------------------------------------------------------

# _get_multipliers returns the multipliers to be used for kilo, mega,
# and giga (un-)formatting. Used in format_bytes and unformat_number.
# For internal use only.

sub _get_multipliers
{
my($base) = @_;
if (!defined($base) || $base == 1024)
{
return ( kilo => 0x00000400,
mega => 0x00100000,
giga => 0x40000000 );
}
elsif ($base == 1000)
{
return ( kilo => 1_000,
mega => 1_000_000,
giga => 1_000_000_000 );
}
else
{
croak "base overflow" if $base **3 > MAX_INT;
croak "base must be a positive integer"
unless $base > 0 && $base == int($base);
return ( kilo => $base,
mega => $base ** 2,
giga => $base ** 3 );
}
}

###---------------------------------------------------------------------

=head1 METHODS

=over 4

=cut

##----------------------------------------------------------------------

=item new( %args )

Creates a new Number::Format object. Valid keys for %args are any of
the parameters described above. Keys may be in all uppercase or all
lowercase, and may optionally be preceded by a hyphen (-) character.
Example:

my $de = new Number::Format(-thousands_sep => '.',
-decimal_point => ',',
-int_curr_symbol => 'DEM');

=cut

sub new
{
my $type = shift;
my %args = @_;

# Fetch defaults from current locale, or failing that, using globals
my $me = {};
# my $locale = setlocale(LC_ALL, "");
my $locale_values = localeconv();

my $arg;

while(my($arg, $default) = each %$DEFAULT_LOCALE)
{
$me->{$arg} = (exists $locale_values->{$arg}
? $locale_values->{$arg}
: $default);

foreach ($arg, uc $arg, "-$arg", uc "-$arg")
{
next unless defined $args{$_};
$me->{$arg} = $args{$_};
delete $args{$_};
last;
}
}

#
# Some broken locales define the decimal_point but not the
# thousands_sep. If decimal_point is set to "," the default
# thousands_sep will be a conflict. In that case, set
# thousands_sep to empty string. Suggested by Moritz Onken.
#
foreach my $prefix ("", "mon_")
{
$me->{"${prefix}thousands_sep"} = ""
if ($me->{"${prefix}decimal_point"} eq
$me->{"${prefix}thousands_sep"});
}

croak "Invalid argument(s)" if %args;
bless $me, $type;
$me;
}

##----------------------------------------------------------------------

=item round($number, $precision)

Rounds the number to the specified precision. If C<$precision> is
omitted, the value of the C<DECIMAL_DIGITS> parameter is used (default
value 2). Both input and output are numeric (the function uses math
operators rather than string manipulation to do its job), The value of
C<$precision> may be any integer, positive or negative. Examples:

round(3.14159) yields 3.14
round(3.14159, 4) yields 3.1416
round(42.00, 4) yields 42
round(1234, -2) yields 1200

Since this is a mathematical rather than string oriented function,
there will be no trailing zeroes to the right of the decimal point,
and the C<DECIMAL_POINT> and C<THOUSANDS_SEP> variables are ignored.
To format your number using the C<DECIMAL_POINT> and C<THOUSANDS_SEP>
variables, use C<format_number()> instead.

=cut

sub round
{
my ($self, $number, $precision) = _get_self @_;
$precision = $self->{decimal_digits} unless defined $precision;
$precision = 2 unless defined $precision;
$number = 0 unless defined $number;

my $sign = $number <=> 0;
my $multiplier = (10 ** $precision);
my $result = abs($number);
my $product = $result * $multiplier;

croak "round() overflow. Try smaller precision or use Math::BigFloat"
if $product > MAX_INT;

# We need to add 1e-14 to avoid some rounding errors due to the
# way floating point numbers work - see string-eq test in t/round.t
$result = int($product + .5 + 1e-14) / $multiplier;
$result = -$result if $sign < 0;
return $result;
}

##----------------------------------------------------------------------

=item format_number($number, $precision, $trailing_zeroes)

Formats a number by adding C<THOUSANDS_SEP> between each set of 3
digits to the left of the decimal point, substituting C<DECIMAL_POINT>
for the decimal point, and rounding to the specified precision using
C<round()>. Note that C<$precision> is a I<maximum> precision
specifier; trailing zeroes will only appear in the output if
C<$trailing_zeroes> is provided, or the parameter C<DECIMAL_FILL> is
set, with a value that is true (not zero, undef, or the empty string).
If C<$precision> is omitted, the value of the C<DECIMAL_DIGITS>
parameter (default value of 2) is used.

If the value is too large or great to work with as a regular number,
but instead must be shown in scientific notation, returns that number
in scientific notation without further formatting.

Examples:

format_number(12345.6789) yields '12,345.68'
format_number(123456.789, 2) yields '123,456.79'
format_number(1234567.89, 2) yields '1,234,567.89'
format_number(1234567.8, 2) yields '1,234,567.8'
format_number(1234567.8, 2, 1) yields '1,234,567.80'
format_number(1.23456789, 6) yields '1.234568'
format_number("0.000020000E+00", 7);' yields '2e-05'

Of course the output would have your values of C<THOUSANDS_SEP> and
C<DECIMAL_POINT> instead of ',' and '.' respectively.

=cut

sub format_number
{
my ($self, $number, $precision, $trailing_zeroes, $mon) = _get_self @_;
$self->_check_seps(); # first make sure the SEP variables are valid

my($thousands_sep, $decimal_point) =
$mon ? @$self{qw(mon_thousands_sep mon_decimal_point)}
: @$self{qw(thousands_sep decimal_point)};

# Set defaults and standardize number
$precision = $self->{decimal_digits} unless defined $precision;
$trailing_zeroes = $self->{decimal_fill} unless defined $trailing_zeroes;

# Handle negative numbers
my $sign = $number <=> 0;
$number = abs($number) if $sign < 0;
$number = $self->round($number, $precision); # round off $number

# detect scientific notation
my $exponent = 0;
if ($number =~ /^(-?[\d.]+)e([+-]\d+)$/)
{
# Don't attempt to format numbers that require scientific notation.
return $number;
}

# Split integer and decimal parts of the number and add commas
my $integer = int($number);
my $decimal;

# Note: In perl 5.6 and up, string representation of a number
# automagically includes the locale decimal point. This way we
# will detect the decimal part correctly as long as the decimal
# point is 1 character.
$decimal = substr($number, length($integer)+1)
if (length($integer) < length($number));
$decimal = '' unless defined $decimal;

# Add trailing 0's if $trailing_zeroes is set.
$decimal .= '0'x( $precision - length($decimal) )
if $trailing_zeroes && $precision > length($decimal);

# Add the commas (or whatever is in thousands_sep). If
# thousands_sep is the empty string, do nothing.
if ($thousands_sep)
{
# Add leading 0's so length($integer) is divisible by 3
$integer = '0'x(3 - (length($integer) % 3)).$integer;

# Split $integer into groups of 3 characters and insert commas
$integer = join($thousands_sep,
grep {$_ ne ''} split(/(...)/, $integer));

# Strip off leading zeroes and/or comma
$integer =~ s/^0+\Q$thousands_sep\E?//;
}
$integer = '0' if $integer eq '';

# Combine integer and decimal parts and return the result.
my $result = ((defined $decimal && length $decimal) ?
join($decimal_point, $integer, $decimal) :
$integer);

return ($sign < 0) ? $self->format_negative($result) : $result;
}

##----------------------------------------------------------------------

=item format_negative($number, $picture)

Formats a negative number. Picture should be a string that contains
the letter C<x> where the number should be inserted. For example, for
standard negative numbers you might use ``C<-x>'', while for
accounting purposes you might use ``C<(x)>''. If the specified number
begins with a ``-'' character, that will be removed before formatting,
but formatting will occur whether or not the number is negative.

=cut

sub format_negative
{
my($self, $number, $format) = _get_self @_;
$format = $self->{neg_format} unless defined $format;
croak "Letter x must be present in picture in format_negative()"
unless $format =~ /x/;
$number =~ s/^-//;
$format =~ s/x/$number/;
return $format;
}

##----------------------------------------------------------------------

=item format_picture($number, $picture)

Returns a string based on C<$picture> with the C<#> characters
replaced by digits from C<$number>. If the length of the integer part
of $number is too large to fit, the C<#> characters are replaced with
asterisks (C<*>) instead. Examples:

format_picture(100.023, 'USD ##,###.##') yields 'USD 100.02'
format_picture(1000.23, 'USD ##,###.##') yields 'USD 1,000.23'
format_picture(10002.3, 'USD ##,###.##') yields 'USD 10,002.30'
format_picture(100023, 'USD ##,###.##') yields 'USD **,***.**'
format_picture(1.00023, 'USD #.###,###') yields 'USD 1.002,300'

The comma (,) and period (.) you see in the picture examples should
match the values of C<THOUSANDS_SEP> and C<DECIMAL_POINT>,
respectively, for proper operation. However, the C<THOUSANDS_SEP>
characters in C<$picture> need not occur every three digits; the
I<only> use of that variable by this function is to remove leading
commas (see the first example above). There may not be more than one
instance of C<DECIMAL_POINT> in C<$picture>.

The value of C<NEG_FORMAT> is used to determine how negative numbers
are displayed. The result of this is that the output of this function
my have unexpected spaces before and/or after the number. This is
necessary so that positive and negative numbers are formatted into a
space the same size. If you are only using positive numbers and want
to avoid this problem, set NEG_FORMAT to "x".

=cut

sub format_picture
{
my ($self, $number, $picture) = _get_self @_;
$self->_check_seps();

# Handle negative numbers
my($neg_prefix) = $self->{neg_format} =~ /^([^x]+)/;
my($pic_prefix) = $picture =~ /^([^\#]+)/;
my $neg_pic = $self->{neg_format};
(my $pos_pic = $self->{neg_format}) =~ s/[^x\s]/ /g;
(my $pos_prefix = $neg_prefix) =~ s/[^x\s]/ /g;
$neg_pic =~ s/x/$picture/;
$pos_pic =~ s/x/$picture/;
my $sign = $number <=> 0;
$number = abs($number) if $sign < 0;
$picture = $sign < 0 ? $neg_pic : $pos_pic;
my $sign_prefix = $sign < 0 ? $neg_prefix : $pos_prefix;

# Split up the picture and die if there is more than one $DECIMAL_POINT
my($pic_int, $pic_dec, @cruft) =
split(/\Q$self->{decimal_point}\E/, $picture);
$pic_int = '' unless defined $pic_int;
$pic_dec = '' unless defined $pic_dec;

croak "Only one decimal separator permitted in picture"
if @cruft;

# Obtain precision from the length of the decimal part...
my $precision = $pic_dec; # start with copying it
$precision =~ s/[^\#]//g; # eliminate all non-# characters
$precision = length $precision; # take the length of the result

# Format the number
$number = $self->round($number, $precision);

# Obtain the length of the integer portion just like we did for $precision
my $intsize = $pic_int; # start with copying it
$intsize =~ s/[^\#]//g; # eliminate all non-# characters
$intsize = length $intsize; # take the length of the result

# Split up $number same as we did for $picture earlier
my($num_int, $num_dec) = split(/\./, $number, 2);
$num_int = '' unless defined $num_int;
$num_dec = '' unless defined $num_dec;

# Check if the integer part will fit in the picture
if (length $num_int > $intsize)
{
$picture =~ s/\#/\*/g; # convert # to * and return it
$pic_prefix = "" unless defined $pic_prefix;
$picture =~ s/^(\Q$sign_prefix\E)(\Q$pic_prefix\E)(\s*)/$2$3$1/;
return $picture;
}

# Split each portion of number and picture into arrays of characters
my @num_int = split(//, $num_int);
my @num_dec = split(//, $num_dec);
my @pic_int = split(//, $pic_int);
my @pic_dec = split(//, $pic_dec);

# Now we copy those characters into @result.
my @result;
@result = ($self->{decimal_point})
if $picture =~ /\Q$self->{decimal_point}\E/;
# For each characture in the decimal part of the picture, replace '#'
# signs with digits from the number.
my $char;
foreach $char (@pic_dec)
{
$char = (shift(@num_dec) || 0) if ($char eq '#');
push (@result, $char);
}

# For each character in the integer part of the picture (moving right
# to left this time), replace '#' signs with digits from the number,
# or spaces if we've run out of numbers.
while ($char = pop @pic_int)
{
$char = pop(@num_int) if ($char eq '#');
$char = ' ' if (!defined($char) ||
$char eq $self->{thousands_sep} && $#num_int < 0);
unshift (@result, $char);
}

# Combine @result into a string and return it.
my $result = join('', @result);
$sign_prefix = '' unless defined $sign_prefix;
$pic_prefix = '' unless defined $pic_prefix;
$result =~ s/^(\Q$sign_prefix\E)(\Q$pic_prefix\E)(\s*)/$2$3$1/;
$result;
}

##----------------------------------------------------------------------

=item format_price($number, $precision, $symbol)

Returns a string containing C<$number> formatted similarly to
C<format_number()>, except that the decimal portion may have trailing
zeroes added to make it be exactly C<$precision> characters long, and
the currency string will be prefixed.

The C<$symbol> attribute may be one of "INT_CURR_SYMBOL" or
"CURRENCY_SYMBOL" (case insensitive) to use the value of that
attribute of the object, or a string containing the symbol to be used.
The default is "INT_CURR_SYMBOL" if this argument is undefined or not
given; if set to the empty string, or if set to undef and the
C<INT_CURR_SYMBOL> attribute of the object is the empty string, no
currency will be added.

If C<$precision> is not provided, the default of 2 will be used.
Examples:

format_price(12.95) yields 'USD 12.95'
format_price(12) yields 'USD 12.00'
format_price(12, 3) yields '12.000'

The third example assumes that C<INT_CURR_SYMBOL> is the empty string.

=cut

sub format_price
{
my ($self, $number, $precision, $curr_symbol) = _get_self @_;

# Determine what the monetary symbol should be
$curr_symbol = $self->{int_curr_symbol}
if (!defined($curr_symbol) || lc($curr_symbol) eq "int_curr_symbol");
$curr_symbol = $self->{currency_symbol}
if (!defined($curr_symbol) || lc($curr_symbol) eq "currency_symbol");
$curr_symbol = "" unless defined($curr_symbol);

# Determine which value to use for frac digits
my $frac_digits = ($curr_symbol eq $self->{int_curr_symbol} ?
$self->{int_frac_digits} : $self->{frac_digits});

# Determine precision for decimal portion
$precision = $frac_digits unless defined $precision;
$precision = $self->{decimal_digits} unless defined $precision; # fallback
$precision = 2 unless defined $precision; # default

# Determine sign and absolute value
my $sign = $number <=> 0;
$number = abs($number) if $sign < 0;

# format it first
$number = $self->format_number($number, $precision, undef, 1);

# Now we make sure the decimal part has enough zeroes
my ($integer, $decimal) =
split(/\Q$self->{mon_decimal_point}\E/, $number, 2);
$decimal = '0'x$precision unless $decimal;
$decimal .= '0'x($precision - length $decimal);

# Extract positive or negative values
my($sep_by_space, $cs_precedes, $sign_posn, $sign_symbol);
if ($sign < 0)
{
$sep_by_space = $self->{n_sep_by_space};
$cs_precedes = $self->{n_cs_precedes};
$sign_posn = $self->{n_sign_posn};
$sign_symbol = $self->{negative_sign};
}
else
{
$sep_by_space = $self->{p_sep_by_space};
$cs_precedes = $self->{p_cs_precedes};
$sign_posn = $self->{p_sign_posn};
$sign_symbol = $self->{positive_sign};
}

# Combine it all back together.
my $result = ($precision ?
join($self->{mon_decimal_point}, $integer, $decimal) :
$integer);

# Determine where spaces go, if any
my($sign_sep, $curr_sep);
if ($sep_by_space == 0)
{
$sign_sep = $curr_sep = "";
}
elsif ($sep_by_space == 1)
{
$sign_sep = "";
$curr_sep = " ";
}
elsif ($sep_by_space == 2)
{
$sign_sep = " ";
$curr_sep = "";
}
else
{
croak "Invalid sep_by_space value";
}

# Add sign, if any
if ($sign_posn >= 0 && $sign_posn <= 2)
{
# Combine with currency symbol and return
if ($curr_symbol ne "")
{
if ($cs_precedes)
{
$result = $curr_symbol.$curr_sep.$result;
}
else
{
$result = $result.$curr_sep.$curr_symbol;
}
}

if ($sign_posn == 0)
{
return "($result)";
}
elsif ($sign_posn == 1)
{
return $sign_symbol.$sign_sep.$result;
}
else # $sign_posn == 2
{
return $result.$sign_sep.$sign_symbol;
}
}

elsif ($sign_posn == 3 || $sign_posn == 4)
{
if ($sign_posn == 3)
{
$curr_symbol = $sign_symbol.$sign_sep.$curr_symbol;
}
else # $sign_posn == 4
{
$curr_symbol = $curr_symbol.$sign_sep.$sign_symbol;
}

# Combine with currency symbol and return
if ($cs_precedes)
{
return $curr_symbol.$curr_sep.$result;
}
else
{
return $result.$curr_sep.$curr_symbol;
}
}

else
{
croak "Invalid *_sign_posn value";
}
}

##----------------------------------------------------------------------

=item format_bytes($number, %options)

=item format_bytes($number, $precision) # deprecated

Returns a string containing C<$number> formatted similarly to
C<format_number()>, except that large numbers may be abbreviated by
adding a suffix to indicate 1024, 1,048,576, or 1,073,741,824 bytes.
Suffix may be the traditional K, M, or G (default); or the IEC
standard 60027 "KiB," "MiB," or "GiB" depending on the "mode" option.

Negative values will result in an error.

The second parameter can be either a hash that sets options, or a
number. Using a number here is deprecated and will generate a
warning; early versions of Number::Format only allowed a numeric
value. A future release of Number::Format will change this warning to
an error. New code should use a hash instead to set options. If it
is a number this sets the value of the "precision" option.

Valid options are:

=over 4

=item precision

Set the precision for displaying numbers. If not provided, a default
of 2 will be used. Examples:

format_bytes(12.95) yields '12.95'
format_bytes(12.95, precision => 0) yields '13'
format_bytes(2048) yields '2K'
format_bytes(2048, mode => "iec") yields '2KiB'
format_bytes(9999999) yields '9.54M'
format_bytes(9999999, precision => 1) yields '9.5M'

=item unit

Sets the default units used for the results. The default is to
determine this automatically in order to minimize the length of the
string. In other words, numbers greater than or equal to 1024 (or
other number given by the 'base' option, q.v.) will be divided by 1024
and C<$KILO_SUFFIX> or C<$KIBI_SUFFIX> added; if greater than or equal
to 1048576 (1024*1024), it will be divided by 1048576 and
C<$MEGA_SUFFIX> or C<$MEBI_SUFFIX> appended to the end; etc.

However if a value is given for C<unit> it will use that value
instead. The first letter (case-insensitive) of the value given
indicates the threshhold for conversion; acceptable values are G (for
giga/gibi), M (for mega/mebi), K (for kilo/kibi), or A (for automatic,
the default). For example:

format_bytes(1048576, unit => 'K') yields '1,024K'
instead of '1M'

Note that the valid values to this option do not vary even when the
suffix configuration variables have been changed.

=item base

Sets the number at which the C<$KILO_SUFFIX> is added. Default is
1024. Set to any value; the only other useful value is probably 1000,
as hard disk manufacturers use that number to make their disks sound
bigger than they really are.

If the mode (see below) is set to "iec" or "iec60027" then setting the
base option results in an error.

=item mode

Traditionally, bytes have been given in SI (metric) units such as
"kilo" and "mega" even though they represent powers of 2 (1024, etc.)
rather than powers of 10 (1000, etc.) This "binary prefix" causes
much confusion in consumer products where "GB" may mean either
1,048,576 or 1,000,000, for example. The International
Electrotechnical Commission has created standard IEC 60027 to
introduce prefixes Ki, Mi, Gi, etc. ("kibibytes," "mebibytes,"
"gibibytes," etc.) to remove this confusion. Specify a mode option
with either "traditional" or "iec60027" (or abbreviate as "trad" or
"iec") to indicate which type of binary prefix you want format_bytes
to use. For backward compatibility, "traditional" is the default.
See http://en.wikipedia.org/wiki/Binary_prefix for more information.

=back

=cut

sub format_bytes
{
my ($self, $number, @options) = _get_self @_;

croak "Negative number not allowed in format_bytes"
if $number < 0;

# If a single scalar is given instead of key/value pairs for
# @options, treat that as the value of the precision option.
my %options;
if (@options == 1)
{
# To be changed to 'croak' in a future release:
carp "format_bytes: number instead of options is deprecated";
%options = ( precision => $options[0] );
}
else
{
%options = @options;
}

# Set default for precision. Test using defined because it may be 0.
$options{precision} = $self->{decimal_digits}
unless defined $options{precision};
$options{precision} = 2
unless defined $options{precision}; # default

$options{mode} ||= "traditional";
my($ksuff, $msuff, $gsuff);
if ($options{mode} =~ /^iec(60027)?$/i)
{
($ksuff, $msuff, $gsuff) =
@$self{qw(kibi_suffix mebi_suffix gibi_suffix)};
croak "base option not allowed in iec60027 mode"
if exists $options{base};
}
elsif ($options{mode} =~ /^trad(itional)?$/i)
{
($ksuff, $msuff, $gsuff) =
@$self{qw(kilo_suffix mega_suffix giga_suffix)};
}
else
{
croak "Invalid mode";
}

# Set default for "base" option. Calculate threshold values for
# kilo, mega, and giga values. On 32-bit systems tera would cause
# overflows so it is not supported. Useful values of "base" are
# 1024 or 1000, but any number can be used. Larger numbers may
# cause overflows for giga or even mega, however.
my %mult = _get_multipliers($options{base});

# Process "unit" option. Set default, then take first character
# and convert to upper case.
$options{unit} = "auto"
unless defined $options{unit};
my $unit = uc(substr($options{unit},0,1));

# Process "auto" first (default). Based on size of number,
# automatically determine which unit to use.
if ($unit eq 'A')
{
if ($number >= $mult{giga})
{
$unit = 'G';
}
elsif ($number >= $mult{mega})
{
$unit = 'M';
}
elsif ($number >= $mult{kilo})
{
$unit = 'K';
}
else
{
$unit = 'N';
}
}

# Based on unit, whether specified or determined above, divide the
# number and determine what suffix to use.
my $suffix = "";
if ($unit eq 'G')
{
$number /= $mult{giga};
$suffix = $gsuff;
}
elsif ($unit eq 'M')
{
$number /= $mult{mega};
$suffix = $msuff;
}
elsif ($unit eq 'K')
{
$number /= $mult{kilo};
$suffix = $ksuff;
}
elsif ($unit ne 'N')
{
croak "Invalid unit option";
}

# Format the number and add the suffix.
return $self->format_number($number, $options{precision}) . $suffix;
}

##----------------------------------------------------------------------

=item unformat_number($formatted)

Converts a string as returned by C<format_number()>,
C<format_price()>, or C<format_picture()>, and returns the
corresponding value as a numeric scalar. Returns C<undef> if the
number does not contain any digits. Examples:

unformat_number('USD 12.95') yields 12.95
unformat_number('USD 12.00') yields 12
unformat_number('foobar') yields undef
unformat_number('1234-567@.8') yields 1234567.8

The value of C<DECIMAL_POINT> is used to determine where to separate
the integer and decimal portions of the input. All other non-digit
characters, including but not limited to C<INT_CURR_SYMBOL> and
C<THOUSANDS_SEP>, are removed.

If the number matches the pattern of C<NEG_FORMAT> I<or> there is a
``-'' character before any of the digits, then a negative number is
returned.

If the number ends with the C<KILO_SUFFIX>, C<KIBI_SUFFIX>,
C<MEGA_SUFFIX>, C<MEBI_SUFFIX>, C<GIGA_SUFFIX>, or C<GIBI_SUFFIX>
characters, then the number returned will be multiplied by the
appropriate multiple of 1024 (or if the base option is given, by the
multiple of that value) as appropriate. Examples:

unformat_number("4K", base => 1024) yields 4096
unformat_number("4K", base => 1000) yields 4000
unformat_number("4KiB", base => 1024) yields 4096
unformat_number("4G") yields 4294967296

=cut

sub unformat_number
{
my ($self, $formatted, %options) = _get_self @_;
$self->_check_seps();
return undef unless $formatted =~ /\d/; # require at least one digit

# Regular expression for detecting decimal point
my $pt = qr/\Q$self->{decimal_point}\E/;

# ru_RU locale has comma for decimal_point, but period for
# mon_decimal_point! But as long as thousands_sep is different
# from either, we can allow either decimal point.
if ($self->{mon_decimal_point} &&
$self->{decimal_point} ne $self->{mon_decimal_point} &&
$self->{decimal_point} ne $self->{mon_thousands_sep} &&
$self->{mon_decimal_point} ne $self->{thousands_sep})
{
$pt = qr/(?:\Q$self->{decimal_point}\E|
\Q$self->{mon_decimal_point}\E)/x;
}

# Detect if it ends with one of the kilo / mega / giga suffixes.
my $kp = ($formatted =~
s/\s*($self->{kilo_suffix}|$self->{kibi_suffix})\s*$//);
my $mp = ($formatted =~
s/\s*($self->{mega_suffix}|$self->{mebi_suffix})\s*$//);
my $gp = ($formatted =~
s/\s*($self->{giga_suffix}|$self->{gibi_suffix})\s*$//);
my %mult = _get_multipliers($options{base});

# Split number into integer and decimal parts
my ($integer, $decimal, @cruft) = split($pt, $formatted);
croak "Only one decimal separator permitted"
if @cruft;

# It's negative if the first non-digit character is a -
my $sign = $formatted =~ /^\D*-/ ? -1 : 1;
my($before_re, $after_re) = split /x/, $self->{neg_format}, 2;
$sign = -1 if $formatted =~ /\Q$before_re\E(.+)\Q$after_re\E/;

# Strip out all non-digits from integer and decimal parts
$integer = '' unless defined $integer;
$decimal = '' unless defined $decimal;
$integer =~ s/\D//g;
$decimal =~ s/\D//g;

# Join back up, using period, and add 0 to make Perl think it's a number
my $number = join('.', $integer, $decimal) + 0;
$number = -$number if $sign < 0;

# Scale the number if it ended in kilo or mega suffix.
$number *= $mult{kilo} if $kp;
$number *= $mult{mega} if $mp;
$number *= $mult{giga} if $gp;

return $number;
}

###---------------------------------------------------------------------

=back

=head1 CAVEATS

Some systems, notably OpenBSD, may have incomplete locale support.
Using this module together with L<setlocale(3)> in OpenBSD may therefore
not produce the intended results.

=head1 BUGS

No known bugs at this time. Report bugs using the CPAN request
tracker at L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Format>
or by email to the author.

=head1 AUTHOR

William R. Ward, SwPrAwM@cpan.org (remove "SPAM" before sending email,
leaving only my initials)

=head1 SEE ALSO

perl(1).

=cut

1;

documentation  perl GD  commify  launcher   home   guestbook  other ephemerides   lunar calendar   Astro-MoonPhase-0.60   Total eclipse at the new moon  top
T here are 5 scripts :-  moondisplay.cgi, astro2.cgi, moontextimage.cgi, Astro::MoonPhase and Number::Format.

You are visitor number   number of hits   Page updated on June 15th. 2014 and again on January 18th. 2018.