1 /* date.c - date conversions */
2 
3 #include <time.h>
4 #include <stdio.h>
5 #include <ctype.h>
6 
7 static int isdst = -1;
8 
initDst()9 static void initDst()
10 {
11   time_t now;
12 
13   now = time(NULL);
14   isdst = !!(localtime(&now)->tm_isdst);
15 }
16 
ftscDate2UnixDate(char * ftscDate)17 time_t ftscDate2UnixDate(char *ftscDate)
18 {
19   struct tm tm;
20   char monthS[21]; // in case the date-field is trashed
21   int i;
22 
23   if (isdst == -1) initDst();
24 
25   sscanf(ftscDate, "%d %s %d %d:%d:%d", &tm.tm_mday, monthS,
26 	 &tm.tm_year, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
27   if ((tm.tm_mday < 1) || (tm.tm_mday > 31)) tm.tm_mday = 1;
28   if ((tm.tm_year < 0) || (tm.tm_year > 99)) tm.tm_year = 0;
29   if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) tm.tm_hour = 0;
30   if ((tm.tm_min < 0) || (tm.tm_min > 59)) tm.tm_min = 0;
31   if ((tm.tm_sec < 0) || (tm.tm_sec > 59)) tm.tm_sec = 0;
32   if (tm.tm_year < 80) tm.tm_year += 100; // years since 1900 !
33   monthS[3] = 0;
34   for (i = 0; i < 3; i++) monthS[i] = tolower(monthS[i]);
35   if (strcmp(monthS, "jan") == 0) tm.tm_mon = 0;
36   else if (strcmp(monthS, "feb") == 0) tm.tm_mon = 1;
37   else if (strcmp(monthS, "mar") == 0) tm.tm_mon = 2;
38   else if (strcmp(monthS, "apr") == 0) tm.tm_mon = 3;
39   else if (strcmp(monthS, "may") == 0) tm.tm_mon = 4;
40   else if (strcmp(monthS, "jun") == 0) tm.tm_mon = 5;
41   else if (strcmp(monthS, "jul") == 0) tm.tm_mon = 6;
42   else if (strcmp(monthS, "aug") == 0) tm.tm_mon = 7;
43   else if (strcmp(monthS, "sep") == 0) tm.tm_mon = 8;
44   else if (strcmp(monthS, "oct") == 0) tm.tm_mon = 9;
45   else if (strcmp(monthS, "nov") == 0) tm.tm_mon = 10;
46   else if (strcmp(monthS, "dec") == 0) tm.tm_mon = 11;
47   else tm.tm_mon = 0;
48   tm.tm_isdst = isdst;
49 
50   return mktime(&tm);
51 }
52 
53