Module: Sequel::Timezones
- Included in:
- Sequel
- Defined in:
- lib/sequel/timezones.rb
Overview
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.
Instance Attribute Summary collapse
-
#application_timezone ⇒ Object
readonly
The timezone you want the application to use.
-
#database_timezone ⇒ Object
readonly
The timezone for storage in the database.
-
#typecast_timezone ⇒ Object
readonly
The timezone that incoming data that Sequel needs to typecast is assumed to be already in (if they don’t include an offset).
Instance Method Summary collapse
-
#application_to_database_timestamp(v) ⇒ Object
Convert the given
Time
/DateTime
object into the database timezone, used when literalizing objects in an SQL string. -
#database_to_application_timestamp(v) ⇒ Object
Convert the given object into an object of
Sequel.datetime_class
in theapplication_timezone
. -
#default_timezone=(tz) ⇒ Object
Sets the database, application, and typecasting timezones to the given timezone.
-
#typecast_to_application_timestamp(v) ⇒ Object
Convert the given object into an object of
Sequel.datetime_class
in theapplication_timezone
.
Instance Attribute Details
#application_timezone ⇒ Object (readonly)
The timezone you want the application to use. This is the timezone that incoming times from the database and typecasting are converted to.
20 21 22 |
# File 'lib/sequel/timezones.rb', line 20 def application_timezone @application_timezone end |
#database_timezone ⇒ Object (readonly)
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).
26 27 28 |
# File 'lib/sequel/timezones.rb', line 26 def database_timezone @database_timezone end |
#typecast_timezone ⇒ Object (readonly)
The timezone that incoming data that Sequel needs to typecast is assumed to be already in (if they don’t include an offset).
30 31 32 |
# File 'lib/sequel/timezones.rb', line 30 def typecast_timezone @typecast_timezone end |
Instance Method Details
#application_to_database_timestamp(v) ⇒ Object
Convert the given Time
/DateTime
object into the database timezone, used when literalizing objects in an SQL string.
38 39 40 |
# File 'lib/sequel/timezones.rb', line 38 def (v) (v, Sequel.database_timezone) end |
#database_to_application_timestamp(v) ⇒ Object
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.
45 46 47 |
# File 'lib/sequel/timezones.rb', line 45 def (v) (v, Sequel.database_timezone) end |
#default_timezone=(tz) ⇒ Object
Sets the database, application, and typecasting timezones to the given timezone.
50 51 52 53 54 |
# File 'lib/sequel/timezones.rb', line 50 def default_timezone=(tz) self.database_timezone = tz self.application_timezone = tz self.typecast_timezone = tz end |
#typecast_to_application_timestamp(v) ⇒ Object
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.
59 60 61 |
# File 'lib/sequel/timezones.rb', line 59 def (v) (v, Sequel.typecast_timezone) end |