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[1] || m[2])
 87     if m[2]
 88       value = value.sub(' BC', '').sub(' ', ' BC ')
 89       conv = defined?(JRUBY_VERSION) && JRUBY_VERSION == '9.2.0.0'
 90     end
 91     if m[1] || 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       dt = dt.to_time unless Sequel.datetime_class == DateTime
103       Sequel.convert_output_timestamp(dt, Sequel.application_timezone)
104     else
105       super(value)
106     end
107   elsif convert_infinite_timestamps
108     case value
109     when *INFINITE_TIMESTAMP_STRINGS
110       infinite_timestamp_value(value)
111     else
112       super
113     end
114   else
115     super
116   end
117 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
122 def infinite_timestamp_value(value)
123   case convert_infinite_timestamps
124   when :nil
125     nil
126   when :string
127     value
128   when :date
129     value == 'infinity' ? PLUS_DATE_INFINITY : MINUS_DATE_INFINITY
130   else
131     value == 'infinity' ? PLUS_INFINITY : MINUS_INFINITY
132   end
133 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
138 def typecast_value_date(value)
139   if convert_infinite_timestamps
140     case value
141     when *INFINITE_DATETIME_VALUES
142       value
143     else
144       super
145     end
146   else
147     super
148   end
149 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
154 def typecast_value_datetime(value)
155   if convert_infinite_timestamps
156     case value
157     when *INFINITE_DATETIME_VALUES
158       value
159     else
160       super
161     end
162   else
163     super
164   end
165 end