Class: SheetsV4::ConvertDatesAndTimes
- Inherits:
-
Object
- Object
- SheetsV4::ConvertDatesAndTimes
- Defined in:
- lib/sheets_v4/convert_dates_and_times.rb
Overview
Convert between Ruby Date and DateTime objects and Google Sheets values
Google Sheets uses decimal values to represent dates and times. These conversion routines allows converting from Ruby Date and DateTime objects and values that Google Sheets recognize.
DateTime objects passed to datetime_to_gs
or date_to_gs
are converted to
the time zone of the spreadsheet given in the initializer. DateTime objects
returned by gs_to_datetime
are always in the spreadsheet's time zone.
Valid time zone names are those listed in one of these two sources:
ActiveSupport::TimeZone.all.map { |tz| tz.tzinfo.name }
ActiveSupport::TimeZone.all.map(&:name)
Instance Attribute Summary collapse
-
#spreadsheet_tz ⇒ ActiveSupport::TimeZone
readonly
The time zone passed into the initializer.
Instance Method Summary collapse
-
#date_to_gs(date) ⇒ Float, String
Convert a Ruby Date object to a Google Sheets date value.
-
#datetime_to_gs(datetime) ⇒ Float, String
Convert a Ruby DateTime object to a Google Sheets date time value.
-
#gs_to_date(gs_date) ⇒ Date?
Convert a Google Sheets date value to a Ruby Date object.
-
#gs_to_datetime(gs_datetime) ⇒ DateTime?
Convert a Google Sheets date time value to a DateTime object.
-
#initialize(spreadsheet_tz) ⇒ ConvertDatesAndTimes
constructor
Initialize the conversion routines for a spreadsheet.
Constructor Details
#initialize(spreadsheet_tz) ⇒ ConvertDatesAndTimes
Initialize the conversion routines for a spreadsheet
60 61 62 63 |
# File 'lib/sheets_v4/convert_dates_and_times.rb', line 60 def initialize(spreadsheet_tz) @spreadsheet_tz = ActiveSupport::TimeZone.new(spreadsheet_tz) raise "Invalid time zone '#{spreadsheet_tz}'" unless @spreadsheet_tz end |
Instance Attribute Details
#spreadsheet_tz ⇒ ActiveSupport::TimeZone (readonly)
The time zone passed into the initializer
48 49 50 |
# File 'lib/sheets_v4/convert_dates_and_times.rb', line 48 def spreadsheet_tz @spreadsheet_tz end |
Instance Method Details
#date_to_gs(date) ⇒ Float, String
Convert a Ruby Date object to a Google Sheets date value
The Google Sheets date value is a float.
133 134 135 136 137 138 139 |
# File 'lib/sheets_v4/convert_dates_and_times.rb', line 133 def date_to_gs(date) return datetime_to_gs(date).to_i if date.is_a?(DateTime) return (date - gs_epoch_start_date).to_i if date.is_a?(Date) '' end |
#datetime_to_gs(datetime) ⇒ Float, String
Convert a Ruby DateTime object to a Google Sheets date time value
78 79 80 81 82 83 |
# File 'lib/sheets_v4/convert_dates_and_times.rb', line 78 def datetime_to_gs(datetime) return '' unless datetime time = datetime.to_time.in_time_zone(spreadsheet_tz) unix_to_gs_epoch(replace_time_zone(time, 'UTC').to_i) end |
#gs_to_date(gs_date) ⇒ Date?
Convert a Google Sheets date value to a Ruby Date object
161 162 163 164 165 166 167 |
# File 'lib/sheets_v4/convert_dates_and_times.rb', line 161 def gs_to_date(gs_date) return nil if gs_date.nil? || gs_date == '' raise 'gs_date is a string' if gs_date.is_a?(String) (gs_epoch_start_date + gs_date.to_i) end |
#gs_to_datetime(gs_datetime) ⇒ DateTime?
Convert a Google Sheets date time value to a DateTime object
Time is rounded to the nearest second. The DateTime object returned is in the spreadsheet's time zone given in the initiaizer.
102 103 104 105 106 107 108 109 110 |
# File 'lib/sheets_v4/convert_dates_and_times.rb', line 102 def gs_to_datetime(gs_datetime) return nil if gs_datetime.nil? || gs_datetime == '' raise 'gs_datetime is a string' if gs_datetime.is_a?(String) unix_epoch_datetime = gs_to_unix_epoch(gs_datetime.to_f) time = Time.at_without_coercion(unix_epoch_datetime, in: 'UTC') replace_time_zone(time, spreadsheet_tz).to_datetime end |