Discordian date: Difference between revisions
(Added PicoLisp) |
(→Tcl: Added implementation) |
||
Line 52: | Line 52: | ||
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> |
||
=={{header|Tcl}}== |
|||
<lang tcl>package require Tcl 8.5 |
|||
proc disdate {year month day} { |
|||
set now [clock scan [format %02d-%02d-%04d $day $month $year] -format %d-%m-%Y] |
|||
scan [clock format $now -format %j] %d doy |
|||
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 |
|||
} |
|||
} |
|||
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: |
|||
<pre> |
|||
Confusion 57, 3176 YOLD |
|||
Chaos 59, 3178 YOLD |
|||
St. Tib's Day, 3178 YOLD |
|||
Chaos 60, 3178 YOLD |
|||
</pre> |
Revision as of 08:53, 22 July 2010
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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} {
set now [clock scan [format %02d-%02d-%04d $day $month $year] -format %d-%m-%Y] scan [clock format $now -format %j] %d doy 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 }
} 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