Time taken algorithm

B

Ben Cameron

Hi
I'm writing a survey tool package and have to work out how long an
interview takes.
I'm having trouble working out how to work out the time taken between
a start time and end time.
I know in most cases if you subtrack the start time from the end time
t leaves you with the time taken. However consider st = 9.55 end time
= 10.05 tt = 10min but if I subtract them I'll get 1.10m.

Can anyone help me with a algorithm to do this?
 
D

David Hilsee

Ben Cameron said:
Hi
I'm writing a survey tool package and have to work out how long an
interview takes.
I'm having trouble working out how to work out the time taken between
a start time and end time.
I know in most cases if you subtrack the start time from the end time
t leaves you with the time taken. However consider st = 9.55 end time
= 10.05 tt = 10min but if I subtract them I'll get 1.10m.

Can anyone help me with a algorithm to do this?

What do you mean? 10 hours, 5 minutes minus 9 hours, 55 minutes is 10
minutes. If you have a bug in your code that instead determines an answer
of 1 hour and 10 minutes, post it, and maybe someone can help you.
 
B

Brian Palmer

Hi
I'm writing a survey tool package and have to work out how long an
interview takes.
I'm having trouble working out how to work out the time taken between
a start time and end time.
I know in most cases if you subtrack the start time from the end time
t leaves you with the time taken. However consider st = 9.55 end time
= 10.05 tt = 10min but if I subtract them I'll get 1.10m.

Can anyone help me with a algorithm to do this?

The trouble is that hours:minutes works on 24:60 bases, instead of
100:100. Convert into units that don't do that... for example, into
seconds. If you don't have to worry about straddling midnight, convert
them into seconds since midnight... if you do, convert them into
seconds since epoch (or some other known time).
 
D

Damian Carrillo

Hi
I'm writing a survey tool package and have to work out how long an
interview takes.
I'm having trouble working out how to work out the time taken between
a start time and end time.
I know in most cases if you subtrack the start time from the end time
t leaves you with the time taken. However consider st = 9.55 end time
= 10.05 tt = 10min but if I subtract them I'll get 1.10m.

Can anyone help me with a algorithm to do this?

Why don't you create a Time class where a time is represented by hours
and minutes instead of using doubles? That would be the logical thing
to do, because in abstract 9.55 does not equal "nine fifty-five". It
represents (9/24)hrs and (55/60)mins and you would probably be best off
to treat it as such. So in your new class, have two variables, minutes
and hours and initialize them when you create a new time, ala:

class Time {
static final MINS_IN_HOUR = 60;

int hours = 0;
int minutes = 0;
int hourFormat = 0;

public Time(int hours, int minutes, int hourFormat) {
this.hours = hours;
this.minutes = minutes;
this.hourFormat = hourFormat // 12 or 24 hour format
}

public Time subtract(Time subtrahend) {
int hourOffset = 0;
int newHours = 0;
int newMinutes = 0;

hourOffset = (this.minutes < subtrahend.minutes) ? 0 : 1;
newMinutes = (MINS_IN_HOUR - this.minutes) + subtrahend.minutes;
newHours = subtrahend.hours - this.hours - hourOffset;

// Account for the case that the newMinutes add up to 60 ie. 5:20-6:40
if (newMinutes = MINS_IN_HOUR) {
newMinutes = 0;
newHours += 1;
}

return new Time(newHours, newMinutes;
}

public void toString() {
String hourFormatLabel = "";
if (hourFormat == 12) {
if (this.hours > 11 && this.hours < 24)
hourFormatLabel = "pm";
else
hourFormatLabel = "am";

return (this.hours % hoursFormat + ":" + this.minutes +
" " + hourFormatLabel);
}
}

I don't know if that'll work off-hand because I coded it off the top of my
grey matter, but I think that's what you're looking for.

Usage:
-------------------------------------------------
int militaryTime = 24;
Time st = new Time(9, 55, militaryTime); // 9:55
Time et = new Time(10, 5, militaryTime); // 10:05
Time tt = st.subtract(et);
-------------------------------------------------
or
-------------------------------------------------
int standard = 12;
Time st = new Time(9, 55, standard); // 9:55
Time et = new Time(10, 5, standard); // 10:05
Time tt = st.subtract(et);
-------------------------------------------------

So what this class does for you is give you a way to represent a time and
a way to represent a difference between times, because the two are
different. For example, 1hr 10mins is not the same as 1:10pm, but they
can both be represented with this class. Hope that helps you!
 
P

P.Hill

Ben said:
Hi
I'm writing a survey tool package and have to work out how long an
interview takes.
I'm having trouble working out how to work out the time taken between
a start time and end time.
I know in most cases if you subtrack the start time from the end time
t leaves you with the time taken. However consider st = 9.55 end time
= 10.05 tt = 10min but if I subtract them I'll get 1.10m.

Can anyone help me with a algorithm to do this?

Since you say you have the times you must have a java.util.Date which is
binary representation of a time right? If you don't have the a Date, I would
just convert the time into a total number seconds or milliseconds (as you get
from, then just subtract the millisecond time.

There are no object in the standard libraries which represent a delta time.

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top