Sunwait

Sunwait is a small C program for calculating sunrise and sunset, as well as civil, nautical, and astronomical twilights. It has features that make it useful for home automation tasks.

NOTE: I developed sunwait in 2000(-ish) and maintained it until 2004. In 2013, Ian Craig forked it and made it better. He maintained it until 2016. In 2019, I moved his code to a repo on GitHub and fixed a bug or two and added some features. As of 2019-03-22, the version at GitHub (https://github.com/risacher/sunwait should be considered "current".


Originally, developed under Linux, it should work on any system with a C compiler and the C standard library. With some minor patches provided by George Gugulea, it will compile on Win32 - which results in sunwait.exe, but I haven't tested this extensively.

The feature that makes this program slightly unique is that it can be set to wait specific event (such as 5 minutes before sunrise), then exit. This makes it useful for 'cron' jobs or 'scheduled tasks' when you want something to happen relative to sunrise, sunset, or some other astronomical event.

For example, consider this line from my crontab:
01 00 * * * sunwait wait civ start 38.794433N 77.069450W ; br b6 off

This line executes at at one minute after midnight, waits until the start of civil twilight, then runs the command br b6 off (which turns off my carriage light).

Thanks to Paul Schlyter, who wrote the math and released it as public domain in 1992.

Thanks to Dr. David M. MacMillan, who suggested some minor, but useful changes for the 20041208 version.

Thanks to Ian Craig, who made many improvements and maintained it for several years.


Calculate sunrise and sunset times for the current or targetted day.
The times can be adjusted either for twilight or fixed durations.

The program can either: wait for sunrise or sunset   (function: wait),
  or return the time (GMT or local) the event occurs (function: list),
  or report the day length and twilight timings      (function: report),
  or simply report if it is DAY or NIGHT             (function: poll).

You should specify the latitude and longitude of your target location.


Usage: sunwait [major options] [minor options] [twilight type] [rise|set] [offset] [latitude] [longitude]

Major options, either:
    poll          Returns immediately indicating DAY or NIGHT. See 'program exit codes'. Default.
    wait          Sleep until specified event occurs. Else exit immediate.
    list [X]      Report twilight times for next 'X' days (inclusive). Default: 1.
    report        Generate a report about the days sunrise and sunset timings.

Minor options, any of:
    [no]debug     Print extra info and returns in one minute. Default: nodebug.
    [no]version   Print the version number. Default: noversion.
    [no]help      Print this help. Default: nohelp.
    [no]gmt       Print times in GMT or local-time.  Default: nogmt.

Twilight types, either:
    daylight      Top of sun just below the horizon. Default.
    civil         Civil Twilight.         -6 degrees below horizon.
    nautical      Nautical twilight.     -12 degrees below horizon.
    astronomical  Astronomical twilight. -18 degrees below horizon.
    angle [X.XX]  User-specified twilight-angle (degrees). Default: 0.

Sunrise/sunset. Only useful with major-options: 'wait' and 'list'. Any of: (default: both)
    rise          Wait for the sun to rise past specified twilight & offset.
    set           Wait for the sun to  set past specified twilight & offset.

Offset:
    offset [MM|HH:MM] Time interval (+ve towards noon) to adjust twilight calculation.

Target date. Only useful with major-options: 'report' or 'list'. Default: today
    d [DD]        Set the target Day-of-Month to calculate for. 1 to 31.
    m [MM]        Set the target Month to calculate for. 1 to 12.
    y [YYYY]      Set the target Year to calculate for. 2000 to 2099.

latitude/longitude coordinates: floating-point degrees, with [NESW] appended. Default: Bingham, England.

Exit (return) codes:
    0           OK: exit from 'wait' or 'list' only.
    1           Error.
    2           Exit from 'poll': it is DAY or twilight.
    3           Exit from 'poll': it is NIGHT (after twilight).

Example 1: sunwait wait rise offset -1:15:10 51.477932N 0.000000E
Wait until 1 hour 15 minutes 10 secs before the sun rises in Greenwich, London.

Example 2: sunwait list 7 civil 55.752163N 37.617524E
List civil sunrise and sunset times for today and next 6 days. Moscow.

Example 3: sunwait poll exit angle 10 54.897786N -1.517536E
Indicate by program exit-code if is Day or Night using a custom twilight angle of 10 degrees above horizon. Washington, UK.

Example 4: sunwait list 7 gmt sunrise angle 3
List next 7 days sunrise times, custom +3 degree twilight angle, default location.
Uses GMT; as any change in daylight saving over the specified period is not considered.

Note that program uses C library functions to determine time and localtime.
Error for timings are estimated at: +/- 4 minutes.


Projects page, Dan Risacher