diff options
author | drh <> | 2021-11-29 18:09:54 +0000 |
---|---|---|
committer | drh <> | 2021-11-29 18:09:54 +0000 |
commit | 78679a49505616c39366afab2c531162383c9eb8 (patch) | |
tree | 41fa0f7e71c0fe68371c16d4a0ea79d362735fda /src | |
parent | 4a0a98d3fdd51cf91b581a2ea2bb12b91ba73b7e (diff) | |
parent | 1312a9c908eb72da0fee79bdd29457555e1b7b33 (diff) | |
download | sqlite-78679a49505616c39366afab2c531162383c9eb8.tar.gz sqlite-78679a49505616c39366afab2c531162383c9eb8.zip |
Merge in the proposed date/time function enhancements: (1) Add the
unixepoch() function, (2) the 'auto' modifier and (3) the 'julianday' modifier.
FossilOrigin-Name: 19c51b46e4095ee28badb10f4e08bbd330bda320c9a8806e93b8fc60ba211a2e
Diffstat (limited to 'src')
-rw-r--r-- | src/date.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/date.c b/src/date.c index 20a0a5d17..dcec9c7cc 100644 --- a/src/date.c +++ b/src/date.c @@ -662,6 +662,45 @@ static int parseModifier( int rc = 1; double r; switch(sqlite3UpperToLower[(u8)z[0]] ){ + case 'a': { + /* + ** auto + ** + ** If rawS is available, then interpret as a julian day number, or + ** a unix timestamp, depending on its magnitude. + */ + if( sqlite3_stricmp(z, "auto")==0 ){ + if( !p->rawS || p->validJD ){ + rc = 0; + p->rawS = 0; + }else if( p->s>=-210866760000 && p->s<=253402300799 ){ + r = p->s*1000.0 + 210866760000000.0; + clearYMD_HMS_TZ(p); + p->iJD = (sqlite3_int64)(r + 0.5); + p->validJD = 1; + p->rawS = 0; + rc = 0; + } + } + break; + } + case 'j': { + /* + ** julianday + ** + ** Always interpret the prior number as a julian-day value. If this + ** is not the first modifier, or if the prior argument is not a numeric + ** value in the allowed range of julian day numbers understood by + ** SQLite (0..5373484.5) then the result will be NULL. + */ + if( sqlite3_stricmp(z, "julianday")==0 ){ + if( p->validJD && p->rawS ){ + rc = 0; + p->rawS = 0; + } + } + break; + } #ifndef SQLITE_OMIT_LOCALTIME case 'l': { /* localtime @@ -927,6 +966,24 @@ static void juliandayFunc( } /* +** unixepoch( TIMESTRING, MOD, MOD, ...) +** +** Return the number of seconds (including fractional seconds) since +** the unix epoch of 1970-01-01 00:00:00 GMT. +*/ +static void unixepochFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + DateTime x; + if( isDate(context, argc, argv, &x)==0 ){ + computeJD(&x); + sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); + } +} + +/* ** datetime( TIMESTRING, MOD, MOD, ...) ** ** Return YYYY-MM-DD HH:MM:SS @@ -1202,6 +1259,7 @@ void sqlite3RegisterDateTimeFunctions(void){ static FuncDef aDateTimeFuncs[] = { #ifndef SQLITE_OMIT_DATETIME_FUNCS PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), + PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), PURE_DATE(date, -1, 0, 0, dateFunc ), PURE_DATE(time, -1, 0, 0, timeFunc ), PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), |