Author Topic: Code for doing math with military time?  (Read 1629 times)

Hi, I can't seem to figure out how to do math with military time.

Code: [Select]
function AddTime(%time1,%time2)
{
%time1 = strReplace(%time1,":","");
%time2 = strReplace(%time2,":","");
%t1m = strLen(%time1) > 3 ? getSubStr(%time1,2,2) : getSubStr(%time1,1,2);
%t2m = strLen(%time2) > 3 ? getSubStr(%time2,2,2) : getSubStr(%time2,1,2);
%t1h = strLen(%time1) > 3 ? getSubStr(%time1,0,2) : getSubStr(%time1,0,1);
%t2h = strlen(%time2) > 3 ? getSubStr(%time2,0,2) : getSubStr(%time2,0,1);
if(%t1m + %t2m > 60)
{
%tm = (%t1m + %t2m) % 60;
%ah = mFloor((%t1m + %t2m) / 60);
}
else
{
%tm = %t1m + %t2m;
}

if(%t1h + %t2h > 23)
{
%th = ((%t1h + %t2h) % 23) + %ah;
}
else
{
%th = (%t1h + %t2h) + %ah;
}

if(%tm == 60)
{
%tm = "00";
%th++;
}
if(strLen(%tm) == 1)
%tm = "0" @ %tm;
if(strLen(%th) == 1)
%th = "0" @ %th;
if(%th @ %tm > 2400)
{
%time = %th @ %tm + (%th @ %tm) % 2400;
}
else
{
%time = %th @ %tm;
}

return %time;
}

This is my code for adding two times. It's not perfect, it doesn't work completely, it can probably be simplified or rewritten, but this is what I've got going on right now. Does anyone have a better way to handle this?

A common pitfall of datetime values is thinking of them as string values, rather than numeric values.

If you can find/write a function to convert hh:mm:ss to a single integer representing number of seconds, you can add them with regular math, then use getTimeString(str) to convert back to hh:mm:ss
« Last Edit: August 11, 2015, 11:54:40 PM by Headcrab Zombie »

A common pitfall of datetime values is thinking of them as string values, rather than numeric values.

If you can find/write a function to convert hh:mm:ss to a single integer representing number of seconds, you can add them with regular math, then use getTimeString(str) to convert back to hh:mm:ss

Code: [Select]
function ConvertTime(%h,%m)
{
%time = 0;
%time += mFloor(%h*60);
%time += %m;
return %time;
}

function UnconvertTime(%time,%modm)
{
%h = mFloor(%time / 60);
%m = strLen(%time % 60) == 1 ? "0" @ %time % 60 : %time % 60;
if(%time % 24 == 0 && %time > 1439)
%h = mFloor(%h/24)-1;
%rh = strLen(mFloor(%h / 24) + %h % 24) == 1 ? "0" @ mFloor(%h / 24) + %h % 24 : mFloor(%h / 24) + %h % 24;
return %rh @ ":" @ %m;
}
Code: [Select]
==>echo(convertTime(24,00));
1440
==>echo(unconvertTime(1440));
00:00
==>echo(convertTime(24,01));
1441
==>echo(unconvertTime(1441));

This is the best I could come up with. ConvertTime() turns hours and minutes into a value, UnconvertTime() turns that values back into hours and minutes.

I'm having a bit of a problem for when UnconvertTime() returns 00:00 (where the argument would be 1440). If I add another second (1441) it turns into 01:01, I'm not sure how to fix this.

Why would you have convertTime take an incorrect time and convert it into seconds? 24:00 doesn't exist in military time... unless you're converting the actual time interval.

EDIT:
As for UnconvertTime, it's because your special case if statement is imperfect and needlessly complicated. Just have it continuously subtract 1440 from %time till %time is < 1440 before doing any of your conversion calculations.
« Last Edit: August 12, 2015, 08:57:26 AM by Conan »

UnconvertTime() turns that values back into hours and minutes.

Why?
use getTimeString(str) to convert back to hh:mm:ss
Don't reinvent the wheel
If you don't want seconds then just cut off after the last colon.

Also, if you want something more versatile, make converttime accept "hh:mm" and split it into hours and minutes within the function instead of requiring it to already be split.

continuously subtract 1440 from %time till %time is < 1440 .
Never do this, this is what the modulus operator is for
%thing = %time % 1440;
Of course he doesn't need either, he just needs the already existing function
« Last Edit: August 12, 2015, 09:08:42 AM by Headcrab Zombie »

EDIT:
As for UnconvertTime, it's because your special case if statement is imperfect and needlessly complicated. Just have it continuously subtract 1440 from %time till %time is < 1440 before doing any of your conversion calculations.

Tried that, didn't fix the 01:00 error.

Don't reinvent the wheel
If you don't want seconds then just cut off after the last colon.

Also, if you want something more versatile, make concert time accept "hh:mm" and split it into hours and minutes within the function

Oh, I've never seen that function before.

Never do this, this is what the modulus operator is for
%thing = %time % 1440;
Of course he doesn't need either, he just needs the already existing function

I managed to get the code working anyways, like I said, I've never seen getTimeString() before. I'll see what I can do with it now.

Code: [Select]
function ConvertTime(%h,%m)
{
%time = 0;
%time += mFloor(%h*60);
%time += %m;
return %time;
}

function UnconvertTime(%time,%modm)
{
%time = %time % 1440;
%h = mFloor(%time / 60);
%m = strLen(%time % 60) == 1 ? "0" @ %time % 60 : %time % 60;
echo(%time);
%rh = strLen(mFloor(%h / 24) + %h % 24) == 1 ? "0" @ mFloor(%h / 24) + %h % 24 : mFloor(%h / 24) + %h % 24;
return %rh @ ":" @ %m;
}

%thing = %time % 1440;
wow how did i overlook that kek thanks for correcting me.

wow how did i overlook that kek thanks for correcting me.

Its worth pointing out that the modulus operator only works for integers. Use this if you have floats:

Code: [Select]
function mMod(%num, %a) { //Supports floats unlike %
return %num - mFloor(%num / %a) * %a;
}

A common pitfall of datetime values is thinking of them as string values, rather than numeric values.
oops

oops
Code: [Select]
// (!%year % 4) wasn't working. why? i don't knowBecause if %year = 2015, !%year = 0. so (!%year % 4) is (0 % 4).
(!(%year % 4)) would be correct.