Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_support/date_and_time_extensions.rb

Instance Method Summary collapse

Instance Method Details

#orig_to_datetimeObject

ISO date format



11
# File 'lib/ndr_support/date_and_time_extensions.rb', line 11

alias orig_to_datetime to_datetime

#orig_to_sObject



25
# File 'lib/ndr_support/date_and_time_extensions.rb', line 25

alias orig_to_s to_s

#to_datetimeObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ndr_support/date_and_time_extensions.rb', line 13

def to_datetime
  # Default timezone for Date is GMT, not local timezone
  default_timezone = if ActiveRecord.respond_to?(:default_timezone)
                       ActiveRecord.default_timezone
                     else
                       ActiveRecord::Base.default_timezone # Rails <= 6.1
                     end
  return in_time_zone.to_datetime if default_timezone == :local

  orig_to_datetime
end

#to_isoObject

to_iso output must be SQL safe for security reasons



7
8
9
# File 'lib/ndr_support/date_and_time_extensions.rb', line 7

def to_iso
  strftime('%Y-%m-%d')
end

#to_s(format = :default) ⇒ Object

Rails 7 stops overriding to_s (without a format specification) (for performance on Ruby 3.1) cf. activesupport-7.0.4/lib/active_support/core_ext/date/deprecated_conversions.rb We keep overriding this for compatibility



30
31
32
33
34
35
36
# File 'lib/ndr_support/date_and_time_extensions.rb', line 30

def to_s(format = :default)
  if format == :default
    to_formatted_s(:default)
  else
    orig_to_s(format)
  end
end