Having trouble writing a calendar, I couldn't find a detailed guide answering my questions anywhere

101 views Asked by At

I need to write the code in C++ for a calendar, using structs or classes, and then I need to write:
a function that returns 1 if the year given year is a leap year.
a function that given two dates, returns the number of days beetween them.

From what I understand there's mainly two ways to represnt a date:

1)Storing the day, month, and year
2)Storing the number of days since a reference date, e.g. January 1, 1900

However, I couldn't figure out how to handle the different amount of days in different months (e.g. February). I know it must be something simple because no one seems to mention it. Of course I'm not asking you to write any code for me, but if you could give me some tips on this matter I'd be extremely grateful.

2

There are 2 answers

0
Bogdan Tkachenko On

every 4th year is a leap year, try to found specific year(not January 1, 1900) that is leap year and start count from there.

1
Howard Hinnant On

Here is a collection of low-level date algorithms, written in C++, which could form a basis of a calendar program:

chrono-Compatible Low-Level Date Algorithms

The more important algorithms are not trivial to derive, nor are they intuitively obvious. The paper explains each algorithm in excruciating detail (for those that want to learn how the algorithm works).

The algorithms are high performance.

The algorithms are valid over extremely large ranges of dates (larger than you need).

The paper includes tests to validate that the algorithms are correct.

The algorithms are in the public domain.