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