Adding two time values from CTime()
xerim
✭
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?
0
Comments
-
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 Justice1
-
Something like this?
NumToStr(nReadTime)+" SECONDS";
NumToStr(nReadTime DIV 60,3)+" MINUTES";NumToStr(nReadTime DIV 60 DIV 60,3)+" HOURS";1 -
Thanks guys0
-
The tricky part will be when you approach the new day, 23:59, 00:00 hour changeover.Lee Justice0
-
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..0
-
Why not? You only need to keep grabbing a snapshot of the current time.Lee Justice1
-
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
ENDMODULE0 -
Good job, thanks for sharing the result.Lee Justice0
-
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);
1 -
I was unaware of this command, thanks Micky0
-
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);
0 -
Nevermind, it is working now.0
Categories
- All Categories
- 5.5K RobotStudio
- 394 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 249 ScreenMaker
- 2.7K Robot Controller
- 309 IRC5
- 59 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 785 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 4 Job listings