Work the Shell - Function Return Codes and Daylight Calculations
Don't worry, I won't burst into a song from Fiddler on the Roof, but sunrise and sunset times are very dependent on not only the time of year but also on your location.
After digging around quite a bit, it seems like Almanac.com is one of the easiest sites to work with, so that's what I'll use. A sunrise/sunset query to Almanac.com ends up with a URL of this form: http://www.almanac.com/astronomy/rise/zipcode/80302/2010-08-01.
You'll have to use date to calculate the current date in the proper format and hard-code the local zipcode into the function.
As with most sites, the HTML generated by the Almanac.com result is not parse-friendly, so I had to dig around for a while to figure out how to proceed. Here's what I came up with:
yourzip="80302" # set this to your local zip code request="http://www.almanac.com/astronomy/rise/zipcode" thedate="$(date +%Y-%m-%d)" url="$request/$yourzip/$thedate" curl --silent "$url" | grep rise_nextprev | \ cut -d\< -f28-30
You can see that the zipcode is indeed hard-coded, and notice how I use the $() notation to grab the date in YYYY-MM-DD format. Curl gives you the resultant HTML page, grep finds the one line you're interested in, and then cut chops out the following snippet:
td> 5:38 A.M.</td><td> 8:34 P.M.
There are a few more hoops to jump through, so you can pull out the hour and minute of sunrise and sunset separately (as you'll have to test that way). Here's the code I came up with:
raw="$(/usr/bin/curl --silent "$url" | \ grep rise_nextprev | cut -d\<-f28-30)" sunrise="$(echo $raw | cut -d\ -f2)" sunset="$(echo $raw | cut -d\ -f4)" srh=$(echo $sunrise | cut -d: -f1) srm=$(echo $sunrise | cut -d: -f2) ssh=$(echo $sunset | cut -d: -f1) ssm=$(echo $sunset | cut -d: -f2)
You could make it a bit faster by avoiding the intermediate calculations of sunrise and sunset, but on modern Linux systems, it should be a matter of milliseconds, so let's leave it just like that.
There's one more important tweak: sunset hour (ssh) needs to be on a 24-hour clock, as that's what you're getting from the date invocation shown earlier. It turns out that you can drop the cut subshell invocation into a calculation:
ssh=$(( $(echo $sunset | cut -d: -f1) + 12 ))
Yes, it works. It looks like I'm moving into LISP territory, but fortunately not!
To work properly, the script needs to do three tests:
Whether it's sunrise hour and greater than sunrise minute.
Whether it's greater than sunrise hour but less than sunset hour.
Whether it's sunset hour but less than sunset minute.
Here's how that looks as script:
if [ $hour -eq $srh -a $min -ge $srm ] ; then return 0 # special case of sunrise hour fi if [ $hour -gt $srh -a $hour -lt $ssh ] ; then return 0 # easy: after sunrise, before sunset fi if [ $hour -eq $ssh -a $min -le $ssm ] ; then return 0 # special case: sunset hour fi
Voilà! Kinda neat, if I say so myself.
My full implementation of isdaytime is available on the Linux Journal FTP server at ftp.linuxjournal.com/pub/lj/listings/issue198/10860.tgz.
Dave Taylor has been hacking shell scripts for a really long time, 30 years. He's the author of the popular Wicked Cool Shell Scripts and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- RSS Feeds
- New Products
- I like your topic on android
15 min 56 sec ago
- Reply to comment | Linux Journal
37 min 6 sec ago
- This is the easiest tutorial
6 hours 51 min ago
- Ahh, the Koolaid.
12 hours 30 min ago
- git-annex assistant
18 hours 29 min ago
- direct cable connection
18 hours 52 min ago
- Agreed on AirDroid. With my
19 hours 2 min ago
- I just learned this
19 hours 6 min ago
19 hours 36 min ago
- not living upto the mobile revolution
22 hours 28 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.