[f351ea24] | 1 | /* date.c |
---|
| 2 | * Routines for date handling |
---|
[85696c3] | 3 | * Copyright (C) 2010,2015 Olly Betts |
---|
[f351ea24] | 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #include "date.h" |
---|
| 25 | |
---|
| 26 | #include "debug.h" |
---|
| 27 | |
---|
| 28 | int |
---|
| 29 | is_leap_year(int year) |
---|
| 30 | { |
---|
| 31 | return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | static int lastday[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
---|
| 35 | |
---|
| 36 | int |
---|
| 37 | last_day(int year, int month) |
---|
| 38 | { |
---|
| 39 | SVX_ASSERT(month >= 1 && month <= 12); |
---|
| 40 | return (month == 2 && is_leap_year(year)) ? 29 : lastday[month - 1]; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int |
---|
| 44 | days_since_1900(int y, int m, int d) |
---|
| 45 | { |
---|
| 46 | static const int m_to_d[12] = { |
---|
| 47 | 0 - (1900 * 365) - 461 + 365, |
---|
| 48 | 31 - (1900 * 365) - 461 + 365, |
---|
| 49 | 59 - (1900 * 365) - 461, |
---|
| 50 | 90 - (1900 * 365) - 461, |
---|
| 51 | 120 - (1900 * 365) - 461, |
---|
| 52 | 151 - (1900 * 365) - 461, |
---|
| 53 | 181 - (1900 * 365) - 461, |
---|
| 54 | 212 - (1900 * 365) - 461, |
---|
| 55 | 243 - (1900 * 365) - 461, |
---|
| 56 | 273 - (1900 * 365) - 461, |
---|
| 57 | 304 - (1900 * 365) - 461, |
---|
| 58 | 334 - (1900 * 365) - 461 |
---|
| 59 | }; |
---|
| 60 | if (m < 3) |
---|
| 61 | --y; |
---|
| 62 | return d + (y * 365) + m_to_d[m - 1] + (y / 4) - (y / 100) + (y / 400); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void |
---|
| 66 | ymd_from_days_since_1900(int days, int * py, int * pm, int * pd) |
---|
| 67 | { |
---|
| 68 | int g, dg, c, dc, b, db, a, da, y, m; |
---|
| 69 | days += 693901; |
---|
| 70 | g = days / 146097; |
---|
| 71 | dg = days % 146097; |
---|
| 72 | c = (dg / 36524 + 1) * 3 / 4; |
---|
| 73 | dc = dg - c * 36524; |
---|
| 74 | b = dc / 1461; |
---|
| 75 | db = dc % 1461; |
---|
| 76 | a = (db / 365 + 1) * 3 / 4; |
---|
| 77 | da = db - a * 365; |
---|
| 78 | y = g * 400 + c * 100 + b * 4 + a; |
---|
| 79 | m = (da * 5 + 308) / 153; |
---|
| 80 | *py = y + m / 12; |
---|
| 81 | *pm = m % 12 + 1; |
---|
| 82 | *pd = da - (m + 2) * 153 / 5 + 123; |
---|
| 83 | } |
---|
| 84 | |
---|
[85696c3] | 85 | double |
---|
| 86 | julian_date_from_days_since_1900(int days) |
---|
| 87 | { |
---|
| 88 | int g, dg, c, dc, b, db, a, da, y, m; |
---|
| 89 | int days_in = days; |
---|
| 90 | int dys; |
---|
| 91 | double scale; |
---|
| 92 | days += 693901; |
---|
| 93 | g = days / 146097; |
---|
| 94 | dg = days % 146097; |
---|
| 95 | c = (dg / 36524 + 1) * 3 / 4; |
---|
| 96 | dc = dg - c * 36524; |
---|
| 97 | b = dc / 1461; |
---|
| 98 | db = dc % 1461; |
---|
| 99 | a = (db / 365 + 1) * 3 / 4; |
---|
| 100 | da = db - a * 365; |
---|
| 101 | y = g * 400 + c * 100 + b * 4 + a; |
---|
| 102 | m = (da * 5 + 308) / 153; |
---|
| 103 | y = y + m / 12; |
---|
| 104 | /* dys is days since 1900 for the start of the year y. */ |
---|
| 105 | dys = (y - 1900) * 365 - 460; |
---|
| 106 | dys += ((y - 1) / 4) - ((y - 1) / 100) + ((y - 1) / 400); |
---|
| 107 | scale = (is_leap_year(y) ? 1 / 366.0 : 1 / 365.0); |
---|
| 108 | return y + scale * (days_in - dys); |
---|
| 109 | } |
---|