1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

LibCore: Moved cal.cpp functions to DateTime

This commit is contained in:
rhin123 2020-03-10 16:41:01 -05:00 committed by Andreas Kling
parent 46a897b59b
commit 08a30a4961
3 changed files with 75 additions and 23 deletions

View file

@ -25,6 +25,7 @@
*/
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@ -43,26 +44,6 @@ int target_day;
int current_year;
int current_month;
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;
}
void append_to_print(char* buffer, int row, int column, char* text)
{
int starting_point = (line_width * row) + (column * column_width);
@ -84,10 +65,10 @@ void insert_month_to_print(int column, int month, int year)
sprintf(temp_buffer, "Su Mo Tu We Th Fr Sa");
append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
printing_row++;
int day_to_print = 1;
int first_day_of_week_for_month = day_of_week(1, month, year);
int days_in_the_month = get_number_of_days(month, year);
auto date_time = Core::DateTime::create(year, month, 1);
int first_day_of_week_for_month = date_time.weekday();
int days_in_the_month = date_time.days_in_month();
int last_written_chars = 0;
for (int i = 1; day_to_print <= days_in_the_month; ++i) {
if (i - 1 < first_day_of_week_for_month) {