Created
November 23, 2011 23:38
-
-
Save mjdominus/1390251 to your computer and use it in GitHub Desktop.
Greek clock program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
my $A = 1.5 * 3600; # Latitude adjustment in seconds | |
my $time = time; | |
my $days_since_equinox = ($time - 1047950185)/86400; | |
my $dst = (localtime($time))[9]; | |
my $PI = atan2(0, -1); | |
my $days_per_year = 365.2422; | |
my $sunrise_adj = $A * sin($days_since_equinox / $days_per_year | |
* 2 * $PI ); | |
my $length_of_daytime = 12 * 3600 + 2 * $sunrise_adj; | |
my $length_of_nighttime = 12 * 3600 - 2 * $sunrise_adj; | |
my $time_of_sunrise = 6 * 3600 - $sunrise_adj; | |
my $time_of_sunset = 18 * 3600 + $sunrise_adj; | |
my ($gh, $gm) = time_to_greek($time); | |
my ($h, $m) = (localtime($time))[2,1]; | |
printf "Standard: %2d:%02d\n", $h, $m; | |
printf " Greek: %2d:%02d\n", $gh, $gm; | |
sub time_to_greek { | |
my ($epoch_time) = shift; | |
my $time_of_day; | |
{ my ($h, $m, $s, $dst) = (localtime($epoch_time))[2,1,0,8]; | |
$time_of_day = ($h-$dst) * 3600 + $m * 60 + $s; | |
} | |
my ($greek, $hour, $min); | |
if ($time_of_day < $time_of_sunrise) { | |
# change early morning into night | |
$time_of_day += 24 * 3600; | |
} | |
if ($time_of_day < $time_of_sunset) { | |
# day | |
my $diff = $time_of_day - $time_of_sunrise; | |
$greek = 6 + ($diff / $length_of_daytime) * 12; | |
} else { | |
# night | |
my $diff = $time_of_day - $time_of_sunset; | |
$greek = 18 + ($diff / $length_of_nighttime) * 12; | |
} | |
$hour = int($greek); | |
$min = int(60 * ($greek - $hour)); | |
($hour, $min); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment