Class: Ourdate

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil) ⇒ Ourdate

Returns a new instance of Ourdate.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ndr_support/ourdate.rb', line 58

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

Instance Attribute Details

#thedateObject (readonly)

Returns the value of attribute thedate.



9
10
11
# File 'lib/ndr_support/ourdate.rb', line 9

def thedate
  @thedate
end

Class Method Details

.build_datetime(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object

Construct a daylight saving time safe datetime, with arguments – FIXME: Note that the arguments should be numbers, not strings – it works with strings arguments only after the 1970 epoch; before, it returns nil. ++



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ndr_support/ourdate.rb', line 33

def self.build_datetime(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0)
  if ActiveRecord::Base.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(ActiveRecord::Base.default_timezone, *time_array).to_datetime
    rescue
      zone_offset = ActiveRecord::Base.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) ⇒ Object

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.



91
92
93
94
# File 'lib/ndr_support/ourdate.rb', line 91

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

.todayObject

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ndr_support/ourdate.rb', line 12

def self.today
  current_time = Time.now
  #--
  # TODO: Use Ourdate.build_datetime everywhere below:
  #++
  if ActiveRecord::Base.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

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/ndr_support/ourdate.rb', line 74

def empty?
  # An unspecified date will be empty. A valid or invalid date will not.
  @thedate.nil? && @source.blank?
end

#to_sObject



70
71
72
# File 'lib/ndr_support/ourdate.rb', line 70

def to_s
  @thedate ? @thedate.to_date.to_s(:ui) : ''
end