Skip to Content.
Sympa Menu

freetds - Date functions

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: JADelinck AT uss.com
  • To: TDS Development Group <freetds AT franklin.oit.unc.edu>
  • Subject: Date functions
  • Date: Tue, 18 Dec 2001 13:13:57 -0600



I find it very convenient to work with "seconds since the epoch" (whatever
the epoch is) in a time_t type. So, I split out the seconds since the epoch
in the dbdatecrack function and also used this value to partially implement
dbdatecmp. (See the /* to do */ for what I think could make it better).

The nice thing about seconds since the epoch is that I can localtime() or
gmtime() it, and I can difftime() it. I can also strftime
(,,myOwnFormat,localtime(&epoch));

I called the function dbdate2epoch. I suppose the function should be in
convert.c and should be called dbdate_convert_to_seconds_since_epoch, or
(minimally) dbdate_to_epoch

Also, (1000/300) is 3 . If 300 things per second is to be converted to
milliseconds, it should be:

(1000*millis)/300 . This is also in the patch.

Here is the patch. Call it what you like. I like it, and I use it all the
time. Maybe many others will also want to make use of it.

diff -r -c old/freetds-0.53/include/sybdb.h freetds-0.53/include/sybdb.h
*** old/freetds-0.53/include/sybdb.h Wed Nov 7 22:50:36 2001
--- freetds-0.53/include/sybdb.h Tue Dec 18 12:50:09 2001
***************
*** 421,427 ****
extern RETCODE dbmny4divide(DBPROCESS *dbproc, DBMONEY4 *m1, DBMONEY4
*m2,
DBMONEY4 *quotient);
extern RETCODE dbmny4cmp(DBPROCESS *dbproc, DBMONEY4 *m1, DBMONEY4
*m2);
! extern RETCODE dbdatecmp(DBPROCESS *dbproc, DBDATETIME *d1,
DBDATETIME *d2);
extern RETCODE dbdatecrack(DBPROCESS *dbproc, DBDATEREC *dateinfo,
DBDATETIME *datetime);
--- 421,428 ----
extern RETCODE dbmny4divide(DBPROCESS *dbproc, DBMONEY4 *m1, DBMONEY4
*m2,
DBMONEY4 *quotient);
extern RETCODE dbmny4cmp(DBPROCESS *dbproc, DBMONEY4 *m1, DBMONEY4
*m2);
! extern time_t dbdate2epoch(DBDATETIME * d1);
! extern int dbdatecmp(DBPROCESS *dbproc, DBDATETIME *d1,
DBDATETIME *d2);
extern RETCODE dbdatecrack(DBPROCESS *dbproc, DBDATEREC *dateinfo,
DBDATETIME *datetime);
diff -r -c old/freetds-0.53/src/dblib/dblib.c
freetds-0.53/src/dblib/dblib.c
*** old/freetds-0.53/src/dblib/dblib.c Thu Nov 22 17:37:16 2001
--- freetds-0.53/src/dblib/dblib.c Tue Dec 18 12:58:02 2001
***************
*** 1655,1675 ****
{
return SUCCEED;
}
! RETCODE dbdatecmp(DBPROCESS *dbproc, DBDATETIME *d1, DBDATETIME *d2)
{
! return SUCCEED;
}
RETCODE dbdatecrack(DBPROCESS *dbproc, DBDATEREC *di, DBDATETIME *dt)
{
struct tm *t;
time_t secs_from_epoch;
int millis;
!
! secs_from_epoch = ((dt->dtdays - 25567)*24*60*60) + (dt->dttime/300);
! /* Mac OS 8/9 uses Jan 1, 1904 as the epoch. (mlilback, 11/7/01) */
! #if TARGET_API_MAC_OS8
! secs_from_epoch += ((365L * 66L) + 17) * 24L * 60L * 60L;
! #endif
millis = dt->dttime%300;
t = (struct tm *) gmtime(&secs_from_epoch);
#ifndef MSDBLIB
--- 1673,1702 ----
{
return SUCCEED;
}
! time_t
! dbdate2epoch(DBDATETIME * dt)
{
! time_t secs_from_epoch;
! secs_from_epoch = ((dt->dtdays - 25567)*24*60*60) + (dt->dttime/300);
! /* Mac OS 8/9 uses Jan 1, 1904 as the epoch. (mlilback, 11/7/01) */
! #if TARGET_API_MAC_OS8
! secs_from_epoch += ((365L * 66L) + 17) * 24L * 60L * 60L;
! #endif
! return secs_from_epoch;
! }
! int dbdatecmp(DBPROCESS *dbproc, DBDATETIME *d1, DBDATETIME *d2)
! {
! /* to do: compare millis for t1 == t2 */
! time_t t1 = dbdate2epoch(d1);
! time_t t2 = dbdate2epoch(d2);
! return cmp(t1,t2);
}
RETCODE dbdatecrack(DBPROCESS *dbproc, DBDATEREC *di, DBDATETIME *dt)
{
struct tm *t;
time_t secs_from_epoch;
int millis;
! secs_from_epoch = dbdate2epoch(dt);
millis = dt->dttime%300;
t = (struct tm *) gmtime(&secs_from_epoch);
#ifndef MSDBLIB
***************
*** 1681,1687 ****
di->datehour = t->tm_hour;
di->dateminute = t->tm_min;
di->datesecond = t->tm_sec;
! di->datemsecond = (1000/300) * millis;
/* dblib manual indicates that tzone is not set by dbdate crack */
/* di->datetzone = 0; */
#else
--- 1708,1714 ----
di->datehour = t->tm_hour;
di->dateminute = t->tm_min;
di->datesecond = t->tm_sec;
! di->datemsecond = (1000*millis)/300;
/* dblib manual indicates that tzone is not set by dbdate crack */
/* di->datetzone = 0; */
#else
***************
*** 1693,1699 ****
di->hour = t->tm_hour;
di->minute = t->tm_min;
di->second = t->tm_sec;
! di->millisecond = (1000/300) * millis;
/* dblib manual indicates that tzone is not set by dbdate crack */
/* di->tzone = 0; */
#endif
--- 1720,1726 ----
di->hour = t->tm_hour;
di->minute = t->tm_min;
di->second = t->tm_sec;
! di->millisecond = (1000*millis)/300;
/* dblib manual indicates that tzone is not set by dbdate crack */
/* di->tzone = 0; */
#endif





  • Date functions, JADelinck, 12/19/2001

Archive powered by MHonArc 2.6.24.

Top of Page