Sha256: dc5b504a1b1fc8ef790d0d0e1a96a6535b59640bea3a2a58ae9a7f2e51fabb2b

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 KB

Contents

# encoding: UTF-8
require "date"

module Axlsx
  # The DateTimeConverter class converts both data and time types to their apprpriate excel serializations
  class DateTimeConverter

    # The date_to_serial method converts Date objects to the equivelant excel serialized forms
    # @param [Date] date the date to be serialized
    # @return [Numeric]
    def self.date_to_serial(date)
      epoch = Axlsx::Workbook::date1904 ? Date.new(1904) : Date.new(1899, 12, 30)
      offset_date = date.respond_to?(:utc_offset) ? date + date.utc_offset.seconds : date
      (offset_date - epoch).to_f
    end

    # The time_to_serial methond converts a Time object its excel serialized form.
    # @param [Time] time the time to be serialized
    # @return [Numeric]
    def self.time_to_serial(time)
      # Using hardcoded offsets here as some operating systems will not except
      # a 'negative' offset from the ruby epoch.
      epoch1900 = -2209161600.0 # Time.utc(1899, 12, 30).to_i
      epoch1904 = -2082844800.0 # Time.utc(1904, 1, 1).to_i
      seconds_per_day = 86400.0 # 60*60*24
      epoch = Axlsx::Workbook::date1904 ? epoch1904 : epoch1900
      (time.utc_offset + time.to_f - epoch)/seconds_per_day
    end
  end
end

Version data entries

20 entries across 20 versions & 6 rubygems

Version Path
caxlsx-3.3.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.1.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.1.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
bonio-axlsx-2.2.3 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.0.4 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.0.3 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.0.2 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-2.0.2 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.0.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
caxlsx-3.0.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-alt-3.0.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-alt-3.0.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-3.0.0.pre lib/axlsx/workbook/worksheet/date_time_converter.rb
bonio-axlsx-2.2.2 lib/axlsx/workbook/worksheet/date_time_converter.rb
bonio-axlsx-2.2.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
dg-axlsx-2.1.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-2.1.0.pre lib/axlsx/workbook/worksheet/date_time_converter.rb
l_axlsx-2.0.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-2.0.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-2.0.0 lib/axlsx/workbook/worksheet/date_time_converter.rb