## convert FETCH history time entry into hours minutes seconds

Solved a problem in an elegant manner and want to show off your code? Know a hard-to-find feature? Post it here for the benefit of others. Questions don't belong here.
steve
Posts: 397
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 83 times

### convert FETCH history time entry into hours minutes seconds

When retrieving the date and time of an entry in the History using the FETCH command, the time value returned is in "seconds elapsed since midnight".
Converting this into hh:mm:ss was not as straightforward as I would think due to inconsistent typecasting of variables and odd handing of the ROUND command.
The code I ended up with is below, which gets the TIME OF DAY of the first entry in the case history

Code: Select all

``````timestamp = FETCH( "CONTENT" , "History" , "created_time" , "FOR FIRST History NO-LOCK WHERE History.case_no = '" + {case.key} + "'" )
#MESSAGE( "timestamp: " , timestamp )
#A=number of hours
A = timestamp / 3600
#B = minutes as fraction of hours
B = A MOD 1
#typecaset B as decimal
PUT( B , {temp decimal.Value} )
#C = whole hours
C = A - {temp decimal.Value}
myhours = C
#MESSAGE( "myhours " , myhours )
#D = remainder as minutes
D = B * 60
#E = seconds as fraction of minutes
E = D MOD 1
#typecast
PUT( E , {temp decimal.Value} )
#whole minutes
F = D - {temp decimal.Value}
mymins = F
#MESSAGE( "mymins " , mymins )
#G = remainder as seconds
G = E * 60
#decimal seconds
H = G MOD 1
#typecast
PUT( H , {temp decimal.Value} )
#whole seconds
I = G - {temp decimal.Value}
myseconds = I
#MESSAGE ("myseconds ",myseconds)
#construct time in H:M:S
#TODO - pad with zeros
mytime = TEXT( myhours ) + ":" + TEXT( mymins ) + ":" + TEXT( myseconds )
MESSAGE( mytime )
result = mytime
``````

Error in ROUND command:

Code: Select all

``````#11:54:12 = 42852 seconds
timestamp = 42852
#convert into decimal hours - should be 11.9033
A = timestamp / 3600
#round down to whole hours - shoud be 11
B = ROUND( A , 0 , DOWN )
#result should be 11, but is actually 12!
MESSAGE( B ) ``````

steve
Posts: 397
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 83 times

### Re: convert FETCH history time entry into hours minutes seco

nb the syntax of the ROUND command is the issue here - in the Keyword example in Proclaim maths it is not defined, but it should be used as
ROUND( variable , <decimalplaces> , <method>)
where <method>:
-1 = round down next digit
0 = if(digit >=5), round up, else round down
1 = round up to next digit
This above example code can therefore be optimised if you wish

steve
Posts: 397
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 83 times

### Re: convert FETCH history time entry into hours minutes seco

ROUND command is exceedingly picky about syntax and format
it MUST be input with spaces surrounding each parameter, namely:
ROUND( variable , decimalplaces , method)
ROUND([space][variable][space][comma][space][decimalplaces][space][comma][space][method][space])

[method] is as before:
-1 = round down next digit
0 = if(digit >=5), round up, else round down
1 = round up to next digit

If this layout is not followed, then the code will compile, but be corrupted upon compilation and not save. The danger is that you click ACCEPT without first CHECKing your code, and this will be saved with the corruption and no error.