diff --git a/wallet/db.c b/wallet/db.c index 768878b63..eb0c26599 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -11,6 +11,7 @@ #include #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; + +} diff --git a/wallet/db.h b/wallet/db.h index aa3567f18..5656d3986 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -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 */