simple_xlsx_reader 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 678c3b84bff4d9f51cd634adbd662cef1a9b52f2
4
- data.tar.gz: 74e1e4b8c143317c9c035e129ead61b9d5e00bf8
3
+ metadata.gz: 94422da0193805c579ba37c7c3e58b35a996dfbc
4
+ data.tar.gz: a9c5e1f01acc0c60165a13adc1af087743a60935
5
5
  SHA512:
6
- metadata.gz: 9c62b675e776bce168a0897cb40415afd1ddfb5812c25f011590195b4d6b391774ecb326f709aa0500a87c6d22de742e0b7b9952689339e42b28e8ff0452a5c7
7
- data.tar.gz: c641c17501d4947205ba1498c6a4955eab28c960afe961c088b214365add2ed396713ab114dcbd72e28e0536224ea819e589d7a108dd06a6927dcba8c5a3429e
6
+ metadata.gz: 33338f8fcf3c180ea346548061598953842358a21acd6d97bf451c07d8655f179af0cf7b7791f7c9de1a8411578e3623faab178b3cd74893aaf6d040a7abde96
7
+ data.tar.gz: 50035b920f6811eed88c318c17b47bf8823aa1ac4bf114af3bc29174edcf08ebd5d16902177aa6a48b70f8e70a745249bb8494101f9f310f24d5f5d5bbc13f27
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 1.0.1
2
+
3
+ * Add support for the 1904 date system [zilverline]
4
+
1
5
  ### 1.0.0
2
6
 
3
7
  No changes since 1.0.0.pre. Releasing 1.0.0 since the project has seen a
@@ -100,6 +100,9 @@ module SimpleXlsxReader
100
100
  ##
101
101
  # For internal use; translates source xml to Sheet objects.
102
102
  class Mapper < Struct.new(:xml)
103
+ DATE_SYSTEM_1900 = Date.new(1899, 12, 30)
104
+ DATE_SYSTEM_1904 = Date.new(1904, 1, 1)
105
+
103
106
  def load_sheets
104
107
  sheet_toc.each_with_index.map do |(sheet_name, _sheet_number), i|
105
108
  parse_sheet(sheet_name, xml.sheets[i]) # sheet_number is *not* the index into xml.sheets
@@ -149,7 +152,8 @@ module SimpleXlsxReader
149
152
 
150
153
  cell = begin
151
154
  self.class.cast(xvalue && xvalue.text.strip, type, style,
152
- :shared_strings => shared_strings)
155
+ :shared_strings => shared_strings,
156
+ :base_date => base_date)
153
157
  rescue => e
154
158
  if !SimpleXlsxReader.configuration.catch_cell_load_errors
155
159
  error = CellLoadError.new(
@@ -343,10 +347,10 @@ module SimpleXlsxReader
343
347
  # the trickiest. note that all these formats can vary on
344
348
  # whether they actually contain a date, time, or datetime.
345
349
  when :date, :time, :date_time
346
- days_since_1900, fraction_of_24 = value.split('.')
350
+ days_since_date_system_start, fraction_of_24 = value.split('.')
347
351
 
348
352
  # http://stackoverflow.com/questions/10559767/how-to-convert-ms-excel-date-from-float-to-date-format-in-ruby
349
- date = Date.new(1899, 12, 30) + Integer(days_since_1900)
353
+ date = options.fetch(:base_date, DATE_SYSTEM_1900) + Integer(days_since_date_system_start)
350
354
 
351
355
  if fraction_of_24 # there is a time associated
352
356
  fraction_of_24 = "0.#{fraction_of_24}".to_f
@@ -372,6 +376,21 @@ module SimpleXlsxReader
372
376
  end
373
377
  end
374
378
 
379
+ ## Returns the base_date from which to calculate dates.
380
+ # Defaults to 1900 (minus two days due to excel quirk), but use 1904 if
381
+ # it's set in the Workbook's workbookPr.
382
+ # http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
383
+ def base_date
384
+ @base_date ||=
385
+ begin
386
+ return DATE_SYSTEM_1900 if xml.workbook == nil
387
+ xml.workbook.xpath("//workbook/workbookPr[@date1904]").each do |workbookPr|
388
+ return DATE_SYSTEM_1904 if workbookPr["date1904"] =~ /true|1/i
389
+ end
390
+ DATE_SYSTEM_1900
391
+ end
392
+ end
393
+
375
394
  # Map of non-custom numFmtId to casting symbol
376
395
  NumFmtMap = {
377
396
  0 => :string, # General
@@ -1,3 +1,3 @@
1
1
  module SimpleXlsxReader
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
Binary file
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ describe SimpleXlsxReader do
4
+ let(:date1904_file) { File.join(File.dirname(__FILE__), 'date1904.xlsx') }
5
+ let(:subject) { SimpleXlsxReader::Document.new(date1904_file) }
6
+
7
+ it 'supports converting dates with the 1904 date system' do
8
+ subject.to_hash.must_equal({
9
+ "date1904" => [[Date.parse("2014-05-01")]]
10
+ })
11
+ end
12
+
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_xlsx_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Woody Peterson
@@ -82,6 +82,8 @@ files:
82
82
  - lib/simple_xlsx_reader.rb
83
83
  - lib/simple_xlsx_reader/version.rb
84
84
  - simple_xlsx_reader.gemspec
85
+ - test/date1904.xlsx
86
+ - test/date1904_test.rb
85
87
  - test/datetime_test.rb
86
88
  - test/datetimes.xlsx
87
89
  - test/performance_test.rb
@@ -114,6 +116,8 @@ signing_key:
114
116
  specification_version: 4
115
117
  summary: Read xlsx data the Ruby way
116
118
  test_files:
119
+ - test/date1904.xlsx
120
+ - test/date1904_test.rb
117
121
  - test/datetime_test.rb
118
122
  - test/datetimes.xlsx
119
123
  - test/performance_test.rb