source: git/trunk/src/date.c @ 7bb8184

Last change on this file since 7bb8184 was 7bb8184, checked in by Olly Betts <olly@…>, 13 years ago

Retagging 1.2.0

git-svn-id: file:///home/survex-svn/survex/tags/1.2.0@3664 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/* date.c
2 * Routines for date handling
3 * Copyright (C) 2010 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
34static int lastday[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
35
36int
37last_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
43int
44days_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
65void
66ymd_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
Note: See TracBrowser for help on using the repository browser.