Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Switch default values to UTC, and add sub-second values #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions data_objects/lib/data_objects/ext/do_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this local call doesn't support setting the timezone AFAIK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we'd need to handle the cases different here then. Use local when there's no UTC offset present, but build an object with the specific UTC offset otherwise.

}

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";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that there was a test for the '.' … or what it was meant to handle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it was actually used. Maybe an attempt to handle this case that never was finished?

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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions do_postgres/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down