0

I have been tasked with building a simple Ruby app that does two things:

Registers new users with a New command Shows their time difference with Work command

It would be for an office building and the purpose would be to keep track of total hours worked.

I am also supposed to pull this information from a .txt file like so:

New Aaron
New Bertha
New Charles
Work  Bertha 06:30 11:12
Work  Bertha 12:03 16:17
Work  Charles 07:52 17:02

With the time expressed in only minutes and hours. I don't think anyone would be working past midnight. This is the part that really confuses me, as I am having a lot of trouble trying to find the difference in their times for work, as shown in an expected output .txt file:

Aaron: 0 hours 0 minutes
Bertha: 8 hours 52 minutes
Charles: 9 hours 10 minutes

Once again, I am ONLY asking for help with finding the difference in each individual's time for work.

Any and all help would be appreciated.

MDB
  • 27
  • 4
  • 2
    You are expected to do the work and come here with *specific* questions. [ask] – glenn jackman Jan 13 '16 at 00:13
  • I apologize for the broad wording and large background. I edited my question. – MDB Jan 13 '16 at 22:50
  • where exactly are you stuck? Do you know how to read the file? do you have any thoughts about how you might find the difference in minutes between those two times? (hint, you can express HH:MM as minutes since midnight) I could feed you an answer, but you haven't given any indication that you have tried to figure it out. – glenn jackman Jan 13 '16 at 23:47

1 Answers1

2

I'd suggest looking into IO#readlines for reading the content of your text file.

Once you're able to work with each line, you can use String#split to obtain the information that you're looking to grab like so:

"Work  Bertha 06:30 11:12".split(' ')
#=> ["Work", "Bertha", "06:30", "11:12"]

Then you need the elapsed time between 2 time points. Ruby has time objects that can parse time from a string, but will set the date as the current date, which shouldn't matter for what you're working on.

require 'time'
employee = ["Work", "Bertha", "06:30", "11:12"]
clock_in  = Time.parse(employee[2])
#=> 2016-01-12 06:30:00 -0600
clock_out = Time.parse(employee[3])
#=> 2016-01-12 11:12:00 -0600

Time objects can be subtracted to find the change in time between the two. It'll return the time in seconds. You can add up all the seconds from times someone clocked in/out

clock_out - clock_in
#=> 16920.0

There are many ways to make this more of a human readable time format. I suggest starting here

Community
  • 1
  • 1
Travis
  • 13,311
  • 4
  • 26
  • 40
  • Thank you for all of your help. Your links helped me tremendously, and I edited the example in my original post. – MDB Jan 13 '16 at 22:51