mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-29 08:01:23 +03:00
Add experimental date and time functions based on julian day number. (CVS 1069)
FossilOrigin-Name: a6197e2075fdf9db862484255ac16b2855bbef0a
This commit is contained in:
322
src/func.c
322
src/func.c
@ -16,13 +16,14 @@
|
||||
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
|
||||
** All other code has file scope.
|
||||
**
|
||||
** $Id: func.c,v 1.26 2003/06/28 16:20:23 drh Exp $
|
||||
** $Id: func.c,v 1.27 2003/08/09 21:32:28 drh Exp $
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
|
||||
/*
|
||||
** Implementation of the non-aggregate min() and max() functions
|
||||
@ -497,6 +498,312 @@ static void minMaxFinalize(sqlite_func *context){
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
** Time and date functions.
|
||||
**
|
||||
** SQLite processes all times and dates as Julian Day numbers. The
|
||||
** dates and times are stored as the number of days since noon
|
||||
** in Greenwich on January 01, 4713 B.C. (a.k.a -4713-01-01 12:00:00)
|
||||
** This implement requires years to be expressed as a 4-digit number
|
||||
** which means that only dates between 0000-01-01 and 9999-12-31 can
|
||||
** be represented, even though julian day numbers allow a much wider
|
||||
** range of dates.
|
||||
**
|
||||
** The Gregorian calendar system is used for all dates and times,
|
||||
** even those that predate the Gregorian calendar. Historians often
|
||||
** use the Julian calendar for dates prior to 1582-10-15 and for some
|
||||
** dates afterwards, depending on locale. Beware of this difference.
|
||||
**
|
||||
** The conversion algorithms are implemented based on descriptions
|
||||
** in the following text:
|
||||
**
|
||||
** Jean Meeus
|
||||
** Astronomical Algorithms, 2nd Edition, 1998
|
||||
** ISBM 0-943396-61-1
|
||||
** Willmann-Bell, Inc
|
||||
** Richmond, Virginia (USA)
|
||||
*/
|
||||
#ifndef SQLITE_OMIT_DATETIME_FUNCS
|
||||
|
||||
/*
|
||||
** Convert N digits from zDate into an integer. Return
|
||||
** -1 if zDate does not begin with N digits.
|
||||
*/
|
||||
static int getDigits(const char *zDate, int N){
|
||||
int val = 0;
|
||||
while( N-- ){
|
||||
if( !isdigit(*zDate) ) return -1;
|
||||
val = val*10 + *zDate - '0';
|
||||
zDate++;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
** Parse dates of the form HH:MM:SS or HH:MM. Store the
|
||||
** result (in days) in *prJD.
|
||||
**
|
||||
** Return 1 if there is a parsing error and 0 on success.
|
||||
*/
|
||||
static int parseHhMmSs(const char *zDate, double *prJD){
|
||||
int h, m, s;
|
||||
h = getDigits(zDate, 2);
|
||||
if( h<0 || zDate[2]!=':' ) return 1;
|
||||
zDate += 3;
|
||||
m = getDigits(zDate, 2);
|
||||
if( m<0 || m>59 ) return 1;
|
||||
zDate += 2;
|
||||
if( *zDate==':' ){
|
||||
s = getDigits(&zDate[1], 2);
|
||||
if( s<0 || s>59 ) return 1;
|
||||
zDate += 3;
|
||||
}else{
|
||||
s = 0;
|
||||
}
|
||||
while( isspace(*zDate) ){ zDate++; }
|
||||
*prJD = (h*3600.0 + m*60.0 + s)/86400.0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Parse dates of the form
|
||||
**
|
||||
** YYYY-MM-DD HH:MM:SS
|
||||
** YYYY-MM-DD HH:MM
|
||||
** YYYY-MM-DD
|
||||
**
|
||||
** Write the result as a julian day number in *prJD. Return 0
|
||||
** on success and 1 if the input string is not a well-formed
|
||||
** date.
|
||||
*/
|
||||
static int parseYyyyMmDd(const char *zDate, double *prJD){
|
||||
int Y, M, D;
|
||||
double rTime;
|
||||
int A, B, X1, X2;
|
||||
|
||||
Y = getDigits(zDate, 4);
|
||||
if( Y<0 || zDate[4]!='-' ) return 1;
|
||||
zDate += 5;
|
||||
M = getDigits(zDate, 2);
|
||||
if( M<=0 || M>12 || zDate[2]!='-' ) return 1;
|
||||
zDate += 3;
|
||||
D = getDigits(zDate, 2);
|
||||
if( D<=0 || D>31 ) return 1;
|
||||
zDate += 2;
|
||||
while( isspace(*zDate) ){ zDate++; }
|
||||
if( isdigit(*zDate) ){
|
||||
if( parseHhMmSs(zDate, &rTime) ) return 1;
|
||||
}else if( *zDate==0 ){
|
||||
rTime = 0.0;
|
||||
}else{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The year, month, and day are now stored in Y, M, and D. Convert
|
||||
** these into the Julian Day number. See Meeus page 61.
|
||||
*/
|
||||
if( M<=2 ){
|
||||
Y--;
|
||||
M += 12;
|
||||
}
|
||||
A = Y/100;
|
||||
B = 2 - A + (A/4);
|
||||
X1 = 365.25*(Y+4716);
|
||||
X2 = 30.6001*(M+1);
|
||||
*prJD = X1 + X2 + D + B - 1524.5 + rTime;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Attempt to parse the given string into a Julian Day Number. Return
|
||||
** the number of errors.
|
||||
**
|
||||
** The following are acceptable forms for the input string:
|
||||
**
|
||||
** YYYY-MM-DD
|
||||
** YYYY-MM-DD HH:MM
|
||||
** YYYY-MM-DD HH:MM:SS
|
||||
** HH:MM
|
||||
** HH:MM:SS
|
||||
** DDDD.DD
|
||||
** now
|
||||
*/
|
||||
static int parseDateOrTime(const char *zDate, double *prJD){
|
||||
int i;
|
||||
for(i=0; isdigit(zDate[i]); i++){}
|
||||
if( i==4 && zDate[i]=='-' ){
|
||||
return parseYyyyMmDd(zDate, prJD);
|
||||
}else if( i==2 && zDate[i]==':' ){
|
||||
return parseHhMmSs(zDate, prJD);
|
||||
}else if( i==0 && sqliteStrICmp(zDate,"now")==0 ){
|
||||
return sqliteOsCurrentTime(prJD);
|
||||
}else if( sqliteIsNumber(zDate) ){
|
||||
*prJD = atof(zDate);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Break up a julian day number into year, month, day, and seconds.
|
||||
** This function assume the Gregorian calendar - even for dates prior
|
||||
** to the invention of the Gregorian calendar in 1582.
|
||||
**
|
||||
** See Meeus page 63.
|
||||
*/
|
||||
static void decomposeDate(double JD, int *pY, int *pM, int *pD, int *pS){
|
||||
int Z, A, B, C, D, E, X1;
|
||||
Z = JD + 0.5;
|
||||
A = (Z - 1867216.25)/36524.25;
|
||||
A = Z + 1 + A - (A/4);
|
||||
B = A + 1524;
|
||||
C = (B - 122.1)/365.25;
|
||||
D = 365.25*C;
|
||||
E = (B-D)/30.6001;
|
||||
X1 = 30.6001*E;
|
||||
*pD = B - D - X1;
|
||||
*pM = E<14 ? E-1 : E-13;
|
||||
*pY = *pD>2 ? C - 4716 : C - 4715;
|
||||
*pS = (JD + 0.5 - Z)*86400.0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Check to see that all arguments are valid date strings. If any is
|
||||
** not a valid date string, return 0. If all are valid, return 1.
|
||||
** Write into *prJD the sum of the julian day numbers for all date
|
||||
** strings.
|
||||
*/
|
||||
static int isDate(
|
||||
sqlite_func *context,
|
||||
int argc,
|
||||
const char **argv,
|
||||
double *prJD
|
||||
){
|
||||
double r;
|
||||
int i;
|
||||
*prJD = 0.0;
|
||||
for(i=0; i<argc; i++){
|
||||
if( argv[i]==0 ) return 0;
|
||||
if( parseDateOrTime(argv[i], &r) ) return 0;
|
||||
*prJD += r;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** The following routines implement the various date and time functions
|
||||
** of SQLite.
|
||||
*/
|
||||
static void juliandayFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
sqlite_set_result_double(context, JD);
|
||||
}
|
||||
}
|
||||
static void timestampFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, h, m, s;
|
||||
char zBuf[100];
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
h = s/3600;
|
||||
s -= h*3600;
|
||||
m = s/60;
|
||||
s -= m*60;
|
||||
sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d", Y, M, D, h, m, s);
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
}
|
||||
static void timeFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, h, m, s;
|
||||
char zBuf[100];
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
h = s/3600;
|
||||
s -= h*3600;
|
||||
m = s/60;
|
||||
s -= m*60;
|
||||
sprintf(zBuf, "%02d:%02d:%02d", h, m, s);
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
}
|
||||
static void dateFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, s;
|
||||
char zBuf[100];
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
sprintf(zBuf, "%04d-%02d-%02d", Y, M, D);
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
}
|
||||
static void yearFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, s;
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
sqlite_set_result_int(context, Y);
|
||||
}
|
||||
}
|
||||
static void monthFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, s;
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
sqlite_set_result_int(context, M);
|
||||
}
|
||||
}
|
||||
static void dayofweekFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Z = JD + 1.5;
|
||||
sqlite_set_result_int(context, Z % 7);
|
||||
}
|
||||
}
|
||||
static void dayofmonthFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, s;
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
sqlite_set_result_int(context, D);
|
||||
}
|
||||
}
|
||||
static void secondFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, h, m, s;
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
h = s/3600;
|
||||
s -= h*3600;
|
||||
m = s/60;
|
||||
s -= m*60;
|
||||
sqlite_set_result_int(context, s);
|
||||
}
|
||||
}
|
||||
static void minuteFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, h, m, s;
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
h = s/3600;
|
||||
s -= h*3600;
|
||||
m = s/60;
|
||||
sqlite_set_result_int(context, m);
|
||||
}
|
||||
}
|
||||
static void hourFunc(sqlite_func *context, int argc, const char **argv){
|
||||
double JD;
|
||||
if( isDate(context, argc, argv, &JD) ){
|
||||
int Y, M, D, h, s;
|
||||
decomposeDate(JD, &Y, &M, &D, &s);
|
||||
h = s/3600;
|
||||
sqlite_set_result_int(context, h);
|
||||
}
|
||||
}
|
||||
#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
|
||||
/***************************************************************************/
|
||||
|
||||
/*
|
||||
** This function registered all of the above C functions as SQL
|
||||
** functions. This should be the only routine in this file with
|
||||
@ -529,6 +836,19 @@ void sqliteRegisterBuiltinFunctions(sqlite *db){
|
||||
{ "glob", 2, SQLITE_NUMERIC, globFunc },
|
||||
{ "nullif", 2, SQLITE_ARGS, nullifFunc },
|
||||
{ "sqlite_version",0,SQLITE_TEXT, versionFunc},
|
||||
#ifndef SQLITE_OMIT_DATETIME_FUNCS
|
||||
{ "julianday", -1, SQLITE_NUMERIC, juliandayFunc },
|
||||
{ "timestamp", -1, SQLITE_TEXT, timestampFunc },
|
||||
{ "time", -1, SQLITE_TEXT, timeFunc },
|
||||
{ "date", -1, SQLITE_TEXT, dateFunc },
|
||||
{ "year", -1, SQLITE_NUMERIC, yearFunc },
|
||||
{ "month", -1, SQLITE_NUMERIC, monthFunc },
|
||||
{ "dayofmonth",-1, SQLITE_NUMERIC, dayofmonthFunc },
|
||||
{ "dayofweek", -1, SQLITE_NUMERIC, dayofweekFunc },
|
||||
{ "hour", -1, SQLITE_NUMERIC, hourFunc },
|
||||
{ "minute", -1, SQLITE_NUMERIC, minuteFunc },
|
||||
{ "second", -1, SQLITE_NUMERIC, secondFunc },
|
||||
#endif
|
||||
#ifdef SQLITE_SOUNDEX
|
||||
{ "soundex", 1, SQLITE_TEXT, soundexFunc},
|
||||
#endif
|
||||
|
15
src/os.c
15
src/os.c
@ -1597,3 +1597,18 @@ char *sqliteOsFullPathname(const char *zRelative){
|
||||
return zFull;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
** Find the current time (in Universal Coordinated Time). Write the
|
||||
** current time and date as a Julian Day number into *prNow and
|
||||
** return 0. Return 1 if the time and date cannot be found.
|
||||
*/
|
||||
int sqliteOsCurrentTime(double *prNow){
|
||||
#if OS_UNIX
|
||||
time_t t;
|
||||
time(&t);
|
||||
*prNow = t/86400.0 + 2440587.5;
|
||||
return 0;
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
1
src/os.h
1
src/os.h
@ -171,6 +171,7 @@ int sqliteOsWriteLock(OsFile*);
|
||||
int sqliteOsUnlock(OsFile*);
|
||||
int sqliteOsRandomSeed(char*);
|
||||
int sqliteOsSleep(int ms);
|
||||
int sqliteOsCurrentTime(double*);
|
||||
void sqliteOsEnterMutex(void);
|
||||
void sqliteOsLeaveMutex(void);
|
||||
char *sqliteOsFullPathname(const char*);
|
||||
|
@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** Internal interface definitions for SQLite.
|
||||
**
|
||||
** @(#) $Id: sqliteInt.h,v 1.195 2003/07/30 12:34:12 drh Exp $
|
||||
** @(#) $Id: sqliteInt.h,v 1.196 2003/08/09 21:32:28 drh Exp $
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "sqlite.h"
|
||||
@ -87,6 +87,7 @@
|
||||
/* #define SQLITE_OMIT_AUTHORIZATION 1 */
|
||||
/* #define SQLITE_OMIT_INMEMORYDB 1 */
|
||||
/* #define SQLITE_OMIT_VACUUM 1 */
|
||||
/* #define SQLITE_OMIT_TIMEDATE_FUNCS 1 */
|
||||
|
||||
/*
|
||||
** Integers of known sizes. These typedefs might change for architectures
|
||||
|
Reference in New Issue
Block a user