Skip to content

Instantly share code, notes, and snippets.

@godDLL
Last active January 13, 2022 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godDLL/c247f130d2d921d023f78765d6bfd477 to your computer and use it in GitHub Desktop.
Save godDLL/c247f130d2d921d023f78765d6bfd477 to your computer and use it in GitHub Desktop.
Time to sunrize or sunset, words or fancy statusline.

Suntime Fish

Print daylight hours remaining, cache sunrize time from wttr.in
Screenshot

Installation

Download, chmod +x suntime.fish and put it in your PATH

fish is required to run this script, but doesn't have to be your SHELL to do it.


You can set a cron job to periodically update sunrize and sunset times:

0 0 */21 * *  /path/to/suntime.fish update

If no LOC is set wttr.in will use geo-ip for location.
You can set this in the script file or like this:

LOC=Stavanger,Norway suntime.fish update

Can chain with other scripts to change themes at sundown:

suntime.fish e  &&  light_theme || dark_theme
#!/usr/bin/env fish
# print hours remaining to sunrise or sunset
# v0.4 gist.github.com/godDLL/c247f130d2d921d023f78765d6bfd477
#
set -q LOC ; or set -l LOC '' #'Stavanger,Norway' # if empty uses IP geolocation
set -l FMT '%D,%s' # dawn,sunset
#set -l FMT '%S,%d' # sunrize,dusk
switch "$argv[1]"
case 'x'
if test -z "$fish_suntime"
set -l -- CMD (status filename)
$CMD update
end
printf '%s %s%s%s %s\n' $fish_suntime
set -e argv[1]
if test -n "$argv"
and suntime $argv
end
case '-h' '--help'
echo -e -- Usage:\ (status basename) "[COMMANDS] [OPTIONS]
Time until sunrise or sunset, rounded to ½ hour.
-c \tWith clock
-n \tNo color, no emoji, plain text
-t \tText with no rounding
-a \tDefault
Commands are silent when used as the only arument:
e \tReturn error if sun is down
u update\tUpdate using wttr.in and set -U in Fish
x \tPrint dawn and sunset time for export
ux \tUpdate and show the updated time
Examples:
suntime x -a \t# Show cached day and night time, and time until sunup/down
suntime e && open_night_terminal \t# Night shift :)
suntime u -a \t# Update and print time until sunup/down live
" >&2
case 'u' 'update' 'ux'
set -l -- SUN (curl wttr.in/$LOC\?format="$FMT" 2>/dev/null | string split -- ,)
if test -n "$SUN"
if string match -q --invert -- '*:*:*' "$SUN"
echo -- $SUN >&2
exit 2
end
set -l up (string split -- : $SUN[1])
set -l down (string split -- : $SUN[2])
set -U fish_suntime $SUN \n (math -s0 "3600* $up[1] + 60* $up[2] + $up[3]") (math -s0 "3600* $down[1] + 60* $down[2] + $down[3]")
else
echo -- 'Error updating dawn and sunset times' >&2
exit 1
end
test 'ux' = "$argv[1]"
and suntime x
set -e argv[1]
if test -n "$argv"
and suntime $argv
end
case '*' #'-a'
if test -z "$fish_suntime"
set -l -- CMD (status filename)
$CMD update
end
set -l D (date +%H\n%M\n%S)
set -l dS (math -s0 "3600* $D[1] + 60* $D[2] + $D[3]")
set -l dH
set -l dM
set -l TO 'sunrise'
set -l EMO '🍄 ' #◯⭘ 🌙☽
if test "$dS" -lt "$fish_suntime[5]" -a "$dS" -ge "$fish_suntime[4]"
set TO 'sunset'
test 'e' = "$argv"
and exit 0
set EMO '🌼 ' #☀⚽︎ ❂ ☀️
set dM "$fish_suntime[5] - $dS"
else if test "$dS" -ge "$fish_suntime[5]"
test 'e' = "$argv"
and exit 1
set dM "$fish_suntime[4] + 86400 - $dS"
else
test 'e' = "$argv"
and exit 1
set dM "$fish_suntime[4] - $dS"
end
set dH (math -s0 "( $dM ) /3600")
set dM (math -s0 "( $dM - $dH *3600) /60")
set -l tM 'minutes'
test 1 = "$dM"
and set tM 'minute'
if contains -- '-t' $argv
if contains -- '-c' $argv
printf '%s:%s.%s, ' $D
end
if test 0 = "$dH"
printf '%s %s to %s\n' $dM $tM $TO
else
set -l tH 'hours'
test 1 = "$dH"
and set tH 'hour'
printf '%s %s %s %s to %s\n' $dH $tH $dM $tM $TO
end
exit
end
if test 0 = "$dH"
if test 45 -le "$dM"
set dH 'about an hour'
else if test 30 -le "$dM"
set dH 'about ½ hour'
else
if test 1 -ge "$dM"
set dH 'a minute'
else
set dH "$dM $tM"
end
end
if contains -- '-n' $argv
if contains -- '-c' $argv
printf '%s:%s.%s, ' $D
end
set dH (string replace -- '½ ' 'half-' $dH)
printf '%s to %s\n' $dH $TO
else
if contains -- '-c' $argv
printf '\e[1m%s:%s\e[2m.%s\e[22m' $D
end
printf ' \e[2m%s\e[22m %s\e[2m to %s\e[22m\n' $EMO $dH $TO
end
else # >= 1hr
set -l tH 'hours'
if test 1 = "$dH"
if test 45 -le "$dM"
set dH 2
else if test 15 -le "$dM"
set dH "$dH½"
else
set tH 'hour'
end
else if test 45 -le "$dM"
set dH (math -s0 "1 + $dH")
else if test 15 -le "$dM"
set dH "$dH½"
end
if contains -- '-n' $argv
if contains -- '-c' $argv
printf '%s:%s.%s, ' $D
end
set dH (string replace -- '½' ' and a half' $dH)
printf '%s %s to %s\n' $dH $tH $TO
else
if contains -- '-c' $argv
printf '\e[1m%s:%s\e[2m.%s\e[22m' $D
end
printf ' \e[2m%s\e[22m %s %s\e[2m to %s\e[22m\n' $EMO $dH $tH $TO
end
end
end #switch
@godDLL
Copy link
Author

godDLL commented Nov 28, 2021

License MIT, free to do whatever. Yours to fuck up.
asciinema.org/a/452003
Screen Shot 2021-11-28 at 09 45 55

@godDLL
Copy link
Author

godDLL commented Nov 29, 2021

v0.2 with more logic, less math and bugs

* use unix `date` for time diff
+ added non-rounded time with -t

@godDLL
Copy link
Author

godDLL commented Nov 30, 2021

v0.3 with even less bugs and more options

* now using dawn time for sunrise
+ add optional clock with -c
+ chain to other scripts with -e for error code

Screen Shot 2021-11-30 at 18 37 19

@kseistrup
Copy link

For my location, wttr.in only gets the sunset right. For ‘sunrise’, it uses ‘dawn’ (the moment when the center of the sun disk is 6% below the horizon) instead.

E.g., Copenhagen, Denmark, today: wttr.in says 07:35:01,15:39:02, but the real time are 08:20:27,15:39:52.

@godDLL
Copy link
Author

godDLL commented Dec 5, 2021

@kseistrup Correct. This is intended, for that's how I use it.

You can change it to curl wttr.in/$LOC\?format="%S,%s" if you like.
❤️

This was altered upon reflection, in v0.3:

* now using dawn time for sunrise

@kseistrup
Copy link

Ah, right… Thanks!

Yes, I like sunrise better than dawn in this context.

@godDLL
Copy link
Author

godDLL commented Dec 22, 2021

v0.4 ~60% faster, more flexible options

+ add combining of commands and formatting options with -a
* more emoji options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment