Следующий код заимствован с этого сайта , вставляя его сюда для удобства (и в случае, если другой сайт не работает). Кажется, делать то, что вы хотите.
# Determine the moon phase of a date given
# Python code by HAB
def moon_phase(month, day, year):
ages = [18, 0, 11, 22, 3, 14, 25, 6, 17, 28, 9, 20, 1, 12, 23, 4, 15, 26, 7]
offsets = [-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9]
description = ["new (totally dark)",
"waxing crescent (increasing to full)",
"in its first quarter (increasing to full)",
"waxing gibbous (increasing to full)",
"full (full light)",
"waning gibbous (decreasing from full)",
"in its last quarter (decreasing from full)",
"waning crescent (decreasing from full)"]
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
if day == 31:
day = 1
days_into_phase = ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) + (year < 1900)) % 30)
index = int((days_into_phase + 2) * 16/59.0)
if index > 7:
index = 7
status = description[index]
# light should be 100% 15 days into phase
light = int(2 * days_into_phase * 100/29)
if light > 100:
light = abs(light - 200);
date = "%d%s%d" % (day, months[month-1], year)
return date, status, light
# put in a date you want ...
month = 5
day = 14
year = 2006 # use yyyy format
date, status, light = moon_phase(month, day, year)
print "moon phase on %s is %s, light = %d%s" % (date, status, light, '%')
Вы можете использовать модуль time
для получения текущего местного времени . Вот как я это сделал (вставьте приведенный ниже код в testrun):
import time
tm = time.localtime()
month = tm.tm_mon
day = tm.tm_mday
year = tm.tm_year
date, status, light = moon_phase(month, day, year)
print "moon phase on %s is %s, light = %d%s" % (date, status, light, '%')
Выход:
moon phase on 22Dec2009 is waxing crescent (increasing to full), light = 34%
Луна - это весело. :)