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;
}

View File

@ -8,6 +8,7 @@
#include <bitcoin/tx.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <ccan/time/time.h>
#include <common/amount.h>
#include <secp256k1_ecdh.h>
#include <sqlite3.h>
@ -212,4 +213,9 @@ void sqlite3_bind_amount_msat(sqlite3_stmt *stmt, int col,
struct amount_msat msat);
void sqlite3_bind_amount_sat(sqlite3_stmt *stmt, int col,
struct amount_sat sat);
/* Helpers to read and write absolute times from and to the database. */
void sqlite3_bind_timeabs(sqlite3_stmt *stmt, int col, struct timeabs t);
struct timeabs sqlite3_column_timeabs(sqlite3_stmt *stmt, int col);
#endif /* LIGHTNING_WALLET_DB_H */