Module: MalawiHivProgramReports::Utils::TimeUtils

Defined in:
app/services/malawi_hiv_program_reports/utils/time_utils.rb

Class Method Summary collapse

Class Method Details

.date_epochObject



11
12
13
# File 'app/services/malawi_hiv_program_reports/utils/time_utils.rb', line 11

def date_epoch
  Date.today - 120.years
end

.day_bounds(datetime) ⇒ Object

Returns a 24 hour period (day) containing the date



16
17
18
19
20
# File 'app/services/malawi_hiv_program_reports/utils/time_utils.rb', line 16

def day_bounds(datetime)
  datetime = datetime&.to_time || datetime
  [datetime.strftime('%Y-%m-%d 00:00:00').to_time,
   datetime.strftime('%Y-%m-%d 23:59:59').to_time]
end

.get_person_age(birthdate:) ⇒ Object



46
47
48
# File 'app/services/malawi_hiv_program_reports/utils/time_utils.rb', line 46

def get_person_age(birthdate:)
  ((Time.zone.now - birthdate.to_time) / 1.year.seconds).floor
end

.parse_date_range(start_date, end_date) ⇒ Object

Parses and validates start_date and end_date provided by users

Returns: A pair of Date objects containing the start_date and end_date

Raises:

  • (::InvalidParameterError)


26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/malawi_hiv_program_reports/utils/time_utils.rb', line 26

def parse_date_range(start_date, end_date)
  raise ::InvalidParameterError, 'start_date is required' if start_date.blank?
  raise ::InvalidParameterError, 'end_date is required' if end_date.blank?

  start_date = start_date.to_date
  end_date = end_date.to_date

  raise ::InvalidParameterError, "start_date can't be greater than end_date" if start_date > end_date

  [start_date, end_date]
end

.retro_timestamp(date) ⇒ Object

Returns a time object comprising the given date plus the current time.



39
40
41
42
43
44
# File 'app/services/malawi_hiv_program_reports/utils/time_utils.rb', line 39

def retro_timestamp(date)
  return nil unless date

  date = date.to_time
  "#{date.strftime('%Y-%m-%d')} #{Time.now.strftime('%H:%M:%S')}".to_time
end

.time_epochObject



7
8
9
# File 'app/services/malawi_hiv_program_reports/utils/time_utils.rb', line 7

def time_epoch
  Time.now - 120.years
end