SQLite

Check-in [5b68dae320]
Login

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

Overview
Comment:Simplifications to the localtime() interface. Fix the case where localtime_r() is available so that it works. Ticket [bd484a090c8077].
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5b68dae320d0fa3dc433826811e5018a47461de7
User & Date: drh 2011-06-21 14:35:30.890
Context
2011-06-21
15:01
Rework the localtime logic yet again in order to make all branches reachable and to follow GNU standards for HAVE_LOCALTIME_R-type macros. Ticket [bd484a090c8077]. (check-in: 176248095b user: drh tags: trunk)
14:35
Simplifications to the localtime() interface. Fix the case where localtime_r() is available so that it works. Ticket [bd484a090c8077]. (check-in: 5b68dae320 user: drh tags: trunk)
13:46
Change the error message returned when localtime_r() fails to "local time unavailable". Ticket [bd484a090c8077] (check-in: 0e82175fd8 user: dan tags: trunk)
Changes
Unified Diff Show Whitespace Changes Patch
Changes to src/date.c.
408
409
410
411
412
413
414
415
416
417
418

419
420
421
422
423


424
425

426
427
428
429
430
431
432
433
434
435
436

437

438
439



440
441

442
443
444
445
446
447
448
449

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464


465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
static void clearYMD_HMS_TZ(DateTime *p){
  p->validYMD = 0;
  p->validHMS = 0;
  p->validTZ = 0;
}

#ifndef SQLITE_OMIT_LOCALTIME

/*
** The following three functions - osLocaltime_r(), osLocaltime_s() and
** osLocaltime() - are wrappers around system functions localtime_r(),

** localtime_s() and localtime(), respectively.
**
** If the sqlite3GlobalConfig.bLocaltimeFault variable is true when one
** of the following wrappers is called, it returns an error.
*/


#ifndef SQLITE_OMIT_BUILTIN_TEST


#ifdef HAVE_LOCALTIME_R
static struct tm * osLocaltime_r(time_t *t, struct tm *pTm){
  if( sqlite3GlobalConfig.bLocaltimeFault ) return 0;
  return localtime_r(t);
}
#elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
static int osLocaltime_s(struct tm *pTm, time_t *t){
  if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
  return (int)localtime_s(pTm, t);
}
#else

static struct tm * osLocaltime(time_t *t){

  if( sqlite3GlobalConfig.bLocaltimeFault ) return 0;
  return localtime(t);



}
#endif


#else
# define osLocaltime_r(x,y) localtime_r(x,y)
# define osLocaltime_s(x,y) localtime_s(x,y)
# define osLocaltime(x)     localtime(x)
#endif



/*
** Compute the difference (in milliseconds) between localtime and UTC
** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
** return this value and set *pRc to SQLITE_OK. 
**
** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
** is undefined in this case.
*/
static sqlite3_int64 localtimeOffset(
  DateTime *p,                    /* Date at which to calculate offset */
  sqlite3_context *pCtx,          /* Write error here if one occurs */
  int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
){
  DateTime x, y;
  time_t t;


  x = *p;
  computeYMD_HMS(&x);
  if( x.Y<1971 || x.Y>=2038 ){
    x.Y = 2000;
    x.M = 1;
    x.D = 1;
    x.h = 0;
    x.m = 0;
    x.s = 0.0;
  } else {
    int s = (int)(x.s + 0.5);
    x.s = s;
  }
  x.tz = 0;
  x.validJD = 0;
  computeJD(&x);
  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
#ifdef HAVE_LOCALTIME_R
  {
    struct tm sLocal;
    if( 0==osLocaltime_r(&t, &sLocal) ){
      sqlite3_result_error(pCtx, "local time unavailable", -1);
      *pRc = SQLITE_ERROR;
      return 0;
    }
    y.Y = sLocal.tm_year + 1900;
    y.M = sLocal.tm_mon + 1;
    y.D = sLocal.tm_mday;
    y.h = sLocal.tm_hour;
    y.m = sLocal.tm_min;
    y.s = sLocal.tm_sec;
  }
#elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
  {
    struct tm sLocal;
    if( 0!=osLocaltime_s(&sLocal, &t) ){
      sqlite3_result_error(pCtx, "local time unavailable", -1);
      *pRc = SQLITE_ERROR;
      return 0;
    }
    y.Y = sLocal.tm_year + 1900;
    y.M = sLocal.tm_mon + 1;
    y.D = sLocal.tm_mday;
    y.h = sLocal.tm_hour;
    y.m = sLocal.tm_min;
    y.s = sLocal.tm_sec;
  }
#else
  {
    struct tm *pTm;
    sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
    pTm = osLocaltime(&t);
    if( pTm ){
      y.Y = pTm->tm_year + 1900;
      y.M = pTm->tm_mon + 1;
      y.D = pTm->tm_mday;
      y.h = pTm->tm_hour;
      y.m = pTm->tm_min;
      y.s = pTm->tm_sec;
    }
    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
    if( !pTm ){
      sqlite3_result_error(pCtx, "local time unavailable", -1);
      *pRc = SQLITE_ERROR;
      return 0;
    }
  }
#endif
  y.validYMD = 1;
  y.validHMS = 1;
  y.validJD = 0;
  y.validTZ = 0;
  computeJD(&y);
  *pRc = SQLITE_OK;
  return y.iJD - x.iJD;







<

|
|
>
|

|
|

>
>

|
>

<
<
|
<

<
<
|
<

>
|
>
|
|
>
>
>


>
|
<
<
<
<
|


>















>
>

















<
<
<
|










<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







408
409
410
411
412
413
414

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429


430

431


432

433
434
435
436
437
438
439
440
441
442
443
444
445




446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483



484
485
486
487
488
489
490
491
492
493
494





































495
496
497
498
499
500
501
static void clearYMD_HMS_TZ(DateTime *p){
  p->validYMD = 0;
  p->validHMS = 0;
  p->validTZ = 0;
}

#ifndef SQLITE_OMIT_LOCALTIME

/*
** The following routine implements the rough equivalent of localtime_r()
** using whatever operating-system specific localtime facility that
** is available.  This routine returns 0 on success and
** non-zero on any kind of error.
**
** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
** routine will always fail.
*/
int osLocaltime(time_t *t, struct tm *pTm){
  int rc;
#ifndef SQLITE_OMIT_BUILTIN_TEST
  if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
#endif
#ifdef HAVE_LOCALTIME_R


  rc = localtime_r(t, pTm)==0;

#elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S


  rc = localtime_s(pTm, t);

#else
  {
    struct tm *pX;
    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
    sqlite3_mutex_enter(mutex);
    pX = localtime(t);
    if( pX ) *pTm = *pX;
    sqlite3_mutex_leave(mutex);
    rc = pX==0;
}
#endif
  return rc;
}




#endif /* SQLITE_OMIT_LOCALTIME */


#ifndef SQLITE_OMIT_LOCALTIME
/*
** Compute the difference (in milliseconds) between localtime and UTC
** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
** return this value and set *pRc to SQLITE_OK. 
**
** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
** is undefined in this case.
*/
static sqlite3_int64 localtimeOffset(
  DateTime *p,                    /* Date at which to calculate offset */
  sqlite3_context *pCtx,          /* Write error here if one occurs */
  int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
){
  DateTime x, y;
  time_t t;
  struct tm sLocal;

  x = *p;
  computeYMD_HMS(&x);
  if( x.Y<1971 || x.Y>=2038 ){
    x.Y = 2000;
    x.M = 1;
    x.D = 1;
    x.h = 0;
    x.m = 0;
    x.s = 0.0;
  } else {
    int s = (int)(x.s + 0.5);
    x.s = s;
  }
  x.tz = 0;
  x.validJD = 0;
  computeJD(&x);
  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);



  if( osLocaltime(&t, &sLocal) ){
      sqlite3_result_error(pCtx, "local time unavailable", -1);
      *pRc = SQLITE_ERROR;
      return 0;
    }
    y.Y = sLocal.tm_year + 1900;
    y.M = sLocal.tm_mon + 1;
    y.D = sLocal.tm_mday;
    y.h = sLocal.tm_hour;
    y.m = sLocal.tm_min;
    y.s = sLocal.tm_sec;





































  y.validYMD = 1;
  y.validHMS = 1;
  y.validJD = 0;
  y.validTZ = 0;
  computeJD(&y);
  *pRc = SQLITE_OK;
  return y.iJD - x.iJD;