Skip to content

Commit

Permalink
Merge pull request #957 from zecke/db-mysql-datetime
Browse files Browse the repository at this point in the history
db_mysql: Let the database convert the timestamp
  • Loading branch information
miconda committed Feb 13, 2017
2 parents da807de + 4afc00b commit 5612f7d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/modules/db_mysql/db_mysql.c
Expand Up @@ -41,6 +41,7 @@ unsigned int my_connect_to = 2; /* 2 s by default */
unsigned int my_send_to = 0; /* enabled only for mysql >= 5.25 */
unsigned int my_recv_to = 0; /* enabled only for mysql >= 5.25 */
unsigned int my_retries = 1; /* Number of retries when command fails */
unsigned int my_server_timezone = 0; /* Use FROM_UNIXTIME() for date conversion */

unsigned long my_client_ver = 0;

Expand Down Expand Up @@ -91,6 +92,7 @@ static param_export_t params[] = {
{"send_timeout", PARAM_INT, &my_send_to},
{"receive_timeout", PARAM_INT, &my_recv_to},
{"retries", PARAM_INT, &my_retries},
{"server_timezone", PARAM_INT, &my_server_timezone},

{"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
{"auto_reconnect", INT_PARAM, &db_mysql_auto_reconnect},
Expand Down
1 change: 1 addition & 0 deletions src/modules/db_mysql/db_mysql.h
Expand Up @@ -44,6 +44,7 @@ extern unsigned int my_send_to;
extern unsigned int my_recv_to;
extern unsigned long my_client_ver;
extern unsigned int my_retries;
extern unsigned int my_server_timezone;

/** @} */

Expand Down
21 changes: 21 additions & 0 deletions src/modules/db_mysql/doc/db_mysql_admin.xml
Expand Up @@ -71,6 +71,27 @@
...
modparam("db_mysql", "ping_interval", 600)
...
</programlisting>
</example>
</section>
<section>
<title><varname>server_timezone</varname> (integer)</title>
<para>
Control if kamailio convers a time to string using the local timezone
or if MySQL will convert it using FROM_UNIXTIME. This option is useful
if kamailio and the database are configured for different timezones.
</para>
<para>
<emphasis>
Default value is 0 (use local conversion)
</emphasis>
</para>
<example>
<title>Set <varname>server_timezone</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("db_mysql", "server_timezone", 1)
...
</programlisting>
</example>
</section>
Expand Down
22 changes: 22 additions & 0 deletions src/modules/db_mysql/km_val.c
Expand Up @@ -29,6 +29,7 @@
#include "../../lib/srdb1/db_ut.h"
#include "km_val.h"
#include "km_my_con.h"
#include "db_mysql.h"


/*!
Expand All @@ -46,6 +47,27 @@ int db_mysql_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _le
int l, tmp;
char* old_s;

switch(VAL_TYPE(_v)) {
case DB1_DATETIME:
if (my_server_timezone) {
/* Let MySQL handle timestamp to internal time representation */
if (!_s || !_len || !*_len) {
LM_ERR("Invalid parameter value\n");
return -1;
}
l = snprintf(_s, *_len, "FROM_UNIXTIME(%d)", VAL_INT(_v));
if (l < 0 || l >= *_len) {
LM_ERR("Error in snprintf\n");
return -1;
}
*_len = l;
return 0;
}
break;
default:
break;
};

tmp = db_val2str(_c, _v, _s, _len);
if (tmp < 1)
return tmp;
Expand Down

0 comments on commit 5612f7d

Please sign in to comment.