Prechádzať zdrojové kódy

- _timegm resets the information about DST in struct tm structure because the
date and time in that structure is in UTC.
- added conversion functions that can convert time_t values between local
timezones and utc.

Jan Janak 18 rokov pred
rodič
commit
b0afad347f
2 zmenil súbory, kde vykonal 30 pridanie a 1 odobranie
  1. 25 1
      ut.c
  2. 5 0
      ut.h

+ 25 - 1
ut.c

@@ -101,7 +101,8 @@ time_t _timegm(struct tm* t)
 {
 	time_t tl, tb;
 	struct tm* tg;
-	
+
+	t->tm_isdst = 0;
 	tl = mktime(t);
 	if (tl == -1) {
 		t->tm_hour--;
@@ -127,6 +128,29 @@ time_t _timegm(struct tm* t)
 }
 
 
+/* Convert time_t value that is relative to local timezone to UTC */
+time_t local2utc(time_t in)
+{
+	struct tm* tt;
+	tt = gmtime(&in);
+	tt->tm_isdst = -1;
+	return mktime(tt);
+}
+
+
+/* Convert time_t value in UTC to to value relative to local time zone */
+time_t utc2local(time_t in)
+{
+	struct tm* tt;
+	tt = localtime(&in);
+#ifdef HAVE_TIMEGM
+	return timegm(tt);
+#else
+	return _timegm(tt);
+#endif
+}
+
+
 /*
  * Return str as zero terminated string allocated
  * using pkg_malloc

+ 5 - 0
ut.h

@@ -526,6 +526,11 @@ int group2gid(int* gid, char* group);
  */
 time_t _timegm(struct tm* t);
 
+/* Convert time_t value that is relative to local timezone to UTC */
+time_t local2utc(time_t in);
+
+/* Convert time_t value in UTC to to value relative to local time zone */
+time_t utc2local(time_t in);
 
 /*
  * Return str as zero terminated string allocated