Module: TempestTime::Helpers::TimeHelper

Instance Method Summary collapse

Instance Method Details

#beginning_of_this_weekObject



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

def beginning_of_this_week
  Date.today - Date.today.wday
end

#beginning_of_week(week_number) ⇒ Object



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

def beginning_of_week(week_number)
  this_week_number = (Date.today + 1).cweek # Add one so weeks begin on Sunday.
  return false unless week_number <= this_week_number
  days_in_the_past = (this_week_number - week_number) * 7
  beginning_of_this_week - days_in_the_past
end

#formatted_date(date) ⇒ Object



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

def formatted_date(date)
  "#{Date::DAYNAMES[date.wday]}, #{Date::MONTHNAMES[date.month]} #{date.day}"
end

#formatted_date_range(start_date, end_date) ⇒ Object



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

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

#formatted_time(seconds) ⇒ Object



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

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

#parsed_date_input(date_input) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tempest_time/helpers/time_helper.rb', line 32

def parsed_date_input(date_input)
  case date_input
  when 'today', nil
    [Date.today]
  when 'yesterday'
    [Date.today.prev_day]
  when 'week'
    this_week
  else
    [Date.parse(date_input)]
  end
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

#this_weekObject



56
57
58
59
60
# File 'lib/tempest_time/helpers/time_helper.rb', line 56

def this_week
  (0..6).map do |n|
    beginning_of_this_week + n
  end
end