module Sequel::Postgres::ExtendedDateSupport
Constants
- CONVERT_TYPES
-
:nocov:
- DATETIME_YEAR_1
- INFINITE_DATETIME_VALUES
- INFINITE_TIMESTAMP_STRINGS
- MINUS_DATE_INFINITY
- PLUS_DATE_INFINITY
- RATIONAL_60
- TIME_CAN_PARSE_BC
- TIME_YEAR_1
Attributes
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
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
Add dataset methods and update the conversion proces for dates and timestamps.
Public Instance Methods
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
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.
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
Set whether to allow infinite timestamps/dates. Make sure the conversion proc for date reflects that setting.
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
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.
Private Instance Methods
Source
# File lib/sequel/extensions/pg_extended_date_support.rb 142 def infinite_timestamp_value(value) 143 case convert_infinite_timestamps 144 when :nil 145 nil 146 when :string 147 value 148 when :date 149 value == 'infinity' ? PLUS_DATE_INFINITY : MINUS_DATE_INFINITY 150 else 151 value == 'infinity' ? PLUS_INFINITY : MINUS_INFINITY 152 end 153 end
Return an appropriate value for the given infinite timestamp string.
Source
# File lib/sequel/extensions/pg_extended_date_support.rb 158 def typecast_value_date(value) 159 if convert_infinite_timestamps 160 case value 161 when *INFINITE_DATETIME_VALUES 162 value 163 else 164 super 165 end 166 else 167 super 168 end 169 end
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.
Source
# File lib/sequel/extensions/pg_extended_date_support.rb 174 def typecast_value_datetime(value) 175 if convert_infinite_timestamps 176 case value 177 when *INFINITE_DATETIME_VALUES 178 value 179 else 180 super 181 end 182 else 183 super 184 end 185 end
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.