Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <LibCore/ArgsParser.h>
28#include <stdio.h>
29#include <time.h>
30
31const int line_width = 70;
32const int line_count = 8;
33const int column_width = 22;
34
35char print_buffer[line_width * line_count];
36char temp_buffer[line_width * 8];
37
38int target_year;
39int target_month;
40int target_day;
41
42int current_year;
43int current_month;
44
45int day_of_week(int day, int month, int year)
46{
47 static const int seek_table[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
48 if (month < 3)
49 --year;
50
51 return (year + year / 4 - year / 100 + year / 400 + seek_table[month - 1] + day) % 7;
52}
53
54int get_number_of_days(int month, int year)
55{
56 bool is_leap_year = ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
57 bool is_long_month = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
58
59 if (month == 2)
60 return is_leap_year ? 29 : 28;
61
62 return is_long_month ? 31 : 30;
63}
64
65void append_to_print(char* buffer, int row, int column, char* text)
66{
67 int starting_point = (line_width * row) + (column * column_width);
68 for (int i = 0; text[i] != '\0'; i++) {
69 buffer[starting_point + i] = text[i];
70 }
71}
72
73void insert_month_to_print(int column, int month, int year)
74{
75 int printing_column = column;
76 int printing_row = 0;
77
78 // FIXME: Both the month name and month header text should be provided by a locale
79 sprintf(temp_buffer, " %02u - %04u ", month, year);
80 append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
81 printing_row++;
82
83 sprintf(temp_buffer, "Su Mo Tu We Th Fr Sa");
84 append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
85 printing_row++;
86
87 int day_to_print = 1;
88 int first_day_of_week_for_month = day_of_week(1, month, year);
89 int days_in_the_month = get_number_of_days(month, year);
90 int last_written_chars = 0;
91 for (int i = 1; day_to_print <= days_in_the_month; ++i) {
92 if (i - 1 < first_day_of_week_for_month) {
93 last_written_chars += sprintf(temp_buffer + last_written_chars, " ");
94 } else {
95 if (year == current_year && month == current_month && target_day == day_to_print) {
96 // FIXME: To replicate Unix cal it would be better to use "\x1b[30;47m%2d\x1b[0m " in here instead of *.
97 // However, doing that messes up the layout.
98 last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d*", day_to_print);
99 } else {
100 last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d ", day_to_print);
101 }
102 day_to_print++;
103 }
104
105 append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
106
107 if (i % 7 == 0) {
108 printing_row++;
109 memset(temp_buffer, ' ', line_width * 8);
110 temp_buffer[line_width * 8 - 1] = '\0';
111 last_written_chars = 0;
112 }
113 }
114}
115
116void clean_buffers()
117{
118 for (int i = 1; i < line_width * line_count; ++i) {
119 print_buffer[i - 1] = i % line_width == 0 ? '\n' : ' ';
120 }
121 print_buffer[line_width * line_count - 1] = '\0';
122
123 for (int i = 0; i < line_width; ++i) {
124 temp_buffer[i] = ' ';
125 }
126 temp_buffer[line_width - 1] = '\0';
127}
128
129int main(int argc, char** argv)
130{
131 int day = 0;
132 int month = 0;
133 int year = 0;
134
135 Core::ArgsParser args_parser;
136 // FIXME: This should ensure two values get parsed as month + year
137 args_parser.add_positional_argument(day, "Day of year", "day", Core::ArgsParser::Required::No);
138 args_parser.add_positional_argument(month, "Month", "month", Core::ArgsParser::Required::No);
139 args_parser.add_positional_argument(year, "Year", "year", Core::ArgsParser::Required::No);
140 args_parser.parse(argc, argv);
141
142 time_t now = time(nullptr);
143 auto* tm = localtime(&now);
144
145 // Hack: workaround two values parsing as day + month.
146 if (day && month && !year) {
147 year = month;
148 month = day;
149 day = 0;
150 }
151
152 bool year_mode = !day && !month && year;
153
154 if (!year)
155 year = tm->tm_year + 1900;
156 if (!month)
157 month = tm->tm_mon + 1;
158 if (!day)
159 day = tm->tm_mday;
160
161 current_year = year;
162 current_month = month;
163
164 clean_buffers();
165
166 if (year_mode) {
167 printf(" ");
168 printf("Year %4d", year);
169 printf(" \n\n");
170
171 for (int i = 1; i < 12; ++i) {
172 insert_month_to_print(0, i++, year);
173 insert_month_to_print(1, i++, year);
174 insert_month_to_print(2, i, year);
175 printf(print_buffer);
176 printf("\n");
177 clean_buffers();
178 }
179 } else {
180 insert_month_to_print(0, month, year);
181 printf(print_buffer);
182 printf("\n\n");
183 clean_buffers();
184 }
185
186 return 0;
187}