mirror of
https://github.com/sqlite/sqlite.git
synced 2025-10-24 09:53:10 +03:00
Revised date/time functions - now broken out into a separate source file.
See the DateAndTimeFunctions wiki page for additional information. (CVS 1116) FossilOrigin-Name: 68ef9b45bd3abdedf3721011ad0fb22e8735e721
This commit is contained in:
400
src/func.c
400
src/func.c
@@ -16,7 +16,7 @@
|
||||
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
|
||||
** All other code has file scope.
|
||||
**
|
||||
** $Id: func.c,v 1.32 2003/10/10 02:09:57 drh Exp $
|
||||
** $Id: func.c,v 1.33 2003/11/01 01:53:54 drh Exp $
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
@@ -539,388 +539,6 @@ static void minMaxFinalize(sqlite_func *context){
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
** Time and date functions.
|
||||
**
|
||||
** 1970-01-01 00:00:00 is JD 2440587.5.
|
||||
** 2000-01-01 00:00:00 is JD 2451544.5
|
||||
**
|
||||
** 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 November 24, 4714 B.C. according to the Gregorian
|
||||
** calendar system.
|
||||
**
|
||||
** 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 usually
|
||||
** 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 a timezone extension on the end of a datetime stamp.
|
||||
** The extension is of the form:
|
||||
**
|
||||
** (+/-)HH:MM
|
||||
**
|
||||
** If the parse is successful, write the number of minutes
|
||||
** of change in *pnMin and return 0. If a parser error occurs,
|
||||
** return 0.
|
||||
**
|
||||
** A missing specifier is not considered an error.
|
||||
*/
|
||||
static int parseTimezone(const char *zDate, int *pnMin){
|
||||
int sgn = 0;
|
||||
int nHr, nMn;
|
||||
while( isspace(*zDate) ){ zDate++; }
|
||||
*pnMin = 0;
|
||||
if( *zDate=='-' ){
|
||||
sgn = -1;
|
||||
}else if( *zDate=='+' ){
|
||||
sgn = +1;
|
||||
}else{
|
||||
return *zDate!=0;
|
||||
}
|
||||
zDate++;
|
||||
nHr = getDigits(zDate, 2);
|
||||
if( nHr<0 || nHr>14 ) return 1;
|
||||
zDate += 2;
|
||||
if( zDate[0]!=':' ) return 1;
|
||||
zDate++;
|
||||
nMn = getDigits(zDate, 2);
|
||||
if( nMn<0 || nMn>59 ) return 1;
|
||||
zDate += 2;
|
||||
*pnMin = sgn*(nMn + nHr*60);
|
||||
while( isspace(*zDate) ){ *zDate++; }
|
||||
return *zDate!=0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
|
||||
** The HH, MM, and SS must each be exactly 2 digits. The
|
||||
** fractional seconds FFFF can be one or more digits.
|
||||
**
|
||||
** The time string can be followed by an optional timezone specifier
|
||||
** of the following form: (+/-)HH:MM.
|
||||
**
|
||||
** Whatever the format, the string is converted into a julian
|
||||
** day number and stored 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, tz;
|
||||
double ms = 0.0;
|
||||
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;
|
||||
if( *zDate=='.' && isdigit(zDate[1]) ){
|
||||
double rScale = 1.0/864000.0;
|
||||
zDate++;
|
||||
while( isdigit(*zDate) ){
|
||||
ms += rScale * (*zDate - '0');
|
||||
rScale *= 0.1;
|
||||
zDate++;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
s = 0;
|
||||
}
|
||||
if( parseTimezone(zDate, &tz) ) return 1;
|
||||
*prJD = (h*3600.0 + (m+tz)*60.0 + s)/86400.0 + ms;
|
||||
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 HH:MM:SS.FFF +/-HH:MM
|
||||
** DDDD.DD
|
||||
** now
|
||||
**
|
||||
** In the first form, the +/-HH:MM is always optional. The fractional
|
||||
** seconds extension (the ".FFF") is optional. The seconds portion
|
||||
** (":SS.FFF") is option. The year and date can be omitted as long
|
||||
** as there is a time string. The time string can be omitted as long
|
||||
** as there is a year and date.
|
||||
**
|
||||
** If the bRelative flag is set and the format is HH:MM or HH:MM:SS then
|
||||
** make the result is relative to midnight instead of noon. In other words,
|
||||
** if bRelative is true, "00:00:00" parses to 0.0 but if bRelative is
|
||||
** false, "00:00:00" parses to 0.5.
|
||||
*/
|
||||
static int parseDateOrTime(const char *zDate, int bRelative, 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]==':' ){
|
||||
if( parseHhMmSs(zDate, prJD) ) return 1;
|
||||
if( !bRelative ) *prJD += 2451544.5;
|
||||
return 0;
|
||||
}else if( i==0 && sqliteStrICmp(zDate,"now")==0 ){
|
||||
return sqliteOsCurrentTime(prJD);
|
||||
}else if( sqliteIsNumber(zDate) ){
|
||||
*prJD = atof(zDate);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** A structure for holding date and time.
|
||||
*/
|
||||
typedef struct DateTime DateTime;
|
||||
struct DateTime {
|
||||
double rJD; /* The julian day number */
|
||||
int Y, M, D; /* Year, month, and day */
|
||||
int h, m; /* Hour and minutes */
|
||||
double s; /* Seconds */
|
||||
};
|
||||
|
||||
/*
|
||||
** Break up a julian day number into year, month, day, hour, minute, second.
|
||||
** This function assume the Gregorian calendar - even for dates prior
|
||||
** to the invention of the Gregorian calendar in 1582.
|
||||
**
|
||||
** See Meeus page 63.
|
||||
**
|
||||
** If mode==1 only the year, month, and day are computed. If mode==2
|
||||
** then only the hour, minute, and second are computed. If mode==3 then
|
||||
** everything is computed. If mode==0, this routine is a no-op.
|
||||
*/
|
||||
static void decomposeDate(DateTime *p, int mode){
|
||||
int Z;
|
||||
Z = p->rJD + 0.5;
|
||||
if( mode & 1 ){
|
||||
int A, B, C, D, E, X1;
|
||||
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;
|
||||
p->D = B - D - X1;
|
||||
p->M = E<14 ? E-1 : E-13;
|
||||
p->Y = p->M>2 ? C - 4716 : C - 4715;
|
||||
}
|
||||
if( mode & 2 ){
|
||||
int s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5;
|
||||
p->s = 0.001*s;
|
||||
s = p->s;
|
||||
p->s -= s;
|
||||
p->h = s/3600;
|
||||
s -= p->h*3600;
|
||||
p->m = s/60;
|
||||
p->s += s - p->m*60;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Check to see that all arguments are valid date strings. If any
|
||||
** argument is not a valid date string, return 0. If all arguments
|
||||
** are valid, return 1 and write into *p->rJD the sum of the julian day
|
||||
** numbers for all date strings.
|
||||
**
|
||||
** A "valid" date string is one that is accepted by parseDateOrTime().
|
||||
**
|
||||
** The mode argument is passed through to decomposeDate() in order to
|
||||
** fill in the year, month, day, hour, minute, and second of the *p
|
||||
** structure, if desired.
|
||||
*/
|
||||
static int isDate(int argc, const char **argv, DateTime *p, int mode){
|
||||
double r;
|
||||
int i;
|
||||
p->rJD = 0.0;
|
||||
for(i=0; i<argc; i++){
|
||||
if( argv[i]==0 ) return 0;
|
||||
if( parseDateOrTime(argv[i], i, &r) ) return 0;
|
||||
p->rJD += r;
|
||||
}
|
||||
decomposeDate(p, mode);
|
||||
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){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 0) ){
|
||||
sqlite_set_result_double(context, x.rJD);
|
||||
}
|
||||
}
|
||||
static void timestampFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 3) ){
|
||||
char zBuf[100];
|
||||
sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
|
||||
(int)(x.s+0.5));
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
}
|
||||
static void timeFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 2) ){
|
||||
char zBuf[100];
|
||||
sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)(x.s+0.5));
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
}
|
||||
static void dateFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 1) ){
|
||||
char zBuf[100];
|
||||
sprintf(zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
}
|
||||
static void yearFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 1) ){
|
||||
sqlite_set_result_int(context, x.Y);
|
||||
}
|
||||
}
|
||||
static void monthFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 1) ){
|
||||
sqlite_set_result_int(context, x.M);
|
||||
}
|
||||
}
|
||||
static void dayofweekFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 0) ){
|
||||
int Z = x.rJD + 1.5;
|
||||
sqlite_set_result_int(context, Z % 7);
|
||||
}
|
||||
}
|
||||
static void dayofmonthFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 1) ){
|
||||
sqlite_set_result_int(context, x.D);
|
||||
}
|
||||
}
|
||||
static void secondFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 2) ){
|
||||
sqlite_set_result_double(context, x.s);
|
||||
}
|
||||
}
|
||||
static void minuteFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 2) ){
|
||||
sqlite_set_result_int(context, x.m);
|
||||
}
|
||||
}
|
||||
static void hourFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 2) ){
|
||||
sqlite_set_result_int(context, x.h);
|
||||
}
|
||||
}
|
||||
static void unixToJdFunc(sqlite_func *context, int argc, const char **argv){
|
||||
sqlite_set_result_double(context, atof(argv[0])/(24.0*3600.0)+2440587.5);
|
||||
}
|
||||
static void unixtimeFunc(sqlite_func *context, int argc, const char **argv){
|
||||
DateTime x;
|
||||
if( isDate(argc, argv, &x, 0) ){
|
||||
sqlite_set_result_double(context, (x.rJD-2440587.5)*24.0*3600.0);
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@@ -954,21 +572,6 @@ void sqliteRegisterBuiltinFunctions(sqlite *db){
|
||||
{ "nullif", 2, SQLITE_ARGS, nullifFunc },
|
||||
{ "sqlite_version",0,SQLITE_TEXT, versionFunc},
|
||||
{ "quote", 1, SQLITE_ARGS, quoteFunc },
|
||||
#ifndef SQLITE_OMIT_DATETIME_FUNCS
|
||||
{ "julianday", -1, SQLITE_NUMERIC, juliandayFunc },
|
||||
{ "unixtime", -1, SQLITE_NUMERIC, unixtimeFunc },
|
||||
{ "unix_to_jd", 1, SQLITE_NUMERIC, unixToJdFunc },
|
||||
{ "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
|
||||
@@ -1010,4 +613,5 @@ void sqliteRegisterBuiltinFunctions(sqlite *db){
|
||||
aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
|
||||
sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
|
||||
}
|
||||
sqliteRegisterDateTimeFunctions(db);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user