mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 05:55:13 +00:00
Userland: Add the cal command (#838)
This is a very simple implementation of the cal command to display a calendar into the command line. For now this only prints the current month highlighting the current day.
This commit is contained in:
parent
2eb5793d55
commit
f8a0eb616c
Notes:
sideshowbarker
2024-07-19 10:59:33 +09:00
Author: https://github.com/zlotny 🔰 Commit: https://github.com/SerenityOS/serenity/commit/f8a0eb616c9 Pull-request: https://github.com/SerenityOS/serenity/pull/838 Reviewed-by: https://github.com/bugaevc Reviewed-by: https://github.com/nicktiberi
1 changed files with 49 additions and 0 deletions
49
Userland/cal.cpp
Normal file
49
Userland/cal.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int day_of_week(int day, int month, int year)
|
||||
{
|
||||
static const int seek_table[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
|
||||
if (month < 3)
|
||||
--year;
|
||||
|
||||
return (year + year / 4 - year / 100 + year / 400 + seek_table[month - 1] + day) % 7;
|
||||
}
|
||||
|
||||
int get_number_of_days(int month, int year)
|
||||
{
|
||||
bool is_leap_year = ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
|
||||
bool is_long_month = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
|
||||
|
||||
if (month == 2)
|
||||
return is_leap_year ? 29 : 28;
|
||||
|
||||
return is_long_month ? 31 : 30;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
time_t now = time(nullptr);
|
||||
auto* tm = localtime(&now);
|
||||
int target_day = tm->tm_mday;
|
||||
int target_month = tm->tm_mon + 1;
|
||||
int target_year = tm->tm_year + 1900;
|
||||
int target_day_of_week = day_of_week(1, target_month, target_year);
|
||||
|
||||
printf(" %02u - %04u \n", target_month, target_year);
|
||||
printf("Su Mo Tu We Th Fr Sa\n");
|
||||
|
||||
for (int i = 1; i <= get_number_of_days(target_month, target_year); ++i) {
|
||||
if (i < target_day_of_week) {
|
||||
printf(" ");
|
||||
} else {
|
||||
printf(i != target_day ? "%2d" : "\x1b[30;47m%2d\x1b[0m", i);
|
||||
}
|
||||
|
||||
printf(i % 7 == 0 ? "\n" : " ");
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
Loading…
Add table
Reference in a new issue