1. Time Calculation
Hi, I'm working on an application for my wife's work, and I need to
calculate how long a worker has worked, given a start time, and an end time.
Each start/end time is in a sequence like this:
{Hour(1-12), minute(0-59), second(0-59), AM/PM (0/1)}
I need to be able to calculate the number of seconds from the start time to
the end time, but the AM/PM thing is realy confusing to program with.
any help would be REALY appreciated!
Thanks,
Mark / Liquid-Nitrogen.
2. Re: Time Calculation
Hi Mark,
Why don't you use the European format; I mean you could program:
if AMPM = 1 then -- if it's afternoon, add 12 to hours
hours += 12
end if
Only problem I see, is for working night shifts; a day number should be
added then.
Hope this helps,
Ad
----- Oorspronkelijk bericht -----
Van: Liquid-Nitrogen Software <nitrogen_069 at HOTMAIL.COM>
Aan: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Verzonden: zaterdag 27 november 1999 11:23
Onderwerp: Time Calculation
> Hi, I'm working on an application for my wife's work, and I need to
> calculate how long a worker has worked, given a start time, and an end
time.
> Each start/end time is in a sequence like this:
>
> {Hour(1-12), minute(0-59), second(0-59), AM/PM (0/1)}
>
> I need to be able to calculate the number of seconds from the start time
to
> the end time, but the AM/PM thing is realy confusing to program with.
>
> any help would be REALY appreciated!
>
> Thanks,
> Mark / Liquid-Nitrogen.
3. Re: Time Calculation
- Posted by nieuwen at XS4ALL.NL
Nov 27, 1999
> Hi, I'm working on an application for my wife's work, and I need to
> calculate how long a worker has worked, given a start time, and an end time.
> Each start/end time is in a sequence like this:
>
> {Hour(1-12), minute(0-59), second(0-59), AM/PM (0/1)}
>
> I need to be able to calculate the number of seconds from the start time to
> the end time, but the AM/PM thing is realy confusing to program with.
Well, take a look at this:
-- This code assumes nobody works more than 23 hours and 59 minutes and 59
seconds
integer shour, smin, ssec, sampm
integer ehour, emin, esec, eampm
-- give those var's some values.
[ some input routines here ]
-- Get rid of the AMPM thing
if eampm != sampm then
ehour = ehour + 12
end if
integer totals, totale
totals = ((shour * 60) + smin)*60
totale = ((ehour * 60) + emin)*60
-- Print the difference (the length of the work time) in seconds
? totale-totals
-- You're welcome.
>
> any help would be REALY appreciated!
>
> Thanks,
> Mark / Liquid-Nitrogen.
>** --------- End Original Message ----------- **
>
Ralf Nieuwenhuijsen
[[ Email ]]
nieuwen at xs4all.nl
ralf_n at email.com
[[ I-Seek-You ]]
UIN: 9389920
[[ The Elevator ]]
http://www.xs4all.nl/~nieuwen
Download NeoPlanet at http://www.neoplanet.com
4. Re: Time Calculation
On Sun, 28 Nov 1999 00:00:44 -0500, Ad Rienks <kwibus at ZONNET.NL>
wrote:
>On Sat, 27 Nov 1999 05:23:00 -0500, Liquid-Nitrogen Software <nitrogen_069 at
>HOTMAIL.COM> wrote:
>>Hi, I'm working on an application for my wife's work, and I need to
>>calculate how long a worker has worked, given a start time, and an end time.
>>Each start/end time is in a sequence like this:
>>{Hour(1-12), minute(0-59), second(0-59), AM/PM (0/1)}
>>I need to be able to calculate the number of seconds from the start time to
>>the end time, but the AM/PM thing is realy confusing to program with.
>>any help would be REALY appreciated!
>Why don't you use the European format; I mean you could program:
>if AMPM = 1 then -- if it's afternoon, add 12 to hours
> hours += 12
>end if
>Only problem I see, is for working night shifts; a day number should be
>added then.
Actually, you'd be better off if you simply check to see if the
AM/PM fields in both records (start and finish) are the same or
not:
:If start[AMPM] != end[AMPM] then -- I always forget whether it's != or <>
: end[HOURS] += 12
:end if
This handles night shifts with no problem.
If _very_ long shifts are a possibility (e.g., start at 10AM,
finish at 2AM next morning), you'll also want to check to see if
the end time is earlier than the start time, and if they are,
swap them, calculate the difference, and then subtract that
difference from 24 hours. That will cover everything except a
shift that's 24 hours or longer.
--
Jeff Zeitlin
jzeitlin at cyburban.com
5. Re: Time Calculation
----- Original Message -----
From: Jeff Zeitlin <jzeitlin at CYBURBAN.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, November 28, 1999 12:38 AM
Subject: Re: Time Calculation
> If _very_ long shifts are a possibility (e.g., start at 10AM,
> finish at 2AM next morning), you'll also want to check to see if
> the end time is earlier than the start time, and if they are,
> swap them, calculate the difference, and then subtract that
> difference from 24 hours. That will cover everything except a
> shift that's 24 hours or longer.
This would work best if you logged the ticks from start of something, rather
than the calendar/clock time. Maybe Rob could put in a function to convert a
date/time to ticks_since_epoch, and back again. That way you can convert the
start time to ticks, then the end time, subtract, and you're done, with no
errors.
; given a start time of 943811806
; echo -s $ctime
943813806
; echo -s $duration($calc(943813806 - 943811806))
33mins 20secs
; given a start time of 943201806
; echo -s $ctime
943813806
; echo -s $duration($calc(943813806 - 943201806))
1wk 2hrs
; given a start time of 943201806
; echo -s $ctime
943813806
; echo -s $duration($calc(943813806 - 943201806))
15hrs 16mins 40secs
Kat,
musing
6. Re: Time Calculation
Thanks for everyone's help! here's what i managed to come up with if
anyone's interested. It works for any time values.
--t1 and t2 are: {hours, minutes, seconds, 0/1 (Am/Pm)}
--returns the number of hours between t1 and t2
function time_difference(sequence t1, sequence t2)
integer shour, smin, ssec, sampm
integer ehour, emin, esec, eampm
atom totals, totale
-- give those var's some values.
shour = t1[1]
smin = t1[2]
ssec = t1[3]
sampm = t1[4]
ehour = t2[1]
emin = t2[2]
esec = t2[3]
eampm = t2[4]
--swap am/pm with 12 hours so it'll add up right.
if ehour = 12 then
eampm = (not eampm)
end if
if shour = 12 then
sampm = (not sampm)
end if
-- Get rid of the AMPM thing
if eampm != sampm then
ehour = ehour + 12
end if
--convert to seconds and add them all up
totals = ((shour * 60) + smin)*60 + ssec
totale = ((ehour * 60) + emin)*60 + esec
--convert to hours
totals = totals / 60 / 60
totale = totale / 60 / 60
if totale = totals then -- start and finish at the same time
return 24
elsif totals < totale then --start time and end time on same day.
totale = totale-totals
return totale
else --start time and end times are on different days
totale = (24-totals) + totale
return totale
end if
end function
--Mark
--Liquid-Nitrogen
7. Re: Time Calculation
On Mon, 29 Nov 1999 00:01:17 -0500, Kat <KSMiTH at PELL.NET> wrote:
>----- Original Message -----
>From: Jeff Zeitlin <jzeitlin at CYBURBAN.COM>
>To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
>Sent: Sunday, November 28, 1999 12:38 AM
>Subject: Re: Time Calculation
>> If _very_ long shifts are a possibility (e.g., start at 10AM,
>> finish at 2AM next morning), you'll also want to check to see if
>> the end time is earlier than the start time, and if they are,
>> swap them, calculate the difference, and then subtract that
>> difference from 24 hours. That will cover everything except a
>> shift that's 24 hours or longer.
>This would work best if you logged the ticks from start of something, rather
>than the calendar/clock time. Maybe Rob could put in a function to convert a
>date/time to ticks_since_epoch, and back again. That way you can convert the
>start time to ticks, then the end time, subtract, and you're done, with no
>errors.
You're absolutely correct, in this - but I'm not aware of any U4
function to convert to epochal ticks, and I was presuming the
worst-case scenario for the incoming data, that its format was
fixed as given, which meant that the date wasn't available.
--
Jeff Zeitlin
jzeitlin at cyburban.com