Skip to content

Commit

Permalink
lib/srdb1: added helper function to convert time to unquoted string
Browse files Browse the repository at this point in the history
(cherry picked from commit b3da13a)
  • Loading branch information
miconda committed May 2, 2016
1 parent 964c05c commit ea7d88c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
48 changes: 29 additions & 19 deletions lib/srdb1/db_ut.c
Expand Up @@ -14,8 +14,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

Expand All @@ -41,8 +41,8 @@
#ifndef __OS_solaris
#define _XOPEN_SOURCE 600 /* glibc2 on linux, bsd */
#define _BSD_SOURCE 1 /* needed on linux to "fix" the effect
of the above define on
features.h/unistd.h syscall() */
* of the above define on
* features.h/unistd.h syscall() */
#else
#define _XOPEN_SOURCE_EXTENDED 1 /* solaris */
#endif
Expand Down Expand Up @@ -74,13 +74,13 @@ inline int db_str2int(const char* _s, int* _v)
char* p = NULL;

if (!_s || !_v) {
LM_ERR("Invalid parameter value\n");
return -1;
LM_ERR("Invalid parameter value\n");
return -1;
}

tmp = strtoul(_s, &p, 10);
if ((tmp == ULONG_MAX && errno == ERANGE) ||
(tmp < INT_MIN) || (tmp > UINT_MAX)) {
if ((tmp == ULONG_MAX && errno == ERANGE) ||
(tmp < INT_MIN) || (tmp > UINT_MAX)) {
LM_ERR("Value out of range\n");
return -1;
}
Expand All @@ -100,8 +100,8 @@ inline int db_str2longlong(const char* _s, long long * _v)
char* p = NULL;

if (!_s || !_v) {
LM_ERR("Invalid parameter value\n");
return -1;
LM_ERR("Invalid parameter value\n");
return -1;
}

tmp = strtoll(_s, &p, 10);
Expand Down Expand Up @@ -204,7 +204,7 @@ inline int db_double2str(double _v, char* _s, int* _l)
}


/*
/*
* Convert a string to time_t
*/
inline int db_str2time(const char* _s, time_t* _v)
Expand All @@ -217,7 +217,7 @@ inline int db_str2time(const char* _s, time_t* _v)
}

/* Convert database time representation to time_t structure
It is necessary to zero tm structure first */
* It is necessary to zero tm structure first */
memset(&time, '\0', sizeof(struct tm));
if (strptime(_s, "%Y-%m-%d %H:%M:%S", &time) == NULL) {
LM_ERR("Error during time conversion\n");
Expand All @@ -228,15 +228,18 @@ inline int db_str2time(const char* _s, time_t* _v)
* 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;
}


inline int db_time2str(time_t _v, char* _s, int* _l)
/**
*
*/
inline int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode)
{
struct tm* t;
int l;
Expand All @@ -246,7 +249,7 @@ inline int db_time2str(time_t _v, char* _s, int* _l)
return -1;
}

*_s++ = '\'';
if(_qmode) *_s++ = '\'';

/* Convert time_t structure to format accepted by the database */
t = localtime(&_v);
Expand All @@ -261,11 +264,18 @@ inline int db_time2str(time_t _v, char* _s, int* _l)
}
*_l = l;

*(_s + l) = '\'';
if(_qmode) *(_s + l) = '\'';
*_l = l + 2;
return 0;
}

/**
*
*/
inline int db_time2str(time_t _v, char* _s, int* _l)
{
return db_time2str_ex(_v, _s, _l, 1);
}

/*
* Print list of columns separated by comma
Expand Down Expand Up @@ -382,7 +392,7 @@ int db_print_where(const db1_con_t* _c, char* _b, const int _l, const db_key_t*
}
return len;

error:
error:
LM_ERR("Error in snprintf\n");
return -1;
}
Expand Down Expand Up @@ -422,7 +432,7 @@ int db_print_set(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k
}
return len;

error:
error:
LM_ERR("Error in snprintf\n");
return -1;
}
Expand Down Expand Up @@ -484,7 +494,7 @@ int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs)
break;
default:
LM_NOTICE("unknown field type: %d, setting value to null\n",
dbval->type);
dbval->type);
pv.flags = PV_VAL_NULL;
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/srdb1/db_ut.h
Expand Up @@ -14,8 +14,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

Expand Down Expand Up @@ -113,6 +113,7 @@ int db_double2str(double _v, char* _s, int* _l);
*/
int db_time2str(time_t _v, char* _s, int* _l);

int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode);

/**
* Converts a char into a time_t value.
Expand Down Expand Up @@ -189,7 +190,7 @@ int db_print_set(const db1_con_t* _c, char* _b, const int _l,

/**
* Convert db_val_t to pv_spec_t
*
*
* \param msg sip msg structure
* \param dbval database value
* \param pvs pv_spec where to put the database value
Expand Down

0 comments on commit ea7d88c

Please sign in to comment.