Browse code

- _timegm added to utils

Jan Janak authored on 15/12/2005 23:17:40
Showing 2 changed files
... ...
@@ -32,6 +32,7 @@
32 32
 #include <pwd.h>
33 33
 #include <grp.h>
34 34
 #include <stdlib.h>
35
+#include <time.h>
35 36
 #include "ut.h"
36 37
 
37 38
 /* converts a username into uid:gid,
... ...
@@ -82,3 +83,38 @@ int group2gid(int* gid, char* group)
82 82
 error:
83 83
 	return -1;
84 84
 }
85
+
86
+
87
+/*
88
+ * Replacement of timegm (does not exists on all platforms
89
+ * Taken from 
90
+ * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
91
+ */
92
+time_t _timegm(struct tm* t)
93
+{
94
+	time_t tl, tb;
95
+	struct tm* tg;
96
+	
97
+	tl = mktime(t);
98
+	if (tl == -1) {
99
+		t->tm_hour--;
100
+		tl = mktime (t);
101
+		if (tl == -1) {
102
+			return -1; /* can't deal with output from strptime */
103
+		}
104
+		tl += 3600;
105
+	}
106
+	
107
+	tg = gmtime(&tl);
108
+	tg->tm_isdst = 0;
109
+	tb = mktime(tg);
110
+	if (tb == -1) {
111
+		tg->tm_hour--;
112
+		tb = mktime (tg);
113
+		if (tb == -1) {
114
+			return -1; /* can't deal with output from gmtime */
115
+		}
116
+		tb += 3600;
117
+	}
118
+	return (tl - (tb - tl));
119
+}
... ...
@@ -50,6 +50,7 @@
50 50
 #include <sys/types.h>
51 51
 #include <sys/time.h>
52 52
 #include <limits.h>
53
+#include <time.h>
53 54
 #include <unistd.h>
54 55
 #include <ctype.h>
55 56
 
... ...
@@ -427,4 +428,11 @@ int user2uid(int* uid, int* gid, char* user);
427 427
  * returns -1 on error, 0 on success */
428 428
 int group2gid(int* gid, char* group);
429 429
 
430
+/*
431
+ * Replacement of timegm (does not exists on all platforms
432
+ * Taken from 
433
+ * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
434
+ */
435
+time_t _timegm(struct tm* t);
436
+
430 437
 #endif