Discordian date: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added missing </lang>)
No edit summary
Line 6: Line 6:
=={{header|Python}}==
=={{header|Python}}==


<lang python>import datetime
<lang python>import datetime, calendar


DISCORDIAN_SEASONS = ["Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"]
DISCORDIAN_SEASONS = ["Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"]
Line 12: Line 12:
def ddate(year, month, day):
def ddate(year, month, day):
today = datetime.date(year, month, day)
today = datetime.date(year, month, day)
is_leap_year = True
is_leap_year = calendar.isleap(year)
if is_leap_year and month == 2 and day == 29:
return "St. Tib's Day, YOLD " + (year + 1166)
day_of_year = today.timetuple().tm_yday - 1
try:
leap_day = datetime.date(year, 2, 29)
if leap_day == today:
return "St. Tib's Day, YOLD " + (year + 1166)
except ValueError:
is_leap_year = False
day_of_year = (today - datetime.date(year, 1, 1)).days
if is_leap_year and day_of_year >= 60:
if is_leap_year and day_of_year >= 60:
day_of_year -= 1 # Compensate for St. Tib's Day
day_of_year -= 1 # Compensate for St. Tib's Day
season = day_of_year // 73
season, dday = divmod(day_of_year, 73)
dday = day_of_year - (season * 73)
return "%s %d, YOLD %d" % (DISCORDIAN_SEASONS[season], dday + 1, year + 1166)
return "%s %d, YOLD %d" % (DISCORDIAN_SEASONS[season], (dday + 1), (year + 1166))
</lang>
</lang>

Revision as of 06:19, 21 July 2010

Task
Discordian date
You are encouraged to solve this task according to the task description, using any language you may know.

Convert a given date from the Gregorian calendar to the Discordian calendar.

See Also

Python

<lang python>import datetime, calendar

DISCORDIAN_SEASONS = ["Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"]

def ddate(year, month, day):

   today = datetime.date(year, month, day)
   is_leap_year = calendar.isleap(year)
   if is_leap_year and month == 2 and day == 29:
       return "St. Tib's Day, YOLD " + (year + 1166)
   
   day_of_year = today.timetuple().tm_yday - 1
   
   if is_leap_year and day_of_year >= 60:
       day_of_year -= 1 # Compensate for St. Tib's Day
   
   season, dday = divmod(day_of_year, 73)
   return "%s %d, YOLD %d" % (DISCORDIAN_SEASONS[season], dday + 1, year + 1166)

</lang>