| Module | Sequel::Postgres::ExtendedDateSupport |
| In: |
lib/sequel/extensions/pg_extended_date_support.rb
|
| 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 |
| 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. |
Add dataset methods and update the conversion proces for dates and timestamps.
# File lib/sequel/extensions/pg_extended_date_support.rb, line 34
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
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, line 49
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
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.
# File lib/sequel/extensions/pg_extended_date_support.rb, line 85
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