Module: Sequel::Postgres::ExtendedDateSupport

Defined in:
lib/sequel/extensions/pg_extended_date_support.rb

Defined Under Namespace

Modules: DatasetMethods

Constant Summary collapse

DATE_YEAR_1 =
Date.new(1)
DATETIME_YEAR_1 =
DateTime.new(1)
TIME_YEAR_1 =
Time.at(-62135596800).utc
INFINITE_TIMESTAMP_STRINGS =
['infinity'.freeze, '-infinity'.freeze].freeze
INFINITE_DATETIME_VALUES =
([PLUS_INFINITY, MINUS_INFINITY] + INFINITE_TIMESTAMP_STRINGS).freeze
PLUS_DATE_INFINITY =
Date::Infinity.new
MINUS_DATE_INFINITY =
-PLUS_DATE_INFINITY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#convert_infinite_timestampsObject

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.


45
46
47
# File 'lib/sequel/extensions/pg_extended_date_support.rb', line 45

def convert_infinite_timestamps
  @convert_infinite_timestamps
end

Class Method Details

.extended(db) ⇒ Object

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


34
35
36
37
38
39
# File 'lib/sequel/extensions/pg_extended_date_support.rb', line 34

def self.extended(db)
  db.extend_datasets(DatasetMethods)
  procs = db.conversion_procs
  procs[1082] = ::Sequel.method(:string_to_date)
  procs[1184] = procs[1114] = db.method(:to_application_timestamp)
end

Instance Method Details

#to_application_timestamp(value) ⇒ Object

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.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sequel/extensions/pg_extended_date_support.rb', line 85

def to_application_timestamp(value)
  if value.is_a?(String) && (m = value.match(/((?:[-+]\d\d:\d\d)(:\d\d)?)?( BC)?\z/)) && (m[2] || m[3])
    if m[3]
      value = value.sub(' BC', '').sub(' ', ' BC ')
      conv = defined?(JRUBY_VERSION) && JRUBY_VERSION == '9.2.0.0'
    end
    if m[2] || conv
      dt = DateTime.parse(value)
      if conv
        # :nocov:
        if Sequel.datetime_class == DateTime
          dt >>= 12
        else
          dt >>= 24
        end
        # :nocov:
      end
      unless Sequel.datetime_class == DateTime
        dt = dt.to_time
        if conv && (timezone == nil || timezone == :local) && !m[1]
          # :nocov:
          dt = Sequel.send(:convert_input_timestamp, dt.strftime("%F %T.%6N"), :local)
          # :nocov:
        end
      end
      Sequel.convert_output_timestamp(dt, Sequel.application_timezone)
    else
      super(value)
    end
  elsif convert_infinite_timestamps
    case value
    when *INFINITE_TIMESTAMP_STRINGS
      infinite_timestamp_value(value)
    else
      super
    end
  else
    super
  end
end