module Sequel::Postgres::ExtendedDateSupport

Constants

CONVERT_TYPES

:nocov:

DATETIME_YEAR_1
DATE_YEAR_1
INFINITE_DATETIME_VALUES
INFINITE_TIMESTAMP_STRINGS
MINUS_DATE_INFINITY
PLUS_DATE_INFINITY
RATIONAL_60
TIME_CAN_PARSE_BC
TIME_YEAR_1

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) click to toggle source

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

   # File lib/sequel/extensions/pg_extended_date_support.rb
36 def self.extended(db)
37   db.extend_datasets(DatasetMethods)
38   procs = db.conversion_procs
39   procs[1082] = ::Sequel.method(:string_to_date)
40   procs[1184] = procs[1114] = db.method(:to_application_timestamp)
41 end

Public Instance Methods

bound_variable_arg(arg, conn) click to toggle source

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.

Calls superclass method
   # File lib/sequel/extensions/pg_extended_date_support.rb
46 def bound_variable_arg(arg, conn)
47   case arg
48   when Date, Time
49     literal(arg)
50   else
51     super
52   end
53 end
convert_infinite_timestamps=(v) click to toggle source

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

   # File lib/sequel/extensions/pg_extended_date_support.rb
63 def convert_infinite_timestamps=(v)
64   @convert_infinite_timestamps = case v
65   when Symbol
66     v
67   when 'nil'
68     :nil
69   when 'string'
70     :string
71   when 'date'
72     :date
73   when 'float'
74     :float
75   when String, true
76     typecast_value_boolean(v)
77   else
78     false
79   end
80 
81   pr = old_pr = Sequel.method(:string_to_date)
82   if @convert_infinite_timestamps
83     pr = lambda do |val|
84       case val
85       when *INFINITE_TIMESTAMP_STRINGS
86         infinite_timestamp_value(val)
87       else
88         old_pr.call(val)
89       end
90     end
91   end
92   add_conversion_proc(1082, pr)
93 end
to_application_timestamp(value) click to toggle source

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.

Calls superclass method
    # File lib/sequel/extensions/pg_extended_date_support.rb
 99 def to_application_timestamp(value)
100   if value.is_a?(String) && (m = /((?:[-+]\d\d:\d\d)(:\d\d)?)?( BC)?\z/.match(value)) && (m[2] || m[3])
101     if m[3]
102       value = value.sub(' BC', '').sub(' ', ' BC ')
103     end
104     if m[2]
105       dt = if Sequel.datetime_class == DateTime
106         DateTime.parse(value)
107       elsif TIME_CAN_PARSE_BC
108         Time.parse(value)
109       # :nocov:
110       else
111         DateTime.parse(value).to_time
112       # :nocov:
113       end
114 
115       Sequel.convert_output_timestamp(dt, Sequel.application_timezone)
116     else
117       super(value)
118     end
119   elsif convert_infinite_timestamps
120     case value
121     when *INFINITE_TIMESTAMP_STRINGS
122       infinite_timestamp_value(value)
123     else
124       super
125     end
126   else
127     super
128   end
129 end

Private Instance Methods

infinite_timestamp_value(value) click to toggle source

Return an appropriate value for the given infinite timestamp string.

    # File lib/sequel/extensions/pg_extended_date_support.rb
134 def infinite_timestamp_value(value)
135   case convert_infinite_timestamps
136   when :nil
137     nil
138   when :string
139     value
140   when :date
141     value == 'infinity' ? PLUS_DATE_INFINITY : MINUS_DATE_INFINITY
142   else
143     value == 'infinity' ? PLUS_INFINITY : MINUS_INFINITY
144   end
145 end
typecast_value_date(value) click to toggle source

If the value is an infinite value (either an infinite float or a string returned by by PostgreSQL for an infinite date), return it without converting it if convert_infinite_timestamps is set.

Calls superclass method
    # File lib/sequel/extensions/pg_extended_date_support.rb
150 def typecast_value_date(value)
151   if convert_infinite_timestamps
152     case value
153     when *INFINITE_DATETIME_VALUES
154       value
155     else
156       super
157     end
158   else
159     super
160   end
161 end
typecast_value_datetime(value) click to toggle source

If the value is an infinite value (either an infinite float or a string returned by by PostgreSQL for an infinite timestamp), return it without converting it if convert_infinite_timestamps is set.

Calls superclass method
    # File lib/sequel/extensions/pg_extended_date_support.rb
166 def typecast_value_datetime(value)
167   if convert_infinite_timestamps
168     case value
169     when *INFINITE_DATETIME_VALUES
170       value
171     else
172       super
173     end
174   else
175     super
176   end
177 end