db: Add timestamp primitives so we can store them in the DB

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2019-04-10 17:08:57 +02:00
committed by Rusty Russell
parent f2ecf8e9c3
commit fcf133cd0a
2 changed files with 23 additions and 0 deletions

View File

@@ -11,6 +11,7 @@
#include <lightningd/plugin_hook.h>
#define DB_FILE "lightningd.sqlite3"
#define NSEC_IN_SEC 1000000000
/* For testing, we want to catch fatal messages. */
#ifndef db_fatal
@@ -1155,3 +1156,19 @@ void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db)
ld->config.fee_base,
ld->config.fee_per_satoshi);
}
void sqlite3_bind_timeabs(sqlite3_stmt *stmt, int col, struct timeabs t)
{
u64 timestamp = t.ts.tv_nsec + (t.ts.tv_sec * NSEC_IN_SEC);
sqlite3_bind_int64(stmt, col, timestamp);
}
struct timeabs sqlite3_column_timeabs(sqlite3_stmt *stmt, int col)
{
struct timeabs t;
u64 timestamp = sqlite3_column_int64(stmt, col);
t.ts.tv_sec = timestamp / NSEC_IN_SEC;
t.ts.tv_nsec = timestamp % NSEC_IN_SEC;
return t;
}