Skip to content

Commit 26e3cdc

Browse files
Merge pull request #27 from kamilhism/fix-dates-convert-issues
Get date attribute from workbookPr and use it during dates conversion
2 parents e33e6c0 + 1f5063c commit 26e3cdc

4 files changed

Lines changed: 30 additions & 22 deletions

File tree

lib/creek/book.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'zip/filesystem'
22
require 'nokogiri'
3+
require 'date'
34

45
module Creek
56

@@ -9,6 +10,9 @@ class Creek::Book
910
:sheets,
1011
:shared_strings
1112

13+
DATE_1900 = Date.new(1899, 12, 30).freeze
14+
DATE_1904 = Date.new(1904, 1, 1).freeze
15+
1216
def initialize path, options = {}
1317
check_file_extension = options.fetch(:check_file_extension, true)
1418
if check_file_extension
@@ -37,5 +41,26 @@ def style_types
3741
def close
3842
@files.close
3943
end
44+
45+
def base_date
46+
@base_date ||=
47+
begin
48+
# Default to 1900 (minus one day due to excel quirk) but use 1904 if
49+
# it's set in the Workbook's workbookPr
50+
# http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
51+
result = DATE_1900 # default
52+
53+
doc = @files.file.open "xl/workbook.xml"
54+
xml = Nokogiri::XML::Document.parse doc
55+
xml.css('workbookPr[date1904]').each do |workbookPr|
56+
if workbookPr['date1904'] =~ /true|1/i
57+
result = DATE_1904
58+
break
59+
end
60+
end
61+
62+
result
63+
end
64+
end
4065
end
4166
end

lib/creek/sheet.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def convert(value, type, style_idx)
8585
end
8686

8787
def converter_options
88-
@converter_options ||= {shared_strings: @book.shared_strings.dictionary}
88+
@converter_options ||= {
89+
shared_strings: @book.shared_strings.dictionary,
90+
base_date: @book.base_date
91+
}
8992
end
9093

9194
##

lib/creek/styles/constants.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'date'
2-
31
module Creek
42
class Styles
53
module Constants
@@ -38,9 +36,6 @@ module Constants
3836
48 => :bignum, # ##0.0E+0
3937
49 => :unsupported # @
4038
}
41-
42-
DATE_SYSTEM_1900 = Date.new(1899, 12, 30)
43-
DATE_SYSTEM_1904 = Date.new(1904, 1, 1)
4439
end
4540
end
4641
end

lib/creek/styles/converter.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def self.convert_date(value, options)
8181
fraction_of_24 = value - days_since_date_system_start
8282

8383
# http://stackoverflow.com/questions/10559767/how-to-convert-ms-excel-date-from-float-to-date-format-in-ruby
84-
date = options.fetch(:base_date, DATE_SYSTEM_1900) + days_since_date_system_start
84+
date = options.fetch(:base_date, Date.new(1899, 12, 30)) + days_since_date_system_start
8585

8686
if fraction_of_24 > 0 # there is a time associated
8787
seconds = (fraction_of_24 * 86400).round
@@ -98,21 +98,6 @@ def self.convert_bignum(value)
9898
value.to_f
9999
end
100100
end
101-
102-
## Returns the base_date from which to calculate dates.
103-
# Defaults to 1900 (minus two days due to excel quirk), but use 1904 if
104-
# it's set in the Workbook's workbookPr.
105-
# http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
106-
def base_date
107-
@base_date ||= begin
108-
# return DATE_SYSTEM_1900 if xml.workbook == nil
109-
# xml.workbook.xpath("//workbook/workbookPr[@date1904]").each do |workbookPr|
110-
# return DATE_SYSTEM_1904 if workbookPr["date1904"] =~ /true|1/i
111-
# end
112-
DATE_SYSTEM_1900
113-
end
114-
end
115-
116101
end
117102
end
118103
end

0 commit comments

Comments
 (0)