Sha256: b7a578f9af379391f35bbe4aeed7ab1028594d0c109b95ed71facb9892a3c69d

Contents?: true

Size: 1.08 KB

Versions: 14

Compression:

Stored size: 1.08 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)
      (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 # Time.utc(1899, 12, 30).to_i
      epoch1904 = -2082844800 # Time.utc(1904, 1, 1).to_i
      seconds_per_day = 86400 # 60*60*24
      epoch = Axlsx::Workbook::date1904 ? epoch1904 : epoch1900
      (time.to_f - epoch)/seconds_per_day
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
axlsx-1.2.3 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.2.2 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.2.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.2.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.8 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.7 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.6 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.5 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.4 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.3 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.2 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.1 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.1.0 lib/axlsx/workbook/worksheet/date_time_converter.rb
axlsx-1.0.18 lib/axlsx/workbook/worksheet/date_time_converter.rb