2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 /* returns the seconds east of UTC given `g' and its corresponding gmtime()
28 static time_t compute_tz (time_t g, struct tm *utc)
30 struct tm *lt = localtime (&g);
34 t = (((lt->tm_hour - utc->tm_hour) * 60) + (lt->tm_min - utc->tm_min)) * 60;
36 if ((yday = (lt->tm_yday - utc->tm_yday)))
38 /* This code is optimized to negative timezones (West of Greenwich) */
39 if (yday == -1 || /* UTC passed midnight before localtime */
40 yday > 1) /* UTC passed new year before localtime */
49 /* Returns the local timezone in seconds east of UTC for the time t,
50 * or for the current time if t is zero.
52 time_t mutt_local_tz (time_t t)
60 /* need to make a copy because gmtime/localtime return a pointer to
61 static memory (grr!) */
62 memcpy (&utc, ptm, sizeof (utc));
63 return (compute_tz (t, &utc));
66 /* converts struct tm to time_t, but does not take the local timezone into
67 account unless ``local'' is nonzero */
68 time_t mutt_mktime (struct tm *t, int local)
72 static int AccumDaysPerMonth[12] = {
73 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
76 /* Compute the number of days since January 1 in the same year */
77 g = AccumDaysPerMonth [t->tm_mon % 12];
79 /* The leap years are 1972 and every 4. year until 2096,
80 * but this algoritm will fail after year 2099 */
82 if ((t->tm_year % 4) || t->tm_mon < 2)
86 /* Compute the number of days since January 1, 1970 */
87 g += (t->tm_year - 70) * 365;
88 g += (t->tm_year - 69) / 4;
90 /* Compute the number of hours */
94 /* Compute the number of minutes */
98 /* Compute the number of seconds */
103 g -= compute_tz (g, t);
108 /* Return 1 if month is February of leap year, else 0 */
109 static int isLeapYearFeb (struct tm *tm)
113 int y = tm->tm_year + 1900;
114 return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0)));
119 void mutt_normalize_time (struct tm *tm)
121 static char DaysPerMonth[12] = {
122 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
126 while (tm->tm_sec < 0)
131 while (tm->tm_sec >= 60)
136 while (tm->tm_min < 0)
141 while (tm->tm_min >= 60)
146 while (tm->tm_hour < 0)
151 while (tm->tm_hour >= 24)
156 /* use loops on NNNdwmy user input values? */
157 while (tm->tm_mon < 0)
162 while (tm->tm_mon >= 12)
167 while (tm->tm_mday <= 0)
176 tm->tm_mday += DaysPerMonth[tm->tm_mon] + isLeapYearFeb (tm);
178 while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] +
179 (nLeap = isLeapYearFeb (tm))))
181 tm->tm_mday -= DaysPerMonth[tm->tm_mon] + nLeap;