module Sequel::Postgres::ExtendedDateSupport

  1. lib/sequel/extensions/pg_extended_date_support.rb

Constants

CONVERT_TYPES = [Java::JavaSQL::Types::DATE, Java::JavaSQL::Types::TIMESTAMP]  

:nocov:

DATETIME_YEAR_1 = DateTime.new(1)  
INFINITE_DATETIME_VALUES = ([PLUS_INFINITY, MINUS_INFINITY] + INFINITE_TIMESTAMP_STRINGS).freeze  
INFINITE_TIMESTAMP_STRINGS = ['infinity'.freeze, '-infinity'.freeze].freeze  
MINUS_DATE_INFINITY = -PLUS_DATE_INFINITY  
PLUS_DATE_INFINITY = Date::Infinity.new  
RATIONAL_60 = Rational(60)  
TIME_CAN_PARSE_BC = RUBY_VERSION >= '2.5'  
TIME_YEAR_1 = Time.at(-62135596800).utc  

Attributes

convert_infinite_timestamps [R]

Whether infinite timestamps/dates should be converted on retrieval. By default, no conversion is done, so an error is raised if you attempt to retrieve an infinite timestamp/date. You can set this to :nil to convert to nil, :string to leave as a string, or :float to convert to an infinite float.

Public Class methods

extended(db)

Add dataset methods and update the conversion proces for dates and timestamps.

[show source]
   # File lib/sequel/extensions/pg_extended_date_support.rb
35 def self.extended(db)
36   db.extend_datasets(DatasetMethods)
37   procs = db.conversion_procs
38   procs[1082] = ::Sequel.method(:string_to_date)
39   procs[1184] = procs[1114] = db.method(:to_application_timestamp)
40   if ocps = db.instance_variable_get(:@oid_convertor_map)
41     # Clear the oid convertor map entries for timestamps if they
42     # exist, so it will regenerate new ones that use this extension.
43     # This is only taken when using the jdbc adapter.
44     Sequel.synchronize do
45       ocps.delete(1184)
46       ocps.delete(1114)
47     end
48   end
49 end

Public Instance methods

bound_variable_arg(arg, conn)

Handle BC dates and times in bound variables. This is necessary for Date values when using both the postgres and jdbc adapters, but also necessary for Time values on jdbc.

[show source]
   # File lib/sequel/extensions/pg_extended_date_support.rb
54 def bound_variable_arg(arg, conn)
55   case arg
56   when Time, Date
57     @default_dataset.literal_date_or_time(arg)
58   else
59     super
60   end
61 end
convert_infinite_timestamps=(v)

Set whether to allow infinite timestamps/dates. Make sure the conversion proc for date reflects that setting.

[show source]
    # File lib/sequel/extensions/pg_extended_date_support.rb
 71 def convert_infinite_timestamps=(v)
 72   @convert_infinite_timestamps = case v
 73   when Symbol
 74     v
 75   when 'nil'
 76     :nil
 77   when 'string'
 78     :string
 79   when 'date'
 80     :date
 81   when 'float'
 82     :float
 83   when String, true
 84     typecast_value_boolean(v)
 85   else
 86     false
 87   end
 88 
 89   pr = old_pr = Sequel.method(:string_to_date)
 90   if @convert_infinite_timestamps
 91     pr = lambda do |val|
 92       case val
 93       when *INFINITE_TIMESTAMP_STRINGS
 94         infinite_timestamp_value(val)
 95       else
 96         old_pr.call(val)
 97       end
 98     end
 99   end
100   add_conversion_proc(1082, pr)
101 end
to_application_timestamp(value)

Handle BC dates in timestamps by moving the BC from after the time to after the date, to appease ruby’s date parser. If convert_infinite_timestamps is true and the value is infinite, return an appropriate value based on the convert_infinite_timestamps setting.

[show source]
    # File lib/sequel/extensions/pg_extended_date_support.rb
107 def to_application_timestamp(value)
108   if value.is_a?(String) && (m = /((?:[-+]\d\d:\d\d)(:\d\d)?)?( BC)?\z/.match(value)) && (m[2] || m[3])
109     if m[3]
110       value = value.sub(' BC', '').sub(' ', ' BC ')
111     end
112     if m[2]
113       dt = if Sequel.datetime_class == DateTime
114         DateTime.parse(value)
115       elsif TIME_CAN_PARSE_BC
116         Time.parse(value)
117       # :nocov:
118       else
119         DateTime.parse(value).to_time
120       # :nocov:
121       end
122 
123       Sequel.convert_output_timestamp(dt, Sequel.application_timezone)
124     else
125       super(value)
126     end
127   elsif convert_infinite_timestamps
128     case value
129     when *INFINITE_TIMESTAMP_STRINGS
130       infinite_timestamp_value(value)
131     else
132       super
133     end
134   else
135     super
136   end
137 end