Module: Trak

Defined in:
lib/trak.rb

Constant Summary collapse

TIME_FORMAT_12HOUR =
"%l:%M %p"
TIME_FORMAT_24HOUR =
"%k:%M"

Class Method Summary collapse

Class Method Details

.minutesToTime(minutes) ⇒ Object

expects an integer



84
85
86
# File 'lib/trak.rb', line 84

def self.minutesToTime(minutes)
  time_with_hours_minutes(minutes / 60, minutes % 60).strftime(TIME_FORMAT_24HOUR).strip
end

.newTimeWithMinutes(start_time, minutes) ⇒ Object



49
50
51
52
# File 'lib/trak.rb', line 49

def self.newTimeWithMinutes(start_time, minutes)
  hm = start_time.split ':'
  Time.at(time_with_hours_minutes(*hm).to_i + minutes.to_i*60).strftime(TIME_FORMAT_24HOUR).strip
end

.printSubReport(report_hash, report_title) ⇒ Object

expects a hash of tasks mapped to time spent, and a sub-report name

(e.g., work, personal)

prints a formatted sub-report returns the total hours worked



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

def self.printSubReport(report_hash, report_title)
  total = 0
  unless report_hash.empty?
    count = 0
    report_out = ""
    report_hash.each do |title, minutes|
      total += minutes.to_i
      count += 1
      report_out += "=> #{timeString(minutes)}: #{title}"
      report_out += "\n" unless count == report_hash.size
    end
    puts "# #{report_title} time (#{timeString(total)})"
    puts report_out
  end
  total
end

.processTimeArgument(time_string) ⇒ Object

expects a single argument - the time argument in the format ##m or ##h if argument has no m/h qualifier, assume m returns a number of minutes



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/trak.rb', line 64

def self.processTimeArgument(time_string)
  if time_string =~ /^(\d*\.?\d+)((m|min|minute|minutes)|(h|hr|hour|hours))?$/i
    time = $1.to_f
    modifier = $2
    minutes = (modifier =~ /h.*/) ? time * 60 : time

    # check enough time has been logged
    if minutes < 15
      STDERR.puts "You must log at least 15 minutes."
      exit 1
    end

    minutes.round_to_nearest 15
  else
    STDERR.puts "Incorrectly formatted argument."
    exit 1
  end
end

.startTimeInMinutes(minutes) ⇒ Object

expects an integer which is the amount of minutes logged



89
90
91
# File 'lib/trak.rb', line 89

def self.startTimeInMinutes(minutes)
  Time.now.to_minutes.round_to_nearest(15) - minutes
end

.time_with_hours_minutes(*hm) ⇒ Object



29
30
31
32
# File 'lib/trak.rb', line 29

def self.time_with_hours_minutes(*hm)
  dmy = Time.now.to_a[3..5].reverse
  Time.new(*dmy, *hm)
end

.timeString(minutes) ⇒ Object

expects a number of minutes if less than 60 returns the number with an “m” otherwise converts to hours and adds an “h”



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/trak.rb', line 37

def self.timeString(minutes)
  if minutes >= 60
    hours = minutes/60.0
    if hours % 1 == 0
      hours = hours.to_i
    end
    "#{hours}h"
  else
    "#{minutes}m"
  end
end

.to12HourTime(time) ⇒ Object



54
55
56
57
58
59
# File 'lib/trak.rb', line 54

def self.to12HourTime(time)
  unless time.blank?
    hm = time.split ':'
    time_with_hours_minutes(*hm).strftime(TIME_FORMAT_12HOUR).strip
  end
end