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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
31 /* returns the seconds east of UTC given `g' and its corresponding gmtime()
33 static time_t compute_tz(time_t g, struct tm *utc)
35 struct tm *lt = localtime (&g);
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);
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 */
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)
65 /* need to make a copy because gmtime/localtime return a pointer to
66 static memory (grr!) */
69 return compute_tz(t, &utc);
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)
76 static short const AccumDaysPerMonth[12] = {
77 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
82 /* Compute the number of days since January 1 in the same year */
83 g = AccumDaysPerMonth[t->tm_mon % 12];
85 /* The leap years are 1972 and every 4. year until 2096,
86 * but this algoritm will fail after year 2099 */
88 if ((t->tm_year % 4) || t->tm_mon < 2)
92 /* Compute the number of days since January 1, 1970 */
93 g += (t->tm_year - 70) * 365;
94 g += (t->tm_year - 69) / 4;
96 /* Compute the number of hours */
100 /* Compute the number of minutes */
104 /* Compute the number of seconds */
109 g -= compute_tz(g, t);
114 /* Return 1 if month is February of leap year, else 0 */
115 __attribute__((const))
116 static int isLeapYearFeb(int m, int y)
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 */
124 __attribute__((const))
125 static inline int nbDaysInMonth(int m, int y)
127 static char const DaysPerMonth[12] = {
128 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
131 return DaysPerMonth[m] + isLeapYearFeb(m, y);
134 void mutt_normalize_time(struct tm *tm)
136 while (tm->tm_sec < 0) {
140 while (tm->tm_sec >= 60) {
144 while (tm->tm_min < 0) {
148 while (tm->tm_min >= 60) {
152 while (tm->tm_hour < 0) {
156 while (tm->tm_hour >= 24) {
160 /* use loops on NNNdwmy user input values? */
161 while (tm->tm_mon < 0) {
165 while (tm->tm_mon >= 12) {
169 while (tm->tm_mday <= 0) {
176 tm->tm_mday += nbDaysInMonth(tm->tm_mon, tm->tm_year);
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) {