more documentation.
[apps/madmutt.git] / lib-lib / date.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
23  *
24  * This file is part of mutt-ng, see http://www.muttng.org/.
25  * It's licensed under the GNU General Public License,
26  * please see the file GPL in the top level source directory.
27  */
28
29 #include "lib-lib.h"
30
31 /* returns the seconds east of UTC given `g' and its corresponding gmtime()
32    representation */
33 static time_t compute_tz(time_t g, struct tm *utc)
34 {
35     struct tm *lt = localtime (&g);
36     time_t t;
37     int yday;
38
39     t    = (lt->tm_hour - utc->tm_hour) * 3600
40          + (lt->tm_min - utc->tm_min) * 60;
41     yday = (lt->tm_yday - utc->tm_yday);
42
43     if (yday) {
44         /* This code is optimized to negative timezones (West of Greenwich) */
45         if (yday == -1 ||           /* UTC passed midnight before localtime */
46             yday > 1)               /* UTC passed new year before localtime */
47             t -= 24 * 60 * 60;
48         else
49             t += 24 * 60 * 60;
50     }
51
52     return t;
53 }
54
55 /* Returns the local timezone in seconds east of UTC for the time t,
56    or for the current time if t is zero.  */
57 time_t mutt_local_tz(time_t t)
58 {
59     struct tm *ptm;
60     struct tm utc;
61
62     if (!t)
63         t = time(NULL);
64
65     /* need to make a copy because gmtime/localtime return a pointer to
66        static memory (grr!) */
67     ptm = gmtime(&t);
68     utc = *ptm;
69     return compute_tz(t, &utc);
70 }
71
72 /* converts struct tm to time_t, but does not take the local timezone into
73    account unless ``local'' is nonzero */
74 time_t mutt_mktime(struct tm *t, int local)
75 {
76     static short const AccumDaysPerMonth[12] = {
77         0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
78     };
79
80     time_t g;
81
82     /* Compute the number of days since January 1 in the same year */
83     g = AccumDaysPerMonth[t->tm_mon % 12];
84
85     /* The leap years are 1972 and every 4. year until 2096,
86      * but this algoritm will fail after year 2099 */
87     g += t->tm_mday;
88     if ((t->tm_year % 4) || t->tm_mon < 2)
89         g--;
90     t->tm_yday = g;
91
92     /* Compute the number of days since January 1, 1970 */
93     g += (t->tm_year - 70) * 365;
94     g += (t->tm_year - 69) / 4;
95
96     /* Compute the number of hours */
97     g *= 24;
98     g += t->tm_hour;
99
100     /* Compute the number of minutes */
101     g *= 60;
102     g += t->tm_min;
103
104     /* Compute the number of seconds */
105     g *= 60;
106     g += t->tm_sec;
107
108     if (local)
109         g -= compute_tz(g, t);
110
111     return g;
112 }
113
114 /* Return 1 if month is February of leap year, else 0 */
115 __attribute__((const))
116 static int isLeapYearFeb(int m, int y)
117 {
118     return (m == 1)                 /* february ? */
119         && !(y & 3)                 /* multiple of 4 ? */
120         && ((y % 100) || !((y + 1900) % 400));
121                                     /* !multiple of 100, or multiple of 400 */
122 }
123
124 __attribute__((const))
125 static inline int nbDaysInMonth(int m, int y)
126 {
127     static char const DaysPerMonth[12] = {
128         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
129     };
130
131     return DaysPerMonth[m] + isLeapYearFeb(m, y);
132 }
133
134 void mutt_normalize_time(struct tm *tm)
135 {
136     while (tm->tm_sec < 0) {
137         tm->tm_sec += 60;
138         tm->tm_min--;
139     }
140     while (tm->tm_sec >= 60) {
141         tm->tm_sec -= 60;
142         tm->tm_min++;
143     }
144     while (tm->tm_min < 0) {
145         tm->tm_min += 60;
146         tm->tm_hour--;
147     }
148     while (tm->tm_min >= 60) {
149         tm->tm_min -= 60;
150         tm->tm_hour++;
151     }
152     while (tm->tm_hour < 0) {
153         tm->tm_hour += 24;
154         tm->tm_mday--;
155     }
156     while (tm->tm_hour >= 24) {
157         tm->tm_hour -= 24;
158         tm->tm_mday++;
159     }
160     /* use loops on NNNdwmy user input values? */
161     while (tm->tm_mon < 0) {
162         tm->tm_mon += 12;
163         tm->tm_year--;
164     }
165     while (tm->tm_mon >= 12) {
166         tm->tm_mon -= 12;
167         tm->tm_year++;
168     }
169     while (tm->tm_mday <= 0) {
170         if (tm->tm_mon) {
171             tm->tm_mon--;
172         } else {
173             tm->tm_mon = 11;
174             tm->tm_year--;
175         }
176         tm->tm_mday += nbDaysInMonth(tm->tm_mon, tm->tm_year);
177     }
178
179     while (tm->tm_mday > nbDaysInMonth(tm->tm_mon, tm->tm_year)) {
180         tm->tm_mday -= nbDaysInMonth(tm->tm_mon, tm->tm_year);
181         if (tm->tm_mon < 11) {
182             tm->tm_mon++;
183         } else {
184             tm->tm_mon = 0;
185             tm->tm_year++;
186         }
187     }
188 }