Adding two time values from CTime()

Is there any easy way to do this? Right now, I am using StrPart to separate the values from CTime(hours:minutes:seconds) into separate parts, and then using StrDigCalc to add a specific amount of time to the current time. Maybe I am overthinking this, is there not an easier way to add a duration to a current time to output a future time? 

Comments

  • lemster68
    lemster68 United States ✭✭✭
    Sure, calculate the total sum of seconds of the hours, minutes and seconds, then add the seconds that you want, then reverse the calculation for hours, minutes and seconds, if necessary.
    Lee Justice
  • jmf
    jmf South Africa
    Something like this?

    NumToStr(nReadTime)+" SECONDS";
    NumToStr(nReadTime DIV 60,3)+" MINUTES";
    NumToStr(nReadTime DIV 60 DIV 60,3)+" HOURS";
  • Thanks guys
  • lemster68
    lemster68 United States ✭✭✭
    The tricky part will be when you approach the new day, 23:59, 00:00 hour changeover.
    Lee Justice
  • I'm so close! I have found a way to take the output string of CTime, split it into 3 sections for hours minutes and seconds, then multiply those values by the respective multipliers, and finally add the values together to return a total seconds. I have a function that converts seconds to the HH:MM:SS time format, but the problem is the function takes a  num as input when CTime is returning a string. I don't think it is possible to use StrToVal when the input is constantly changing..
  • lemster68
    lemster68 United States ✭✭✭
    Why not?  You only need to keep grabbing a snapshot of the current time.
    Lee Justice
  • xerim
    xerim
    edited April 23
    Ha..you are correct. I have figured it out now. Here is the solution for anyone that is curious

    MODULE TimerModule
       
    PROC ReadTimeInSeconds()
       
        VAR stringdig time;
        VAR stringdig hours;
        VAR stringdig minutes;
       
        VAR string HoursInSeconds;
        VAR string MinutesInSeconds;
        VAR string totalSeconds;
       
        VAR num hours_length;
        VAR num minutes_num;
        VAR num totalSeconds_num;
       
        VAR num duration:=11400;
       
        VAR bool seconds_as_num;
        VAR bool zeroes;
       
       
        time:=CTime();
       
        hours:=StrPart(time,1,2);
        minutes:=StrPart(time,4,2);
       
        hours_length:=strLen(hours);
        zeroes:= StrMemb(hours,1,"0");
       
       IF zeroes = TRUE THEN
            hours:=StrPart(time,2,1);
        ELSE IF zeroes = FALSE THEN
           hours:=StrPart(time,1,2);
        ENDIF
       ENDIF
       
        HoursInSeconds:=StrDigCalc(hours, OpMult, "3600");
       
        MinutesInSeconds:=StrDigCalc(minutes,OpMult,"60");
       
        totalSeconds:=StrDigCalc(HoursInSeconds, OpAdd, MinutesInSeconds);
       
        seconds_as_num:=StrToVal(totalSeconds, totalSeconds_num);
       
            TPWrite("Cycle will finish at " + SecondtoTime(totalSeconds_num + duration));
    ENDPROC

     FUNC string SecondtoTime(num s)
    VAR num seconds:=0;
    VAR num minutes:=0;
    VAR num hours:=0;

         s:=Round(s);
         seconds:=0;
         minutes:=0;
         hours:=0;
         
         IF s>=60 THEN
             minutes:= s DIV 60;
             seconds:= s MOD 60;
         ELSE
             seconds:=s;
         ENDIF
         minutes:=Trunc(minutes);
         
         IF minutes >= 60 THEN
            hours:= minutes DIV 60;
            minutes:= minutes MOD 60;
         ELSE
             minutes:=minutes;
         ENDIF
    RETURN NumToStr(hours,0) + ":" +  NumToStr(minutes,0);
     ENDFUNC
    ENDMODULE

  • lemster68
    lemster68 United States ✭✭✭
    Good job, thanks for sharing the result.
    Lee Justice
  • Micky
    Micky Germany ✭✭✭
    edited April 26
    Hello,
    why don't you use the function "GetTime - Reads the current time as a numeric value"?

    It already returns the day of the week, hour, minute and seconds as a numeric value.

    Example from the manual:

    weekday := GetTime(\WDay);
    hour := GetTime(\Hour);

  • I was unaware of this command, thanks Micky
  • Micky said:
    Hello,
    why don't you use the function "GetTime - Reads the current time as a numeric value"?

    It already returns the day of the week, hour, minute and seconds as a numeric value.

    Example from the manual:

    weekday := GetTime(\WDay);
    hour := GetTime(\Hour);

     For some reason when I try this in RobotStudio it gives me "Invalid Identifier" for the GetTime command
  • Nevermind, it is working now. 
Sign In or Register to comment.