Modifies '*days_' to be the day offset within the year, and returns the year.
| 157 | // Modifies '*days_' to be the day offset within the year, |
| 158 | // and returns the year. |
| 159 | static int64_t days_to_yearsdays(int64_t* days_) { |
| 160 | const int64_t days_per_400years = (400 * 365 + 100 - 4 + 1); |
| 161 | // Adjust so it's relative to the year 2000 (divisible by 400) |
| 162 | int64_t days = (*days_) - (365 * 30 + 7); |
| 163 | int64_t year; |
| 164 | |
| 165 | // Break down the 400 year cycle to get the year and day within the year |
| 166 | if (days >= 0) { |
| 167 | year = 400 * (days / days_per_400years); |
| 168 | days = days % days_per_400years; |
| 169 | } else { |
| 170 | year = 400 * ((days - (days_per_400years - 1)) / days_per_400years); |
| 171 | days = days % days_per_400years; |
| 172 | if (days < 0) { |
| 173 | days += days_per_400years; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Work out the year/day within the 400 year cycle |
| 178 | if (days >= 366) { |
| 179 | year += 100 * ((days - 1) / (100 * 365 + 25 - 1)); |
| 180 | days = (days - 1) % (100 * 365 + 25 - 1); |
| 181 | if (days >= 365) { |
| 182 | year += 4 * ((days + 1) / (4 * 365 + 1)); |
| 183 | days = (days + 1) % (4 * 365 + 1); |
| 184 | if (days >= 366) { |
| 185 | year += (days - 1) / 365; |
| 186 | days = (days - 1) % 365; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | *days_ = days; |
| 192 | return year + 2000; |
| 193 | } |
| 194 | |
| 195 | // Extracts the month and year and day number from a number of days |
| 196 | static void get_date_from_days(int64_t days, int64_t* date_year, int64_t* date_month, |
no outgoing calls
no test coverage detected