Module: TempestTime::Helpers::TimeHelper

Instance Method Summary collapse

Instance Method Details

#beginning_of_week(weeks_ago = 0) ⇒ Object



46
47
48
49
# File 'lib/tempest_time/helpers/time_helper.rb', line 46

def beginning_of_week(weeks_ago = 0)
  return unless weeks_ago >= 0
  (Date.today - Date.today.wday) - (weeks_ago * 7)
end

#dates_in_range(days_before: 0, days_after: 0) ⇒ Object



67
68
69
# File 'lib/tempest_time/helpers/time_helper.rb', line 67

def dates_in_range(days_before: 0, days_after: 0)
  future_dates(days_after).merge(past_dates(days_before))
end

#end_of_week(weeks_ago = 0) ⇒ Object



51
52
53
# File 'lib/tempest_time/helpers/time_helper.rb', line 51

def end_of_week(weeks_ago = 0)
  beginning_of_week(weeks_ago) + 6
end

#formatted_date(date) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/tempest_time/helpers/time_helper.rb', line 38

def formatted_date(date)
  Date::DAYNAMES[date.wday] +
    ', ' +
    Date::MONTHNAMES[date.month] +
    ' ' +
    date.day.to_s
end

#formatted_date_range(start_date, end_date) ⇒ Object



33
34
35
36
# File 'lib/tempest_time/helpers/time_helper.rb', line 33

def formatted_date_range(start_date, end_date)
  return formatted_date(start_date) if end_date.nil?
  "#{formatted_date(start_date)} - #{formatted_date(end_date)}"
end

#formatted_time(seconds) ⇒ Object



19
20
21
22
# File 'lib/tempest_time/helpers/time_helper.rb', line 19

def formatted_time(seconds)
  return "#{seconds / 60} minutes" if seconds < 3600
  "#{(seconds / 3600.to_f).round(2)} hours"
end

#formatted_time_for_input(seconds) ⇒ Object



24
25
26
27
# File 'lib/tempest_time/helpers/time_helper.rb', line 24

def formatted_time_for_input(seconds)
  return "#{(seconds / 60).to_i}m" if seconds < 3600
  "#{(seconds / 3600.to_f).round(2)}h"
end

#formatted_time_long(seconds) ⇒ Object



29
30
31
# File 'lib/tempest_time/helpers/time_helper.rb', line 29

def formatted_time_long(seconds)
  Time.at(seconds).utc.strftime("%H:%M:%S")
end

#future_dates(number_of_days) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/tempest_time/helpers/time_helper.rb', line 80

def future_dates(number_of_days)
  dates = {}
  number_of_days.downto(0).each do |num|
    date = Date.today + num
    dates[formatted_date(date)] = date
  end
  dates
end

#parsed_time(time) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tempest_time/helpers/time_helper.rb', line 6

def parsed_time(time)
  # Returns seconds.
  return time if time.is_a?(Integer)

  if /^\d*\.{0,1}\d{1,2}h$/.match(time)
    return (time.chomp('h').to_f * 60 * 60).to_i
  elsif /^\d+m$/.match(time)
    return time.chomp('m').to_i * 60
  end

  abort('Please provide time in the correct format. e.g. 0.5h, .5h, 30m')
end

#past_dates(number_of_days) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/tempest_time/helpers/time_helper.rb', line 71

def past_dates(number_of_days)
  dates = {}
  (0..number_of_days).each do |num|
    date = Date.today - num
    dates[formatted_date(date)] = date
  end
  dates
end

#past_week_selections(number_of_weeks) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/tempest_time/helpers/time_helper.rb', line 59

def past_week_selections(number_of_weeks)
  weeks = {}
  week_beginnings(number_of_weeks).each do |beginning|
    weeks[formatted_date_range(beginning, beginning + 6)] = beginning
  end
  weeks
end

#week_beginnings(number_of_weeks) ⇒ Object



55
56
57
# File 'lib/tempest_time/helpers/time_helper.rb', line 55

def week_beginnings(number_of_weeks)
  (0..number_of_weeks).map { |weeks_ago| beginning_of_week(weeks_ago) }
end

#week_dates(first_day) ⇒ Object



89
90
91
# File 'lib/tempest_time/helpers/time_helper.rb', line 89

def week_dates(first_day)
  (0..6).map { |days| first_day + days }
end