1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/mutil.h>
#define SECS_PER_DAY 86400
#define ADJUSTMENT -719162L
static time_t
jan1st (int year)
{
year--; /* Do not consider the current year */
return year*365L
+ year/4L /* Years divisible by 4 are leap years */
+ year/400L /* Years divisible by 400 are always leap years */
- year/100L; /* Years divisible by 100 but not 400 aren't */
}
static int month_start[]=
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
31 28 31 30 31 30 31 31 30 31 30 31
*/
/* NOTE: ignore GCC warning. The precedence of operators is OK here */
#define leap_year(y) ((y) % 4 == 0 && (y) % 100 != 0 || (y) % 400 == 0)
static int
dayofyear (time_t *pday, int year, int month, int day)
{
int leap, month_days;
if (year < 0 || month < 0 || month > 11)
return -1;
leap = leap_year (year);
month_days = month_start[month + 1] - month_start[month]
+ ((month == 2) ? leap : 0);
if (day < 0 || day > month_days)
return -1; /* Illegal Date */
if (month <= 2)
leap = 0;
*pday = month_start[month] + day + leap;
return 0;
}
/* Convert struct tm into time_t, taking into account timezone offset. */
/* FIXME: It does not take DST into account */
time_t
mu_tm2time (struct tm *tm, mu_timezone *tz)
{
time_t t;
if (dayofyear (&t, tm->tm_year, tm->tm_mon, tm->tm_mday - 1))
return -1;
t = (t + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
+ (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
- tz->utc_offset;
return t;
}
/* Convert time 0 at UTC to our localtime, that tells us the offset
of our current timezone from UTC. */
time_t
mu_utc_offset (void)
{
time_t t = 0;
struct tm *tm = gmtime (&t);
return - mktime (tm);
}
static const char *months[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
};
static const char *wdays[] =
{
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
};
int
mu_parse_imap_date_time (const char **p, struct tm *tm, mu_timezone *tz)
{
int year, mon, day, hour, min, sec;
char zone[6] = "+0000"; /* ( "+" / "-" ) hhmm */
char month[5] = "";
int hh = 0;
int mm = 0;
int sign = 1;
int scanned = 0, scanned3;
int i;
int tzoffset;
day = mon = year = hour = min = sec = 0;
memset (tm, 0, sizeof (*tm));
switch (sscanf (*p,
"%2d-%3s-%4d%n %2d:%2d:%2d %5s%n",
&day, month, &year, &scanned3, &hour, &min, &sec, zone,
&scanned))
{
case 3:
scanned = scanned3;
break;
case 7:
break;
default:
return -1;
}
tm->tm_sec = sec;
tm->tm_min = min;
tm->tm_hour = hour;
tm->tm_mday = day;
for (i = 0; i < 12; i++)
{
if (strncasecmp (month, months[i], 3) == 0)
{
mon = i;
break;
}
}
tm->tm_mon = mon;
tm->tm_year = (year > 1900) ? year - 1900 : year;
tm->tm_yday = 0; /* unknown. */
tm->tm_wday = 0; /* unknown. */
#if HAVE_STRUCT_TM_TM_ISDST
tm->tm_isdst = -1; /* unknown. */
#endif
hh = (zone[1] - '0') * 10 + (zone[2] - '0');
mm = (zone[3] - '0') * 10 + (zone[4] - '0');
sign = (zone[0] == '-') ? -1 : +1;
tzoffset = sign * (hh * 60 * 60 + mm * 60);
#if HAVE_STRUCT_TM_TM_GMTOFF
tm->tm_gmtoff = tzoffset;
#endif
if (tz)
{
tz->utc_offset = tzoffset;
tz->tz_name = NULL;
}
*p += scanned;
return 0;
}
/* "ctime" format is: Thu Jul 01 15:58:27 1999, with no trailing \n. */
int
mu_parse_ctime_date_time (const char **p, struct tm *tm, mu_timezone * tz)
{
int wday = 0;
int year = 0;
int mon = 0;
int day = 0;
int hour = 0;
int min = 0;
int sec = 0;
int n = 0;
int i;
char weekday[5] = "";
char month[5] = "";
if (sscanf (*p, "%3s %3s %2d %2d:%2d:%2d %d%n\n",
weekday, month, &day, &hour, &min, &sec, &year, &n) != 7)
return -1;
*p += n;
for (i = 0; i < 7; i++)
{
if (strncasecmp (weekday, wdays[i], 3) == 0)
{
wday = i;
break;
}
}
for (i = 0; i < 12; i++)
{
if (strncasecmp (month, months[i], 3) == 0)
{
mon = i;
break;
}
}
if (tm)
{
memset (tm, 0, sizeof (struct tm));
tm->tm_sec = sec;
tm->tm_min = min;
tm->tm_hour = hour;
tm->tm_mday = day;
tm->tm_wday = wday;
tm->tm_mon = mon;
tm->tm_year = (year > 1900) ? year - 1900 : year;
#ifdef HAVE_STRUCT_TM_TM_ISDST
tm->tm_isdst = -1; /* unknown. */
#endif
}
/* ctime has no timezone information, set tz to UTC if they ask. */
if (tz)
memset (tz, 0, sizeof (struct mu_timezone));
return 0;
}
|