I have a task with a twist, and I don't know where to start. I need to write a function that will add days to a given date (dd/mm/yyyy), and return a date in result, but the problem is, the year not neccessarily needs to be georgian, eg. It can have 13 months, where a 13th month has 40 days. I do not need a code, I need some kind of algorithm to know, where to start. I tried to use Julian Day Number, but I cannot edit it for my needs. If anyone has suggestions, they'd be appreciated. I cannot use any wrappers like time_t

网友答案:

You can write 2 functions:

int DateToDay(const Date& date);
Date DayToDate(int day);

Which would convert Date to days spent since 00/00/00 and back in your calendar (this is straightforward, and can be expressed in generic formulas - you need to know month per Year, and days per each month which define your calendar + some logic for leap years). After that, you can add day to date by

Date result_date = DayToDate(DateToDay(start_date)+days_to_add);

网友答案:

First of all, consider the good way to store information that describe your calendar, i.e. how to store number of monthes in a year and number of days in each month. Find answer also for question: are there leap years and what the difference from non-leap ones?

Then, design logic for desired operations, like
http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
but with your specificity

网友答案:

In the most basic form, the years all have the same number of days, and every month has a specified number of days as well. So, for example, the ith month has d_i days, and there are n months in a year. We can look at this as a table:

month | 1 | 2 | 3 | ... | n
days | d1| d2| d3| ... | dn

from this a year is going to have d1+d2+d3+...+dn days.

Now, as mentioned before, the algorithm to convert a date (dd/mm/yyyy) to number of days is just going to use the table above to get the number of days that has passed since a certain date, for example, since 01/01/1000.

number of days in whole years = (yyyy - 1000) * days_in_year
number of days in whole months = sum(d_i) for all i < mm
number of days in current month = dd
total number of days = sum of the three numbers you get above

The reverse is going to be similar, you have the number of days (d) that have passed since that specific date, and you want to get the calendar date from it, so you need to find:

If you want to take leap years into account as well, you just need to adjust the algorithms a bit to account for the logic of leap years. For example, if leap years happen every 5 years, then you need to find out how many leap years were in between years 1000 and yyyy and add one day for each of those leap years. Also, you need to tell the reverse algorithm which month is going to have an extra day during the leap year.

Leap years in Gregorian calendar have a very strange pattern: they happen every four years (if the year is divisible by four it is a leap year), but not if the year is divisible by 100. However, if the year is divisible by 400, then it is a leap year. So you might need to consider a complex case like that in your algorithm.