RosettaCodeData/Task/Calendar/D/calendar.d

44 lines
1.3 KiB
D
Raw Permalink Normal View History

2014-01-17 05:32:22 +00:00
import std.stdio, std.datetime, std.string, std.conv;
2013-04-10 16:57:12 -07:00
2014-01-17 05:32:22 +00:00
void printCalendar(in uint year, in uint nCols)
in {
assert(nCols > 0 && nCols <= 12);
} body {
immutable rows = 12 / nCols + (12 % nCols != 0);
2013-04-10 16:57:12 -07:00
auto date = Date(year, 1, 1);
2015-02-20 00:35:01 -05:00
int offs = date.dayOfWeek;
2014-01-17 05:32:22 +00:00
const months = "January February March April May June
July August September October November December".split;
2013-04-10 16:57:12 -07:00
string[8][12] mons;
2014-01-17 05:32:22 +00:00
foreach (immutable m; 0 .. 12) {
mons[m][0] = months[m].center(21);
2013-04-10 16:57:12 -07:00
mons[m][1] = " Su Mo Tu We Th Fr Sa";
2014-01-17 05:32:22 +00:00
immutable dim = date.daysInMonth;
foreach (immutable d; 1 .. 43) {
2013-04-10 16:57:12 -07:00
immutable day = d > offs && d <= offs + dim;
immutable str = day ? format(" %2s", d-offs) : " ";
mons[m][2 + (d - 1) / 7] ~= str;
}
offs = (offs + dim) % 7;
date.add!"months"(1);
}
2014-01-17 05:32:22 +00:00
"[Snoopy Picture]".center(nCols * 24 + 4).writeln;
writeln(year.text.center(nCols * 24 + 4), "\n");
foreach (immutable r; 0 .. rows) {
string[8] s;
foreach (immutable c; 0 .. nCols) {
if (r * nCols + c > 11)
break;
foreach (immutable i, line; mons[r * nCols + c])
2013-04-10 16:57:12 -07:00
s[i] ~= format(" %s", line);
}
2014-01-17 05:32:22 +00:00
writefln("%-(%s\n%)\n", s);
2013-04-10 16:57:12 -07:00
}
}
void main() {
printCalendar(1969, 3);
}