Finding time differences
Author |
Message |
Aziz
|
Posted: Tue Jul 18, 2006 2:34 pm Post subject: Finding time differences |
|
|
Say, for example, you want to input times and find the duration.
such as an event starts at 4:30 and ends at 9:15. I know there are a few ways to do this, I was thinking which was the most effecient.
You could convert the time to hours, such a 4:30 = 4.5, or 9:15 = 9.25. I imagine you could also sutract the hours, and then subtract the minutes, or convert the minutes to base 60.... haven't gone through all the possibilites, but I was just sparking a discussion from my thought. What would not only be the most effecient but the simplest way? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Tue Jul 18, 2006 3:19 pm Post subject: (No subject) |
|
|
I think the easiest is like this:
Find the difference in hours:
9 - 4 = 5
Find the difference in minutes:
15 - 30 = -15
Combine them:
(5hours) + -15minutes
= (4hours + 60minutes) + -15minutes
= 4hours 45minutes |
|
|
|
|
|
Aziz
|
Posted: Tue Jul 18, 2006 9:32 pm Post subject: (No subject) |
|
|
Cervantes wrote: Combine them:
(5hours) + -15minutes
= (4hours + 60minutes) + -15minutes
= 4hours 45minutes
But what if the minutes is positive, like this example:
9:30 -> 11:50 (I'm going to work through it in this post, tell me if I'm not seeing something here)
11-9 = 2
50-30 = 20
= (2h) + (20min)
= (2h - 1h) + (20min + 60min)
= (1h) + (80min)
and then do something along the lines of:
hours = (1h) + (80min) / 60 (the division is truncated)
minutes = remainder of (80min) / 60
so in code, perhaps:
code: | hours = time2.hour - time1.hour
mins = time2.min - time1.min
hours--
mins += 60
hours = hours + (int) (mins / 60)
mins = mins % 60
timeStr = hours + "h " + mins + "min" |
I'm not sure how truncated division is done in anything but Turing, but I think (without trying...okay i tried it works). Hmm... any flaws in that? I think I know I just worked this out myself, but I have another question: What if the user enters a time that is into the next day? such as 20:30 -> 1:50? I suppose you would ask the user to either enter it as 25:50 or ask if it spans into the next day (or how many days) and add 24*number of days it spans? |
|
|
|
|
|
Cervantes
|
Posted: Tue Jul 18, 2006 10:32 pm Post subject: (No subject) |
|
|
Aziz wrote:
But what if the minutes is positive, like this example:
9:30 -> 11:50 (I'm going to work through it in this post, tell me if I'm not seeing something here)
11-9 = 2
50-30 = 20
= (2h) + (20min)
= (2h - 1h) + (20min + 60min)
= (1h) + (80min)
Stop right there.
Back up.
= (2h) + (20min)
That's it. You stop there. |
|
|
|
|
|
r.3volved
|
Posted: Tue Jul 18, 2006 10:39 pm Post subject: (No subject) |
|
|
what language are you using??
why not do a datetime subtraction...look in the documentation for the particular language |
|
|
|
|
|
Aziz
|
Posted: Wed Jul 19, 2006 8:48 am Post subject: (No subject) |
|
|
Cervantes wrote: Stop right there.
Back up.
= (2h) + (20min)
That's it. You stop there.
Yes, I realized that But I was trying to get a general equation, that would work for everything.
r.3volved wrote: what language are you using??
why not do a datetime subtraction...look in the documentation for the particular language
I'm going to be doing this in Java. I already made a small proggy in Turing that accepts time durations from the user, then adds the up and gets $/hour and % deductions then spits out there paystub. I did it because it was easier than adding all my hours together. I usually just rounded before, but now I'm suspicious of some of the shift managers at work messing around with my punch in/out times so I write down the exact times I punch in/out.
I plan on arranging it like this:
Having a JList and then Punch In and Punch Out buttons. Which will pop up a dialog with two spinners and a check box asking if it expands into the next day. The two in/out buttons will only be enabled when applicable (eg punch in is enabled to start, then when you type in a punch in time, the punch out becomes available) I think then a view textboxes for the date and either textboxes for your wage/h and deductions or have a "configuration" panel where you save them. I'm not sure what layout I'm going to use but I will check out datetime subtraction in Java. Thanks. anymore question I'll bring to the Java forum |
|
|
|
|
|
r.3volved
|
|
|
|
|
Cervantes
|
Posted: Wed Jul 19, 2006 11:50 am Post subject: (No subject) |
|
|
Aziz wrote:
Yes, I realized that But I was trying to get a general equation, that would work for everything.
You'd just have an if statement to determine if the minutes are negative. If so, you perform the additional operations.
This could be expanded, of course, to include more than just minutes and hours. Your if statement for the negative minutes could be reused (not copy-and paste; the same code) for all the other measurements. All you need to know is how many minutes in an hour, hours in a day, days in a year, etc. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|