Sequel doesn’t pay much attention to timezones by default, but you can set it handle timezones if you want. There are three separate timezone settings, application_timezone, database_timezone, and typecast_timezone. All three timezones have getter and setter methods. You can set all three timezones to the same value at once via Sequel.default_timezone=.
The only timezone values that are supported by default are :utc (convert to UTC), :local (convert to local time), and nil (don’t convert). If you need to convert to a specific timezone, or need the timezones being used to change based on the environment (e.g. current user), you need to use the named_timezones extension (and use DateTime as the datetime_class). Sequel also ships with a thread_local_timezones extensions which allows each thread to have its own timezone values for each of the timezones.
The timezone you want the application to use. This is the timezone that incoming times from the database and typecasting are converted to.
The timezone that incoming data that Sequel needs to typecast is assumed to be already in (if they don’t include an offset).
Convert the given Time/DateTime object into the database timezone, used when literalizing objects in an SQL string.
# File lib/sequel/timezones.rb, line 38 38: def application_to_database_timestamp(v) 39: convert_output_timestamp(v, Sequel.database_timezone) 40: end
Converts the object to the given output_timezone.
# File lib/sequel/timezones.rb, line 43 43: def convert_output_timestamp(v, output_timezone) 44: if output_timezone 45: if v.is_a?(DateTime) 46: case output_timezone 47: when :utc 48: v.new_offset(0) 49: when :local 50: v.new_offset(local_offset_for_datetime(v)) 51: else 52: convert_output_datetime_other(v, output_timezone) 53: end 54: else 55: v.send(output_timezone == :utc ? :getutc : :getlocal) 56: end 57: else 58: v 59: end 60: end
Converts the given object from the given input timezone to the application_timezone using convert_input_timestamp and convert_output_timestamp.
# File lib/sequel/timezones.rb, line 65 65: def convert_timestamp(v, input_timezone) 66: begin 67: if v.is_a?(Date) && !v.is_a?(DateTime) 68: # Dates handled specially as they are assumed to already be in the application_timezone 69: if datetime_class == DateTime 70: DateTime.civil(v.year, v.month, v.day, 0, 0, 0, application_timezone == :local ? (defined?(Rational) ? Rational(Time.local(v.year, v.month, v.day).utc_offset, 86400) : Time.local(v.year, v.month, v.day).utc_offset/86400.0) : 0) 71: else 72: Time.send(application_timezone == :utc ? :utc : :local, v.year, v.month, v.day) 73: end 74: else 75: convert_output_timestamp(convert_input_timestamp(v, input_timezone), application_timezone) 76: end 77: rescue InvalidValue 78: raise 79: rescue => e 80: raise convert_exception_class(e, InvalidValue) 81: end 82: end
Convert the given object into an object of Sequel.datetime_class in the application_timezone. Used when coverting datetime/timestamp columns returned by the database.
# File lib/sequel/timezones.rb, line 87 87: def database_to_application_timestamp(v) 88: convert_timestamp(v, Sequel.database_timezone) 89: end
Sets the database, application, and typecasting timezones to the given timezone.
# File lib/sequel/timezones.rb, line 92 92: def default_timezone=(tz) 93: self.database_timezone = tz 94: self.application_timezone = tz 95: self.typecast_timezone = tz 96: end
Convert the given object into an object of Sequel.datetime_class in the application_timezone. Used when typecasting values when assigning them to model datetime attributes.
# File lib/sequel/timezones.rb, line 101 101: def typecast_to_application_timestamp(v) 102: convert_timestamp(v, Sequel.typecast_timezone) 103: end
Convert the given DateTime to the given input_timezone, keeping the same time and just modifying the timezone.
# File lib/sequel/timezones.rb, line 109 109: def convert_input_datetime_no_offset(v, input_timezone) 110: case input_timezone 111: when :utc, nil 112: v # DateTime assumes UTC if no offset is given 113: when :local 114: offset = local_offset_for_datetime(v) 115: v.new_offset(offset) - offset 116: else 117: convert_input_datetime_other(v, input_timezone) 118: end 119: end
Convert the given DateTime to the given input_timezone that is not supported by default (i.e. one other than nil, :local, or :utc). Raises an InvalidValue by default. Can be overridden in extensions.
# File lib/sequel/timezones.rb, line 124 124: def convert_input_datetime_other(v, input_timezone) 125: raise InvalidValue, "Invalid input_timezone: #{input_timezone.inspect}" 126: end
Converts the object from a String, Array, Date, DateTime, or Time into an instance of Sequel.datetime_class. If given an array or a string that doesn’t contain an offset, assume that the array/string is already in the given input_timezone.
# File lib/sequel/timezones.rb, line 131 131: def convert_input_timestamp(v, input_timezone) 132: case v 133: when String 134: v2 = Sequel.string_to_datetime(v) 135: if !input_timezone || Date._parse(v).has_key?(:offset) 136: v2 137: else 138: # Correct for potentially wrong offset if string doesn't include offset 139: if v2.is_a?(DateTime) 140: v2 = convert_input_datetime_no_offset(v2, input_timezone) 141: else 142: # Time assumes local time if no offset is given 143: v2 = v2.getutc + v2.utc_offset if input_timezone == :utc 144: end 145: v2 146: end 147: when Array 148: y, mo, d, h, mi, s, ns, off = v 149: if datetime_class == DateTime 150: s += (defined?(Rational) ? Rational(ns, 1000000000) : ns/1000000000.0) if ns 151: if off 152: DateTime.civil(y, mo, d, h, mi, s, off) 153: else 154: convert_input_datetime_no_offset(DateTime.civil(y, mo, d, h, mi, s), input_timezone) 155: end 156: else 157: Time.send(input_timezone == :utc ? :utc : :local, y, mo, d, h, mi, s, (ns ? ns / 1000.0 : 0)) 158: end 159: when Hash 160: ary = [:year, :month, :day, :hour, :minute, :second, :nanos].map{|x| (v[x] || v[x.to_s]).to_i} 161: if (offset = (v[:offset] || v['offset'])) 162: ary << offset 163: end 164: convert_input_timestamp(ary, input_timezone) 165: convert_input_timestamp(ary, input_timezone) 166: when Time 167: if datetime_class == DateTime 168: v.respond_to?(:to_datetime) ? v.to_datetime : string_to_datetime(v.iso8601) 169: else 170: v 171: end 172: when DateTime 173: if datetime_class == DateTime 174: v 175: else 176: v.respond_to?(:to_time) ? v.to_time : string_to_datetime(v.to_s) 177: end 178: else 179: raise InvalidValue, "Invalid convert_input_timestamp type: #{v.inspect}" 180: end 181: end
Convert the given DateTime to the given output_timezone that is not supported by default (i.e. one other than nil, :local, or :utc). Raises an InvalidValue by default. Can be overridden in extensions.
# File lib/sequel/timezones.rb, line 186 186: def convert_output_datetime_other(v, output_timezone) 187: raise InvalidValue, "Invalid output_timezone: #{output_timezone.inspect}" 188: end
Convert the timezone setter argument. Returns argument given by default, exists for easier overriding in extensions.
# File lib/sequel/timezones.rb, line 192 192: def convert_timezone_setter_arg(tz) 193: tz 194: end
Takes a DateTime dt, and returns the correct local offset for that dt, daylight savings included.
# File lib/sequel/timezones.rb, line 197 197: def local_offset_for_datetime(dt) 198: time_offset_to_datetime_offset Time.local(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec).utc_offset 199: end
Caches offset conversions to avoid excess Rational math.
# File lib/sequel/timezones.rb, line 202 202: def time_offset_to_datetime_offset(offset_secs) 203: @local_offsets ||= {} 204: @local_offsets[offset_secs] ||= respond_to?(:Rational, true) ? Rational(offset_secs, 60*60*24) : offset_secs/60/60/24.0 205: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.