Results 1 to 7 of 7

Thread: Shortest possible way to write this function

  1. #1
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default Shortest possible way to write this function

    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).

    Simba Code:
    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;
    Last edited by The Mayor; 08-16-2014 at 11:57 AM. Reason: Made it clearer.

  2. #2
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Just convert to milliseconds and do the math there.

  3. #3
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by The Mayor View Post
    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'

    Simba Code:
    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?
    Simba Code:
    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
    Quote Originally Posted by The Mayor View Post
    myTime := [19, 30, 10, 0];
    timeNow := [20, 25, 5, 0];

    would return '23h 5m 5s 0ms'
    Last edited by riwu; 08-16-2014 at 09:30 AM.

  4. #4
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default

    Quote Originally Posted by tls View Post
    Just convert to milliseconds and do the math there.
    I don't think it's that simple

    Quote Originally Posted by riwu View Post
    Assuming no use of srl include etc?
    Simba Code:
    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.

  5. #5
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by The Mayor View Post
    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
    Simba Code:
    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:
    Simba Code:
    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.
    Last edited by riwu; 08-16-2014 at 03:16 PM.

  6. #6
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    ahh i see...that indeed makes it more complicated

    EDIT: if u prefer a few lines shorter...cleaned up a bit:
    Simba Code:
    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%!

  7. #7
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by The Mayor View Post
    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...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •