Sha256: 09d7adf2b6cb21113019ac253d614f7c3eccaed1bada36098dfa307d517d3b49

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

require 'date'

module Roo
  class Excelx
    class Cell
      class Time < Roo::Excelx::Cell::DateTime
        attr_reader :value, :formula, :format, :cell_value, :link, :coordinate

        def initialize(value, formula, excelx_type, style, link, base_date, coordinate)
          # NOTE: Pass all arguments to DateTime super class.
          super
          @type = :time
          @format = excelx_type.last
          @datetime = create_datetime(base_date, value)
          @value = link? ? Roo::Link.new(link, value) : (value.to_f * 86_400).to_i
        end

        def formatted_value
          formatter = @format.gsub(/#{TIME_FORMATS.keys.join('|')}/, TIME_FORMATS)
          @datetime.strftime(formatter)
        end

        alias_method :to_s, :formatted_value

        private

        def create_datetime(base_date, value)
          date = base_date + value.to_f.round(6)
          datetime_string = date.strftime('%Y-%m-%d %H:%M:%S.%N')
          t = round_datetime(datetime_string)

          ::DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec)
        end

        def round_datetime(datetime_string)
          /(?<yyyy>\d+)-(?<mm>\d+)-(?<dd>\d+) (?<hh>\d+):(?<mi>\d+):(?<ss>\d+.\d+)/ =~ datetime_string

          ::Time.new(yyyy.to_i, mm.to_i, dd.to_i, hh.to_i, mi.to_i, ss.to_r).round(0)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
roo-2.6.0 lib/roo/excelx/cell/time.rb
roo-2.5.1 lib/roo/excelx/cell/time.rb
roo-2.5.0 lib/roo/excelx/cell/time.rb
roo-2.4.0 lib/roo/excelx/cell/time.rb
roo-2.3.2 lib/roo/excelx/cell/time.rb
roo-2.3.1 lib/roo/excelx/cell/time.rb
roo-2.3.0 lib/roo/excelx/cell/time.rb
roo-2.2.0 lib/roo/excelx/cell/time.rb