module Sequel::NamedTimezones
Attributes
Handles TZInfo::AmbiguousTime exceptions automatically by providing a proc called with both the datetime value being converted as well as the array of TZInfo::TimezonePeriod results. Example:
Sequel.tzinfo_disambiguator = proc{|datetime, periods| periods.first}
Private Instance Methods
Assume the given DateTime has a correct time but a wrong timezone. It is currently in UTC timezone, but it should be converted to the input_timezone. Keep the time the same but convert the timezone to the input_timezone. Expects the input_timezone to be a TZInfo::Timezone instance.
# File lib/sequel/extensions/named_timezones.rb 70 def convert_input_datetime_other(v, input_timezone) 71 local_offset = input_timezone.period_for_local(v, &tzinfo_disambiguator_for(v)).utc_total_offset_rational 72 (v - local_offset).new_offset(local_offset) 73 end
Convert the given DateTime to use the given output_timezone. Expects the output_timezone to be a TZInfo::Timezone instance.
# File lib/sequel/extensions/named_timezones.rb 77 def convert_output_datetime_other(v, output_timezone) 78 # TZInfo converts times, but expects the given DateTime to have an offset 79 # of 0 and always leaves the timezone offset as 0 80 v = output_timezone.utc_to_local(v.new_offset(0)) 81 local_offset = output_timezone.period_for_local(v, &tzinfo_disambiguator_for(v)).utc_total_offset_rational 82 # Convert timezone offset from UTC to the offset for the output_timezone 83 (v - local_offset).new_offset(local_offset) 84 end
Returns TZInfo::Timezone instance if given a String
.
# File lib/sequel/extensions/named_timezones.rb 87 def convert_timezone_setter_arg(tz) 88 tz.is_a?(String) ? TZInfo::Timezone.get(tz) : super 89 end
Return a disambiguation proc that provides both the datetime value and the periods, in order to allow the choice of period to depend on the datetime value.
# File lib/sequel/extensions/named_timezones.rb 94 def tzinfo_disambiguator_for(v) 95 if pr = @tzinfo_disambiguator 96 proc{|periods| pr.call(v, periods)} 97 end 98 end