]> diplodocus.org Git - nmh/blobdiff - sbr/datetime.c
ssequal.c: Move interface to own file.
[nmh] / sbr / datetime.c
index 9357532ba2db7a700ed6d126e98161b9eca79482..e4d2d148924588edafad3bef70e968edaaaf63aa 100644 (file)
@@ -6,8 +6,9 @@
  */
 
 #include "h/mh.h"
+#include "error.h"
 #include "h/icalendar.h"
-#include <h/fmt_scan.h>
+#include "h/fmt_scan.h"
 #include "h/tws.h"
 #include "h/utils.h"
 #include "unquote.h"
@@ -59,7 +60,8 @@ struct tzdesc {
  */
 static int
 parse_datetime (const char *datetime, const char *zone, bool dst,
-                struct tws *tws) {
+                struct tws *tws)
+{
     char utc_indicator;
     bool form_1;
     int items_matched;
@@ -153,7 +155,8 @@ parse_datetime (const char *datetime, const char *zone, bool dst,
 }
 
 tzdesc_t
-load_timezones (const contentline *clines) {
+load_timezones (const contentline *clines)
+{
     tzdesc_t timezones = NULL, timezone = NULL;
     bool in_vtimezone, in_standard, in_daylight;
     tzparams *params = NULL;
@@ -173,19 +176,19 @@ load_timezones (const contentline *clines) {
 
                 if (in_standard) { in_standard = false; }
                 else if (in_daylight) { in_daylight = false; }
-                if (parse_datetime (params->dtstart, params->offsetfrom,
-                                    in_daylight,
-                                    &tws) == OK) {
-                    if (tws.tw_year >= 1970) {
-                        /* dmktime() falls apart for, e.g., the year 1601. */
-                        params->start_dt = tws.tw_clock;
-                    }
-                } else {
+
+                if (parse_datetime(params->dtstart, params->offsetfrom,
+                    in_daylight, &tws) != OK) {
                     inform("failed to parse start time %s for %s",
-                            params->dtstart,
-                            in_daylight ? "daylight" : "standard");
+                        params->dtstart,
+                        in_daylight ? "daylight" : "standard");
                     return NULL;
                 }
+
+                if (tws.tw_year >= 1970) {
+                    /* dmktime() falls apart for, e.g., the year 1601. */
+                    params->start_dt = tws.tw_clock;
+                }
                 params = NULL;
             } else if (! strcasecmp ("DTSTART", node->name)) {
                 /* Save DTSTART for use after getting TZOFFSETFROM. */
@@ -240,7 +243,8 @@ load_timezones (const contentline *clines) {
 }
 
 void
-free_timezones (tzdesc_t timezone) {
+free_timezones (tzdesc_t timezone)
+{
     tzdesc_t next;
 
     for ( ; timezone; timezone = next) {
@@ -270,7 +274,8 @@ free_timezones (tzdesc_t timezone) {
  */
 time_t
 rrule_clock (const char *rrule, const char *starttime, const char *zone,
-             unsigned int year) {
+             unsigned int year)
+{
     time_t clock = 0;
 
     if (nmh_strcasestr (rrule, "FREQ=YEARLY;INTERVAL=1")  ||
@@ -333,7 +338,8 @@ fail:
 }
 
 char *
-format_datetime (tzdesc_t timezones, const contentline *node) {
+format_datetime (tzdesc_t timezones, const contentline *node)
+{
     param_list *p;
     char *dt_timezone = NULL;
     int dst = 0;
@@ -360,11 +366,11 @@ format_datetime (tzdesc_t timezones, const contentline *node) {
     if (! dt_timezone) {
         /* Form #1: DATE WITH LOCAL TIME, i.e., no time zone, or
            Form #2: DATE WITH UTC TIME */
-        if (parse_datetime (node->value, NULL, false, &tws[0]) == OK) {
-            return strdup (dasctime (&tws[0], 0));
+        if (parse_datetime(node->value, NULL, false, &tws[0]) != OK) {
+            inform("unable to parse datetime %s", node->value);
+            return NULL;
         }
-        inform("unable to parse datetime %s", node->value);
-        return NULL;
+        return strdup (dasctime (&tws[0], 0));
     }
 
     /*
@@ -382,13 +388,12 @@ format_datetime (tzdesc_t timezones, const contentline *node) {
         if (tz->tzid  &&  ! strcasecmp (dt_timezone, tz->tzid)) { break; }
     }
 
-    if (tz) {
-        free(dt_timezone);
-    } else {
+    if (!tz) {
         inform("did not find VTIMEZONE section for %s", dt_timezone);
         free(dt_timezone);
         return NULL;
     }
+    free(dt_timezone);
 
     /* Determine if it's Daylight Saving. */
     tp_std = strchr (tz->standard_params.dtstart, 'T');
@@ -433,28 +438,23 @@ format_datetime (tzdesc_t timezones, const contentline *node) {
             return NULL;
         }
 
-        if (parse_datetime (node->value, tz->standard_params.offsetto,
-                            false, &tws[0]) == OK) {
-            dt[0] = tws[0].tw_clock;
-        } else {
+        if (parse_datetime(node->value, tz->standard_params.offsetto,
+            false, &tws[0]) != OK) {
             inform("unable to parse datetime %s", node->value);
             return NULL;
         }
+        dt[0] = tws[0].tw_clock;
 
         if (tp_dst) {
             if (dt[0] < transition[1]) {
                 dst = 0;
             } else {
-                if (parse_datetime (node->value,
-                                    tz->daylight_params.offsetto, true,
-                                    &tws[1]) == OK) {
-                    dt[1] = tws[1].tw_clock;
-                } else {
-                    inform("unable to parse datetime %s",
-                            node->value);
+                if (parse_datetime(node->value,
+                    tz->daylight_params.offsetto, true, &tws[1]) != OK) {
+                    inform("unable to parse datetime %s", node->value);
                     return NULL;
                 }
-
+                dt[1] = tws[1].tw_clock;
                 dst = dt[1] <= transition[0];
             }
         }