Average of times
[1/4] from: ml::sproingle::com at: 3-May-2004 9:30
I have a series of times which represents session durations of users.
They are in the format dd:hh:mm
What I want to figure out is how long the average session takes.
Do I have to parse out the days and hours into minutes and then work out the average
that way and then convert the result back into dd:hh:mm or does someone know of a better
way?
Stuart
[2/4] from: antonr:lexicon at: 4-May-2004 0:23
Let b be a block containing your session times.
>> b: [1:00:21 2:00 3:30]
== [1:00:21 2:00 3:30]
>> t: 0:0 ; total
== 0:00
>> foreach time b [t: t + time]
== 6:30:21
>> t / length? b
== 2:10:07
Anton.
[3/4] from: SunandaDH:aol at: 3-May-2004 14:47
Anton:
> Let b be a block containing your session times.
That works if the durations are hours:minutes:seconds -- you are using
REBOL's standard date arithmetic.
But, if I read Stuart's question right, he has days:hours:minutes. They don't
add right in the same way:
1:12:00 + 1:12:00 should be 3:00:00 in his example (1 day & 12 hours, twice)
In REBOL, 1:12:00 + 1:12:00 = 2:24
Here's a couple of functions that misuse the date format to treat it as d:h:m
for addition.
The result is in hours:minutes -- you'll need a bit more arithmetic to
convert that to days:hours:minutes -- after dividing to get the average, that is.
make-hm: func [d1 [time!] ][
return to-time join d1/minute [":" d1/second ":00"]
]
add-dhm: func [d1 [time!] d2 [time!] ][
return (make-hm d1)
+ (make-hm d2)
+ (24 * 3600 * to-time (d1/hour + d2/hour))
]
;; examples:
add-dhm 1:12:0 1:12:0 ;; 1.5 days + 1.5 days
== 72:00 ;; seventy-two hours = three days
add-dhm 0:30:30 0:30:30
== 61:00
add-dhm 2:1:45 0:0:15
== 50:00
Sunanda.
Sunanda
[4/4] from: greggirwin:mindspring at: 3-May-2004 15:44
Hi Stuart,
M> I have a series of times which represents session durations of users.
M> They are in the format dd:hh:mm
M> What I want to figure out is how long the average session takes.
This is a good example of something that could be done many different
ways in REBOL (like a lot of things :). Here's what came into my head.
make-dhm-time: func [time /local ms] [
ms: time // 24:00
to time! reduce [to integer! time / 24:00 ms/hour ms/minute]
]
avg-dhm-times: func [block /local result] [
result: 0:0:0
foreach t block [
result: add result to time! reduce [
t/hour * 24 + t/minute
to integer! t/second
]
]
make-dhm-time result / length? block
]
-- Gregg