class Ourdate

Convert a string into a single date (helped by String.thedate)

Attributes

thedate[R]

Public Class Methods

build_datetime(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) click to toggle source

Construct a daylight saving time safe datetime, with arguments

# File lib/ndr_support/ourdate/build_datetime.rb, line 10
def self.build_datetime(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0)
  return nil if year.nil?

  default_timezone = ActiveRecord.default_timezone
  if default_timezone == :local
    # Time.local_time(year, month, day, hour, min, sec, usec).to_datetime
    # Behave like oracle_adapter.rb
    seconds = sec + Rational(usec, 10**6)
    time_array = [year, month, day, hour, min, seconds]
    begin
      #--
      # TODO: Fails unit tests unless we .to_datetime here
      #       but the risk is we lose the usec component unnecesssarily.
      #       Investigate removing .to_datetime below.
      #++
      Time.send(default_timezone, *time_array).to_datetime
    rescue
      zone_offset = default_timezone == :local ? DateTime.now.offset : 0
      # Append zero calendar reform start to account for dates skipped by calendar reform
      DateTime.new(*time_array[0..5] << zone_offset << 0) rescue nil
    end
  else
    # Only supports fake GMT time -- needs improvement
    # Maybe use Time.zone.local or Time.local_time(year, month, day)
    Time.utc(year, month, day, hour, min, sec, usec).to_datetime
  end
end
date_difference_in_years(date2, date1) click to toggle source

Compute date difference in years (e.g. patient age), as an integer For a positive result, the later date should be the first argument. Leap days are treated as for age calculations.

# File lib/ndr_support/ourdate.rb, line 63
def self.date_difference_in_years(date2, date1)
  (date2.strftime('%Y%m%d').sub(/0229$/, '0228').to_i -
   date1.strftime('%Y%m%d').sub(/0229$/, '0228').to_i) / 10_000
end
new(x = nil) click to toggle source
# File lib/ndr_support/ourdate.rb, line 30
def initialize(x = nil)
  if x.is_a?(Date)
    @thedate = x
  elsif x.is_a?(Time)
    @thedate = x.to_datetime
  elsif x.is_a?(String)
    self.source = x
  else
    @thedate = nil
  end
end
today() click to toggle source

We need a daylight saving time safe was of defining the date today.

# File lib/ndr_support/ourdate.rb, line 13
def self.today
  current_time = Time.now
  #--
  # TODO: Use Ourdate.build_datetime everywhere below:
  #++
  default_timezone = ActiveRecord.default_timezone
  if default_timezone == :local
    build_datetime(current_time.year, current_time.month, current_time.day)
  else
    #--
    # Only supports fake GMT time -- needs improvement
    # Maybe use Time.zone.local or Time.local_time(year, month, day)
    #++
    Time.gm(current_time.year, current_time.month, current_time.day, 0, 0, 0, 0).to_datetime
  end
end

Public Instance Methods

empty?() click to toggle source
# File lib/ndr_support/ourdate.rb, line 46
def empty?
  # An unspecified date will be empty. A valid date will not.
  @thedate.nil?
end
to_s() click to toggle source
# File lib/ndr_support/ourdate.rb, line 42
def to_s
  @thedate ? @thedate.to_date.to_formatted_s(:ui) : ''
end

Private Instance Methods

source=(s) click to toggle source
# File lib/ndr_support/ourdate.rb, line 51
def source=(s)
  dr = Daterange.new(s)
  if dr.date1 == dr.date2
    @thedate = dr.date1
  else
    @thedate = nil
  end
end