Created
May 8, 2015 20:31
-
-
Save gavinking/8a1ede8380a23bd5318f to your computer and use it in GitHub Desktop.
An internationalized version of @chochos' calendar example, with a few changes, just for fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ceylon.locale { | |
systemLocale | |
} | |
import ceylon.time { | |
today, | |
date | |
} | |
import ceylon.time.base { | |
monthOf | |
} | |
String weekHeader | |
= " ".join(systemLocale.formats.weekdayAbbreviations); | |
String monthHeader(Integer month) { | |
assert (exists name | |
= systemLocale.formats.monthNames[month-1]); | |
return name.pad(27); | |
} | |
{Integer?*} daysInRow(Integer row, Integer month) { | |
value now = today(); | |
value indent = | |
date(now.year, month, 1).dayOfWeek.integer; | |
value lastOfMonth = | |
monthOf(month).numberOfDays(now.leapYear); | |
value firstOfWeek = row*7-indent+1; | |
return (firstOfWeek:7) | |
.map((i) => 0 < i <= lastOfMonth then i); | |
} | |
String row(Integer row, Integer month) | |
=> " ".join(daysInRow(row, month) | |
.map((d) => (d?.string else "").padLeading(3))); | |
shared void run() { | |
value quarters = (1..12).partition(3); | |
for (q in quarters) { | |
print(" ".join(q.map(monthHeader))); | |
print(" ".join(q.map((m) => weekHeader))); | |
for (w in 0:6) { | |
print(" ".join(q.map((m) => row(w, m)))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original code here: https://gist.github.com/chochos/8ffb7944ed0ea989e80c
My version uses
map()
instead of comprehensions, andceylon.locale
for the month and day names. It's also lightly refactored for understandability.