SayoriOS 0.3.3

calendar.c
1 // Calendar - original code by NDRAEY for SayoriOS (c) 2023
2
3 #include "common.h"
4 #include "drv/cmos.h"
5 #include "io/tty.h"
6
7 #define printf(M, ...) _tty_printf(M, ##__VA_ARGS__)
8
9 unsigned int days_in_months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
10
11 char* months[12] = {
12 "January",
13 "February",
14 "March",
15 "April",
16 "May",
17 "June",
18 "July",
19 "August",
20 "September",
21 "October",
22 "November",
23 "December"
24 };
25
26 unsigned int dayofweek(unsigned int d, unsigned int m, unsigned int y) {
27 int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
28
29 y -= m < 3;
30
31 return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
32 }
33
34 bool is_leap(uint32_t year) {
35 return (year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0);
36 }
37
38 unsigned int days_in_month(unsigned int month, unsigned int year) {
39 if(month == 1) {
40 return is_leap(year) ? 29 : 28;
41 }
42
43 return days_in_months[month];
44 }
45
46 int month_by_name(char* name, int fallback) {
47 for(int i = 0; i < 12; i++) {
48 if(strcmp(months[i], name) == 0) {
49 return i;
50 }
51 }
52
53 return fallback;
54 }
55
56 void calendar(int argc, char** argv) {
57 sayori_time_t tm = get_time();
58
59 // In SayoriOS month starts from 1, not 0.
60 tm.month -= 1;
61
62 int month = tm.month;
63
64 if(argc == 2) {
65 month = month_by_name(argv[argc - 1], month);
66 }
67
68 char* days_of_week[7] = {
69 "Sunday",
70 "Monday",
71 "Tuesday",
72 "Wednesday",
73 "Thursday",
74 "Friday",
75 "Saturday"
76 };
77
78 int year = tm.year;
79
80 printf("%10s %d\n", months[month], year);
81
82 for(int i = 0; i < 7; i++) {
83 // printf("%.2s ", days_of_week[i]);
84 printf("%c%c ", days_of_week[i][0], days_of_week[i][1]);
85 }
86
87 printf("\n");
88
89 unsigned int first_day = dayofweek(1, month + 1, year);
90
91 printf("%*s", 3 * (first_day), "");
92
93 for(unsigned int i = 1; i <= days_in_month(month, year); i++) {
94 unsigned int dow = dayofweek(i, month + 1, year);
95
96 if(dow == 0 && i != 1) {
97 printf("\n");
98 }
99
100 if((int)i == tm.day && tm.month == month) {
101 // printf("\033[7m%2d\033[0m ", i);
102
103 tty_setcolor(0xff00ff);
104 printf("%2d ", i);
105 tty_setcolor(VESA_WHITE);
106
107 continue;
108 }
109
110 printf("%2d ", i);
111 }
112
113 printf("\n\n");
114 }
get_time
sayori_time_t get_time()
Считывает время и передает в удобной структуре
Definition: cmos.c:144
common.h
Основные определения ядра
strcmp
int strcmp(const char *s1, const char *s2)
Сравнение строк
Definition: string.c:253
sayori_time
Definition: cmos.h:5
tm
Definition: portability.h:97
tty_setcolor
void tty_setcolor(uint32_t color)
Изменение цвета текста
Definition: tty.c:108