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)  
DATE_YEAR_1 = Date.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  
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
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)

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
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)

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
 85 def to_application_timestamp(value)
 86   if value.is_a?(String) && (m = /((?:[-+]\d\d:\d\d)(:\d\d)?)?( BC)?\z/.match(value)) && (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