source: git/src/date.c

walls-data
Last change on this file was 4c83f84, checked in by Olly Betts <olly@…>, 5 days ago

Don't check HAVE_CONFIG_H in most cases

This check is only useful for img.c, which is intended to be usable
outside of Survex (and had fallbacks for functions which may not be
available which will get used if built in a non-autotools project).
For all the other source files it's just useless boilerplate.

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