From 0449055b7e20ebbdac8ce8d2535fa91324691aa8 Mon Sep 17 00:00:00 2001 From: Tony Pitale Date: Wed, 11 Mar 2015 16:56:12 -0400 Subject: [PATCH 1/3] Switch default values to UTC, and add sub-second values --- data_objects/lib/data_objects/ext/do_common.c | 20 +++++++++++-------- .../spec/shared/typecast/datetime_spec.rb | 16 ++++++++------- .../spec/shared/typecast/time_spec.rb | 2 +- do_postgres/spec/spec_helper.rb | 6 +++--- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/data_objects/lib/data_objects/ext/do_common.c b/data_objects/lib/data_objects/ext/do_common.c index 04481e2e..30891337 100644 --- a/data_objects/lib/data_objects/ext/do_common.c +++ b/data_objects/lib/data_objects/ext/do_common.c @@ -193,11 +193,11 @@ VALUE data_objects_parse_date(const char *date) { } VALUE data_objects_parse_time(const char *date) { - static char const* const _fmt_datetime = "%4d-%2d-%2d%*c%2d:%2d:%2d%7lf"; - int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, usec = 0; + static char const* const _fmt_datetime = "%4d-%2d-%2d%*c%2d:%2d:%2d%7lf%3d:%2d"; + int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, usec = 0, hour_offset, minute_offset; double subsec = 0; - switch (sscanf(date, _fmt_datetime, &year, &month, &day, &hour, &min, &sec, &subsec)) { + switch (sscanf(date, _fmt_datetime, &year, &month, &day, &hour, &min, &sec, &subsec, &hour_offset, &minute_offset)) { case 0: case EOF: return Qnil; @@ -210,18 +210,20 @@ VALUE data_objects_parse_time(const char *date) { return Qnil; } + // TODO: support the timezone being returned from return rb_funcall(rb_cTime, rb_intern("local"), 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), INT2NUM(usec)); } VALUE data_objects_parse_date_time(const char *date) { - static char const* const _fmt_datetime_tz_normal = "%4d-%2d-%2d%*c%2d:%2d:%2d%3d:%2d"; - static char const* const _fmt_datetime_tz_subsec = "%4d-%2d-%2d%*c%2d:%2d:%2d.%*d%3d:%2d"; + static char const* const _fmt_datetime_tz_normal = "%4d-%2d-%2d%*c%2d:%2d:%2d%7lf%3d:%2d"; + // static char const* const _fmt_datetime_tz_subsec = "%4d-%2d-%2d%*c%2d:%2d:%2d.%*d%3d:%2d"; int tokens_read; const char *fmt_datetime; VALUE offset; - int year, month, day, hour, min, sec, hour_offset, minute_offset; + int year, month, day, hour, min, sec, usec, hour_offset, minute_offset; + double subsec; struct tm timeinfo; time_t target_time; @@ -238,8 +240,10 @@ VALUE data_objects_parse_date_time(const char *date) { * - DateTime [6 tokens, missing 2] * - DateTime with hour, possibly minute TZ offset [7-8 tokens] */ - fmt_datetime = strchr(date, '.') ? _fmt_datetime_tz_subsec : _fmt_datetime_tz_normal; - tokens_read = sscanf(date, fmt_datetime, &year, &month, &day, &hour, &min, &sec, &hour_offset, &minute_offset); + // fmt_datetime = strchr(date, '.') ? _fmt_datetime_tz_subsec : _fmt_datetime_tz_normal; + tokens_read = sscanf(date, _fmt_datetime_tz_normal, &year, &month, &day, &hour, &min, &sec, &subsec, &hour_offset, &minute_offset); + + usec = (int) (subsec * 1000000); if(!year && !month && !day && !hour && !min && !sec) { return Qnil; diff --git a/data_objects/lib/data_objects/spec/shared/typecast/datetime_spec.rb b/data_objects/lib/data_objects/spec/shared/typecast/datetime_spec.rb index 7e6dc818..9edad2c5 100644 --- a/data_objects/lib/data_objects/spec/shared/typecast/datetime_spec.rb +++ b/data_objects/lib/data_objects/spec/shared/typecast/datetime_spec.rb @@ -36,8 +36,9 @@ it 'should return the correct result' do date = @values.first - local_offset = Rational(Time.local(2008, 2, 14).utc_offset, 86400) - date.should == DateTime.civil(2008, 2, 14, 00, 31, 12, local_offset) + # local_offset = Rational(Time.local(2008, 2, 14).utc_offset, 86400) + zone_offset = 0 # UTC is the default for this column + date.should == DateTime.civil(2008, 2, 14, 00, 31, 12, 905000, zone_offset) end end @@ -83,11 +84,11 @@ end it 'should return the correct offset in Feb' do - (@feb_row.first.offset * 86400).to_i.should == Time.local(2008, 2, 14, 0, 31, 12).utc_offset + (@feb_row.first.offset * 86400).to_i.should == Time.utc(2008, 2, 14, 0, 31, 12).utc_offset end it 'should return the correct offset in Jul' do - (@jul_row.first.offset * 86400).to_i.should == Time.local(2008, 7, 14, 0, 31, 12).utc_offset + (@jul_row.first.offset * 86400).to_i.should == Time.utc(2008, 7, 14, 0, 31, 12).utc_offset end end @@ -97,8 +98,9 @@ describe 'writing an DateTime' do before do - local_offset = Rational(Time.local(2008, 2, 14).utc_offset, 86400) - @reader = @connection.create_command("SELECT id FROM widgets WHERE release_datetime = ? ORDER BY id").execute_reader(DateTime.civil(2008, 2, 14, 00, 31, 12, local_offset)) + # local_offset = Rational(Time.local(2008, 2, 14).utc_offset, 86400) + # zone_offset = 0 # UTC zone is the default value for this column + @reader = @connection.create_command("SELECT id FROM widgets WHERE release_datetime = ? ORDER BY id").execute_reader(DateTime.new(2008, 2, 14, 0, 31, 12.905).iso8601(6)) @reader.next! @values = @reader.values end @@ -149,7 +151,7 @@ end it 'should return the correct result' do - @values.first.should == Time.local(2008, 2, 14, 00, 31, 12).send(:to_datetime) + @values.first.should == Time.utc(2008, 2, 14, 00, 31, 12).send(:to_datetime) end end diff --git a/data_objects/lib/data_objects/spec/shared/typecast/time_spec.rb b/data_objects/lib/data_objects/spec/shared/typecast/time_spec.rb index 629e3291..f5290038 100644 --- a/data_objects/lib/data_objects/spec/shared/typecast/time_spec.rb +++ b/data_objects/lib/data_objects/spec/shared/typecast/time_spec.rb @@ -67,7 +67,7 @@ describe 'writing an Time' do before do - @reader = @connection.create_command("SELECT id FROM widgets WHERE release_datetime = ? ORDER BY id").execute_reader(Time.local(2008, 2, 14, 00, 31, 12)) + @reader = @connection.create_command("SELECT id FROM widgets WHERE release_datetime = ? ORDER BY id").execute_reader(Time.utc(2008, 2, 14, 00, 31, 12, 905000)) @reader.next! @values = @reader.values end diff --git a/do_postgres/spec/spec_helper.rb b/do_postgres/spec/spec_helper.rb index 9d94756f..2de44f04 100644 --- a/do_postgres/spec/spec_helper.rb +++ b/do_postgres/spec/spec_helper.rb @@ -100,8 +100,8 @@ def setup_test_environment "cost1" double precision default 10.23, "cost2" decimal(8,2) default 50.23, "release_date" date default '2008-02-14', - "release_datetime" timestamp default '2008-02-14 00:31:12', - "release_timestamp" timestamp with time zone default '2008-02-14 00:31:31', + "release_datetime" timestamp default '2008-02-14 00:31:12.905', + "release_timestamp" timestamp with time zone default '2008-02-14 00:31:31.118+00', PRIMARY KEY ("id") ); EOF @@ -145,7 +145,7 @@ def setup_test_environment EOF conn.create_command(<<-EOF).execute_non_query - update widgets set release_datetime = '2008-07-14 00:31:12' where id = 10 + update widgets set release_datetime = '2008-07-14 00:31:12.905+00' where id = 10 EOF conn.close From 271c6b22eee7ed4a95191af23b124781b6362925 Mon Sep 17 00:00:00 2001 From: Tony Pitale Date: Fri, 13 Mar 2015 17:34:30 -0400 Subject: [PATCH 2/3] update mysql data setup in spec helper --- do_mysql/spec/spec_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/do_mysql/spec/spec_helper.rb b/do_mysql/spec/spec_helper.rb index 8271e976..d9b28621 100644 --- a/do_mysql/spec/spec_helper.rb +++ b/do_mysql/spec/spec_helper.rb @@ -114,8 +114,8 @@ def setup_test_environment `cost1` double default 10.23, `cost2` decimal(8,2) default 50.23, `release_date` date default '2008-02-14', - `release_datetime` datetime default '2008-02-14 00:31:12', - `release_timestamp` timestamp NULL default '2008-02-14 00:31:31', + `release_datetime` datetime default '2008-02-14 00:31:12.905', + `release_timestamp` timestamp NULL default '2008-02-14 00:31:31.118+00', `status` enum('active','out of stock') NOT NULL default 'active', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; @@ -160,7 +160,7 @@ def setup_test_environment EOF conn.create_command(<<-EOF).execute_non_query - update widgets set release_datetime = '2008-07-14 00:31:12' where id = 10 + update widgets set release_datetime = '2008-07-14 00:31:12.905+00' where id = 10 EOF conn.close From 690ac1ff9fb4e179e7d986623f0e58f3489a5c38 Mon Sep 17 00:00:00 2001 From: Tony Pitale Date: Sat, 14 Mar 2015 14:09:29 -0400 Subject: [PATCH 3/3] revert attempts to fix mysql specs --- do_mysql/spec/spec_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/do_mysql/spec/spec_helper.rb b/do_mysql/spec/spec_helper.rb index d9b28621..8271e976 100644 --- a/do_mysql/spec/spec_helper.rb +++ b/do_mysql/spec/spec_helper.rb @@ -114,8 +114,8 @@ def setup_test_environment `cost1` double default 10.23, `cost2` decimal(8,2) default 50.23, `release_date` date default '2008-02-14', - `release_datetime` datetime default '2008-02-14 00:31:12.905', - `release_timestamp` timestamp NULL default '2008-02-14 00:31:31.118+00', + `release_datetime` datetime default '2008-02-14 00:31:12', + `release_timestamp` timestamp NULL default '2008-02-14 00:31:31', `status` enum('active','out of stock') NOT NULL default 'active', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; @@ -160,7 +160,7 @@ def setup_test_environment EOF conn.create_command(<<-EOF).execute_non_query - update widgets set release_datetime = '2008-07-14 00:31:12.905+00' where id = 10 + update widgets set release_datetime = '2008-07-14 00:31:12' where id = 10 EOF conn.close