* Calculates the days offset from the 1970 epoch. */
| 118 | * Calculates the days offset from the 1970 epoch. |
| 119 | */ |
| 120 | NPY_NO_EXPORT npy_int64 |
| 121 | get_datetimestruct_days(const npy_datetimestruct *dts) |
| 122 | { |
| 123 | int i, month; |
| 124 | npy_int64 year, days = 0; |
| 125 | int *month_lengths; |
| 126 | |
| 127 | year = dts->year - 1970; |
| 128 | days = year * 365; |
| 129 | |
| 130 | /* Adjust for leap years */ |
| 131 | if (days >= 0) { |
| 132 | /* |
| 133 | * 1968 is the closest leap year before 1970. |
| 134 | * Exclude the current year, so add 1. |
| 135 | */ |
| 136 | year += 1; |
| 137 | /* Add one day for each 4 years */ |
| 138 | days += year / 4; |
| 139 | /* 1900 is the closest previous year divisible by 100 */ |
| 140 | year += 68; |
| 141 | /* Subtract one day for each 100 years */ |
| 142 | days -= year / 100; |
| 143 | /* 1600 is the closest previous year divisible by 400 */ |
| 144 | year += 300; |
| 145 | /* Add one day for each 400 years */ |
| 146 | days += year / 400; |
| 147 | } |
| 148 | else { |
| 149 | /* |
| 150 | * 1972 is the closest later year after 1970. |
| 151 | * Include the current year, so subtract 2. |
| 152 | */ |
| 153 | year -= 2; |
| 154 | /* Subtract one day for each 4 years */ |
| 155 | days += year / 4; |
| 156 | /* 2000 is the closest later year divisible by 100 */ |
| 157 | year -= 28; |
| 158 | /* Add one day for each 100 years */ |
| 159 | days -= year / 100; |
| 160 | /* 2000 is also the closest later year divisible by 400 */ |
| 161 | /* Subtract one day for each 400 years */ |
| 162 | days += year / 400; |
| 163 | } |
| 164 | |
| 165 | month_lengths = _days_per_month_table[is_leapyear(dts->year)]; |
| 166 | month = dts->month - 1; |
| 167 | |
| 168 | /* Add the months */ |
| 169 | for (i = 0; i < month; ++i) { |
| 170 | days += month_lengths[i]; |
| 171 | } |
| 172 | |
| 173 | /* Add the days */ |
| 174 | days += dts->day - 1; |
| 175 | |
| 176 | return days; |
| 177 | } |
no test coverage detected