Skip to content

Commit

Permalink
Merge pull request #168 from p-eye/fix_string_to_time_converter
Browse files Browse the repository at this point in the history
add TIME type conversion to string converter
  • Loading branch information
hiroyuki-sato authored Aug 27, 2024
2 parents ee1b21d + 3c0744f commit e9b1c03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/embulk/output/bigquery/value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def string_converter
val # Users must care of BQ timestamp format
}
end
when 'TIME'
# TimeWithZone doesn't affect any change to the time value
Proc.new {|val|
next nil if val.nil?
with_typecast_error(val) do |val|
TimeWithZone.set_zone_offset(Time.parse(val), zone_offset).strftime("%H:%M:%S.%6N")
end
}
when 'RECORD'
Proc.new {|val|
next nil if val.nil?
Expand Down Expand Up @@ -271,6 +279,11 @@ def timestamp_converter
next nil if val.nil?
val.localtime(zone_offset).strftime("%Y-%m-%d %H:%M:%S.%6N")
}
when 'TIME'
Proc.new {|val|
next nil if val.nil?
val.localtime(zone_offset).strftime("%H:%M:%S.%6N")
}
else
raise NotSupportedType, "cannot take column type #{type} for timestamp column"
end
Expand Down
35 changes: 35 additions & 0 deletions test/test_value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ def test_datetime
assert_equal "2016-02-26 00:00:00", converter.call("2016-02-26 00:00:00")
end

def test_time
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
assert_equal "00:03:22.000000", converter.call("00:03:22")
assert_equal "15:22:00.000000", converter.call("3:22 PM")
assert_equal "03:22:00.000000", converter.call("3:22 AM")
assert_equal "00:00:00.000000", converter.call("2016-02-26 00:00:00")

# TimeWithZone doesn't affect any change to the time value
converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'TIME', timezone: 'Asia/Tokyo'
).create_converter
assert_equal "15:00:01.000000", converter.call("15:00:01")

assert_raise { converter.call('foo') }
end

def test_record
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'RECORD').create_converter
assert_equal({'foo'=>'foo'}, converter.call(%Q[{"foo":"foo"}]))
Expand Down Expand Up @@ -350,6 +367,24 @@ def test_datetime
assert_raise { converter.call('foo') }
end

def test_time
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
timestamp = Time.parse("2016-02-26 00:00:00.500000 +00:00")
expected = "00:00:00.500000"
assert_equal expected, converter.call(timestamp)

converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'TIME', timezone: 'Asia/Tokyo'
).create_converter
assert_equal nil, converter.call(nil)
timestamp = Time.parse("2016-02-25 15:00:00.500000 +00:00")
expected = "00:00:00.500000"
assert_equal expected, converter.call(timestamp)

assert_raise { converter.call('foo') }
end

def test_record
assert_raise { ValueConverterFactory.new(SCHEMA_TYPE, 'RECORD').create_converter }
end
Expand Down

0 comments on commit e9b1c03

Please sign in to comment.