File: sortdate.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (328 lines) | stat: -rw-r--r-- 7,927 bytes parent folder | download | duplicates (2)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 This file is part of SLRN.

 Copyright (c) 1994, 1999, 2007-2016 John E. Davis <jed@jedsoft.org>
 Copyright (c) 2001-2006 Thomas Schultz <tststs@gmx.de>

 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the Free
 Software Foundation; either version 2 of the License, or (at your option)
 any later version.

 This program 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 General Public License for
 more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "slrnfeat.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "jdmacros.h"
#include "sortdate.h"
#include "strutil.h"

/* This routine parses the date field of a header and returns it as a time_t
 * object.  The main difficulity is the lack of uniformity in the date formats
 * and the fact that there are leap years, etc...
 *
 * Grepping my news spool of more than 12000 articles revealed that the
 * date headers of the vast majority of headers appear in one of the
 * following forms:
 *
 *   03 Feb 1997 10:09:58 +0100
 *   5 Feb 1997 15:32:05 GMT
 *   Wed, 05 Feb 1997 11:58:33 -0800
 *
 * A few look like:
 *   Fri, 3 Jan 1997 07:35:15 -0800 (PST)
 *
 * and very few (less than 1 percent) of the articles had Date headers of the
 * form:
 *
 *   Tue, 21 Jan 1997 14:17:31 UNDEFINED
 *   02 Feb 97 14:20:35 EDT
 *   Tue, 4 Feb 1997 19:06:46 LOCAL
 *   Tue, 3 Feb 1997 11:51:27 UT
 *
 * This means that the timezone can be assumed to be GMT or specified via an
 * offset from GMT.  All of the above dates have the format:
 *
 *    [Weekday,] Day Month Year Hour:Min:Sec Timezone
 *
 * which will make the parsing somewhat simple.
 *
 * Beware: the calculations involving leap years, etc... are naive.
 */

#define THE_YEAR_0		1970L  /* no leap year */
#define IS_LEAP_YEAR(y)		(((y) % 4) == 0)

typedef struct
{
   char name[5];
   int tz;
}
Timezone_Table_Type;

static Timezone_Table_Type Timezone_Table [] =
{
   {"EST", -500},		/* US Eastern Standard Time*/
   {"CST", -600},		/* US Central */
   {"MST", -700},		/* US Mountain */
   {"PST", -800},		/* US Pacific  */
   {"EDT", -400},		/* US Eastern Daylight Time */
   {"CDT", -500},		/* US Central Daylight Time */
   {"MDT", -600},		/* US Mountain Daylight Time */
   {"PDT", -700},		/* US Pacific Daylight Time */
   {"CET", 100},		       /* Central European */
   {"MET", 100},		       /* Middle European */
   {"MEZ", 100},		       /* Middle European */
   {"MSK", 300},		       /* Moscow */
   {"HKT", 800},
   {"JST", 900},
   {"KST", 900},		       /* Korean Standard */
   {"CAST", 930},		       /* Central Autsralian */
   {"EAST", 1000},		       /* Eastern Autsralian */
   {"NZST", 1200},		       /* New Zealand Autsralian */
   {"EET", 200},		       /* Eastern European */
   {"", 0}
};

static int parse_timezone (char *t)
{
   char ch;
   Timezone_Table_Type *table;

   table = Timezone_Table;

   while (0 != (ch = table->name [0]))
     {
	if ((*t == ch) && (0 == strncmp (t, table->name, sizeof (table->name))))
	  return table->tz;

	table++;
     }

   return 0;
}

long slrn_date_to_order_parm (char *date)
{
   char ch;
   long day, month, year, minutes, seconds, hours, tz;
   int sign;

   date = slrn_skip_whitespace (date);
   if ((date == NULL) || (*date == 0))
     return 0;

   /* Look for a weekday, if found skip it */
   while (isalpha (*date))
     date++;
   if (*date == ',') date++;

   date = slrn_skip_whitespace (date);

   /* expecting "03 Feb 1997 10:09:58 +0100" */
   day = 0;
   while (ch = *date, isdigit (ch))
     {
	day = 10 * day + (long) (ch - '0');
	date++;
     }
   if ((day == 0) || (day > 31))
     return 0;
   day--;

   date = slrn_skip_whitespace (date);
   month = 0;
   switch (*date++)
     {
      default:
	return 0;
      case 'J':			       /* Jan, Jun, Jul */
	ch = *date++;
	if ((ch == 'u') || (ch == 'U'))
	  {
	     ch = *date++;
	     if ((ch == 'n') || (ch == 'N'))
	       month = 6;
	     else if ((ch == 'l') || (ch == 'L'))
	       month = 7;
	  }
	else if ((ch == 'a') || (ch == 'A'))
	  month = 1;
	break;

      case 'F': case 'f':	       /* Feb */
	month = 2;
	break;
      case 'M': case 'm':	       /* May, Mar */
	ch = *date++;
	if ((ch == 'a') || (ch == 'A'))
	  {
	     ch = *date++;
	     if ((ch == 'y') || (ch == 'Y')) month = 5;
	     else if ((ch == 'r') || (ch == 'R')) month = 3;
	  }
	break;
      case 'A': case 'a':	       /* Apr, Aug */
	ch = *date++;
	if ((ch == 'p') || (ch == 'P')) month = 4;
	else if ((ch == 'u') || (ch == 'U')) month = 8;
	break;

      case 'S': case 's':	       /* Sep */
	month = 9;
	break;
      case 'O': case 'o':	       /* Oct */
	month = 10;
	break;
      case 'N': case 'n':	       /* Nov */
	month = 11;
	break;
      case 'D': case 'd':	       /* Dec */
	month = 12;
	break;
     }

   if (month == 0)
     return 0;

   month--;

   /* skip past month onto year. */
   while (isalpha (*date))
     date++;
   date = slrn_skip_whitespace (date);

   year = 0;
   while (ch = *date, isdigit (ch))
     {
	year = year * 10 + (long) (ch - '0');
	date++;
     }
#if 1
   /* Sigh.  Y2K problem handling dates of the form 02 Feb 97 14:20:35 EDT. */
   if (year < 100)
     {
	if (year > 50)
	  year += 1900;
	else
	  year += 2000;
     }
#endif

   date = slrn_skip_whitespace (date);

   /* Now parse hh:mm:ss */
   hours = 0;
   while (ch = *date, isdigit (ch))
     {
	hours = hours * 10 + (long) (ch - '0');
	date++;
     }
   if ((ch != ':') || (hours >= 24)) return 0;
   date++;

   minutes = 0;
   while (ch = *date, isdigit (ch))
     {
	minutes = minutes * 10 + (long) (ch - '0');
	date++;
     }
   if (minutes >= 60)
     return 0;

   /* Seconds may not be present */
   seconds = 0;
   if (ch == ':')
     {
	date++;
	while (ch = *date, isdigit (ch))
	  {
	     seconds = seconds * 10 + (long) (ch - '0');
	     date++;
	  }
	if (seconds >= 60) return 0;
     }

   /* Now timezone */
   date = slrn_skip_whitespace (date);

   sign = 1;
   if (*date == '+')
     date++;
   else if (*date == '-')
     {
	sign = -1;
	date++;
     }

   tz = 0;
   while (ch = *date, isdigit(ch))
     {
	tz = tz * 10 + (long) (ch - '0');
	date++;
     }
   tz = sign * tz;

   date = slrn_skip_whitespace (date);
   if (isalpha (*date))
     {
	sign = 1;
	if (((*date != 'G') || (*(date+1) != 'M') || (*(date+2) != 'T')) &&
	    ((*date != 'U') || (*(date+1) != 'T')))
	  tz = parse_timezone (date);
     }

   /* Compute the number of days since beginning of year. */
   day = 31 * month + day;
   if (month > 1)
     {
	day -= (month * 4 + 27)/10;
	if (IS_LEAP_YEAR(year)) day++;
     }

   /* add that to number of days since beginning of time */
   year -= THE_YEAR_0;
   day += year * 365 + year / 4;
   if ((year % 4) == 3) day++;

   /* Adjust hours for timezone */
   hours -= tz / 100;
   minutes -= tz % 100;		       /* ?? */

   /* Now convert to secs */
   seconds += 60L * (minutes + 60L * (hours +  24L * day));

   return seconds;
}

/* Formats a given "date" (as understood by slrn_date_to_order_parm) according
 * to the format specification "format" (see strftime(3)) */
void slrn_strftime (char *s, size_t max, const char *format, char *date,
		    int use_localtime)
{
   time_t date_t;
   struct tm *date_tm;

   date_t = (time_t) slrn_date_to_order_parm (date);

#if defined(VMS) || defined(__BEOS__)
   (void) use_localtime; /* gmtime is broken on BEOS */
#else
   if (0 == use_localtime)
     date_tm = gmtime (&date_t);
   else
#endif
     date_tm = localtime (&date_t);

   (void) strftime (s, max, format, date_tm);
}