class Roo::Excelx::Cell::DateTime
Constants
- DATE_FORMATS
- SECONDS_IN_DAY
- TIME_FORMATS
Attributes
cell_value[R]
coordinate[R]
format[R]
formula[R]
value[R]
Public Class Methods
new(value, formula, excelx_type, style, link, base_timestamp, coordinate)
click to toggle source
Calls superclass method
Roo::Excelx::Cell::Base::new
# File lib/roo/excelx/cell/datetime.rb, line 15 def initialize(value, formula, excelx_type, style, link, base_timestamp, coordinate) super(value, formula, excelx_type, style, nil, coordinate) @format = excelx_type.last @value = link ? Roo::Link.new(link, value) : create_datetime(base_timestamp, value) end
Public Instance Methods
formatted_value()
click to toggle source
Public: Returns formatted value for a datetime. Format’s can be an
standard excel format, or a custom format. Standard formats follow certain conventions. Date fields for days, months, and years are separated with hyhens or slashes ("-", /") (e.g. 01-JAN, 1/13/15). Time fields for hours, minutes, and seconds are separated with a colon (e.g. 12:45:01). If a custom format follows those conventions, then the custom format will be used for the a cell's formatted value. Otherwise, the formatted value will be in the following format: 'YYYY-mm-dd HH:MM:SS' (e.g. "2015-07-10 20:33:15").
Examples
formatted_value #=> '01-JAN'
Returns a String
representation of a cell’s value.
# File lib/roo/excelx/cell/datetime.rb, line 39 def formatted_value formatter = @format.downcase.split(' ').map do |part| if (parsed_format = parse_date_or_time_format(part)) parsed_format else warn 'Unable to parse custom format. Using "YYYY-mm-dd HH:MM:SS" format.' return @value.strftime('%F %T') end end.join(' ') @value.strftime(formatter) end
Private Instance Methods
create_datetime(base_timestamp, value)
click to toggle source
# File lib/roo/excelx/cell/datetime.rb, line 100 def create_datetime(base_timestamp, value) timestamp = (base_timestamp + (value.to_f.round(6) * SECONDS_IN_DAY)).round(0) ::Time.at(timestamp).utc.to_datetime end
parse_date_or_time_format(part)
click to toggle source
# File lib/roo/excelx/cell/datetime.rb, line 54 def parse_date_or_time_format(part) date_regex = /(?<date>[dmy]+[\-\/][dmy]+([\-\/][dmy]+)?)/ time_regex = /(?<time>(\[?[h]\]?+:)?[m]+(:?ss|:?s)?)/ if part[date_regex] == part formats = DATE_FORMATS elsif part[time_regex] formats = TIME_FORMATS else return false end part.gsub(/#{formats.keys.join('|')}/, formats) end