16
16
** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
17
17
** All other code has file scope.
19
** $Id: date.c,v 1.58 2006/09/25 18:05:04 drh Exp $
19
** $Id: date.c,v 1.79 2008/03/20 14:03:29 drh Exp $
23
21
** SQLite processes all times and dates as Julian Day numbers. The
24
22
** dates and times are stored as the number of days since noon
134
** Or the "zulu" notation:
137
138
** If the parse is successful, write the number of minutes
138
** of change in *pnMin and return 0. If a parser error occurs,
139
** of change in p->tz and return 0. If a parser error occurs,
141
142
** A missing specifier is not considered an error.
143
144
static int parseTimezone(const char *zDate, DateTime *p){
146
148
while( isspace(*(u8*)zDate) ){ zDate++; }
150
}else if( *zDate=='+' ){
155
}else if( c=='Z' || c=='z' ){
156
162
if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
305
312
** as there is a time string. The time string can be omitted as long
306
313
** as there is a year and date.
308
static int parseDateOrTime(const char *zDate, DateTime *p){
315
static int parseDateOrTime(
316
sqlite3_context *context,
309
320
memset(p, 0, sizeof(*p));
310
321
if( parseYyyyMmDd(zDate,p)==0 ){
652
664
** argv[1] and following are modifiers. Parse them all and write
653
665
** the resulting time into the DateTime structure p. Return 0
654
666
** on success and 1 if there are any errors.
668
** If there are zero parameters (if even argv[0] is undefined)
669
** then assume a default value of "now" for argv[0].
656
static int isDate(int argc, sqlite3_value **argv, DateTime *p){
672
sqlite3_context *context,
674
sqlite3_value **argv,
658
if( argc==0 ) return 1;
659
if( SQLITE_NULL==sqlite3_value_type(argv[0]) ||
660
parseDateOrTime((char*)sqlite3_value_text(argv[0]), p) ) return 1;
678
const unsigned char *z;
679
static const unsigned char zDflt[] = "now";
683
z = sqlite3_value_text(argv[0]);
685
if( !z || parseDateOrTime(context, (char*)z, p) ){
661
688
for(i=1; i<argc; i++){
662
if( SQLITE_NULL==sqlite3_value_type(argv[i]) ||
663
parseModifier((char*)sqlite3_value_text(argv[i]), p) ) return 1;
689
if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
699
727
sqlite3_value **argv
702
if( isDate(argc, argv, &x)==0 ){
730
if( isDate(context, argc, argv, &x)==0 ){
704
732
computeYMD_HMS(&x);
705
sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
733
sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
734
x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
707
735
sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
719
747
sqlite3_value **argv
722
if( isDate(argc, argv, &x)==0 ){
750
if( isDate(context, argc, argv, &x)==0 ){
725
sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
753
sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
726
754
sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
738
766
sqlite3_value **argv
741
if( isDate(argc, argv, &x)==0 ){
769
if( isDate(context, argc, argv, &x)==0 ){
744
sprintf(zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
772
sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
745
773
sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
771
799
sqlite3_value **argv
776
805
const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
778
if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return;
807
if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
779
808
for(i=0, n=1; zFmt[i]; i++, n++){
780
809
if( zFmt[i]=='%' ){
781
810
switch( zFmt[i+1] ){
825
860
switch( zFmt[i] ){
826
case 'd': sprintf(&z[j],"%02d",x.D); j+=2; break;
861
case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
829
864
if( s>59.999 ) s = 59.999;
830
sqlite3_snprintf(7, &z[j],"%02.3f", s);
865
sqlite3_snprintf(7, &z[j],"%06.3f", s);
831
866
j += strlen(&z[j]);
834
case 'H': sprintf(&z[j],"%02d",x.h); j+=2; break;
869
case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
835
870
case 'W': /* Fall thru */
837
872
int nDay; /* Number of days since 1st day of year */
843
nDay = x.rJD - y.rJD;
878
nDay = x.rJD - y.rJD + 0.5;
844
879
if( zFmt[i]=='W' ){
845
880
int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
846
881
wd = ((int)(x.rJD+0.5)) % 7;
847
sprintf(&z[j],"%02d",(nDay+7-wd)/7);
882
sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
850
sprintf(&z[j],"%03d",nDay+1);
885
sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
855
case 'J': sprintf(&z[j],"%.16g",x.rJD); j+=strlen(&z[j]); break;
856
case 'm': sprintf(&z[j],"%02d",x.M); j+=2; break;
857
case 'M': sprintf(&z[j],"%02d",x.m); j+=2; break;
891
sqlite3_snprintf(20, &z[j],"%.16g",x.rJD);
895
case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
896
case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
859
sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0 + 0.5));
898
sqlite3_snprintf(30,&z[j],"%d",
899
(int)((x.rJD-2440587.5)*86400.0 + 0.5));
860
900
j += strlen(&z[j]);
863
case 'S': sprintf(&z[j],"%02d",(int)(x.s+0.5)); j+=2; break;
903
case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
864
904
case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break;
865
case 'Y': sprintf(&z[j],"%04d",x.Y); j+=strlen(&z[j]); break;
866
case '%': z[j++] = '%'; break;
905
case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break;
906
default: z[j++] = '%'; break;
871
sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
911
sqlite3_result_text(context, z, -1,
912
z==zBuf ? SQLITE_TRANSIENT : sqlite3_free);
921
949
sqlite3_value **argv
923
sqlite3_value *pVal = sqlite3ValueNew();
925
sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
926
datetimeFunc(context, 1, &pVal);
927
sqlite3ValueFree(pVal);
951
datetimeFunc(context, 0, 0);
930
953
#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
950
973
char *zFormat = (char *)sqlite3_user_data(context);
956
extern int sqlite3_current_time; /* See os_XXX.c */
957
if( sqlite3_current_time ){
958
t = sqlite3_current_time;
978
db = sqlite3_context_db_handle(context);
979
sqlite3OsCurrentTime(db->pVfs, &rT);
980
t = 86400.0*(rT - 2440587.5) + 0.5;
963
981
#ifdef HAVE_GMTIME_R