Skip to content

Instantly share code, notes, and snippets.

@gavinking
Created May 8, 2015 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavinking/8a1ede8380a23bd5318f to your computer and use it in GitHub Desktop.
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.
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))));
}
}
}
@gavinking
Copy link
Author

Original code here: https://gist.github.com/chochos/8ffb7944ed0ea989e80c

My version uses map() instead of comprehensions, and ceylon.locale for the month and day names. It's also lightly refactored for understandability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment