From 12c344a9acb56dea2ded46408f9a335d2822c10d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Thu, 19 Mar 2009 14:55:27 +0000 Subject: [PATCH] Using second fractions in timestamps. --- org.glite.lbjp-common.db/interface/db.h | 14 +++++++++++++- org.glite.lbjp-common.db/src/db.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/org.glite.lbjp-common.db/interface/db.h b/org.glite.lbjp-common.db/interface/db.h index ef7555d..bd7d7e0 100644 --- a/org.glite.lbjp-common.db/interface/db.h +++ b/org.glite.lbjp-common.db/interface/db.h @@ -260,6 +260,17 @@ int glite_lbu_QueryIndices(glite_lbu_DBContext ctx, const char *table, char ***k void glite_lbu_TimeToDB(time_t t, char **str); +/** + * Convert double into database-specific time string. + * + * The result string can be used directly in SQL commands. + * + * \param[in] t the converted time + * \param[out] str result allocated string + */ +void glite_lbu_TimestampToDB(double t, char **str); + + /** * Convert database-specific time string to time_t. * @@ -327,7 +338,8 @@ int glite_lbu_PrepareStmt(glite_lbu_DBContext ctx, const char *sql, glite_lbu_St * \param GLITE_LBU_DB_TYPE_...BLOB/TEXT void *b, unsigned long len * \param GLITE_LBU_DB_TYPE_[VAR]CHAR char *str * \param GLITE_LBU_DB_TYPE_DATE/TIME/DATETIME time_t t - * \param GLITE_LBU_DB_TYPE_TIMESTAMP time_t t + * \param GLITE_LBU_DB_TYPE_TIMESTAMP double t + * \param GLITE_LBU_DB_TYPE_BOOLEAN int b * \param GLITE_LBU_DB_TYPE_NULL - * * \return number of affected rows, -1 on error diff --git a/org.glite.lbjp-common.db/src/db.c b/org.glite.lbjp-common.db/src/db.c index f60b3d9..1c91526 100644 --- a/org.glite.lbjp-common.db/src/db.c +++ b/org.glite.lbjp-common.db/src/db.c @@ -164,7 +164,7 @@ static void db_close(MYSQL *mysql); static int transaction_test(glite_lbu_DBContext ctx); static int FetchRowSimple(glite_lbu_DBContext ctx, MYSQL_RES *result, unsigned long *lengths, char **results); static int FetchRowPrepared(glite_lbu_DBContext ctx, glite_lbu_Statement stmt, unsigned int n, unsigned long *lengths, char **results); -static void set_time(MYSQL_TIME *mtime, const time_t time); +static void set_time(MYSQL_TIME *mtime, const double time); static void glite_lbu_DBCleanup(void); static void glite_lbu_FreeStmt_int(glite_lbu_Statement stmt); @@ -575,6 +575,16 @@ void glite_lbu_TimeToDB(time_t t, char **str) { } +void glite_lbu_TimestampToDB(double t, char **str) { + time_t tsec = t; + struct tm *tm = gmtime(&tsec); + + t = t - tsec + tm->tm_sec; + asprintf(str,"'%4d-%02d-%02d %02d:%02d:%02.09f'",tm->tm_year+1900,tm->tm_mon+1, + tm->tm_mday,tm->tm_hour,tm->tm_min,t); +} + + time_t glite_lbu_DBToTime(const char *str) { struct tm tm; @@ -633,6 +643,7 @@ int glite_lbu_ExecPreparedStmt_v(glite_lbu_Statement stmt, int n, va_list ap) { int i, prepare_retry; glite_lbu_DBType type; char *pchar; + int *pint; long int *plint; MYSQL_TIME *ptime; glite_lbu_DBContext ctx; @@ -661,6 +672,11 @@ int glite_lbu_ExecPreparedStmt_v(glite_lbu_Statement stmt, int n, va_list ap) { *plint = va_arg(ap, long int); break; + case GLITE_LBU_DB_TYPE_BOOLEAN: + pint = binds[i].buffer = data[i] = malloc(sizeof(int)); + *pint = va_arg(ap, int) ? 1 : 0; + break; + case GLITE_LBU_DB_TYPE_TINYBLOB: case GLITE_LBU_DB_TYPE_TINYTEXT: case GLITE_LBU_DB_TYPE_BLOB: @@ -684,11 +700,15 @@ int glite_lbu_ExecPreparedStmt_v(glite_lbu_Statement stmt, int n, va_list ap) { case GLITE_LBU_DB_TYPE_DATE: case GLITE_LBU_DB_TYPE_TIME: case GLITE_LBU_DB_TYPE_DATETIME: - case GLITE_LBU_DB_TYPE_TIMESTAMP: ptime = binds[i].buffer = data[i] = malloc(sizeof(MYSQL_TIME)); set_time(ptime, va_arg(ap, time_t)); break; + case GLITE_LBU_DB_TYPE_TIMESTAMP: + ptime = binds[i].buffer = data[i] = malloc(sizeof(MYSQL_TIME)); + set_time(ptime, va_arg(ap, double)); + break; + case GLITE_LBU_DB_TYPE_NULL: break; @@ -1190,10 +1210,12 @@ quit: } -static void set_time(MYSQL_TIME *mtime, const time_t time) { +static void set_time(MYSQL_TIME *mtime, const double time) { struct tm tm; + time_t itime; - gmtime_r(&time, &tm); + itime = time; + gmtime_r(&itime, &tm); memset(mtime, 0, sizeof *mtime); mtime->year = tm.tm_year + 1900; mtime->month = tm.tm_mon + 1; @@ -1201,6 +1223,7 @@ static void set_time(MYSQL_TIME *mtime, const time_t time) { mtime->hour = tm.tm_hour; mtime->minute = tm.tm_min; mtime->second = tm.tm_sec; + mtime->second_part = (time - itime) * 1000; } -- 1.8.2.3