问题描述:

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 `i`

th 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:

```
how many years have passed = floor(d / days_in_year)
yyyy = 1000 + how_many_years_have_passed
```

and so on and so forth for `mm`

and `dd`

.

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.

您可能感兴趣的文章：

- sandbox - What causes this error: [ERROR] [kaa_logging.c:804] (-5) - Failed to upload log bucket, id '1' (delivery error code '1')
- javascript - Angular seo friendly urls and page reload issue
- javascript - How to add optional parameters with AngularJS router?
- Install or upload laravel 5.2 into godaddy plesk?
- Python: Print out each item in a for loop individually
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial