source: git/src/date.c @ 76cf7f1

RELEASE/1.2debug-cidebug-ci-sanitiserswalls-data
Last change on this file since 76cf7f1 was 734f5f4, checked in by Olly Betts <olly@…>, 6 years ago

Warn about 2 digit years being assumed to be 19xx

We can't change the assumption without risking breaking existing
datasets, but the further we get into this century, the more likely such
an assumption is to catch someone out. The warning can easily be
quashed by explicitly adding the assumed "19".

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/* date.c
2 * Routines for date handling
3 * Copyright (C) 2010,2015,2018 Olly Betts
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
28int
29is_leap_year(int year)
30{
31    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
32}
33
34unsigned int
35last_day(int year, int month)
36{
37    static const unsigned char lastday[13] = {
38        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
39    };
40    SVX_ASSERT(month >= 1 && month <= 12);
41    return (month == 2 && is_leap_year(year)) ? 29 : lastday[month];
42}
43
44int
45days_since_1900(int y, int m, int d)
46{
47    static const int m_to_d[12] = {
48        0 - (1900 * 365) - 461 + 365,
49        31 - (1900 * 365) - 461 + 365,
50        59 - (1900 * 365) - 461,
51        90 - (1900 * 365) - 461,
52        120 - (1900 * 365) - 461,
53        151 - (1900 * 365) - 461,
54        181 - (1900 * 365) - 461,
55        212 - (1900 * 365) - 461,
56        243 - (1900 * 365) - 461,
57        273 - (1900 * 365) - 461,
58        304 - (1900 * 365) - 461,
59        334 - (1900 * 365) - 461
60    };
61    if (m < 3)
62        --y;
63    return d + (y * 365) + m_to_d[m - 1] + (y / 4) - (y / 100) + (y / 400);
64}
65
66void
67ymd_from_days_since_1900(int days, int * py, int * pm, int * pd)
68{
69    int g, dg, c, dc, b, db, a, da, y, m;
70    days += 693901;
71    g = days / 146097;
72    dg = days % 146097;
73    c = (dg / 36524 + 1) * 3 / 4;
74    dc = dg - c * 36524;
75    b = dc / 1461;
76    db = dc % 1461;
77    a = (db / 365 + 1) * 3 / 4;
78    da = db - a * 365;
79    y = g * 400 + c * 100 + b * 4 + a;
80    m = (da * 5 + 308) / 153;
81    *py = y + m / 12;
82    *pm = m % 12 + 1;
83    *pd = da - (m + 2) * 153 / 5 + 123;
84}
85
86double
87julian_date_from_days_since_1900(int days)
88{
89    int g, dg, c, dc, b, db, a, da, y, m;
90    int days_in = days;
91    int dys;
92    double scale;
93    days += 693901;
94    g = days / 146097;
95    dg = days % 146097;
96    c = (dg / 36524 + 1) * 3 / 4;
97    dc = dg - c * 36524;
98    b = dc / 1461;
99    db = dc % 1461;
100    a = (db / 365 + 1) * 3 / 4;
101    da = db - a * 365;
102    y = g * 400 + c * 100 + b * 4 + a;
103    m = (da * 5 + 308) / 153;
104    y = y + m / 12;
105    /* dys is days since 1900 for the start of the year y. */
106    dys = (y - 1900) * 365 - 460;
107    dys += ((y - 1) / 4) - ((y - 1) / 100) + ((y - 1) / 400);
108    scale = (is_leap_year(y) ? 1 / 366.0 : 1 / 365.0);
109    return y + scale * (days_in - dys);
110}
Note: See TracBrowser for help on using the repository browser.