Source

watches / lunar / generate-sunrise

Full commit
#!/opt/local/bin/perl
# Generate the set of sunrise times for every day of the year
# for the given latitude.
use warnings;
use strict;
use Astro::Sunrise;

die "Usage: $0 longitude latitude (in degrees)\n"
	unless @ARGV == 2;

my $longitude = shift;
my $latitude = shift;

my $year = 2011;
my $tz = -5;
my $text_output = 0;

for my $month (1..12)
{
	for my $day (1..31)
	{
		my ($sunrise,$sunset) = sunrise(
			$year,
			$month,
			$day,
			$longitude,
			$latitude,
			$tz,
			0
		);

		# Convert into minutes past midnight
		my ($h,$m) = split /:/, $sunrise;
		$sunrise = $h * 60 + $m;
		($h,$m) = split /:/, $sunset;
		$sunset = $h * 60 + $m;

		if ($text_output)
		{
			# Output a 
			printf "%d %d %d %d\n",
				$month,
				$day,
				$sunrise,
				$sunset,
				;
		} else {
			# Output the time as a packed array of 16-bit ints
			print pack("nn", $sunrise, $sunset);
		}
	}

	# Output an extra day so that we have 32 days per month
	print pack("nn", 0, 0)
		unless $text_output
}