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.
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))) {
37 /* This code is optimized to negative timezones (West of Greenwich) */
38 if (yday == -1 || /* UTC passed midnight before localtime */
39 yday > 1) /* UTC passed new year before localtime */
48 /* Returns the local timezone in seconds east of UTC for the time t,
49 * or for the current time if t is zero.
51 time_t mutt_local_tz (time_t t)
59 /* need to make a copy because gmtime/localtime return a pointer to
60 static memory (grr!) */
61 memcpy (&utc, ptm, sizeof (utc));
62 return (compute_tz (t, &utc));
65 /* converts struct tm to time_t, but does not take the local timezone into
66 account unless ``local'' is nonzero */
67 time_t mutt_mktime (struct tm * t, int local)
71 static int AccumDaysPerMonth[12] = {
72 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
75 /* Compute the number of days since January 1 in the same year */
76 g = AccumDaysPerMonth[t->tm_mon % 12];
78 /* The leap years are 1972 and every 4. year until 2096,
79 * but this algoritm will fail after year 2099 */
81 if ((t->tm_year % 4) || t->tm_mon < 2)
85 /* Compute the number of days since January 1, 1970 */
86 g += (t->tm_year - 70) * 365;
87 g += (t->tm_year - 69) / 4;
89 /* Compute the number of hours */
93 /* Compute the number of minutes */
97 /* Compute the number of seconds */
102 g -= compute_tz (g, t);
107 /* Return 1 if month is February of leap year, else 0 */
108 static int isLeapYearFeb (struct tm *tm)
110 if (tm->tm_mon == 1) {
111 int y = tm->tm_year + 1900;
113 return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0)));
118 void mutt_normalize_time (struct tm *tm)
120 static char DaysPerMonth[12] = {
121 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
125 while (tm->tm_sec < 0) {
129 while (tm->tm_sec >= 60) {
133 while (tm->tm_min < 0) {
137 while (tm->tm_min >= 60) {
141 while (tm->tm_hour < 0) {
145 while (tm->tm_hour >= 24) {
149 /* use loops on NNNdwmy user input values? */
150 while (tm->tm_mon < 0) {
154 while (tm->tm_mon >= 12) {
158 while (tm->tm_mday <= 0) {
165 tm->tm_mday += DaysPerMonth[tm->tm_mon] + isLeapYearFeb (tm);
167 while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] +
168 (nLeap = isLeapYearFeb (tm)))) {
169 tm->tm_mday -= DaysPerMonth[tm->tm_mon] + nLeap;