Handle dates from 1/1/100 to 12/31/9999

This commit is contained in:
whydoubt 2004-12-29 03:26:45 +00:00
parent d271b5fae5
commit 58c1aafd2a
2 changed files with 53 additions and 7 deletions

View File

@ -1,3 +1,6 @@
Tue Dec 28 21:25:24 CST 2004 Jeff Smith <whydoubt@yahoo.com>
* src/libmdb/data.c: Handle dates from 1/1/100 to 12/31/9999
Sat Dec 11 00:03:17 CST 2004 Jeff Smith <whydoubt@yahoo.com>
* HACKING:
* include/mdbtools.h:

View File

@ -699,11 +699,17 @@ char *mdb_col_to_string(MdbHandle *mdb, unsigned char *buf, int start, int datat
{
/* FIX ME -- not thread safe */
static char text[MDB_BIND_SIZE];
time_t t;
int i, n;
int n;
float tf;
double td;
struct tm t;
long int day, time;
int yr, q;
int *cal;
int noleap_cal[] = {0,31,59,90,120,150,181,212,243,273,304,334,365};
int leap_cal[] = {0,31,60,91,121,151,182,213,244,274,305,335,366};
switch (datatype) {
case MDB_BOOL:
/* shouldn't happen. bools are handled specially
@ -741,13 +747,50 @@ char *mdb_col_to_string(MdbHandle *mdb, unsigned char *buf, int start, int datat
return text;
break;
case MDB_SDATETIME:
/* Date/Time is stored as a double, where the whole
part is the days from 12/30/1899 and the fractional
part is the fractional part of one day. */
td = mdb_get_double(mdb->pg_buf, start);
if (td > 1) {
t = (long int)((td - 25569.0) * 86400.0);
} else {
t = (long int)(td * 86400.0);
day = (long int)(td);
time = (long int)(fabs(td - day) * 86400.0 + 0.5);
t.tm_hour = time / 3600;
t.tm_min = (time / 60) % 60;
t.tm_sec = time % 60;
t.tm_year = 1 - 1900;
day += 693593; /* Days from 1/1/1 to 12/31/1899 */
t.tm_wday = (day+1) % 7;
q = day / 146097; /* 146097 days in 400 years */
t.tm_year += 400 * q;
day -= q * 146097;
q = day / 36524; /* 36524 days in 100 years */
if (q > 3) q = 3;
t.tm_year += 100 * q;
day -= q * 36524;
q = day / 1461; /* 1461 days in 4 years */
t.tm_year += 4 * q;
day -= q * 1461;
q = day / 365; /* 365 days in 1 year */
if (q > 3) q = 3;
t.tm_year += q;
day -= q * 365;
yr = t.tm_year + 1900;
cal = ((yr)%4==0 && ((yr)%100!=0 || (yr)%400==0)) ?
leap_cal : noleap_cal;
for (t.tm_mon=0; t.tm_mon<12; t.tm_mon++) {
if (day < cal[t.tm_mon+1]) break;
}
strftime(text, MDB_BIND_SIZE, date_fmt, (struct tm*)gmtime(&t));
t.tm_mday = day - cal[t.tm_mon] + 1;
t.tm_yday = day;
t.tm_isdst = -1;
strftime(text, MDB_BIND_SIZE, date_fmt, &t);
return text;
break;