/**
* calendar.js
*
*/
/**
* 1 → Januar, 2 → Februar, ... , 12 → Dezember
*
* 0 → Sontag, 1 → Montag, ..., 6 → Samstag
* */
import {terminal} from "./dfhi.js";
let Calendar = {
months: [undefined,
"JAN", "FEB", "MÄR", "APR",
"MAI", "JUN", "JUL", "AUG",
"SEP", "OKT", "NOV", "DEZ"],
days: ["SO","MO", "DI", "MI", "DO", "FR", "SA"],
daysInMonth : [undefined,
31, 28, 31, 40,
31, 30, 31, 31,
30, 31, 30, 31
],
firstDay: 1,
// current date
date: 1,
month: 1,
year: 2021,
toString: function() {
return `${this.date}. ${this.month}. ${this.year}`;
},
showCalendar: function() {
let title = `${this.months[this.month]} ${this.year}
\n`;
let table = `${title}
\n`
// print header
for(let i = 0; i < this.days.length; ++i) {
let nameOfDay = this.days[(i+this.firstDay)%7];
table += `${nameOfDay} | `;
}
table += "
\n"
// print (empty) days of last month
let firstDateOfMonth = new Date(this.year, this.month-1, 1);
let dayOfFirstDate = firstDateOfMonth.getDay();
let diff = dayOfFirstDate - this.firstDay;
let space = "",
countSpace = diff;
while(countSpace > 0) {
--countSpace;
space += " | "
}
let firstWeek = "";
let d = 1;
for (; d + diff <= 7; d++) {
firstWeek += `${d} | `
}
table += `${space}${firstWeek}
`;
// append weeks to table
let week = "";
let lastDateOfMonth = this.daysInMonth[this.month];
if (isLeapYear(this.year)) {
lastDateOfMonth = 29;
}
for(;d <= lastDateOfMonth; ++d) {
week += `${d} | `;
if ( (d + diff) % 7 === 0 ) {
table += `${week}
\n`;
week = "";
}
}
if (week !== "") {
table += `${week}
\n`;
}
return table;
},
};
function isLeapYear(year) {
let leapYear = (year % 4 === 0);
leapYear = leapYear && (year % 100 !== 0);
leapYear = leapYear || (year % 400 === 0);
return leapYear;
}
window.main = function(...argv) {
let month = Number.parseInt(argv[0]);
let year = Number.parseInt( argv[1] || 2021 );
Calendar.month = month;
Calendar.year = year;
let cal = Calendar.showCalendar();
terminal.printh(cal);
};