More support for pre-Epoch dates for Windows.
[claws.git] / src / procheader.c
index 36298be712d220ea4b40e440c0c99163dd525f82..a9bfa5ddd058117ef2e93c2930b6decac56eff3a 100644 (file)
@@ -337,7 +337,7 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
                               gboolean full, gboolean decrypted)
 {
-       struct stat s;
+       GStatBuf s;
        FILE *fp;
        MsgInfo *msginfo;
 
@@ -791,23 +791,23 @@ static gint procheader_scan_date_string(const gchar *str,
        gint result;
        gint month_n;
        gint secfract;
-       gint zone1, zone2;
-       gchar offset_sign;
+       gint zone1 = 0, zone2 = 0;
+       gchar offset_sign, zonestr[7];
        gchar sep1;
 
        if (str == NULL)
                return -1;
 
-       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
                        weekday, day, month, year, hh, mm, ss, zone);
        if (result == 8) return 0;
 
        /* RFC2822 */
-       result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
+       result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
                        weekday, day, month, year, hh, mm, ss, zone);
        if (result == 8) return 0;
 
-       result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
+       result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
                        day, month, year, hh, mm, ss, zone);
        if (result == 7) return 0;
 
@@ -821,7 +821,7 @@ static gint procheader_scan_date_string(const gchar *str,
        if (result == 6) return 0;
 
        *ss = 0;
-       result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
                        weekday, day, month, year, hh, mm, zone);
        if (result == 7) return 0;
 
@@ -841,29 +841,35 @@ static gint procheader_scan_date_string(const gchar *str,
        *weekday = '\0';
 
        /* RFC3339 subset, with fraction of second */
-       /* TODO: Allow "Z" instead of time zone offset, since RFC allows
-        * that too. */
-       result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%1d%c%2d:%2d",
-                       year, &month_n, day, &sep1, hh, mm, ss, &secfract,
-                       &offset_sign, &zone1, &zone2);
-       if (result == 11
+       result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
+                       year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
+       if (result == 9
                        && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
                if (month_n >= 1 && month_n <= 12) {
                        strncpy2(month, monthstr+((month_n-1)*3), 4);
-                       sprintf(zone, "%c%2d%2d", offset_sign, zone1, zone2);
+                       if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
+                               strcat(zone, "+00:00");
+                       } else if (sscanf(zonestr, "%c%2d:%2d",
+                                               &offset_sign, &zone1, &zone2) == 3) {
+                               strcat(zone, zonestr);
+                       }
                        return 0;
                }
        }
 
        /* RFC3339 subset, no fraction of second */
-       result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%c%2d:%2d",
-                       year, &month_n, day, &sep1, hh, mm, ss,
-                       &offset_sign, &zone1, &zone2);
-       if (result == 10
+       result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
+                       year, &month_n, day, &sep1, hh, mm, ss, zonestr);
+       if (result == 8
                        && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
                if (month_n >= 1 && month_n <= 12) {
                        strncpy2(month, monthstr+((month_n-1)*3), 4);
-                       sprintf(zone, "%c%2d%2d", offset_sign, zone1, zone2);
+                       if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
+                               strcat(zone, "+00:00");
+                       } else if (sscanf(zonestr, "%c%2d:%2d",
+                                               &offset_sign, &zone1, &zone2) == 3) {
+                               strcat(zone, zonestr);
+                       }
                        return 0;
                }
        }
@@ -905,7 +911,7 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
 
        if (procheader_scan_date_string(src, weekday, &day, month, &year,
                                        &hh, &mm, &ss, zone) < 0) {
-               g_warning("Invalid date: %s\n", src);
+               g_warning("Invalid date: %s", src);
                return FALSE;
        }
 
@@ -921,7 +927,7 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
        if ((p = strstr(monthstr, month)) != NULL)
                dmonth = (gint)(p - monthstr) / 3 + 1;
        else {
-               g_warning("Invalid month: %s\n", month);
+               g_warning("Invalid month: %s", month);
                dmonth = G_DATE_BAD_MONTH;
        }
 
@@ -947,12 +953,10 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
        gchar month[10];
        gint year;
        gint hh, mm, ss;
-       gchar zone[6];
+       gchar zone[7];
        GDateMonth dmonth = G_DATE_BAD_MONTH;
-       struct tm t;
        gchar *p;
        time_t timer;
-       time_t tz_offset;
 
        if (procheader_scan_date_string(src, weekday, &day, month, &year,
                                        &hh, &mm, &ss, zone) < 0) {
@@ -961,14 +965,6 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                return 0;
        }
 
-       /* Y2K compliant :) */
-       if (year < 1000) {
-               if (year < 50)
-                       year += 2000;
-               else
-                       year += 1900;
-       }
-
        month[3] = '\0';
        for (p = monthstr; *p != '\0'; p += 3) {
                if (!g_ascii_strncasecmp(p, month, 3)) {
@@ -977,6 +973,30 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                }
        }
 
+#ifdef G_OS_WIN32
+       GTimeZone *tz;
+       GDateTime *dt;
+
+       tz = g_time_zone_new(zone);
+       dt = g_date_time_new(tz, year, dmonth, day, hh, mm, ss);
+
+       timer = g_date_time_to_unix(dt);
+
+       g_date_time_unref(dt);
+       g_time_zone_unref(tz);
+
+#else
+       struct tm t;
+       time_t tz_offset;
+
+       /* Y2K compliant :) */
+       if (year < 1000) {
+               if (year < 50)
+                       year += 2000;
+               else
+                       year += 1900;
+       }
+
        t.tm_sec = ss;
        t.tm_min = mm;
        t.tm_hour = hh;
@@ -994,6 +1014,7 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 
        if (dest)
                procheader_date_get_localtime(dest, len, timer);
+#endif
 
        return timer;
 }