2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
17 /* returns the seconds east of UTC given `g' and its corresponding gmtime()
19 static time_t compute_tz (time_t g, struct tm *utc)
21 struct tm *lt = localtime (&g);
25 t = (((lt->tm_hour - utc->tm_hour) * 60) + (lt->tm_min - utc->tm_min)) * 60;
27 if ((yday = (lt->tm_yday - utc->tm_yday))) {
28 /* This code is optimized to negative timezones (West of Greenwich) */
29 if (yday == -1 || /* UTC passed midnight before localtime */
30 yday > 1) /* UTC passed new year before localtime */
39 /* Returns the local timezone in seconds east of UTC for the time t,
40 * or for the current time if t is zero.
42 time_t mutt_local_tz (time_t t)
50 /* need to make a copy because gmtime/localtime return a pointer to
51 static memory (grr!) */
52 memcpy (&utc, ptm, sizeof (utc));
53 return (compute_tz (t, &utc));
56 /* converts struct tm to time_t, but does not take the local timezone into
57 account unless ``local'' is nonzero */
58 time_t mutt_mktime (struct tm * t, int local)
62 static int AccumDaysPerMonth[12] = {
63 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
66 /* Compute the number of days since January 1 in the same year */
67 g = AccumDaysPerMonth[t->tm_mon % 12];
69 /* The leap years are 1972 and every 4. year until 2096,
70 * but this algoritm will fail after year 2099 */
72 if ((t->tm_year % 4) || t->tm_mon < 2)
76 /* Compute the number of days since January 1, 1970 */
77 g += (t->tm_year - 70) * 365;
78 g += (t->tm_year - 69) / 4;
80 /* Compute the number of hours */
84 /* Compute the number of minutes */
88 /* Compute the number of seconds */
93 g -= compute_tz (g, t);
98 /* Return 1 if month is February of leap year, else 0 */
99 static int isLeapYearFeb (struct tm *tm)
101 if (tm->tm_mon == 1) {
102 int y = tm->tm_year + 1900;
104 return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0)));
109 void mutt_normalize_time (struct tm *tm)
111 static char DaysPerMonth[12] = {
112 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
116 while (tm->tm_sec < 0) {
120 while (tm->tm_sec >= 60) {
124 while (tm->tm_min < 0) {
128 while (tm->tm_min >= 60) {
132 while (tm->tm_hour < 0) {
136 while (tm->tm_hour >= 24) {
140 /* use loops on NNNdwmy user input values? */
141 while (tm->tm_mon < 0) {
145 while (tm->tm_mon >= 12) {
149 while (tm->tm_mday <= 0) {
156 tm->tm_mday += DaysPerMonth[tm->tm_mon] + isLeapYearFeb (tm);
158 while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] +
159 (nLeap = isLeapYearFeb (tm)))) {
160 tm->tm_mday -= DaysPerMonth[tm->tm_mon] + nLeap;