Andreas Krennmair:
[apps/madmutt.git] / date.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
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.
8  * 
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.
13  * 
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.
17  */ 
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include <string.h>
25
26 /* returns the seconds east of UTC given `g' and its corresponding gmtime()
27    representation */
28 static time_t compute_tz (time_t g, struct tm *utc)
29 {
30   struct tm *lt = localtime (&g);
31   time_t t;
32   int yday;
33
34   t = (((lt->tm_hour - utc->tm_hour) * 60) + (lt->tm_min - utc->tm_min)) * 60;
35
36   if ((yday = (lt->tm_yday - utc->tm_yday)))
37   {
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 */
41       t -= 24 * 60 * 60;
42     else
43       t += 24 * 60 * 60;
44   }
45
46   return t;
47 }
48
49 /* Returns the local timezone in seconds east of UTC for the time t,
50  * or for the current time if t is zero.
51  */
52 time_t mutt_local_tz (time_t t)
53 {
54   struct tm *ptm;
55   struct tm utc;
56
57   if (!t)
58     t = time (NULL);
59   ptm = gmtime (&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));
64 }
65
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)
69 {
70   time_t g;
71
72   static int AccumDaysPerMonth[12] = {
73     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
74   };
75
76   /* Compute the number of days since January 1 in the same year */
77   g = AccumDaysPerMonth [t->tm_mon % 12];
78
79   /* The leap years are 1972 and every 4. year until 2096,
80    * but this algoritm will fail after year 2099 */
81   g += t->tm_mday;
82   if ((t->tm_year % 4) || t->tm_mon < 2)
83     g--;
84   t->tm_yday = g;
85
86   /* Compute the number of days since January 1, 1970 */
87   g += (t->tm_year - 70) * 365;
88   g += (t->tm_year - 69) / 4;
89
90   /* Compute the number of hours */
91   g *= 24;
92   g += t->tm_hour;
93
94   /* Compute the number of minutes */
95   g *= 60;
96   g += t->tm_min;
97
98   /* Compute the number of seconds */
99   g *= 60;
100   g += t->tm_sec;
101
102   if (local)
103     g -= compute_tz (g, t);
104
105   return (g);
106 }
107
108 /* Return 1 if month is February of leap year, else 0 */
109 static int isLeapYearFeb (struct tm *tm)
110 {
111   if (tm->tm_mon == 1)
112   {
113     int y = tm->tm_year + 1900;
114     return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0)));
115   }
116   return (0);
117 }
118
119 void mutt_normalize_time (struct tm *tm)
120 {
121   static char DaysPerMonth[12] = {
122     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
123   };
124   int nLeap;
125
126   while (tm->tm_sec < 0)
127   {
128     tm->tm_sec += 60;
129     tm->tm_min--;
130   }
131   while (tm->tm_sec >= 60)
132   {
133     tm->tm_sec -= 60;
134     tm->tm_min++;
135   }
136   while (tm->tm_min < 0)
137   {
138     tm->tm_min += 60;
139     tm->tm_hour--;
140   }
141   while (tm->tm_min >= 60)
142   {
143     tm->tm_min -= 60;
144     tm->tm_hour++;
145   }
146   while (tm->tm_hour < 0)
147   {
148     tm->tm_hour += 24;
149     tm->tm_mday--;
150   }
151   while (tm->tm_hour >= 24)
152   {
153     tm->tm_hour -= 24;
154     tm->tm_mday++;
155   }
156   /* use loops on NNNdwmy user input values? */
157   while (tm->tm_mon < 0)
158   {
159     tm->tm_mon += 12;
160     tm->tm_year--;
161   }
162   while (tm->tm_mon >= 12)
163   {
164     tm->tm_mon -= 12;
165     tm->tm_year++;
166   }
167   while (tm->tm_mday <= 0)
168   {
169     if (tm->tm_mon)
170       tm->tm_mon--;
171     else
172     {
173       tm->tm_mon = 11;
174       tm->tm_year--;
175     }
176     tm->tm_mday += DaysPerMonth[tm->tm_mon] + isLeapYearFeb (tm);
177   }
178   while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] + 
179         (nLeap = isLeapYearFeb (tm))))
180   {
181     tm->tm_mday -= DaysPerMonth[tm->tm_mon] + nLeap;
182     if (tm->tm_mon < 11)
183       tm->tm_mon++;
184     else
185     {
186       tm->tm_mon = 0;
187       tm->tm_year++;
188     }
189   }
190 }