Class: Roo::Excelx::Cell::DateTime
- Defined in:
- lib/roo/excelx/cell/datetime.rb
Constant Summary collapse
- SECONDS_IN_DAY =
60 * 60 * 24
Instance Attribute Summary collapse
-
#cell_value ⇒ Object
readonly
Returns the value of attribute cell_value.
-
#coordinate ⇒ Object
readonly
Returns the value of attribute coordinate.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#formula ⇒ Object
readonly
Returns the value of attribute formula.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from Base
Instance Method Summary collapse
-
#formatted_value ⇒ Object
Public: Returns formatted value for a datetime.
-
#initialize(value, formula, excelx_type, style, link, base_timestamp, coordinate) ⇒ DateTime
constructor
A new instance of DateTime.
Methods inherited from Base
#empty?, #excelx_type, #excelx_value, #formula?, #hyperlink, #link, #link?, #presence, #to_s, #type
Methods included from Helpers::DefaultAttrReader
Constructor Details
#initialize(value, formula, excelx_type, style, link, base_timestamp, coordinate) ⇒ DateTime
Returns a new instance of DateTime.
15 16 17 18 19 |
# File 'lib/roo/excelx/cell/datetime.rb', line 15 def initialize(value, formula, excelx_type, style, link, , coordinate) super(value, formula, excelx_type, style, nil, coordinate) @format = excelx_type.last @value = link ? Roo::Link.new(link, value) : create_datetime(, value) end |
Instance Attribute Details
#cell_value ⇒ Object (readonly)
Returns the value of attribute cell_value.
11 12 13 |
# File 'lib/roo/excelx/cell/datetime.rb', line 11 def cell_value @cell_value end |
#coordinate ⇒ Object (readonly)
Returns the value of attribute coordinate.
11 12 13 |
# File 'lib/roo/excelx/cell/datetime.rb', line 11 def coordinate @coordinate end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
11 12 13 |
# File 'lib/roo/excelx/cell/datetime.rb', line 11 def format @format end |
#formula ⇒ Object (readonly)
Returns the value of attribute formula.
11 12 13 |
# File 'lib/roo/excelx/cell/datetime.rb', line 11 def formula @formula end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
11 12 13 |
# File 'lib/roo/excelx/cell/datetime.rb', line 11 def value @value end |
Instance Method Details
#formatted_value ⇒ Object
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.
39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 |