Last active
August 29, 2015 14:20
-
-
Save chochos/8ffb7944ed0ea989e80c to your computer and use it in GitHub Desktop.
Calendar in Ceylon, similar to *nix `cal`
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.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) }); | |
} | |
} | |
} |
I've modified the code, the output is still the same but I think I like it better now.
I think it’s missing an import for saturday
now
thanks @lucaswerkmeister good catch
@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.
Forgot about the "a<b<c" thing. And the original output is 2-char weekday names that's why I just cut the string.
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
That's cool.
Maybe you could add this to to https://github.com/ceylon/ceylon-examples