Skip to content

Instantly share code, notes, and snippets.

@chochos
Last active August 29, 2015 14:20
Show Gist options
  • Save chochos/8ffb7944ed0ea989e80c to your computer and use it in GitHub Desktop.
Save chochos/8ffb7944ed0ea989e80c to your computer and use it in GitHub Desktop.
Calendar in Ceylon, similar to *nix `cal`
import ceylon.time {
today, date
}
import ceylon.time.base {
monthOf, Month, sunday, saturday
}
shared void run() {
value hoy = today();
value quarters = [ for (m in (1..12).partition(3)) m.collect(monthOf) ];
String monthHeaders([Month+] months) =>
" ".join { for (mo in months) mo.string.pad(20) };
String weekHeaders([Month+] months) =>
" ".join { for (mo in months)
" ".join { for (d in sunday..saturday) d.string.initial(2) }
};
String weekDays(Integer num, Month mo) => let (
day0 = date(hoy.year, mo, 1),
diff = day0.dayOfWeek.offset(sunday),
lastDay = mo.numberOfDays(hoy.leapYear),
weekStart = num*7-diff+1)
" ".join { for (i in weekStart:7)
if (0 < i <= lastDay)
then " "
else i.string.padLeading(2) };
for (q in quarters) {
print(monthHeaders(q));
print(weekHeaders(q));
for (w in 0:6) {
print(" ".join { for (mo in q) weekDays(w, mo) });
}
}
}
@gavinking
Copy link

@chochos in weekdays(), I prefer:

" ".join { for (i in weekStart:7)
           if (0 < i <= lastDay) 
           then i.string.padLeading(2)
           else "  " };

Also, I exposed stuff in ceylon.locale.Formats that wasn't available before. But note that the weekday abbreviations there are 3-character abbreviations, not 2-character.

@chochos
Copy link
Author

chochos commented May 22, 2015

Forgot about the "a<b<c" thing. And the original output is 2-char weekday names that's why I just cut the string.

@DiegoCoronel
Copy link

Cool, but line 26 and 27 are inverted, and with latest code of ceylon.time you can do now: value quarters = [for (m in (january..december).partition(3)) m]; :)

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