| 517 | |
| 518 | /** Civil date from days since 1970-01-01, after Howard Hinnant's algorithm. */ |
| 519 | const civilFromDays = (days: number): { year: number; month: number; day: number } => { |
| 520 | const z = days + 719468 |
| 521 | const era = Math.floor(z / 146097) |
| 522 | const doe = z - era * 146097 |
| 523 | const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365) |
| 524 | const y = yoe + era * 400 |
| 525 | const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100)) |
| 526 | const mp = Math.floor((5 * doy + 2) / 153) |
| 527 | const day = doy - Math.floor((153 * mp + 2) / 5) + 1 |
| 528 | const month = mp < 10 ? mp + 3 : mp - 9 |
| 529 | return { year: month <= 2 ? y + 1 : y, month, day } |
| 530 | } |
| 531 | |
| 532 | /** Days since 1970-01-01 from a civil date. */ |
| 533 | const daysFromCivil = (year: number, month: number, day: number): number => { |