Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add experimental date and time functions based on julian day number. (CVS 1069) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
a6197e2075fdf9db862484255ac16b28 |
User & Date: | drh 2003-08-09 21:32:28.000 |
Context
2003-08-10
| ||
01:50 | tighter coding of the date and time functions. Better comments. A bug fix. (CVS 1070) (check-in: 94243edac1 user: drh tags: trunk) | |
2003-08-09
| ||
21:32 | Add experimental date and time functions based on julian day number. (CVS 1069) (check-in: a6197e2075 user: drh tags: trunk) | |
2003-08-05
| ||
13:13 | Provide a more informative error message when a uniqueness constraint fails. Ticket #419. (CVS 1068) (check-in: 086aa1c992 user: drh tags: trunk) | |
Changes
Changes to src/func.c.
︙ | ︙ | |||
12 13 14 15 16 17 18 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** | | > | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** ** $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 */ static void minFunc(sqlite_func *context, int argc, const char **argv){ const char *zBest; int i; |
︙ | ︙ | |||
493 494 495 496 497 498 499 500 501 502 503 504 505 506 | sqlite_set_result_string(context, p->z, strlen(p->z)); } if( p && p->z && p->z!=p->zBuf ){ sqliteFree(p->z); } } /* ** This function registered all of the above C functions as SQL ** functions. This should be the only routine in this file with ** external linkage. */ void sqliteRegisterBuiltinFunctions(sqlite *db){ static struct { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | sqlite_set_result_string(context, p->z, strlen(p->z)); } if( p && p->z && p->z!=p->zBuf ){ sqliteFree(p->z); } } /**************************************************************************** ** 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 ** external linkage. */ void sqliteRegisterBuiltinFunctions(sqlite *db){ static struct { |
︙ | ︙ | |||
525 526 527 528 529 530 531 532 533 534 535 536 537 538 | { "coalesce", 1, 0, 0 }, { "ifnull", 2, SQLITE_ARGS, ifnullFunc }, { "random", -1, SQLITE_NUMERIC, randomFunc }, { "like", 2, SQLITE_NUMERIC, likeFunc }, { "glob", 2, SQLITE_NUMERIC, globFunc }, { "nullif", 2, SQLITE_ARGS, nullifFunc }, { "sqlite_version",0,SQLITE_TEXT, versionFunc}, #ifdef SQLITE_SOUNDEX { "soundex", 1, SQLITE_TEXT, soundexFunc}, #endif #ifdef SQLITE_TEST { "randstr", 2, SQLITE_TEXT, randStr }, #endif }; | > > > > > > > > > > > > > | 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 | { "coalesce", 1, 0, 0 }, { "ifnull", 2, SQLITE_ARGS, ifnullFunc }, { "random", -1, SQLITE_NUMERIC, randomFunc }, { "like", 2, SQLITE_NUMERIC, likeFunc }, { "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 #ifdef SQLITE_TEST { "randstr", 2, SQLITE_TEXT, randStr }, #endif }; |
︙ | ︙ |
Changes to src/os.c.
︙ | ︙ | |||
1593 1594 1595 1596 1597 1598 1599 | char zBuf[_MAX_PATH+1]; sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), zRelative, 0); } } return zFull; #endif } | > > > > > > > > > > > > > > > | 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 | char zBuf[_MAX_PATH+1]; sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), zRelative, 0); } } 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; } |
Changes to src/os.h.
︙ | ︙ | |||
167 168 169 170 171 172 173 174 175 176 177 178 179 180 | int sqliteOsTruncate(OsFile*, off_t size); int sqliteOsFileSize(OsFile*, off_t *pSize); int sqliteOsReadLock(OsFile*); int sqliteOsWriteLock(OsFile*); int sqliteOsUnlock(OsFile*); int sqliteOsRandomSeed(char*); int sqliteOsSleep(int ms); void sqliteOsEnterMutex(void); void sqliteOsLeaveMutex(void); char *sqliteOsFullPathname(const char*); #endif /* _SQLITE_OS_H_ */ | > | 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | int sqliteOsTruncate(OsFile*, off_t size); int sqliteOsFileSize(OsFile*, off_t *pSize); int sqliteOsReadLock(OsFile*); 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*); #endif /* _SQLITE_OS_H_ */ |
Changes to src/sqliteInt.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** ** @(#) $Id: sqliteInt.h,v 1.196 2003/08/09 21:32:28 drh Exp $ */ #include "config.h" #include "sqlite.h" #include "hash.h" #include "vdbe.h" #include "parse.h" #include "btree.h" |
︙ | ︙ | |||
83 84 85 86 87 88 89 90 91 92 93 94 95 96 | ** you can define one or more of the following macros to omit extra ** features of the library and thus keep the size of the library to ** a minimum. */ /* #define SQLITE_OMIT_AUTHORIZATION 1 */ /* #define SQLITE_OMIT_INMEMORYDB 1 */ /* #define SQLITE_OMIT_VACUUM 1 */ /* ** Integers of known sizes. These typedefs might change for architectures ** where the sizes very. Preprocessor macros are available so that the ** types can be conveniently redefined at compile-type. Like this: ** ** cc '-DUINTPTR_TYPE=long long int' ... | > | 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | ** you can define one or more of the following macros to omit extra ** features of the library and thus keep the size of the library to ** a minimum. */ /* #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 ** where the sizes very. Preprocessor macros are available so that the ** types can be conveniently redefined at compile-type. Like this: ** ** cc '-DUINTPTR_TYPE=long long int' ... |
︙ | ︙ |