aboutsummaryrefslogtreecommitdiff
path: root/mail/evolution/files/patch-libical_src_libical_icaltime.c
blob: 6f24e0f4cbf49ba9f6cad313ba8e5cc0425a341c (plain) (blame)
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
--- libical/src/libical/icaltime.c.orig	Tue Aug 26 20:25:02 2003
+++ libical/src/libical/icaltime.c	Tue Aug 26 20:29:05 2003
@@ -46,6 +46,76 @@
 
 #include "icaltimezone.h"
 
+static time_t make_time(struct tm *tm, int tzm)
+{
+  time_t tim;
+
+  static int days[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 };
+
+  /* check that year specification within range */
+
+  if (tm->tm_year < 70 || tm->tm_year > 138)
+    return((time_t) -1);
+
+  /* check that month specification within range */
+
+  if (tm->tm_mon < 0 || tm->tm_mon > 11)
+    return((time_t) -1);
+
+  /* check for upper bound of Jan 17, 2038 (to avoid possibility of
+     32-bit arithmetic overflow) */
+  
+  if (tm->tm_year == 138) {
+    if (tm->tm_mon > 0)
+      return((time_t) -1);
+    else if (tm->tm_mday > 17)
+      return((time_t) -1);
+  }
+
+  /*
+   *  calculate elapsed days since start of the epoch (midnight Jan
+   *  1st, 1970 UTC) 17 = number of leap years between 1900 and 1970
+   *  (number of leap days to subtract)
+   */
+
+  tim = (tm->tm_year - 70) * 365 + ((tm->tm_year - 1) / 4) - 17;
+
+  /* add number of days elapsed in the current year */
+
+  tim += days[tm->tm_mon];
+
+  /* check and adjust for leap years (the leap year check only valid
+     during the 32-bit era */
+
+  if ((tm->tm_year & 3) == 0 && tm->tm_mon > 1)
+    tim += 1;
+
+  /* elapsed days to current date */
+
+  tim += tm->tm_mday;
+
+
+  /* calculate elapsed hours since start of the epoch */
+
+  tim = tim * 24 + tm->tm_hour;
+
+  /* calculate elapsed minutes since start of the epoch */
+
+  tim = tim * 60 + tm->tm_min;
+  
+  /* adjust per time zone specification */
+  
+  tim -= tzm;
+  
+  /* calculate elapsed seconds since start of the epoch */
+  
+  tim = tim * 60 + tm->tm_sec;
+  
+  /* return number of seconds since start of the epoch */
+  
+  return(tim);
+}
+
 
 struct icaltimetype 
 icaltime_from_timet(time_t tm, int is_date)
@@ -221,13 +291,7 @@
     stm.tm_year = tt.year-1900;
     stm.tm_isdst = -1;
 
-    if(tt.is_utc == 1 || tt.is_date == 1){
-        char *old_tz = set_tz("UTC");
-	t = mktime(&stm);
-	unset_tz(old_tz);
-    } else {
-	t = mktime(&stm);
-    }
+    t = make_time(&stm, 0);
 
     return t;
 
@@ -269,10 +333,7 @@
     stm.tm_year = tt.year-1900;
     stm.tm_isdst = -1;
 
-    /* Set TZ to UTC and use mktime to convert to a time_t. */
-    old_tz = set_tz ("UTC");
-    t = mktime (&stm);
-    unset_tz (old_tz);
+    t = make_time(&stm, 0);
 
     return t;
 }