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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 /* returns the seconds east of UTC given `g' and its corresponding gmtime()
24 static time_t compute_tz (time_t g, struct tm *utc)
26 struct tm *lt = localtime (&g);
30 t = (((lt->tm_hour - utc->tm_hour) * 60) + (lt->tm_min - utc->tm_min)) * 60;
32 if ((yday = (lt->tm_yday - utc->tm_yday)))
34 /* This code is optimized to negative timezones (West of Greenwich) */
35 if (yday == -1 || /* UTC passed midnight before localtime */
36 yday > 1) /* UTC passed new year before localtime */
45 /* Returns the local timezone in seconds east of UTC for the time t,
46 * or for the current time if t is zero.
48 time_t mutt_local_tz (time_t t)
56 /* need to make a copy because gmtime/localtime return a pointer to
57 static memory (grr!) */
58 memcpy (&utc, ptm, sizeof (utc));
59 return (compute_tz (t, &utc));
62 /* converts struct tm to time_t, but does not take the local timezone into
63 account unless ``local'' is nonzero */
64 time_t mutt_mktime (struct tm *t, int local)
68 static int AccumDaysPerMonth[12] = {
69 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
72 /* Compute the number of days since January 1 in the same year */
73 g = AccumDaysPerMonth [t->tm_mon % 12];
75 /* The leap years are 1972 and every 4. year until 2096,
76 * but this algoritm will fail after year 2099 */
78 if ((t->tm_year % 4) || t->tm_mon < 2)
82 /* Compute the number of days since January 1, 1970 */
83 g += (t->tm_year - 70) * 365;
84 g += (t->tm_year - 69) / 4;
86 /* Compute the number of hours */
90 /* Compute the number of minutes */
94 /* Compute the number of seconds */
99 g -= compute_tz (g, t);
104 /* Return 1 if month is February of leap year, else 0 */
105 static int isLeapYearFeb (struct tm *tm)
109 int y = tm->tm_year + 1900;
110 return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0)));
115 void mutt_normalize_time (struct tm *tm)
117 static char DaysPerMonth[12] = {
118 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
122 while (tm->tm_sec < 0)
127 while (tm->tm_sec >= 60)
132 while (tm->tm_min < 0)
137 while (tm->tm_min >= 60)
142 while (tm->tm_hour < 0)
147 while (tm->tm_hour >= 24)
152 /* use loops on NNNdwmy user input values? */
153 while (tm->tm_mon < 0)
158 while (tm->tm_mon >= 12)
163 while (tm->tm_mday <= 0)
172 tm->tm_mday += DaysPerMonth[tm->tm_mon] + isLeapYearFeb (tm);
174 while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] +
175 (nLeap = isLeapYearFeb (tm))))
177 tm->tm_mday -= DaysPerMonth[tm->tm_mon] + nLeap;