# A Perl Script to Tell You The Sunrise and Sunset

That's right, I can still do Perl.

In college, I wanted to do bioinformatics research with Dr Babbitt. I pleaded to him and the dean of the medical school to allow a philosophy student with no coding skills (at the time) to do this work. Babbitt said, take these books on Perl, R, and statistics over the summer and talk to me in the fall. So that’s how I learned Perl. And R. Statistics came a bit later.

I don’t actually dislike Perl, in fact I like it quite a bit. Nostalgia, no doubt. Anyway, another thing I learned to like during college was waking up around the same time as sunrise. It’s definitely my greatest productivity hack. I’m not saying this script will teach you to get up with the sun, but running this in your systray, or as a cron job that sends you an SMS with tomorrow’s sunrise time, or even maybe even on a Raspberry Pi alarm clock, could potentially help you get up earlier.

So here is the script, it’s rather simple and self-explanatory I think. You probably need to use CPAN to install the DateTime::Event::Sunrise package. And don’t forget to set the location variables.

#!/usr/bin/env perl

use strict;
use utf8;

use DateTime;
use DateTime::Event::Sunrise;

# Location
my $lat = "FIXME"; my$lon = "FIXME";

# Current date
my $now = DateTime->now;$now->set_time_zone("local");

my $fmt = "%H:%M"; # Get sunrise and sunset data my$sun = DateTime::Event::Sunrise->new (
longitude => $lon, latitude =>$lat,
precise   => 1
);

print "Sunrise: ", $sun->sunrise_datetime($now)->strftime($fmt); print "\n"; print "Sunset: ",$sun->sunset_datetime($now)->strftime($fmt);
print "\n";

If you end up building that RPi alarm clock, do send me an email describing how you did it. That sounds super cool.

—Ben Sima, 2017.05.02