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
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
34 def self.extended(db)
35   db.extend_datasets(DatasetMethods)
36   procs = db.conversion_procs
37   procs[1082] = ::Sequel.method(:string_to_date)
38   procs[1184] = procs[1114] = db.method(:to_application_timestamp)
39 end

Public Instance Methods

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
49 def convert_infinite_timestamps=(v)
50   @convert_infinite_timestamps = case v
51   when Symbol
52     v
53   when 'nil'
54     :nil
55   when 'string'
56     :string
57   when 'date'
58     :date
59   when 'float'
60     :float
61   when String, true
62     typecast_value_boolean(v)
63   else
64     false
65   end
66 
67   pr = old_pr = Sequel.method(:string_to_date)
68   if @convert_infinite_timestamps
69     pr = lambda do |val|
70       case val
71       when *INFINITE_TIMESTAMP_STRINGS
72         infinite_timestamp_value(val)
73       else
74         old_pr.call(val)
75       end
76     end
77   end
78   add_conversion_proc(1082, pr)
79 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
 85 def to_application_timestamp(value)
 86   if value.is_a?(String) && (m = value.match(/((?:[-+]\d\d:\d\d)(:\d\d)?)?( BC)?\z/)) && (m[2] || m[3])
 87     if m[3]
 88       value = value.sub(' BC', '').sub(' ', ' BC ')
 89       conv = defined?(JRUBY_VERSION) && JRUBY_VERSION == '9.2.0.0'
 90     end
 91     if m[2] || conv
 92       dt = DateTime.parse(value)
 93       if conv
 94         # :nocov:
 95         if Sequel.datetime_class == DateTime
 96           dt >>= 12
 97         else
 98           dt >>= 24
 99         end
100         # :nocov:
101       end
102       unless Sequel.datetime_class == DateTime
103         dt = dt.to_time
104         if conv && (timezone == nil || timezone == :local) && !m[1]
105           # :nocov:
106           dt = Sequel.send(:convert_input_timestamp, dt.strftime("%F %T.%6N"), :local)
107           # :nocov:
108         end
109       end
110       Sequel.convert_output_timestamp(dt, Sequel.application_timezone)
111     else
112       super(value)
113     end
114   elsif convert_infinite_timestamps
115     case value
116     when *INFINITE_TIMESTAMP_STRINGS
117       infinite_timestamp_value(value)
118     else
119       super
120     end
121   else
122     super
123   end
124 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
129 def infinite_timestamp_value(value)
130   case convert_infinite_timestamps
131   when :nil
132     nil
133   when :string
134     value
135   when :date
136     value == 'infinity' ? PLUS_DATE_INFINITY : MINUS_DATE_INFINITY
137   else
138     value == 'infinity' ? PLUS_INFINITY : MINUS_INFINITY
139   end
140 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
145 def typecast_value_date(value)
146   if convert_infinite_timestamps
147     case value
148     when *INFINITE_DATETIME_VALUES
149       value
150     else
151       super
152     end
153   else
154     super
155   end
156 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
161 def typecast_value_datetime(value)
162   if convert_infinite_timestamps
163     case value
164     when *INFINITE_DATETIME_VALUES
165       value
166     else
167       super
168     end
169   else
170     super
171   end
172 end