Skip to content

Commit

Permalink
SQL jsontime function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Oct 30, 2023
1 parent 2157d0f commit 289f186
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 7 deletions.
Binary file modified embed/sqlite3.wasm
Binary file not shown.
57 changes: 57 additions & 0 deletions sqlite3/date.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--- sqlite3.c.orig
+++ sqlite3.c
@@ -340,6 +340,7 @@ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
p->iJD = sqlite3StmtCurrentTime(context);
if( p->iJD>0 ){
p->validJD = 1;
+ p->tzSet = 1;
return 0;
}else{
return 1;
@@ -355,6 +356,7 @@ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
static void setRawDateNumber(DateTime *p, double r){
p->s = r;
p->rawS = 1;
+ p->tzSet = 1;
if( r>=0.0 && r<5373484.5 ){
p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
p->validJD = 1;
@@ -572,6 +574,7 @@ static int toLocaltime(
time_t t;
struct tm sLocal;
int iYearDiff;
+ DateTime x;

/* Initialize the contents of sLocal to avoid a compiler warning. */
memset(&sLocal, 0, sizeof(sLocal));
@@ -585,7 +588,7 @@ static int toLocaltime(
** SQLite attempts to map the year into an equivalent year within this
** range, do the calculation, then map the year back.
*/
- DateTime x = *p;
+ x = *p;
computeYMD_HMS(&x);
iYearDiff = (2000 + x.Y%4) - x.Y;
x.Y += iYearDiff;
@@ -610,8 +613,13 @@ static int toLocaltime(
p->validHMS = 1;
p->validJD = 0;
p->rawS = 0;
+ p->tzSet = 0;
p->validTZ = 0;
p->isError = 0;
+ x = *p;
+ computeJD(&x);
+ p->tz = (x.iJD-p->iJD)/60000;
+ if( abs(p->tz)>= 900 ) p->tz = 0;
return SQLITE_OK;
}
#endif /* SQLITE_OMIT_LOCALTIME */
@@ -781,6 +789,7 @@ static int parseModifier(
p->validJD = 1;
p->tzSet = 1;
}
+ p->tz = 0;
rc = SQLITE_OK;
}
#endif
20 changes: 18 additions & 2 deletions sqlite3/sqlite_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@
#define SQLITE_OS_OTHER 1
#define SQLITE_BYTEORDER 1234

#define HAVE_INT8_T 1
#define HAVE_INT16_T 1
#define HAVE_INT32_T 1
#define HAVE_INT64_T 1
#define HAVE_UINT8_T 1
#define HAVE_UINT16_T 1
#define HAVE_UINT32_T 1
#define HAVE_UINT64_T 1
#define HAVE_STDINT_H 1
#define HAVE_INTTYPES_H 1

#define HAVE_LOG2 1
#define HAVE_LOG10 1
#define HAVE_ISNAN 1

#define HAVE_USLEEP 1
#define HAVE_NANOSLEEP 1

#define HAVE_GMTIME_R 1
#define HAVE_LOCALTIME_S 1

#define HAVE_MALLOC_H 1
#define HAVE_MALLOC_USABLE_SIZE 1

// Recommended Options
Expand Down Expand Up @@ -51,11 +67,11 @@
#define SQLITE_ENABLE_RTREE 1
#define SQLITE_ENABLE_GEOPOLY 1

#define SQLITE_SOUNDEX

// Session Extension
// #define SQLITE_ENABLE_SESSION
// #define SQLITE_ENABLE_PREUPDATE_HOOK

#define SQLITE_SOUNDEX

// Implemented in vfs.c.
int localtime_s(struct tm *const pTm, time_t const *const pTime);
57 changes: 56 additions & 1 deletion sqlite3/time.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stddef.h>
#include <string.h>

#include "sqlite3.h"
Expand Down Expand Up @@ -26,7 +27,61 @@ static int time_collation(void *pArg, int nKey1, const void *pKey1, int nKey2,
return rc;
}

static void jsontime_func(sqlite3_context *context, int argc,
sqlite3_value **argv) {
DateTime x;
if (isDate(context, argc, argv, &x)) return;
if (x.tzSet && x.tz) {
x.iJD += x.tz * 60000;
if (!validJulianDay(x.iJD)) return;
x.validYMD = 0;
x.validHMS = 0;
}
computeYMD_HMS(&x);

sqlite3 *db = sqlite3_context_db_handle(context);
sqlite3_str *res = sqlite3_str_new(db);

sqlite3_str_appendf(res, "%04d-%02d-%02dT%02d:%02d:%02d", //
x.Y, x.M, x.D, //
x.h, x.m, (int)(x.iJD / 1000 % 60));

if (x.useSubsec) {
int rem = x.iJD % 1000;
if (rem) {
sqlite3_str_appendchar(res, 1, '.');
sqlite3_str_appendchar(res, 1, '0' + rem / 100);
if ((rem %= 100)) {
sqlite3_str_appendchar(res, 1, '0' + rem / 10);
if ((rem %= 10)) {
sqlite3_str_appendchar(res, 1, '0' + rem);
}
}
}
}

if (x.tz) {
sqlite3_str_appendf(res, "%+03d:%02d", x.tz / 60, abs(x.tz) % 60);
} else {
sqlite3_str_appendchar(res, 1, 'Z');
}

int rc = sqlite3_str_errcode(res);
if (rc) {
sqlite3_result_error_code(context, rc);
return;
}

int n = sqlite3_str_length(res);
sqlite3_result_text(context, sqlite3_str_finish(res), n, sqlite3_free);
}

int sqlite3_time_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi) {
return sqlite3_create_collation(db, "time", SQLITE_UTF8, 0, time_collation);
sqlite3_create_collation_v2(db, "time", SQLITE_UTF8, NULL, time_collation,
NULL);
sqlite3_create_function_v2(
db, "jsontime", -1, SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_INNOCUOUS,
NULL, jsontime_func, NULL, NULL, NULL);
return SQLITE_OK;
}
4 changes: 2 additions & 2 deletions vfs/tests/mptest/testdata/mptest.wasm.bz2
Git LFS file not shown
4 changes: 2 additions & 2 deletions vfs/tests/speedtest1/testdata/speedtest1.wasm.bz2
Git LFS file not shown

0 comments on commit 289f186

Please sign in to comment.