CalcTime function ?

cwatrelot
Member Posts: 14
We want to display the elapsed time spend to create a report.
For example Started at 9:10:00, completed at 9:15:40 = Elapsed 5'40", or Started 23:55:00 and completed next day at 0:10:30 = Elapsed 15'30". C/Side can compute Date2 - Date1 but not Time2 - Time1.
Another function should be used to add times, for example one task need a process time of 5'45" minutes, second task is 3'20", total time is 9'05". Result can't be of time type because it can be more than 24 hours.
For example Started at 9:10:00, completed at 9:15:40 = Elapsed 5'40", or Started 23:55:00 and completed next day at 0:10:30 = Elapsed 15'30". C/Side can compute Date2 - Date1 but not Time2 - Time1.
Another function should be used to add times, for example one task need a process time of 5'45" minutes, second task is 3'20", total time is 9'05". Result can't be of time type because it can be more than 24 hours.
0
Comments
-
I'm afraid I don't understand your question because why wouldn't following example fit your needs :
Time1 and Time2 defined as Time
Diff defined as Integer
Diff:=Time2-Time1 gives how many milliseconds you have between Time1 and Time2... You just to write a function to convert this result in hh::mm::ss format.
Let me know if I was wrong about your problem.fhh0 -
C/AL can compute Time2 - Time1 but Time1 + Time2 is an error.
And what about Time1 before midnight and Time2 on the next day.
We expect a function as Elapsed(22/08/00D,23:50:10T,23/08/00D,01:10:35T) to return a formatted text like 1h20'25"
Surely we can write this function but perhaps there is a better C/AL solution (with Format ?), or someone who have already write it !0 -
Create a Function pass in the 4 values and Try this Code!
IF (FromDate=0D)OR(ToDate=0D)OR(FromTime=0T)OR(ToTime=0T) THEN
EXIT;
IF (FromDate=ToDate)AND(FromTime>ToTime) THEN
EXIT;
IF FromDate>ToDate THEN
EXIT;
// Caculate the Duration in Whole Minutes
Minutes:=((ToDate-FromDate)*1440)+ROUND((ToTime-FromTime)/60000,1);
// Caculate the Days
// Days need to be Caculated like this for spans of
// Monday 11:00 pm to Tuesday 01:00am (2 hours) days = 0 not 1
Days := (Minutes DIV 1440);
// Caculate the Remaining Hours
Hours := (Minutes MOD 1440) DIV 60;
// Caculate the Remaining Minutes
Minutes := (Minutes MOD 60);
Note:
For yor Question you do not require the No of Days Just Hours
So you would use
Hours := Minutes DIV 60;
Minutes := (Minutes MOD 60);
Duration := Hours + (Minutes/100);
The Minutes will be the No of Minutes not the Decimal
IE: 30 Minutes not 0.30 of an hour
This will give you a decimal 1.30 for 1 and a half hours
Hope this helps!
MindSource (UK) Limited
Navision Service Partner
david@mindsource.co.uk
info@mindsource.co.uk
[This message has been edited by Dave Cox (edited 23-08-2000).]
[This message has been edited by Dave Cox (edited 23-08-2000).]
[This message has been edited by Dave Cox (edited 23-08-2000).]0 -
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by cwatrelot:
C/AL can compute Time2 - Time1 but Time1 + Time2 is an error.
<HR></BLOCKQUOTE>
I recommend to convert time into decimal, do the calculation and later convert the result back into time:
Internally, Navision stores time as Integer in milliseconds.
09:00 AM is being stored as
9 * 3600 * 1000 = 9 * 3600000 = 32400000
A direct conversion from Time to decimal in the style
"Decimalvalue := Timevalue / 3600000"
is not possible.
However it is generally possible to add time-values in order to get the decimalvalue. Such as:
Decimalvalue := 0900T + 0T
theretically this should give the desired result 32400000
But this approach also fails because it is not possible to use the uninitialized time-value 0T in a calculation.
To circumvent this, I'm using the following trick: Add one Second to the Time-Value to get the Decimal-Value and then subtract one second from the Decimal Value:
DecimalValue := 0900T + 000001T // 32401000
Now we subtract 1000 Milliseconds ---
DecimalValue := 32401000 - 1000; // 32400000
Procedure TimeToDecimal (T : Time) : Decimal;
// 09:45 (Time) --> 9.75 (Decimal)
Begin
IF T = 0T THEN
EXIT(0.0);
d := T- 000001T + 1000.0;
EXIT(d / 3600000);
END;
Procedure DecimalToTime(d : Decimal) : Time;
Begin
IF d = 0.0 THEN
EXIT(0T)
ELSE
EXIT(000001T + (ROUND((d * 3600000 - 1000),1)));
End;
Marcus
Marcus Fabian
m.fabian@thenet.ch
+41 79 439 78 72With best regards from Switzerland
Marcus Fabian0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions