SQLite

Check-in [e1155d6a]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add support in the strftime() SQL function for conversion letters %G, %g, %U, and %V.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e1155d6aa4b960ecfd14fa3467f28672af3327699c547f5b9e75da3ac1348ff7
User & Date: drh 2024-01-18 16:50:25
Context
2024-01-18
16:52
Increase the version number to 3.46.0 to begin the next development cycle. (check-in: 1481baf3 user: drh tags: trunk)
16:50
Add support in the strftime() SQL function for conversion letters %G, %g, %U, and %V. (check-in: e1155d6a user: drh tags: trunk)
2024-01-16
16:14
Fix harmless "unused parameter" compiler warning in the new fts3IntegrityMethod implementation. (check-in: bb1fe53a user: drh tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/date.c.

1231
1232
1233
1234
1235
1236
1237
1238
1239






























































1240
1241
1242
1243
1244

1245



1246


1247
1248

1249
1250



1251
1252


1253


1254
1255
1256
1257
1258
1259
1260
1261
      zBuf[0] = '-';
      sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT);
    }else{
      sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT);
    }
  }
}

/*






























































**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
**
** Return a string described by FORMAT.  Conversions as follows:
**
**   %d  day of month

**   %f  ** fractional seconds  SS.SSS



**   %H  hour 00-24


**   %j  day of year 000-366
**   %J  ** julian day number

**   %m  month 01-12
**   %M  minute 00-59



**   %s  seconds since 1970-01-01
**   %S  seconds 00-59


**   %w  day of week 0-6  Sunday==0


**   %W  week of year 00-53
**   %Y  year 0000-9999
**   %%  %
*/
static void strftimeFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|
>

>
>
>

>
>
|

>


>
>
>


>
>
|
>
>
|







1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
      zBuf[0] = '-';
      sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT);
    }else{
      sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT);
    }
  }
}

/*
** Compute the one-based day of the year for the DateTime pDate.
** Jan01 = 1,  Jan02 = 2, ... Dec31 = 356 or 366.
*/
static int dayOfYear(DateTime *pDate){
  DateTime jan01 = *pDate;
  assert( jan01.validYMD );
  assert( jan01.validHMS );
  assert( pDate->validJD );
  jan01.validJD = 0;
  jan01.M = 1;
  jan01.D = 1;
  computeJD(&jan01);
  return (int)((pDate->iJD-jan01.iJD+45300000)/86400000) + 1;
}

/*
** Return the day of the week.  1==Monday, 2=Tues, ..., 7=Sunday.
*/
static int dayOfWeek(DateTime *pDate){
  int w;
  assert( pDate->validJD );
  w = ((pDate->iJD+129600000)/86400000) % 7;
  if( w==0 ) w = 7;
  return w;
}

/*
** Compute the day-of-week (0=Sunday, 1=Monday, ..., 6=Saturday) for
** the last day of the calendar year Y.
*/
static int lastDayOfYear(int Y){
  return (Y + (Y/4) - (Y/100) + (Y/400))%7;
}

/*
** Return the number of ISO weeks in calendar year Y.  The answer is
** either 52 or 53.
*/
static int weeksInYear(int Y){
  if( lastDayOfYear(Y)==4 || lastDayOfYear(Y-1)==3 ){
    return 53;
  }else{
    return 52;
  }
}

/*
** Compute the number days since the start of the ISO-week year for pDate.
** The ISO-week year starts on the first day of the week (always a Monday)
** that contains the first Thursday on or after January 1.
*/
static int isoWeekNumber(DateTime *pDate){
  int wn = (10 + dayOfYear(pDate) - dayOfWeek(pDate))/7;
  if( wn<1 ){
    wn = weeksInYear(pDate->Y-1);
  }else if( wn>weeksInYear(pDate->Y) ){
    wn = 1;
  }
  return wn;
}

/*
**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
**
** Return a string described by FORMAT.  Conversions as follows:
**
**   %d  day of month  01-31
**   %e  day of month  1-31
**   %f  ** fractional seconds  SS.SSS
**   %F  ISO date.  YYYY-MM-DD
**   %G  ISO year corresponding to %V 0000-9999.
**   %g  2-digit ISO year corresponding to %V 00-99
**   %H  hour 00-24
**   %k  hour  0-24  (leading zero converted to space)
**   %I  hour 01-12
**   %j  day of year 001-366
**   %J  ** julian day number
**   %l  hour  1-12  (leading zero converted to space)
**   %m  month 01-12
**   %M  minute 00-59
**   %p  "am" or "pm"
**   %P  "AM" or "PM"
**   %R  time as HH:MM
**   %s  seconds since 1970-01-01
**   %S  seconds 00-59
**   %T  time as HH:MM:SS
**   %u  day of week 1-7  Monday==1, Sunday==7
**   %w  day of week 0-6  Sunday==0, Monday==1
**   %U  week of year 00-53  (First Sunday is start of week 01)
**   %V  week of year 01-53  (First week containing Thursday is week 01)
**   %W  week of year 00-53  (First Monday is start of week 01)
**   %Y  year 0000-9999
**   %%  %
*/
static void strftimeFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299















1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
    cf = zFmt[i];
    switch( cf ){
      case 'd':  /* Fall thru */
      case 'e': {
        sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D);
        break;
      }
      case 'f': {
        double s = x.s;
        if( s>59.999 ) s = 59.999;
        sqlite3_str_appendf(&sRes, "%06.3f", s);
        break;
      }
      case 'F': {
        sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D);
        break;















      }
      case 'H':
      case 'k': {
        sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h);
        break;
      }
      case 'I': /* Fall thru */
      case 'l': {
        int h = x.h;
        if( h>12 ) h -= 12;
        if( h==0 ) h = 12;
        sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h);
        break;
      }
      case 'W': /* Fall thru */
      case 'j': {
        int nDay;             /* Number of days since 1st day of year */
        DateTime y = x;
        y.validJD = 0;
        y.M = 1;
        y.D = 1;
        computeJD(&y);
        nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
        if( cf=='W' ){
          int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
          wd = (int)(((x.iJD+43200000)/86400000)%7);
          sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
        }else{
          sqlite3_str_appendf(&sRes,"%03d",nDay+1);
        }
        break;
      }
      case 'J': {
        sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0);
        break;
      }
      case 'm': {
        sqlite3_str_appendf(&sRes,"%02d",x.M);
        break;
      }







|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>














<
|
<
|
<
<
<
<
<
<
<
<
|
<
<
<


|







1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404

1405

1406








1407



1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
    cf = zFmt[i];
    switch( cf ){
      case 'd':  /* Fall thru */
      case 'e': {
        sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D);
        break;
      }
      case 'f': {  /* Fractional seconds.  (Non-standard) */
        double s = x.s;
        if( s>59.999 ) s = 59.999;
        sqlite3_str_appendf(&sRes, "%06.3f", s);
        break;
      }
      case 'F': {
        sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D);
        break;
      }
      case 'G': /* Fall thru */
      case 'g': {
        int Y = x.Y;
        if( x.M==12 && isoWeekNumber(&x)==1 ){
          Y++;
        }else if( x.M==1 && isoWeekNumber(&x)>=52 ){
          Y--;
        }
        if( cf=='g' ){
          sqlite3_str_appendf(&sRes, "%02d", Y%100);
        }else{
          sqlite3_str_appendf(&sRes, "%04d", Y);
        }
        break;
      }
      case 'H':
      case 'k': {
        sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h);
        break;
      }
      case 'I': /* Fall thru */
      case 'l': {
        int h = x.h;
        if( h>12 ) h -= 12;
        if( h==0 ) h = 12;
        sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h);
        break;
      }

      case 'j': {  /* Day of year.  Jan01==1, Jan02==2, and so forth */

        int nDay = dayOfYear(&x);








        sqlite3_str_appendf(&sRes,"%03d",nDay);



        break;
      }
      case 'J': {  /* Julian day number.  (Non-standard) */
        sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0);
        break;
      }
      case 'm': {
        sqlite3_str_appendf(&sRes,"%02d",x.M);
        break;
      }
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380




















1381
1382
1383
1384
1385
1386
1387
        sqlite3_str_appendf(&sRes,"%02d",(int)x.s);
        break;
      }
      case 'T': {
        sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s);
        break;
      }
      case 'u': /* Fall thru */
      case 'w': {
        char c = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
        if( c=='0' && cf=='u' ) c = '7';
        sqlite3_str_appendchar(&sRes, 1, c);
        break;




















      }
      case 'Y': {
        sqlite3_str_appendf(&sRes,"%04d",x.Y);
        break;
      }
      case '%': {
        sqlite3_str_appendchar(&sRes, 1, '%');







|
|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
        sqlite3_str_appendf(&sRes,"%02d",(int)x.s);
        break;
      }
      case 'T': {
        sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s);
        break;
      }
      case 'u':    /* Day of week.  1 to 7.  Monday==1, Sunday==7 */
      case 'w': {  /* Day of week.  0 to 6.  Sunday==0, Monday==1 */
        char c = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
        if( c=='0' && cf=='u' ) c = '7';
        sqlite3_str_appendchar(&sRes, 1, c);
        break;
      }
      case 'U': {  /* Week num. 00-53. First Sun of the year is week 01 */
        int wd;    /* 0=Sunday, 1=Monday, 2=Tuesday, ... 7=Saturday */
        int nDay;  /* Day of the year.  0..364 or 0..365 for leapyears */
        nDay = dayOfYear(&x);
        wd = (int)(((x.iJD+43200000)/86400000 + 1)%7);
        sqlite3_str_appendf(&sRes,"%02d",(nDay+6-wd)/7);
        break;
      }
      case 'V': {  /* Week num. 01-53. First week with a Thur is week 01 */
        sqlite3_str_appendf(&sRes,"%02d", isoWeekNumber(&x));
        break;
      }
      case 'W': {  /* Week num. 00-53. First Mon of the year is week 01 */
        int wd;    /* 0=Monday, 1=Tuesday, ... 6=Sunday */
        int nDay;  /* Day of the year.  0..364 or 0..365 for leapyears */
        nDay = dayOfYear(&x);
        wd = (int)(((x.iJD+43200000)/86400000)%7);
        sqlite3_str_appendf(&sRes,"%02d",(nDay+6-wd)/7);
        break;
      }
      case 'Y': {
        sqlite3_str_appendf(&sRes,"%04d",x.Y);
        break;
      }
      case '%': {
        sqlite3_str_appendchar(&sRes, 1, '%');

Changes to test/date.test.

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  }
  return $x
}
datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
    [repeat 200 abc10123]

foreach c {a b c g h i n o q r t v x y z
           A B C D E G K L N O Q V Z
           0 1 2 3 4 5 6 6 7 9 _} {
  datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL
}
datetest 3.20 {strftime('%e','2023-08-09')} { 9}
datetest 3.21 {strftime('%F %T','2023-08-09 01:23')} {2023-08-09 01:23:00}
datetest 3.22 {strftime('%k','2023-08-09 04:59:59')} { 4}
datetest 3.23 {strftime('%I%P','2023-08-09 11:59:59')} {11am}







|
|







205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  }
  return $x
}
datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
    [repeat 200 abc10123]

foreach c {a b c h i n o q r t v x y z
           A B C D E K L N O Q Z
           0 1 2 3 4 5 6 6 7 9 _} {
  datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL
}
datetest 3.20 {strftime('%e','2023-08-09')} { 9}
datetest 3.21 {strftime('%F %T','2023-08-09 01:23')} {2023-08-09 01:23:00}
datetest 3.22 {strftime('%k','2023-08-09 04:59:59')} { 4}
datetest 3.23 {strftime('%I%P','2023-08-09 11:59:59')} {11am}

Changes to test/date4.test.

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#
ifcapable {!datetime} {
  finish_test
  return
}

if {$tcl_platform(os)=="Linux"} {
  set FMT {%d,%e,%F,%H,%k,%I,%l,%j,%m,%M,%u,%w,%W,%Y,%%,%P,%p}
} else {
  set FMT {%d,%e,%F,%H,%I,%j,%p,%R,%u,%w,%W,%%}
}
for {set i 0} {$i<=24854} {incr i} {
  set TS [expr {$i*86401}]
  do_execsql_test date4-$i {
    SELECT strftime($::FMT,$::TS,'unixepoch');







|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#
ifcapable {!datetime} {
  finish_test
  return
}

if {$tcl_platform(os)=="Linux"} {
  set FMT {%d,%e,%F,%H,%k,%I,%l,%j,%m,%M,%u,%w,%W,%Y,%%,%P,%p,%U,%V,%G,%g}
} else {
  set FMT {%d,%e,%F,%H,%I,%j,%p,%R,%u,%w,%W,%%}
}
for {set i 0} {$i<=24854} {incr i} {
  set TS [expr {$i*86401}]
  do_execsql_test date4-$i {
    SELECT strftime($::FMT,$::TS,'unixepoch');