Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * 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 = `<span class="calendar-title">${this.months[this.month]} ${this.year}</span><br/>\n`;
        let table = `${title}<table><tr>\n`
        // print header
        for(let i = 0; i < this.days.length; ++i) {
            let nameOfDay = this.days[(i+this.firstDay)%7];
            table += `<th>${nameOfDay}</th>`;
        }
        table += "</tr>\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 += "<td> </td>"
        }
        let firstWeek = "";
        let d = 1;
        for (; d + diff <= 7; d++) {
            firstWeek += `<td>${d}</td>`
        }
        table += `<tr>${space}${firstWeek}</tr>`;
        // append weeks to table
        let week = "";
        let lastDateOfMonth = this.daysInMonth[this.month];
        if (isLeapYear(this.year)) {
            lastDateOfMonth = 29;
        }
        for(;d <= lastDateOfMonth; ++d) {
            week += `<td>${d}</td>`;
            if ( (d + diff) % 7 === 0 ) {
                table += `<tr>${week}</tr>\n`;
                week = "";
            }
        }
        if (week !== "") {
            table += `<tr>${week}</tr>\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);
};