PDA

View Full Version : Shortest possible way to write this function



The Mayor
08-16-2014, 06:49 AM
I have a question for you smart people out there. I want to know the shortest/best possible way to write this function.

The purpose of this function is to return the difference in time between the current system time, and another time I input. The time I input (myTime) will look like:

[hours, minutes, seconds, milliseconds]

I can return the current system time (timeNow) in the same format. I want to return the difference between these two times as a string (e.g., 2h 23m 2s 456ms).

For example:

myTime := [19, 30, 15, 0];
timeNow := [18, 30, 15, 0];

would return ‘1h 0m 0s 0ms’ (because 7:30 PM is one hour from the current time, 6:30 PM) and

myTime := [19, 30, 10, 0];
timeNow := [20, 25, 5, 0];

would return '23h 5m 5s 0ms' (Because 7:30:10 PM has already passed, the next time it will occur is 23 hours from now).


function getTime(): array [0..3] of word;
begin
decodeTime(now(), result[0], result[1], result[2], result[3]);
end;

function differenceInTime(): string;
var
mytime, timeNow: array [0..3] of integer;
begin
myTime := [18, 30, 15, 0]; // [hour, min, sec, ms]
timeNow := getTime();

//halp

result := 'Xh ' + 'Xm ' + 'Xs ' + 'Xms';
end;

tls
08-16-2014, 08:47 AM
Just convert to milliseconds and do the math there.

riwu
08-16-2014, 09:24 AM
I have a question for you smart people out there. I want to know the shortest/best possible way to write this function.

The purpose of this function is to return the difference in time between the current system time, and another time I input. The time I input (myTime) will look like:

[hours, minutes, seconds, milliseconds]

I can return the current system time (timeNow) in the same format. I want to return the difference between these two times as a string (e.g., 2h 23m 2s 456ms).

For example:

myTime := [18, 30, 15, 0];
timeNow := [19, 30, 15, 0];

would return ‘1h 0m 0s 0ms’ and

myTime := [19, 30, 10, 0];
timeNow := [20, 25, 5, 0];

would return '23h 5m 5s 0ms'


function getTime(): array [0..3] of word;
begin
decodeTime(now(), result[0], result[1], result[2], result[3]);
end;

function differenceInTime(): string;
var
mytime, timeNow: array [0..3] of integer;
begin
myTime := [18, 30, 15, 0]; // [hour, min, sec, ms]
timeNow := getTime();

//halp

result := 'Xh ' + 'Xm ' + 'Xs ' + 'Xms';
end;

Assuming no use of srl include etc?

var
mytime, timeNow: array [0..3] of integer;
h, m, s: Integer;

begin
myTime := [18, 30, 15, 0];
timeNow := [19, 30, 15, 0];
ConvertTime((timeNow[0] * 3600000 + timeNow[1] * 60000 + timeNow[2] * 1000) - (myTime[0] * 3600000 + myTime[1] * 60000 + myTime[2] * 1000), h, m, s);
writeln(toStr(h) + 'h ' + toStr(m) + 'm ' + toStr(s) + 's ' + toStr(timeNow[3] - myTime[3]) + 'ms');
end.

i'm not sure if you want to input 2 timing urself manually or if timeNow is just GetSystemTime()?
also why


myTime := [19, 30, 10, 0];
timeNow := [20, 25, 5, 0];

would return '23h 5m 5s 0ms'

The Mayor
08-16-2014, 10:15 AM
Just convert to milliseconds and do the math there.

I don't think it's that simple


Assuming no use of srl include etc?

var
mytime, timeNow: array [0..3] of integer;
h, m, s: Integer;

begin
myTime := [18, 30, 15, 0];
timeNow := [19, 30, 15, 0];
ConvertTime((timeNow[0] * 3600000 + timeNow[1] * 60000 + timeNow[2] * 1000) - (myTime[0] * 3600000 + myTime[1] * 60000 + myTime[2] * 1000), h, m, s);
writeln(toStr(h) + 'h ' + toStr(m) + 'm ' + toStr(s) + 's ' + toStr(timeNow[3] - myTime[3]) + 'ms');
end.

i'm not sure if you want to input 2 timing urself manually or if timeNow is just GetSystemTime()?
also why

Not quite. The:


myTime := [19, 30, 10, 0];
timeNow := [20, 25, 5, 0];

Basically means the current time is 8:25:05 PM and I want to know how far away myTime is (which is 7:30:10 PM). I'm not after the negative difference, but the time until myTime occurs again. As the current time is after myTime, the next time 7:30 PM will occur is the next day, hence the 23 hours.

riwu
08-16-2014, 02:54 PM
I don't think it's that simple



Not quite. The:


myTime := [19, 30, 10, 0];
timeNow := [20, 25, 5, 0];

Basically means the current time is 8:25:05 PM and I want to know how far away myTime is (which is 7:30:10 PM). I'm not after the negative difference, but the time until myTime occurs again. As the current time is after myTime, the next time 7:30 PM will occur is the next day, hence the 23 hours.
ahh i see...that indeed makes it more complicated

var
mytime, timeNow: array [0..3] of integer;
diff, h, m, s, msDiff: Integer;

begin
myTime := [18, 30, 15, 0];
timeNow := [19, 30, 15, 30];

diff := (myTime[0] * 3600000 + myTime[1] * 60000 + myTime[2] * 1000) - (timeNow[0] * 3600000 + timeNow[1] * 60000 + timeNow[2] * 1000);
if (diff > 0) or ((diff = 0) and (myTime[3] >= timeNow[3])) then
begin
if myTime[3] < timeNow[3] then
Dec(diff, 1000);
ConvertTime(diff, h, m, s);
end else
begin
if myTime[3] < timeNow[3] then
Dec(diff, 1000);
ConvertTime(24 * 3600000 + diff, h, m, s);
end;

if myTime[3] < timeNow[3] then
msDiff := 100 - (timeNow[3] - myTime[3])
else
msDiff := myTime[3] - timeNow[3];

writeln(toStr(h) + 'h ' + toStr(m) + 'm ' + toStr(s) + 's ' + toStr(msDiff) + 'ms');
end.

might've made some minor mistakes but the general concept should be right :)

EDIT: if u prefer a few lines shorter...cleaned up a bit:

var
mytime, timeNow: array [0..3] of integer;
diff, h, m, s, msDiff: Integer;
earlier: Boolean;

begin
myTime := [18, 30, 15, 0];
timeNow := [19, 30, 15, 30];

diff := (myTime[0] * 3600000 + myTime[1] * 60000 + myTime[2] * 1000) - (timeNow[0] * 3600000 + timeNow[1] * 60000 + timeNow[2] * 1000);
earlier := (diff > 0) or ((diff = 0) and (myTime[3] >= timeNow[3]));

if myTime[3] < timeNow[3] then
Dec(diff, 1000);

if earlier then
ConvertTime(diff, h, m, s)
else
ConvertTime(24 * 3600000 + diff, h, m, s);

msDiff := myTime[3] - timeNow[3];
if msDiff < 0 then
Inc(msDiff, 100);

writeln(toStr(h) + 'h ' + toStr(m) + 'm ' + toStr(s) + 's ' + toStr(msDiff) + 'ms');
end.

The Mayor
08-16-2014, 09:48 PM
ahh i see...that indeed makes it more complicated

EDIT: if u prefer a few lines shorter...cleaned up a bit:

var
mytime, timeNow: array [0..3] of integer;
diff, h, m, s, msDiff: Integer;
earlier: Boolean;

begin
myTime := [18, 30, 15, 0];
timeNow := [19, 30, 15, 30];

diff := (myTime[0] * 3600000 + myTime[1] * 60000 + myTime[2] * 1000) - (timeNow[0] * 3600000 + timeNow[1] * 60000 + timeNow[2] * 1000);
earlier := (diff > 0) or ((diff = 0) and (myTime[3] >= timeNow[3]));

if myTime[3] < timeNow[3] then
Dec(diff, 1000);

if earlier then
ConvertTime(diff, h, m, s)
else
ConvertTime(24 * 3600000 + diff, h, m, s);

msDiff := myTime[3] - timeNow[3];
if msDiff < 0 then
Inc(msDiff, 100);

writeln(toStr(h) + 'h ' + toStr(m) + 'm ' + toStr(s) + 's ' + toStr(msDiff) + 'ms');
end.


Nice one. This is a lot shorter that what I had. Just had to change the inc(msDiff, 100) to 1000 and it seems to work 100%!

riwu
08-17-2014, 12:06 AM
Nice one. This is a lot shorter that what I had. Just had to change the inc(msDiff, 100) to 1000 and it seems to work 100%!
ahh right....carelessness will be the death of me...