Browse Source

- move most of the time and date handling functions for databases to db_ut.h
- delete the implementations in unixodbc, mysql and partly in postgresql
- fix header includes for these modules
- postgresql saves the time and date with the timezone attached, so this
module can not use the common db_time2str
- change the _XOPEN_SOURCE define to 600 use the SuSv3 definition for strptime,
the old define conflicts with some string handling functions
- delete the unneded time related defines in postgresql module
- add undefs after the defines to avoid changes for subsequent includes


git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@2157 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt 18 years ago
parent
commit
94b24ee285
2 changed files with 80 additions and 8 deletions
  1. 65 7
      lib/srdb1/db_ut.c
  2. 15 1
      lib/srdb1/db_ut.h

+ 65 - 7
lib/srdb1/db_ut.c

@@ -22,12 +22,15 @@
  */
 
 
+#include "db_ut.h"
+
 #include "../dprint.h"
 #include <limits.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 
 
 /*
@@ -38,14 +41,14 @@ inline int db_str2int(const char* _s, int* _v)
 	long tmp;
 
 	if (!_s || !_v) {
-	       LOG(L_ERR, "str2int: Invalid parameter value\n");
+	       LOG(L_ERR, "ERROR:db_str2int: Invalid parameter value\n");
 	       return -1;
 	}
 
 	tmp = strtoul(_s, 0, 10);
 	if ((tmp == ULONG_MAX && errno == ERANGE) || 
 	    (tmp < INT_MIN) || (tmp > UINT_MAX)) {
-		printf("str2int: Value out of range\n");
+		LOG(L_ERR, "ERROR:db_str2int: Value out of range\n");
 		return -1;
 	}
 
@@ -60,7 +63,7 @@ inline int db_str2int(const char* _s, int* _v)
 inline int db_str2double(const char* _s, double* _v)
 {
 	if ((!_s) || (!_v)) {
-		LOG(L_ERR, "str2double: Invalid parameter value\n");
+		LOG(L_ERR, "ERROR:db_str2double: Invalid parameter value\n");
 		return -1;
 	}
 
@@ -78,13 +81,13 @@ inline int db_int2str(int _v, char* _s, int* _l)
 	int ret;
 
 	if ((!_s) || (!_l) || (!*_l)) {
-		LOG(L_ERR, "int2str: Invalid parameter value\n");
+		LOG(L_ERR, "ERROR:db_int2str: Invalid parameter value\n");
 		return -1;
 	}
 
 	ret = snprintf(_s, *_l, "%-d", _v);
 	if (ret < 0 || ret >= *_l) {
-		LOG(L_ERR, "int2str: Error in sprintf\n");
+		LOG(L_ERR, "ERROR:db_int2str: error in sprintf\n");
 		return -1;
 	}
 	*_l = ret;
@@ -101,16 +104,71 @@ inline int db_double2str(double _v, char* _s, int* _l)
 	int ret;
 
 	if ((!_s) || (!_l) || (!*_l)) {
-		LOG(L_ERR, "double2str: Invalid parameter value\n");
+		LOG(L_ERR, "ERROR:db_double2str: Invalid parameter value\n");
 		return -1;
 	}
 
 	ret = snprintf(_s, *_l, "%-10.2f", _v);
 	if (ret < 0 || ret >= *_l) {
-		LOG(L_ERR, "double2str: Error in snprintf\n");
+		LOG(L_ERR, "ERROR:db_double2str: Error in snprintf\n");
 		return -1;
 	}
 	*_l = ret;
 
 	return 0;
 }
+
+
+/* 
+ * Convert a string to time_t
+ */
+inline int db_str2time(const char* _s, time_t* _v)
+{
+	if ((!_s) || (!_v))
+	{
+		LOG(L_ERR, "ERROR:db_str2time: Invalid parameter value\n");
+		return -1;
+	}
+
+	// Convert database time representation to time_t structure
+	struct tm time;
+	/* It is necessary to zero tm structure first */
+	memset(&time, '\0', sizeof(struct tm));
+	strptime(_s, "%Y-%m-%d %H:%M:%S", &time);
+
+	/* Daylight saving information got lost in the database
+	* so let mktime to guess it. This eliminates the bug when
+	* contacts reloaded from the database have different time
+	* of expiration by one hour when daylight saving is used
+	*/ 
+	time.tm_isdst = -1;
+	*_v = mktime(&time);
+
+	return 0;
+}
+
+
+/*
+ * Convert time_t to string
+ */
+inline int db_time2str(time_t _v, char* _s, int* _l)
+{
+	int l;
+
+	if ((!_s) || (!_l) || (*_l < 2))
+	{
+		LOG(L_ERR, "ERROR:db_time2str: Invalid parameter value\n");
+		return -1;
+	}
+
+	*_s++ = '\'';
+
+	// Convert time_t structure to format accepted by the database
+	struct tm* t;
+	t = localtime(&_v);
+	l = strftime(_s, *_l -1, "%Y-%m-%d %H:%M:%S", t);
+
+	*(_s + l) = '\'';
+	*_l = l + 2;
+	return 0;
+}

+ 15 - 1
lib/srdb1/db_ut.h

@@ -25,12 +25,26 @@
 #ifndef DB_UT_H
 #define DB_UT_H
 
+
+/* for strptime, use 600 for 'Single UNIX Specification, Version 3' */
+#define _XOPEN_SOURCE 600          /* glibc2 on linux, bsd */
+#define _XOPEN_SOURCE_EXTENDED 1   /* solaris */
+
+#include <time.h>
+
+#undef _XOPEN_SOURCE
+#undef _XOPEN_SOURCE_EXTENDED
+
+
 int db_str2int(const char* _s, int* _v);
 
 int db_str2double(const char* _s, double* _v);
 
 int db_int2str(int _v, char* _s, int* _l);
 
-inline int db_double2str(double _v, char* _s, int* _l);
+int db_double2str(double _v, char* _s, int* _l);
+
+int db_time2str(time_t _v, char* _s, int* _l);
 
+int db_str2time(const char* _s, time_t* _v);
 #endif