Module | Sequel::Timezones |
In: |
lib/sequel/timezones.rb
|
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.
application_timezone | [R] | The timezone you want the application to use. This is the timezone that incoming times from the database and typecasting are converted to. |
database_timezone | [R] | The timezone for storage in the database. This is the timezone to which Sequel will convert timestamps before literalizing them for storage in the database. It is also the timezone that Sequel will assume database timestamp values are already in (if they don‘t include an offset). |
typecast_timezone | [R] | The timezone that incoming data that Sequel needs to typecast is assumed to be already in (if they don‘t include an offset). |
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 converting 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