engine/sqlite/src/date.cpp
author Sebastian Brannstrom <sebastianb@symbian.org>
Fri, 09 Jul 2010 23:58:47 +0100
branch3rded
changeset 175 cd124c4eb6b9
parent 2 29cda98b007e
permissions -rw-r--r--
Started port to 3rd edition
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     1
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     2
** 2003 October 31
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     3
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     4
** The author disclaims copyright to this source code.  In place of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     5
** a legal notice, here is a blessing:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     6
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     7
**    May you do good and not evil.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     8
**    May you find forgiveness for yourself and forgive others.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     9
**    May you share freely, never taking more than you give.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    10
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    11
*************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    12
** This file contains the C functions that implement date and time
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    13
** functions for SQLite.  
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    14
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    15
** There is only one exported symbol in this file - the function
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    16
** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    17
** All other code has file scope.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    18
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    19
** $Id: date.cpp 1282 2008-11-13 09:31:33Z LarsPson $
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    20
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    21
** SQLite processes all times and dates as Julian Day numbers.  The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    22
** dates and times are stored as the number of days since noon
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    23
** in Greenwich on November 24, 4714 B.C. according to the Gregorian
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    24
** calendar system. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    25
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    26
** 1970-01-01 00:00:00 is JD 2440587.5
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    27
** 2000-01-01 00:00:00 is JD 2451544.5
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    28
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    29
** This implemention requires years to be expressed as a 4-digit number
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    30
** which means that only dates between 0000-01-01 and 9999-12-31 can
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    31
** be represented, even though julian day numbers allow a much wider
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    32
** range of dates.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    33
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    34
** The Gregorian calendar system is used for all dates and times,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    35
** even those that predate the Gregorian calendar.  Historians usually
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    36
** use the Julian calendar for dates prior to 1582-10-15 and for some
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    37
** dates afterwards, depending on locale.  Beware of this difference.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    38
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    39
** The conversion algorithms are implemented based on descriptions
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    40
** in the following text:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    41
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    42
**      Jean Meeus
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    43
**      Astronomical Algorithms, 2nd Edition, 1998
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    44
**      ISBM 0-943396-61-1
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    45
**      Willmann-Bell, Inc
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    46
**      Richmond, Virginia (USA)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    47
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    48
#include "sqliteInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    49
#include <ctype.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    50
#include <stdlib.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    51
#include <assert.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    52
#include <time.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    53
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    54
#ifndef SQLITE_OMIT_DATETIME_FUNCS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    55
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    56
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    57
** A structure for holding a single date and time.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    58
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    59
typedef struct DateTime DateTime;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    60
struct DateTime {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    61
  double rJD;      /* The julian day number */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    62
  int Y, M, D;     /* Year, month, and day */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    63
  int h, m;        /* Hour and minutes */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    64
  int tz;          /* Timezone offset in minutes */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    65
  double s;        /* Seconds */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    66
  char validYMD;   /* True if Y,M,D are valid */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    67
  char validHMS;   /* True if h,m,s are valid */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    68
  char validJD;    /* True if rJD is valid */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    69
  char validTZ;    /* True if tz is valid */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    70
};
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    71
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    72
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    73
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    74
** Convert zDate into one or more integers.  Additional arguments
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    75
** come in groups of 5 as follows:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    76
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    77
**       N       number of digits in the integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    78
**       min     minimum allowed value of the integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    79
**       max     maximum allowed value of the integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    80
**       nextC   first character after the integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    81
**       pVal    where to write the integers value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    82
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    83
** Conversions continue until one with nextC==0 is encountered.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    84
** The function returns the number of successful conversions.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    85
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    86
static int getDigits(const char *zDate, ...){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    87
  va_list ap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    88
  int val;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    89
  int N;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    90
  int min;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    91
  int max;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    92
  int nextC;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    93
  int *pVal;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    94
  int cnt = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    95
  va_start(ap, zDate);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    96
  do{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    97
    N = va_arg(ap, int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    98
    min = va_arg(ap, int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    99
    max = va_arg(ap, int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   100
    nextC = va_arg(ap, int);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   101
    pVal = va_arg(ap, int*);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   102
    val = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   103
    while( N-- ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   104
      if( !isdigit(*(u8*)zDate) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   105
        goto end_getDigits;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   106
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   107
      val = val*10 + *zDate - '0';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   108
      zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   109
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   110
    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   111
      goto end_getDigits;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   112
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   113
    *pVal = val;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   114
    zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   115
    cnt++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   116
  }while( nextC );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   117
end_getDigits:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   118
  va_end(ap);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   119
  return cnt;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   120
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   121
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   122
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   123
** Read text from z[] and convert into a floating point number.  Return
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   124
** the number of digits converted.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   125
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   126
#define getValue sqlite3AtoF
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   127
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   128
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   129
** Parse a timezone extension on the end of a date-time.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   130
** The extension is of the form:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   131
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   132
**        (+/-)HH:MM
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   133
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   134
** If the parse is successful, write the number of minutes
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   135
** of change in *pnMin and return 0.  If a parser error occurs,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   136
** return 0.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   137
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   138
** A missing specifier is not considered an error.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   139
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   140
static int parseTimezone(const char *zDate, DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   141
  int sgn = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   142
  int nHr, nMn;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   143
  while( isspace(*(u8*)zDate) ){ zDate++; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   144
  p->tz = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   145
  if( *zDate=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   146
    sgn = -1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   147
  }else if( *zDate=='+' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   148
    sgn = +1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   149
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   150
    return *zDate!=0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   151
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   152
  zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   153
  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   154
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   155
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   156
  zDate += 5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   157
  p->tz = sgn*(nMn + nHr*60);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   158
  while( isspace(*(u8*)zDate) ){ zDate++; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   159
  return *zDate!=0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   160
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   161
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   162
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   163
** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   164
** The HH, MM, and SS must each be exactly 2 digits.  The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   165
** fractional seconds FFFF can be one or more digits.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   166
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   167
** Return 1 if there is a parsing error and 0 on success.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   168
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   169
static int parseHhMmSs(const char *zDate, DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   170
  int h, m, s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   171
  double ms = 0.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   172
  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   173
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   174
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   175
  zDate += 5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   176
  if( *zDate==':' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   177
    zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   178
    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   179
      return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   180
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   181
    zDate += 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   182
    if( *zDate=='.' && isdigit((u8)zDate[1]) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   183
      double rScale = 1.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   184
      zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   185
      while( isdigit(*(u8*)zDate) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   186
        ms = ms*10.0 + *zDate - '0';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   187
        rScale *= 10.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   188
        zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   189
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   190
      ms /= rScale;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   191
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   192
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   193
    s = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   194
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   195
  p->validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   196
  p->validHMS = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   197
  p->h = h;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   198
  p->m = m;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   199
  p->s = s + ms;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   200
  if( parseTimezone(zDate, p) ) return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   201
  p->validTZ = p->tz!=0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   202
  return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   203
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   204
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   205
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   206
** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   207
** that the YYYY-MM-DD is according to the Gregorian calendar.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   208
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   209
** Reference:  Meeus page 61
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   210
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   211
static void computeJD(DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   212
  int Y, M, D, A, B, X1, X2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   213
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   214
  if( p->validJD ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   215
  if( p->validYMD ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   216
    Y = p->Y;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   217
    M = p->M;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   218
    D = p->D;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   219
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   220
    Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   221
    M = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   222
    D = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   223
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   224
  if( M<=2 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   225
    Y--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   226
    M += 12;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   227
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   228
  A = Y/100;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   229
  B = 2 - A + (A/4);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   230
  X1 = 365.25*(Y+4716);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   231
  X2 = 30.6001*(M+1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   232
  p->rJD = X1 + X2 + D + B - 1524.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   233
  p->validJD = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   234
  if( p->validHMS ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   235
    p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   236
    if( p->validTZ ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   237
      p->rJD -= p->tz*60/86400.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   238
      p->validYMD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   239
      p->validHMS = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   240
      p->validTZ = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   241
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   242
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   243
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   244
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   245
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   246
** Parse dates of the form
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   247
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   248
**     YYYY-MM-DD HH:MM:SS.FFF
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   249
**     YYYY-MM-DD HH:MM:SS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   250
**     YYYY-MM-DD HH:MM
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   251
**     YYYY-MM-DD
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   252
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   253
** Write the result into the DateTime structure and return 0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   254
** on success and 1 if the input string is not a well-formed
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   255
** date.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   256
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   257
static int parseYyyyMmDd(const char *zDate, DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   258
  int Y, M, D, neg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   259
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   260
  if( zDate[0]=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   261
    zDate++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   262
    neg = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   263
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   264
    neg = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   265
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   266
  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   267
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   268
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   269
  zDate += 10;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   270
  while( isspace(*(u8*)zDate) || 'T'==*(u8*)zDate ){ zDate++; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   271
  if( parseHhMmSs(zDate, p)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   272
    /* We got the time */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   273
  }else if( *zDate==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   274
    p->validHMS = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   275
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   276
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   277
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   278
  p->validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   279
  p->validYMD = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   280
  p->Y = neg ? -Y : Y;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   281
  p->M = M;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   282
  p->D = D;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   283
  if( p->validTZ ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   284
    computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   285
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   286
  return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   287
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   288
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   289
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   290
** Attempt to parse the given string into a Julian Day Number.  Return
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   291
** the number of errors.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   292
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   293
** The following are acceptable forms for the input string:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   294
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   295
**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   296
**      DDDD.DD 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   297
**      now
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   298
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   299
** In the first form, the +/-HH:MM is always optional.  The fractional
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   300
** seconds extension (the ".FFF") is optional.  The seconds portion
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   301
** (":SS.FFF") is option.  The year and date can be omitted as long
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   302
** as there is a time string.  The time string can be omitted as long
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   303
** as there is a year and date.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   304
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   305
static int parseDateOrTime(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   306
  sqlite3_context *context, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   307
  const char *zDate, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   308
  DateTime *p
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   309
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   310
  memset(p, 0, sizeof(*p));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   311
  if( parseYyyyMmDd(zDate,p)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   312
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   313
  }else if( parseHhMmSs(zDate, p)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   314
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   315
  }else if( sqlite3StrICmp(zDate,"now")==0){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   316
    double r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   317
    sqlite3OsCurrentTime((sqlite3_vfs *)sqlite3_user_data(context), &r);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   318
    p->rJD = r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   319
    p->validJD = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   320
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   321
  }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   322
    getValue(zDate, &p->rJD);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   323
    p->validJD = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   324
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   325
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   326
  return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   327
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   328
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   329
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   330
** Compute the Year, Month, and Day from the julian day number.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   331
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   332
static void computeYMD(DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   333
  int Z, A, B, C, D, E, X1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   334
  if( p->validYMD ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   335
  if( !p->validJD ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   336
    p->Y = 2000;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   337
    p->M = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   338
    p->D = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   339
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   340
    Z = p->rJD + 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   341
    A = (Z - 1867216.25)/36524.25;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   342
    A = Z + 1 + A - (A/4);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   343
    B = A + 1524;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   344
    C = (B - 122.1)/365.25;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   345
    D = 365.25*C;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   346
    E = (B-D)/30.6001;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   347
    X1 = 30.6001*E;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   348
    p->D = B - D - X1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   349
    p->M = E<14 ? E-1 : E-13;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   350
    p->Y = p->M>2 ? C - 4716 : C - 4715;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   351
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   352
  p->validYMD = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   353
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   354
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   355
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   356
** Compute the Hour, Minute, and Seconds from the julian day number.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   357
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   358
static void computeHMS(DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   359
  int Z, s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   360
  if( p->validHMS ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   361
  computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   362
  Z = p->rJD + 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   363
  s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   364
  p->s = 0.001*s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   365
  s = p->s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   366
  p->s -= s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   367
  p->h = s/3600;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   368
  s -= p->h*3600;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   369
  p->m = s/60;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   370
  p->s += s - p->m*60;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   371
  p->validHMS = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   372
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   373
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   374
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   375
** Compute both YMD and HMS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   376
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   377
static void computeYMD_HMS(DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   378
  computeYMD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   379
  computeHMS(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   380
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   381
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   382
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   383
** Clear the YMD and HMS and the TZ
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   384
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   385
static void clearYMD_HMS_TZ(DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   386
  p->validYMD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   387
  p->validHMS = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   388
  p->validTZ = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   389
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   390
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   391
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   392
** Compute the difference (in days) between localtime and UTC (a.k.a. GMT)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   393
** for the time value p where p is in UTC.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   394
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   395
static double localtimeOffset(DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   396
  DateTime x, y;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   397
  time_t t;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   398
  x = *p;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   399
  computeYMD_HMS(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   400
  if( x.Y<1971 || x.Y>=2038 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   401
    x.Y = 2000;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   402
    x.M = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   403
    x.D = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   404
    x.h = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   405
    x.m = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   406
    x.s = 0.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   407
  } else {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   408
    int s = x.s + 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   409
    x.s = s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   410
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   411
  x.tz = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   412
  x.validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   413
  computeJD(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   414
  t = (x.rJD-2440587.5)*86400.0 + 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   415
#ifdef HAVE_LOCALTIME_R
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   416
  {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   417
    struct tm sLocal;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   418
    localtime_r(&t, &sLocal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   419
    y.Y = sLocal.tm_year + 1900;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   420
    y.M = sLocal.tm_mon + 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   421
    y.D = sLocal.tm_mday;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   422
    y.h = sLocal.tm_hour;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   423
    y.m = sLocal.tm_min;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   424
    y.s = sLocal.tm_sec;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   425
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   426
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   427
  {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   428
    struct tm *pTm;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   429
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   430
    pTm = localtime(&t);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   431
    y.Y = pTm->tm_year + 1900;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   432
    y.M = pTm->tm_mon + 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   433
    y.D = pTm->tm_mday;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   434
    y.h = pTm->tm_hour;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   435
    y.m = pTm->tm_min;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   436
    y.s = pTm->tm_sec;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   437
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   438
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   439
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   440
  y.validYMD = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   441
  y.validHMS = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   442
  y.validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   443
  y.validTZ = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   444
  computeJD(&y);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   445
  return y.rJD - x.rJD;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   446
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   447
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   448
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   449
** Process a modifier to a date-time stamp.  The modifiers are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   450
** as follows:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   451
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   452
**     NNN days
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   453
**     NNN hours
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   454
**     NNN minutes
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   455
**     NNN.NNNN seconds
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   456
**     NNN months
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   457
**     NNN years
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   458
**     start of month
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   459
**     start of year
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   460
**     start of week
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   461
**     start of day
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   462
**     weekday N
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   463
**     unixepoch
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   464
**     localtime
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   465
**     utc
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   466
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   467
** Return 0 on success and 1 if there is any kind of error.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   468
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   469
static int parseModifier(const char *zMod, DateTime *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   470
  int rc = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   471
  int n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   472
  double r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   473
  char *z, zBuf[30];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   474
  z = zBuf;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   475
  for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   476
    z[n] = tolower(zMod[n]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   477
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   478
  z[n] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   479
  switch( z[0] ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   480
    case 'l': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   481
      /*    localtime
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   482
      **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   483
      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   484
      ** show local time.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   485
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   486
      if( strcmp(z, "localtime")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   487
        computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   488
        p->rJD += localtimeOffset(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   489
        clearYMD_HMS_TZ(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   490
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   491
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   492
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   493
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   494
    case 'u': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   495
      /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   496
      **    unixepoch
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   497
      **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   498
      ** Treat the current value of p->rJD as the number of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   499
      ** seconds since 1970.  Convert to a real julian day number.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   500
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   501
      if( strcmp(z, "unixepoch")==0 && p->validJD ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   502
        p->rJD = p->rJD/86400.0 + 2440587.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   503
        clearYMD_HMS_TZ(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   504
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   505
      }else if( strcmp(z, "utc")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   506
        double c1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   507
        computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   508
        c1 = localtimeOffset(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   509
        p->rJD -= c1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   510
        clearYMD_HMS_TZ(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   511
        p->rJD += c1 - localtimeOffset(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   512
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   513
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   514
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   515
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   516
    case 'w': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   517
      /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   518
      **    weekday N
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   519
      **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   520
      ** Move the date to the same time on the next occurrence of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   521
      ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   522
      ** date is already on the appropriate weekday, this is a no-op.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   523
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   524
      if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   525
                 && (n=r)==r && n>=0 && r<7 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   526
        int Z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   527
        computeYMD_HMS(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   528
        p->validTZ = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   529
        p->validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   530
        computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   531
        Z = p->rJD + 1.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   532
        Z %= 7;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   533
        if( Z>n ) Z -= 7;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   534
        p->rJD += n - Z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   535
        clearYMD_HMS_TZ(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   536
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   537
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   538
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   539
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   540
    case 's': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   541
      /*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   542
      **    start of TTTTT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   543
      **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   544
      ** Move the date backwards to the beginning of the current day,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   545
      ** or month or year.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   546
      */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   547
      if( strncmp(z, "start of ", 9)!=0 ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   548
      z += 9;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   549
      computeYMD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   550
      p->validHMS = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   551
      p->h = p->m = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   552
      p->s = 0.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   553
      p->validTZ = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   554
      p->validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   555
      if( strcmp(z,"month")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   556
        p->D = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   557
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   558
      }else if( strcmp(z,"year")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   559
        computeYMD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   560
        p->M = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   561
        p->D = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   562
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   563
      }else if( strcmp(z,"day")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   564
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   565
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   566
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   567
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   568
    case '+':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   569
    case '-':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   570
    case '0':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   571
    case '1':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   572
    case '2':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   573
    case '3':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   574
    case '4':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   575
    case '5':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   576
    case '6':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   577
    case '7':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   578
    case '8':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   579
    case '9': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   580
      n = getValue(z, &r);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   581
      assert( n>=1 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   582
      if( z[n]==':' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   583
        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   584
        ** specified number of hours, minutes, seconds, and fractional seconds
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   585
        ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   586
        ** omitted.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   587
        */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   588
        const char *z2 = z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   589
        DateTime tx;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   590
        int day;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   591
        if( !isdigit(*(u8*)z2) ) z2++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   592
        memset(&tx, 0, sizeof(tx));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   593
        if( parseHhMmSs(z2, &tx) ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   594
        computeJD(&tx);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   595
        tx.rJD -= 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   596
        day = (int)tx.rJD;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   597
        tx.rJD -= day;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   598
        if( z[0]=='-' ) tx.rJD = -tx.rJD;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   599
        computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   600
        clearYMD_HMS_TZ(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   601
        p->rJD += tx.rJD;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   602
        rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   603
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   604
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   605
      z += n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   606
      while( isspace(*(u8*)z) ) z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   607
      n = strlen(z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   608
      if( n>10 || n<3 ) break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   609
      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   610
      computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   611
      rc = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   612
      if( n==3 && strcmp(z,"day")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   613
        p->rJD += r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   614
      }else if( n==4 && strcmp(z,"hour")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   615
        p->rJD += r/24.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   616
      }else if( n==6 && strcmp(z,"minute")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   617
        p->rJD += r/(24.0*60.0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   618
      }else if( n==6 && strcmp(z,"second")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   619
        p->rJD += r/(24.0*60.0*60.0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   620
      }else if( n==5 && strcmp(z,"month")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   621
        int x, y;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   622
        computeYMD_HMS(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   623
        p->M += r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   624
        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   625
        p->Y += x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   626
        p->M -= x*12;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   627
        p->validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   628
        computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   629
        y = r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   630
        if( y!=r ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   631
          p->rJD += (r - y)*30.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   632
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   633
      }else if( n==4 && strcmp(z,"year")==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   634
        computeYMD_HMS(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   635
        p->Y += r;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   636
        p->validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   637
        computeJD(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   638
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   639
        rc = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   640
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   641
      clearYMD_HMS_TZ(p);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   642
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   643
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   644
    default: {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   645
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   646
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   647
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   648
  return rc;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   649
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   650
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   651
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   652
** Process time function arguments.  argv[0] is a date-time stamp.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   653
** argv[1] and following are modifiers.  Parse them all and write
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   654
** the resulting time into the DateTime structure p.  Return 0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   655
** on success and 1 if there are any errors.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   656
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   657
static int isDate(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   658
  sqlite3_context *context, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   659
  int argc, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   660
  sqlite3_value **argv, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   661
  DateTime *p
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   662
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   663
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   664
  const unsigned char *z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   665
  if( argc==0 ) return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   666
  z = sqlite3_value_text(argv[0]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   667
  if( !z || parseDateOrTime(context, (char*)z, p) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   668
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   669
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   670
  for(i=1; i<argc; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   671
    if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   672
      return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   673
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   674
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   675
  return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   676
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   677
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   678
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   679
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   680
** The following routines implement the various date and time functions
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   681
** of SQLite.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   682
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   683
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   684
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   685
**    julianday( TIMESTRING, MOD, MOD, ...)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   686
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   687
** Return the julian day number of the date specified in the arguments
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   688
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   689
static void juliandayFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   690
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   691
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   692
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   693
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   694
  DateTime x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   695
  if( isDate(context, argc, argv, &x)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   696
    computeJD(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   697
    sqlite3_result_double(context, x.rJD);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   698
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   699
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   700
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   701
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   702
**    datetime( TIMESTRING, MOD, MOD, ...)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   703
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   704
** Return YYYY-MM-DD HH:MM:SS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   705
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   706
static void datetimeFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   707
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   708
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   709
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   710
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   711
  DateTime x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   712
  if( isDate(context, argc, argv, &x)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   713
    char zBuf[100];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   714
    computeYMD_HMS(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   715
    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   716
                     x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   717
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   718
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   719
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   720
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   721
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   722
**    time( TIMESTRING, MOD, MOD, ...)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   723
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   724
** Return HH:MM:SS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   725
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   726
static void timeFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   727
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   728
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   729
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   730
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   731
  DateTime x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   732
  if( isDate(context, argc, argv, &x)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   733
    char zBuf[100];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   734
    computeHMS(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   735
    sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   736
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   737
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   738
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   739
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   740
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   741
**    date( TIMESTRING, MOD, MOD, ...)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   742
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   743
** Return YYYY-MM-DD
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   744
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   745
static void dateFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   746
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   747
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   748
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   749
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   750
  DateTime x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   751
  if( isDate(context, argc, argv, &x)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   752
    char zBuf[100];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   753
    computeYMD(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   754
    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   755
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   756
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   757
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   758
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   759
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   760
**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   761
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   762
** Return a string described by FORMAT.  Conversions as follows:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   763
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   764
**   %d  day of month
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   765
**   %f  ** fractional seconds  SS.SSS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   766
**   %H  hour 00-24
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   767
**   %j  day of year 000-366
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   768
**   %J  ** Julian day number
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   769
**   %m  month 01-12
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   770
**   %M  minute 00-59
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   771
**   %s  seconds since 1970-01-01
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   772
**   %S  seconds 00-59
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   773
**   %w  day of week 0-6  sunday==0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   774
**   %W  week of year 00-53
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   775
**   %Y  year 0000-9999
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   776
**   %%  %
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   777
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   778
static void strftimeFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   779
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   780
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   781
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   782
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   783
  DateTime x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   784
  u64 n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   785
  int i, j;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   786
  char *z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   787
  const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   788
  char zBuf[100];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   789
  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   790
  for(i=0, n=1; zFmt[i]; i++, n++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   791
    if( zFmt[i]=='%' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   792
      switch( zFmt[i+1] ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   793
        case 'd':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   794
        case 'H':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   795
        case 'm':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   796
        case 'M':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   797
        case 'S':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   798
        case 'W':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   799
          n++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   800
          /* fall thru */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   801
        case 'w':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   802
        case '%':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   803
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   804
        case 'f':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   805
          n += 8;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   806
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   807
        case 'j':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   808
          n += 3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   809
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   810
        case 'Y':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   811
          n += 8;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   812
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   813
        case 's':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   814
        case 'J':
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   815
          n += 50;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   816
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   817
        default:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   818
          return;  /* ERROR.  return a NULL */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   819
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   820
      i++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   821
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   822
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   823
  if( n<sizeof(zBuf) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   824
    z = zBuf;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   825
  }else if( n>SQLITE_MAX_LENGTH ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   826
    sqlite3_result_error_toobig(context);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   827
    return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   828
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   829
    z = (char*)sqlite3_malloc( n );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   830
    if( z==0 ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   831
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   832
  computeJD(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   833
  computeYMD_HMS(&x);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   834
  for(i=j=0; zFmt[i]; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   835
    if( zFmt[i]!='%' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   836
      z[j++] = zFmt[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   837
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   838
      i++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   839
      switch( zFmt[i] ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   840
        case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   841
        case 'f': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   842
          double s = x.s;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   843
          if( s>59.999 ) s = 59.999;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   844
          sqlite3_snprintf(7, &z[j],"%06.3f", s);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   845
          j += strlen(&z[j]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   846
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   847
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   848
        case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   849
        case 'W': /* Fall thru */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   850
        case 'j': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   851
          int nDay;             /* Number of days since 1st day of year */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   852
          DateTime y = x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   853
          y.validJD = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   854
          y.M = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   855
          y.D = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   856
          computeJD(&y);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   857
          nDay = x.rJD - y.rJD + 0.5;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   858
          if( zFmt[i]=='W' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   859
            int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   860
            wd = ((int)(x.rJD+0.5)) % 7;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   861
            sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   862
            j += 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   863
          }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   864
            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   865
            j += 3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   866
          }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   867
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   868
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   869
        case 'J': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   870
          sqlite3_snprintf(20, &z[j],"%.16g",x.rJD);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   871
          j+=strlen(&z[j]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   872
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   873
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   874
        case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   875
        case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   876
        case 's': {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   877
          sqlite3_snprintf(30,&z[j],"%d",
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   878
                           (int)((x.rJD-2440587.5)*86400.0 + 0.5));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   879
          j += strlen(&z[j]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   880
          break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   881
        }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   882
        case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   883
        case 'w':  z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   884
        case 'Y':  sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   885
        case '%':  z[j++] = '%'; break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   886
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   887
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   888
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   889
  z[j] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   890
  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   891
  if( z!=zBuf ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   892
    sqlite3_free(z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   893
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   894
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   895
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   896
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   897
** current_time()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   898
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   899
** This function returns the same value as time('now').
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   900
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   901
static void ctimeFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   902
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   903
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   904
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   905
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   906
  sqlite3_value *pVal = sqlite3ValueNew(0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   907
  if( pVal ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   908
    sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   909
    timeFunc(context, 1, &pVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   910
    sqlite3ValueFree(pVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   911
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   912
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   913
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   914
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   915
** current_date()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   916
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   917
** This function returns the same value as date('now').
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   918
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   919
static void cdateFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   920
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   921
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   922
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   923
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   924
  sqlite3_value *pVal = sqlite3ValueNew(0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   925
  if( pVal ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   926
    sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   927
    dateFunc(context, 1, &pVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   928
    sqlite3ValueFree(pVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   929
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   930
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   931
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   932
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   933
** current_timestamp()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   934
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   935
** This function returns the same value as datetime('now').
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   936
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   937
static void ctimestampFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   938
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   939
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   940
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   941
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   942
  sqlite3_value *pVal = sqlite3ValueNew(0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   943
  if( pVal ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   944
    sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   945
    datetimeFunc(context, 1, &pVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   946
    sqlite3ValueFree(pVal);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   947
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   948
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   949
#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   950
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   951
#ifdef SQLITE_OMIT_DATETIME_FUNCS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   952
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   953
** If the library is compiled to omit the full-scale date and time
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   954
** handling (to get a smaller binary), the following minimal version
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   955
** of the functions current_time(), current_date() and current_timestamp()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   956
** are included instead. This is to support column declarations that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   957
** include "DEFAULT CURRENT_TIME" etc.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   958
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   959
** This function uses the C-library functions time(), gmtime()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   960
** and strftime(). The format string to pass to strftime() is supplied
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   961
** as the user-data for the function.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   962
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   963
static void currentTimeFunc(
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   964
  sqlite3_context *context,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   965
  int argc,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   966
  sqlite3_value **argv
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   967
){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   968
  time_t t;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   969
  char *zFormat = (char *)sqlite3_user_data(context);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   970
  char zBuf[20];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   971
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   972
  time(&t);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   973
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   974
  {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   975
    extern int sqlite3_current_time;  /* See os_XXX.c */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   976
    if( sqlite3_current_time ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   977
      t = sqlite3_current_time;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   978
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   979
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   980
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   981
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   982
#ifdef HAVE_GMTIME_R
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   983
  {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   984
    struct tm sNow;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   985
    gmtime_r(&t, &sNow);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   986
    strftime(zBuf, 20, zFormat, &sNow);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   987
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   988
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   989
  {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   990
    struct tm *pTm;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   991
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   992
    pTm = gmtime(&t);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   993
    strftime(zBuf, 20, zFormat, pTm);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   994
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   995
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   996
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   997
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   998
  sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   999
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1000
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1001
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1002
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1003
** This function registered all of the above C functions as SQL
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1004
** functions.  This should be the only routine in this file with
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1005
** external linkage.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1006
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1007
void sqlite3RegisterDateTimeFunctions(sqlite3 *db){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1008
#ifndef SQLITE_OMIT_DATETIME_FUNCS
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1009
  static const struct {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1010
     char *zName;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1011
     int nArg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1012
     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1013
  } aFuncs[] = {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1014
    { "julianday", -1, juliandayFunc   },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1015
    { "date",      -1, dateFunc        },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1016
    { "time",      -1, timeFunc        },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1017
    { "datetime",  -1, datetimeFunc    },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1018
    { "strftime",  -1, strftimeFunc    },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1019
    { "current_time",       0, ctimeFunc      },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1020
    { "current_timestamp",  0, ctimestampFunc },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1021
    { "current_date",       0, cdateFunc      },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1022
  };
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1023
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1024
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1025
  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1026
    sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1027
        SQLITE_UTF8, (void *)(db->pVfs), aFuncs[i].xFunc, 0, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1028
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1029
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1030
  static const struct {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1031
     char *zName;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1032
     char *zFormat;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1033
  } aFuncs[] = {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1034
    { "current_time", "%H:%M:%S" },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1035
    { "current_date", "%Y-%m-%d" },
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1036
    { "current_timestamp", "%Y-%m-%d %H:%M:%S" }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1037
  };
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1038
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1039
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1040
  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1041
    sqlite3CreateFunc(db, aFuncs[i].zName, 0, SQLITE_UTF8, 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1042
        aFuncs[i].zFormat, currentTimeFunc, 0, 0);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1043
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1044
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
  1045
}