Browse Source

- _timegm added to utils

Jan Janak 19 years ago
parent
commit
74a8dcdf05
2 changed files with 44 additions and 0 deletions
  1. 36 0
      ut.c
  2. 8 0
      ut.h

+ 36 - 0
ut.c

@@ -32,6 +32,7 @@
 #include <pwd.h>
 #include <pwd.h>
 #include <grp.h>
 #include <grp.h>
 #include <stdlib.h>
 #include <stdlib.h>
+#include <time.h>
 #include "ut.h"
 #include "ut.h"
 
 
 /* converts a username into uid:gid,
 /* converts a username into uid:gid,
@@ -82,3 +83,38 @@ int group2gid(int* gid, char* group)
 error:
 error:
 	return -1;
 	return -1;
 }
 }
+
+
+/*
+ * Replacement of timegm (does not exists on all platforms
+ * Taken from 
+ * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
+ */
+time_t _timegm(struct tm* t)
+{
+	time_t tl, tb;
+	struct tm* tg;
+	
+	tl = mktime(t);
+	if (tl == -1) {
+		t->tm_hour--;
+		tl = mktime (t);
+		if (tl == -1) {
+			return -1; /* can't deal with output from strptime */
+		}
+		tl += 3600;
+	}
+	
+	tg = gmtime(&tl);
+	tg->tm_isdst = 0;
+	tb = mktime(tg);
+	if (tb == -1) {
+		tg->tm_hour--;
+		tb = mktime (tg);
+		if (tb == -1) {
+			return -1; /* can't deal with output from gmtime */
+		}
+		tb += 3600;
+	}
+	return (tl - (tb - tl));
+}

+ 8 - 0
ut.h

@@ -50,6 +50,7 @@
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/time.h>
 #include <limits.h>
 #include <limits.h>
+#include <time.h>
 #include <unistd.h>
 #include <unistd.h>
 #include <ctype.h>
 #include <ctype.h>
 
 
@@ -427,4 +428,11 @@ int user2uid(int* uid, int* gid, char* user);
  * returns -1 on error, 0 on success */
  * returns -1 on error, 0 on success */
 int group2gid(int* gid, char* group);
 int group2gid(int* gid, char* group);
 
 
+/*
+ * Replacement of timegm (does not exists on all platforms
+ * Taken from 
+ * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
+ */
+time_t _timegm(struct tm* t);
+
 #endif
 #endif