ext/result.cc in swift-0.5.1 vs ext/result.cc in swift-0.6.0

- old
+ new

@@ -1,11 +1,12 @@ #include "result.h" -VALUE cSwiftResult; +VALUE cBigDecimal; +VALUE cDate; VALUE cDateTime; VALUE cStringIO; -VALUE cBigDecimal; +VALUE cSwiftResult; VALUE fNew, fNewBang; uint64_t epoch_ajd_n, epoch_ajd_d; VALUE daysecs, sg; @@ -89,10 +90,11 @@ // Calculates local offset at a given time, including dst. int64_t client_tzoffset(int64_t local, int isdst) { struct tm tm; gmtime_r((const time_t*)&local, &tm); + // TODO: This won't work in Lord Howe Island, Australia which uses half hour shift. return (int64_t)(local + (isdst ? 3600 : 0) - mktime(&tm)); } // pinched from do_postgres static void reduce(uint64_t *numerator, uint64_t *denominator) { @@ -104,11 +106,11 @@ } *numerator = *numerator / b; *denominator = *denominator / b; } -VALUE typecast_datetime(const char *data, uint64_t len) { +VALUE typecast_timestamp(VALUE klass, const char *data, uint64_t len) { struct tm tm; int64_t epoch, adjust, offset; double usec = 0; char tzsign = 0; @@ -139,36 +141,41 @@ ? (time_t)tzhour * 3600 + (time_t)tzmin * 60 : (time_t)tzhour * -3600 + (time_t)tzmin * -60; } // 32bit platforms are for weenies - uint64_t ajd_n = (epoch + adjust - offset), ajd_d = 86400L; + uint64_t ajd_n = (epoch + adjust - offset)*1000000 + usec*1000000, ajd_d = DAYMICROSECS; reduce(&ajd_n, &ajd_d); ajd_n = epoch_ajd_n*ajd_d + ajd_n*epoch_ajd_d; ajd_d = epoch_ajd_d*ajd_d; reduce(&ajd_n, &ajd_d); VALUE ajd = rb_rational_new(SIZET2NUM(ajd_n), SIZET2NUM(ajd_d)); - return rb_funcall(cDateTime, fNewBang, 3, ajd, rb_rational_new(INT2FIX(adjust), daysecs), sg); + return rb_funcall(klass, fNewBang, 3, ajd, rb_rational_new(INT2FIX(adjust), daysecs), sg); } // TODO: throw a warning ? return rb_str_new(data, len); } +#define typecast_datetime(data,len) typecast_timestamp(cDateTime, data, len) +#define typecast_date(data,len) typecast_timestamp(cDate, data, len) + VALUE typecast_field(int type, const char *data, uint64_t length) { switch(type) { case DBI_TYPE_BOOLEAN: - return strcmp(data, "t") == 0 || strcmp(data, "1") == 0 ? Qtrue : Qfalse; + return (data && (data[0] =='t' || data[0] == '1')) ? Qtrue : Qfalse; case DBI_TYPE_INT: return rb_cstr2inum(data, 10); case DBI_TYPE_BLOB: return rb_funcall(cStringIO, fNew, 1, rb_str_new(data, length)); case DBI_TYPE_TEXT: return rb_enc_str_new(data, length, rb_utf8_encoding()); case DBI_TYPE_TIME: return typecast_datetime(data, length); + case DBI_TYPE_DATE: + return typecast_date(data, length); case DBI_TYPE_NUMERIC: return rb_funcall(cBigDecimal, fNew, 1, rb_str_new2(data)); case DBI_TYPE_FLOAT: return rb_float_new(atof(data)); } @@ -217,9 +224,10 @@ rb_require("date"); VALUE mSwift = rb_define_module("Swift"); cSwiftResult = rb_define_class_under(mSwift, "Result", rb_cObject); cDateTime = CONST_GET(rb_mKernel, "DateTime"); + cDate = CONST_GET(rb_mKernel, "Date"); cStringIO = CONST_GET(rb_mKernel, "StringIO"); cBigDecimal = CONST_GET(rb_mKernel, "BigDecimal"); fNew = rb_intern("new"); fNewBang = rb_intern("new!");