Discordian date: Difference between revisions
(→Tcl: Added implementation) |
m (→{{header|Tcl}}: comment adding) |
||
Line 56: | Line 56: | ||
<lang tcl>package require Tcl 8.5 |
<lang tcl>package require Tcl 8.5 |
||
proc disdate {year month day} { |
proc disdate {year month day} { |
||
# Get the day of the year |
|||
set now [clock scan [format %02d-%02d-%04d $day $month $year] -format %d-%m-%Y] |
set now [clock scan [format %02d-%02d-%04d $day $month $year] -format %d-%m-%Y] |
||
scan [clock format $now -format %j] %d doy |
scan [clock format $now -format %j] %d doy |
||
⚫ | |||
# Handle leap years |
|||
⚫ | |||
if {$doy == 60} { |
if {$doy == 60} { |
||
return "St. Tib's Day, [expr {$year + 1166}] YOLD" |
return "St. Tib's Day, [expr {$year + 1166}] YOLD" |
||
Line 65: | Line 68: | ||
} |
} |
||
} |
} |
||
# Main conversion to discordian format now that special cases are handled |
|||
incr doy -1; # Allow div/mod to work right |
incr doy -1; # Allow div/mod to work right |
||
set season [lindex {Chaos Discord Confusion Bureaucracy {The Aftermath}} \ |
set season [lindex {Chaos Discord Confusion Bureaucracy {The Aftermath}} \ |
Revision as of 08:57, 22 July 2010
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
J
<lang j>disc=: +"1&1166 0 0 </lang>
Example use:
<lang> disc 2012 2 29 3718 29</lang>
see talk page.
PicoLisp
<lang PicoLisp>(de disdate (Year Month Day)
(let? Date (date Year Month Day) (let (Leap (date Year 2 29) D (- Date (date Year 1 1))) (if (and Leap (= 2 Month) (= 29 Day)) (pack "St. Tib's Day, YOLD " (+ Year 1166)) (and Leap (>= D 60) (dec 'D)) (pack (get '("Chaos" "Discord" "Confusion" "Bureaucracy" "The Aftermath") (inc (/ D 73)) ) " " (inc (% D 73)) ", YOLD " (+ Year 1166) ) ) ) ) )</lang>
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>
Tcl
<lang tcl>package require Tcl 8.5 proc disdate {year month day} {
# Get the day of the year set now [clock scan [format %02d-%02d-%04d $day $month $year] -format %d-%m-%Y] scan [clock format $now -format %j] %d doy
# Handle leap years if {!($year%4) && (($year%100) || !($year%400))} {
if {$doy == 60} { return "St. Tib's Day, [expr {$year + 1166}] YOLD" } elseif {$doy > 60} { incr doy -1 }
}
# Main conversion to discordian format now that special cases are handled incr doy -1; # Allow div/mod to work right set season [lindex {Chaos Discord Confusion Bureaucracy {The Aftermath}} \
[expr {$doy / 73}]]
set dos [expr {$doy % 73 + 1}] incr year 1166 return "$season $dos, $year YOLD"
}</lang> Demonstrating: <lang tcl>puts [disdate 2010 7 22]; # Today puts [disdate 2012 2 28] puts [disdate 2012 2 29] puts [disdate 2012 3 1]</lang> Output:
Confusion 57, 3176 YOLD Chaos 59, 3178 YOLD St. Tib's Day, 3178 YOLD Chaos 60, 3178 YOLD