More fixes for parsing dates in vcalendar on Windows.
[claws.git] / src / plugins / vcalendar / libical / libical / icaltime.c
index 85596671a22076cd95dbf38f2bd411c400a24233..7d3ee1cf685a71ff0f402b4d759f73c2522e5c67 100644 (file)
@@ -94,13 +94,13 @@ struct set_tz_save set_tz(const char* tzid)
     savetz.orig_tzid = 0;
     savetz.new_env_str = 0;
 
-    if(getenv("TZ") != 0){
-       orig_tzid = (char*)icalmemory_strdup(getenv("TZ"));
+    if (g_getenv("TZ") != NULL) {
+               orig_tzid = (char*)icalmemory_strdup(g_getenv("TZ"));
 
-       if(orig_tzid == 0){
+               if (orig_tzid == 0) {
             icalerror_set_errno(ICAL_NEWFAILED_ERROR);
-           return savetz;
-       }
+                       return savetz;
+               }
     }
 
     tmp_sz =strlen(tzid)+4; 
@@ -108,6 +108,7 @@ struct set_tz_save set_tz(const char* tzid)
 
     if(new_env_str == 0){
         icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+       free(orig_tzid);
        return savetz;
     }
     
@@ -160,8 +161,37 @@ void unset_tz(struct set_tz_save savetz)
 
 time_t icaltime_as_timet(struct icaltimetype tt)
 {
+       time_t t;
+
+#ifdef G_OS_WIN32
+       GTimeZone *zone;
+       GDateTime *dt;
+
+       if (tt.is_utc == 1)
+               zone = g_time_zone_new_utc();
+       else
+               zone = g_time_zone_new_local();
+
+       dt = g_date_time_new(
+                               zone,
+                               tt.year,
+                               tt.month,
+                               tt.day,
+                               tt.hour,
+                               tt.minute,
+                               tt.second);
+
+       /* Got to return something... */
+       if (dt == NULL)
+               return 0;
+
+       t = g_date_time_to_unix(dt);
+
+       g_date_time_unref(dt);
+       g_time_zone_unref(zone);
+
+#else
     struct tm stm;
-    time_t t;
 
     memset(&stm,0,sizeof( struct tm));
 
@@ -177,16 +207,16 @@ time_t icaltime_as_timet(struct icaltimetype tt)
     stm.tm_year = tt.year-1900;
     stm.tm_isdst = -1;
 
-    if(tt.is_utc == 1 || tt.is_date == 1){
+    if(tt.is_utc == 1 && tt.is_date == 0){
        struct set_tz_save old_tz = set_tz("UTC");
        t = mktime(&stm);
        unset_tz(old_tz);
     } else {
        t = mktime(&stm);
     }
+#endif
 
     return t;
-
 }
 
 char* icaltime_as_ical_string(struct icaltimetype tt)
@@ -282,6 +312,7 @@ int icaltime_utc_offset(struct icaltimetype ictt, const char* tzid)
 
 struct icaltimetype icaltime_normalize(struct icaltimetype tt)
 {
+#ifndef G_OS_WIN32
     struct tm stm, buft;
     time_t tut;
 
@@ -306,7 +337,7 @@ struct icaltimetype icaltime_normalize(struct icaltimetype tt)
     tt.day = stm.tm_mday;
     tt.month = stm.tm_mon +1;
     tt.year = stm.tm_year+1900;
-
+#endif
     return tt;
 }
 
@@ -485,6 +516,35 @@ short icaltime_week_number(struct icaltimetype ictt)
 
 
 short icaltime_day_of_year(struct icaltimetype t){
+#ifdef G_OS_WIN32
+       GTimeZone *zone;
+       GDateTime *dt;
+
+       if (t.is_utc == 1)
+               zone = g_time_zone_new_utc();
+       else
+               zone = g_time_zone_new_local();
+
+       dt = g_date_time_new(
+                               zone,
+                               t.year,
+                               t.month,
+                               t.day,
+                               t.hour,
+                               t.minute,
+                               t.second);
+
+       /* Got to return something... */
+       if (dt == NULL)
+               return 1;
+
+       gint doy = g_date_time_get_day_of_year(dt) - 1;
+       g_date_time_unref(dt);
+       g_time_zone_unref(zone);
+
+       return doy;
+
+#else
     time_t tt = icaltime_as_timet(t);
     struct tm *stm, buft;
 
@@ -495,12 +555,38 @@ short icaltime_day_of_year(struct icaltimetype t){
     }
 
     return stm->tm_yday+1;
-    
+#endif
 }
 
 /* Jan 1 is day #1, not 0 */
 struct icaltimetype icaltime_from_day_of_year(short doy,  short year)
 {
+#ifdef G_OS_WIN32
+       GDateTime *dt = g_date_time_new_utc(
+                       year,
+                       1,
+                       1,
+                       0,
+                       0,
+                       0);
+       GDateTime *dtd = g_date_time_add_days(dt, doy);
+
+       g_date_time_unref(dt);
+       struct icaltimetype t = icaltime_null_time();
+
+       t.year = g_date_time_get_year(dtd);
+       t.month = g_date_time_get_month(dtd);
+       t.day = g_date_time_get_day_of_month(dtd);
+       t.hour = g_date_time_get_hour(dtd);
+       t.minute = g_date_time_get_minute(dtd);
+       t.second = g_date_time_get_second(dtd);
+       t.is_utc = 1;
+       t.is_date = 1;
+       g_date_time_unref(dtd);
+
+       return t;
+
+#else
     struct tm stm; 
     time_t tt;
     struct set_tz_save old_tz = set_tz("UTC");
@@ -520,6 +606,7 @@ struct icaltimetype icaltime_from_day_of_year(short doy,  short year)
     tt += doy *60*60*24;
 
     return icaltime_from_timet(tt, 1);
+#endif
 }
 
 struct icaltimetype icaltime_null_time()