Skip to content

Instantly share code, notes, and snippets.

@ablwr
Created September 24, 2016 15:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ablwr/127b3053d0f1a0316230b65f8bef6ccb to your computer and use it in GitHub Desktop.
Save ablwr/127b3053d0f1a0316230b65f8bef6ccb to your computer and use it in GitHub Desktop.
get the moon in your bash profile
function moonphase() {
/usr/bin/env ruby <<-EORUBY
# Convert a date to Julian.
def julian(year, month, day)
a = (14-month)/12
y = year+4800-a
m = (12*a)-3+month
return day + (153*m+2)/5 + (365*y) + y/4 - y/100 + y/400 - 32045
end
# Define the phases of the moon.
def phase(year, month, day)
p = (julian(year, month, day) - julian(2000, 1, 6)) % 29.530588853
if p < 1.84566 then return "πŸŒ‘" # new
elsif p < 5.53699 then return "πŸŒ’" # waxing crescent
elsif p < 9.22831 then return "πŸŒ“" # first quarter
elsif p < 12.91963 then return "πŸŒ”" # waxing gibbous
elsif p < 16.61096 then return "πŸŒ•" # full
elsif p < 20.30228 then return "πŸŒ–" # waning gibbous
elsif p < 23.99361 then return "πŸŒ—" # last quarter
elsif p < 27.68493 then return "🌘" # waning crescent
else return "πŸŒ‘" # new
end
end
# Get today's phase.
today = Time.new
print "#{phase(today.year, today.month, today.day)}"
EORUBY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment