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