Skip to content

coordinate time format behavior for date convert in STRING #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 13 additions & 6 deletions lib/embulk/output/bigquery/value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,19 @@ def string_converter
}
end
when 'DATE'
Proc.new {|val|
next nil if val.nil?
with_typecast_error(val) do |val|
TimeWithZone.set_zone_offset(Time.parse(val), zone_offset).strftime("%Y-%m-%d")
end
}
if @timestamp_format
Proc.new {|val|
next nil if val.nil?
with_typecast_error(val) do |val|
TimeWithZone.set_zone_offset(Time.parse(val), zone_offset).strftime("%Y-%m-%d")
end
}
Copy link
Member

@sonots sonots Jun 10, 2021

Choose a reason for hiding this comment

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

Using @timestamp_format as a flag looks weird because it is not required to parse val here. TIMESTAMP and DATETIME uses @timestamp_format, but DATE does not.

Time.strptime(val, @timestamp_format).strftime("%Y-%m-%d %H:%M:%S.%6N")

else
Proc.new {|val|
next nil if val.nil?
val # Users must care of BQ timestamp format
}
end
when 'DATETIME'
if @timestamp_format
Proc.new {|val|
Expand Down
10 changes: 8 additions & 2 deletions test/test_value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,17 @@ def test_timestamp
end

def test_date
converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'DATE',
timestamp_format: '%Y/%m/%d'
).create_converter
assert_equal nil, converter.call(nil)
assert_equal "2016-02-26", converter.call("2016/02/26")

# Users must care of BQ date format by themselves with no timestamp_format
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'DATE').create_converter
assert_equal nil, converter.call(nil)
assert_equal "2016-02-26", converter.call("2016-02-26")
assert_equal "2016-02-26", converter.call("2016-02-26 00:00:00")
assert_raise { converter.call('foo') }
end

def test_datetime
Expand Down