Skip to content

Commit 1f5063c

Browse files
committed
extract dates constants to book class and freeze them
1 parent 4c52f05 commit 1f5063c

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

lib/creek/book.rb

Lines changed: 6 additions & 2 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
@@ -44,13 +48,13 @@ def base_date
4448
# Default to 1900 (minus one day due to excel quirk) but use 1904 if
4549
# it's set in the Workbook's workbookPr
4650
# http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
47-
result = Date.new(1899, 12, 30) # default
51+
result = DATE_1900 # default
4852

4953
doc = @files.file.open "xl/workbook.xml"
5054
xml = Nokogiri::XML::Document.parse doc
5155
xml.css('workbookPr[date1904]').each do |workbookPr|
5256
if workbookPr['date1904'] =~ /true|1/i
53-
result = Date.new(1904, 1, 1)
57+
result = DATE_1904
5458
break
5559
end
5660
end

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 & 1 deletion
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

0 commit comments

Comments
 (0)