While working on my back testing scripts I came to a need to convert the time quite often.

Luckily, there is an (undocumented) feature that allows me to do it easily. As You probably know, to get the current time you can use the getsecond() function that will leave you with the time in seconds since midnight format, but sometimes we need just the minutes or the seconds in our scripts so here is the easy way to extract those as numbers.
$TIME=getsecond();
$seconds=$TIME%60;
$minutes=($TIME-$TIME%60)/60%60;
$hours=($TIME-$TIME%3600)/3600%24;
To make it a function we just need to make sure we set the TIME variable before doing the math with the seconds minutes and hours.
Explanation of the syntax
The %60 and %24 are the interesting parts here. It wraps around the numbers to the specified number.
% 60
→ keeps minutes and seconds within 0–59
% 24
→ keeps hours within 0–23
Simplified Example
Without % 24
If getsecond()
just keeps increasing forever, dividing by 3600 gives you the total hours since the start.
getsecond() = 90000
→ that is 90,000 seconds.90000/3600 = 25
→ total hours passed.
But 25 hours does not make sense on a 24-hour clock.
With % 24
(90000 / 3600) % 24 = 25 % 24 = 1
So the time is 01:00:00 on the next day.
The problem is that in DAS the resulting number is a decimal so a few more math is needed
When you do:
$minutes = getsecond() / 60
If
getsecond()
is not a multiple of 60, dividing produces a decimal
(e.g.3725/60=62.0833
).We want the whole number of minutes, not the fraction.
The subtraction trick
Instead of dividing directly, we can do:
$minutes = (getsecond()-getsecond()%60)/60
Example with getsecond()=3725
:
getsecond()%60=3725%60=5
→ the extra leftover seconds.getsecond()-(getsecond()%60)=3725-5=3720
→ now it is an exact multiple of 60.3720/60=62
→ clean number, with no decimals.
For techniques how to extract it from strings please read my previous posts