Skip to content

Commit

Permalink
Allow more ISO 8601 datetime variants
Browse files Browse the repository at this point in the history
This allows to use text format ISO 8601 timestampts without seconds,
with nano-, micro-, milli-seconds and Z or +/-HH:MM time zone offset.
  • Loading branch information
felixbuenemann committed Sep 14, 2017
1 parent 7aed109 commit d6fd07a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/xlsxtream/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class Row
ENCODING = Encoding.find('UTF-8')

NUMBER_PATTERN = /\A-?[0-9]+(\.[0-9]+)?\z/.freeze
DATE_PATTERN = /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze # yyyy-mm-dd
TIME_PATTERN = /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}/.freeze # yyyy-mm-ddThh:mm:ss
# ISO 8601 yyyy-mm-dd
DATE_PATTERN = /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
# ISO 8601 yyyy-mm-ddThh:mm:ss(.s)(Z|+hh:mm|-hh:mm)
TIME_PATTERN = /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:\.[0-9]{1,9})?)?(?:Z|[+-][0-9]{2}:[0-9]{2})?\z/.freeze

DATE_STYLE = 1
TIME_STYLE = 2
Expand Down
18 changes: 16 additions & 2 deletions test/xlsxtream/row_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,24 @@ def test_date_time_column
end

def test_text_date_time_column
row = Row.new(['1900-01-01T12:00:00+00:00'], 1, :auto_format => true)
candidates = [
'1900-01-01T12:00',
'1900-01-01T12:00Z',
'1900-01-01T12:00+00:00',
'1900-01-01T12:00:00+00:00',
'1900-01-01T12:00:00.000+00:00',
'1900-01-01T12:00:00.000000000Z'
]
candidates.each do |timestamp|
row = Row.new([timestamp], 1, :auto_format => true)
actual = row.to_xml
expected = '<row r="1"><c r="A1" s="2"><v>2.5</v></c></row>'
assert_equal expected, actual
end
row = Row.new(['1900-01-01T12'], 1, :auto_format => true)
actual = row.to_xml
expected = '<row r="1"><c r="A1" s="2"><v>2.5</v></c></row>'
assert_equal expected, actual
refute_equal expected, actual
end

def test_time_column
Expand Down

0 comments on commit d6fd07a

Please sign in to comment.