diff --git a/lib/embulk/output/bigquery/value_converter_factory.rb b/lib/embulk/output/bigquery/value_converter_factory.rb index ac0bae3..fda53e5 100644 --- a/lib/embulk/output/bigquery/value_converter_factory.rb +++ b/lib/embulk/output/bigquery/value_converter_factory.rb @@ -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? @@ -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 diff --git a/test/test_value_converter_factory.rb b/test/test_value_converter_factory.rb index fcfa116..89c31c1 100644 --- a/test/test_value_converter_factory.rb +++ b/test/test_value_converter_factory.rb @@ -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"}])) @@ -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