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

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.


43
44
45
# File 'lib/sequel/extensions/pg_extended_date_support.rb', line 43

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.


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

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.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sequel/extensions/pg_extended_date_support.rb', line 81

def to_application_timestamp(value)
  if value.is_a?(String) && (m = value.match(/(?:(?:[-+]\d\d:\d\d)(:\d\d)?)?( BC)?\z/)) && (m[1] || m[2])
    if m[2]
      value = value.sub(' BC', '').sub(' ', ' BC ')
    end
    if m[1]
      dt = DateTime.parse(value)
      dt = dt.to_time unless Sequel.datetime_class == DateTime
      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