~ubuntu-branches/ubuntu/maverick/sqlite3/maverick-updates

« back to all changes in this revision

Viewing changes to src/date.c

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2008-10-01 20:16:18 UTC
  • mfrom: (3.1.20 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081001201618-yfvqqj1qs29wdtcc
Tags: 3.5.9-5
Backport fix for distinct on indexes (closes: #500792).

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
17
17
** All other code has file scope.
18
18
**
19
 
** $Id: date.c,v 1.58 2006/09/25 18:05:04 drh Exp $
20
 
**
21
 
** NOTES:
 
19
** $Id: date.c,v 1.79 2008/03/20 14:03:29 drh Exp $
22
20
**
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
48
46
**      Richmond, Virginia (USA)
49
47
*/
50
48
#include "sqliteInt.h"
51
 
#include "os.h"
52
49
#include <ctype.h>
53
50
#include <stdlib.h>
54
51
#include <assert.h>
134
131
**
135
132
**        (+/-)HH:MM
136
133
**
 
134
** Or the "zulu" notation:
 
135
**
 
136
**        Z
 
137
**
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
 
** return 0.
 
139
** of change in p->tz and return 0.  If a parser error occurs,
 
140
** return non-zero.
140
141
**
141
142
** A missing specifier is not considered an error.
142
143
*/
143
144
static int parseTimezone(const char *zDate, DateTime *p){
144
145
  int sgn = 0;
145
146
  int nHr, nMn;
 
147
  int c;
146
148
  while( isspace(*(u8*)zDate) ){ zDate++; }
147
149
  p->tz = 0;
148
 
  if( *zDate=='-' ){
 
150
  c = *zDate;
 
151
  if( c=='-' ){
149
152
    sgn = -1;
150
 
  }else if( *zDate=='+' ){
 
153
  }else if( c=='+' ){
151
154
    sgn = +1;
 
155
  }else if( c=='Z' || c=='z' ){
 
156
    zDate++;
 
157
    goto zulu_time;
152
158
  }else{
153
 
    return *zDate!=0;
 
159
    return c!=0;
154
160
  }
155
161
  zDate++;
156
162
  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
158
164
  }
159
165
  zDate += 5;
160
166
  p->tz = sgn*(nMn + nHr*60);
 
167
zulu_time:
161
168
  while( isspace(*(u8*)zDate) ){ zDate++; }
162
169
  return *zDate!=0;
163
170
}
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.
307
314
*/
308
 
static int parseDateOrTime(const char *zDate, DateTime *p){
 
315
static int parseDateOrTime(
 
316
  sqlite3_context *context, 
 
317
  const char *zDate, 
 
318
  DateTime *p
 
319
){
309
320
  memset(p, 0, sizeof(*p));
310
321
  if( parseYyyyMmDd(zDate,p)==0 ){
311
322
    return 0;
313
324
    return 0;
314
325
  }else if( sqlite3StrICmp(zDate,"now")==0){
315
326
    double r;
316
 
    sqlite3OsCurrentTime(&r);
 
327
    sqlite3 *db = sqlite3_context_db_handle(context);
 
328
    sqlite3OsCurrentTime(db->pVfs, &r);
317
329
    p->rJD = r;
318
330
    p->validJD = 1;
319
331
    return 0;
425
437
#else
426
438
  {
427
439
    struct tm *pTm;
428
 
    sqlite3OsEnterMutex();
 
440
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
429
441
    pTm = localtime(&t);
430
442
    y.Y = pTm->tm_year + 1900;
431
443
    y.M = pTm->tm_mon + 1;
433
445
    y.h = pTm->tm_hour;
434
446
    y.m = pTm->tm_min;
435
447
    y.s = pTm->tm_sec;
436
 
    sqlite3OsLeaveMutex();
 
448
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
437
449
  }
438
450
#endif
439
451
  y.validYMD = 1;
577
589
    case '8':
578
590
    case '9': {
579
591
      n = getValue(z, &r);
580
 
      if( n<=0 ) break;
 
592
      assert( n>=1 );
581
593
      if( z[n]==':' ){
582
594
        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
583
595
        ** specified number of hours, minutes, seconds, and fractional seconds
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.
 
667
**
 
668
** If there are zero parameters (if even argv[0] is undefined)
 
669
** then assume a default value of "now" for argv[0].
655
670
*/
656
 
static int isDate(int argc, sqlite3_value **argv, DateTime *p){
 
671
static int isDate(
 
672
  sqlite3_context *context, 
 
673
  int argc, 
 
674
  sqlite3_value **argv, 
 
675
  DateTime *p
 
676
){
657
677
  int i;
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";
 
680
  if( argc==0 ){
 
681
    z = zDflt;
 
682
  }else{
 
683
    z = sqlite3_value_text(argv[0]);
 
684
  }
 
685
  if( !z || parseDateOrTime(context, (char*)z, p) ){
 
686
    return 1;
 
687
  }
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) ){
 
690
      return 1;
 
691
    }
664
692
  }
665
693
  return 0;
666
694
}
682
710
  sqlite3_value **argv
683
711
){
684
712
  DateTime x;
685
 
  if( isDate(argc, argv, &x)==0 ){
 
713
  if( isDate(context, argc, argv, &x)==0 ){
686
714
    computeJD(&x);
687
715
    sqlite3_result_double(context, x.rJD);
688
716
  }
699
727
  sqlite3_value **argv
700
728
){
701
729
  DateTime x;
702
 
  if( isDate(argc, argv, &x)==0 ){
 
730
  if( isDate(context, argc, argv, &x)==0 ){
703
731
    char zBuf[100];
704
732
    computeYMD_HMS(&x);
705
 
    sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
706
 
           (int)(x.s));
 
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);
708
736
  }
709
737
}
719
747
  sqlite3_value **argv
720
748
){
721
749
  DateTime x;
722
 
  if( isDate(argc, argv, &x)==0 ){
 
750
  if( isDate(context, argc, argv, &x)==0 ){
723
751
    char zBuf[100];
724
752
    computeHMS(&x);
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);
727
755
  }
728
756
}
738
766
  sqlite3_value **argv
739
767
){
740
768
  DateTime x;
741
 
  if( isDate(argc, argv, &x)==0 ){
 
769
  if( isDate(context, argc, argv, &x)==0 ){
742
770
    char zBuf[100];
743
771
    computeYMD(&x);
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);
746
774
  }
747
775
}
771
799
  sqlite3_value **argv
772
800
){
773
801
  DateTime x;
774
 
  int n, i, j;
 
802
  u64 n;
 
803
  int i, j;
775
804
  char *z;
776
805
  const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
777
806
  char zBuf[100];
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] ){
811
840
  }
812
841
  if( n<sizeof(zBuf) ){
813
842
    z = zBuf;
 
843
  }else if( n>sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH] ){
 
844
    sqlite3_result_error_toobig(context);
 
845
    return;
814
846
  }else{
815
 
    z = sqliteMalloc( n );
816
 
    if( z==0 ) return;
 
847
    z = sqlite3_malloc( n );
 
848
    if( z==0 ){
 
849
      sqlite3_result_error_nomem(context);
 
850
      return;
 
851
    }
817
852
  }
818
853
  computeJD(&x);
819
854
  computeYMD_HMS(&x);
823
858
    }else{
824
859
      i++;
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;
827
862
        case 'f': {
828
863
          double s = x.s;
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]);
832
867
          break;
833
868
        }
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 */
836
871
        case 'j': {
837
872
          int nDay;             /* Number of days since 1st day of year */
840
875
          y.M = 1;
841
876
          y.D = 1;
842
877
          computeJD(&y);
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);
848
883
            j += 2;
849
884
          }else{
850
 
            sprintf(&z[j],"%03d",nDay+1);
 
885
            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
851
886
            j += 3;
852
887
          }
853
888
          break;
854
889
        }
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;
 
890
        case 'J': {
 
891
          sqlite3_snprintf(20, &z[j],"%.16g",x.rJD);
 
892
          j+=strlen(&z[j]);
 
893
          break;
 
894
        }
 
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;
858
897
        case 's': {
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]);
861
901
          break;
862
902
        }
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;
867
907
      }
868
908
    }
869
909
  }
870
910
  z[j] = 0;
871
 
  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
872
 
  if( z!=zBuf ){
873
 
    sqliteFree(z);
874
 
  }
 
911
  sqlite3_result_text(context, z, -1,
 
912
                      z==zBuf ? SQLITE_TRANSIENT : sqlite3_free);
875
913
}
876
914
 
877
915
/*
884
922
  int argc,
885
923
  sqlite3_value **argv
886
924
){
887
 
  sqlite3_value *pVal = sqlite3ValueNew();
888
 
  if( pVal ){
889
 
    sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
890
 
    timeFunc(context, 1, &pVal);
891
 
    sqlite3ValueFree(pVal);
892
 
  }
 
925
  timeFunc(context, 0, 0);
893
926
}
894
927
 
895
928
/*
902
935
  int argc,
903
936
  sqlite3_value **argv
904
937
){
905
 
  sqlite3_value *pVal = sqlite3ValueNew();
906
 
  if( pVal ){
907
 
    sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
908
 
    dateFunc(context, 1, &pVal);
909
 
    sqlite3ValueFree(pVal);
910
 
  }
 
938
  dateFunc(context, 0, 0);
911
939
}
912
940
 
913
941
/*
920
948
  int argc,
921
949
  sqlite3_value **argv
922
950
){
923
 
  sqlite3_value *pVal = sqlite3ValueNew();
924
 
  if( pVal ){
925
 
    sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
926
 
    datetimeFunc(context, 1, &pVal);
927
 
    sqlite3ValueFree(pVal);
928
 
  }
 
951
  datetimeFunc(context, 0, 0);
929
952
}
930
953
#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
931
954
 
948
971
){
949
972
  time_t t;
950
973
  char *zFormat = (char *)sqlite3_user_data(context);
 
974
  sqlite3 *db;
 
975
  double rT;
951
976
  char zBuf[20];
952
977
 
953
 
  time(&t);
954
 
#ifdef SQLITE_TEST
955
 
  {
956
 
    extern int sqlite3_current_time;  /* See os_XXX.c */
957
 
    if( sqlite3_current_time ){
958
 
      t = sqlite3_current_time;
959
 
    }
960
 
  }
961
 
#endif
962
 
 
 
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
964
982
  {
965
983
    struct tm sNow;
969
987
#else
970
988
  {
971
989
    struct tm *pTm;
972
 
    sqlite3OsEnterMutex();
 
990
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
973
991
    pTm = gmtime(&t);
974
992
    strftime(zBuf, 20, zFormat, pTm);
975
 
    sqlite3OsLeaveMutex();
 
993
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
976
994
  }
977
995
#endif
978
996