Convert hh:dd:ss to seconds for timer

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
LosWochos
Newbie
Posts: 2
Joined: Thu May 10, 2018 10:21 am

Convert hh:dd:ss to seconds for timer

Post by LosWochos » Thu May 10, 2018 10:28 am

Hello everyone,

I am currently facing an issue I cannot solve.

Here's what I want to do:

I have a time that I capture using OCR. This can be in format hh:mm:ss or mm:ss.

I need to convert these (either hours, minutes and seconds or just minutes and seconds if less than one hour) to either seconds or milliseconds and save it to a variable or file.

Then I will start a timer that will periodically be checked.

Once the value returned by the timer is less than the value that I have captured using OCR and converted to (milli)seconds, I will make that script run again.

This one will run as a subroutine along others that will be called every time, yet this one should only be called once the timer has run out.

I've got pretty much everything worked out, but I do not know and could not find, how to convert the captured time to seconds or milliseconds. Especially since the format varies, depending on how long is left.

Your help will be greatly appreciated!

Thanks a lot in advance!

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Convert hh:dd:ss to seconds for timer

Post by hagchr » Thu May 10, 2018 11:19 am

Hi, If the you always have two characters for each component (eg 05:13 and not 5:13 etc) then you can use something like:

Code: Select all

Let>str1=04:33:20
Let>str2=07:21

Gosub>Get_secs,str1
MDL>res_secs

Gosub>Get_secs,str2
MDL>res_secs


SRT>Get_secs
  // Get the input variable into a variable str
  Let>str=Get_secs_VAR_1
  
  // Remove any initial or trailing spaces
  Trim>str,str
  
  // Append 00: in the beginning
  Let>str=00:%str%
  
  // Get the ending 8 characters
  Length>str,nLength
  MidStr>str,{%nLength%-7},8,res

  // Get the time parts
  TimePart>res,H,resH
  TimePart>res,M,resM
  TimePart>res,S,resS

  // Calculate seconds
  Let>res_secs={%resH%*60*60+%resM%*60+%resS%}
END>Get_secs

LosWochos
Newbie
Posts: 2
Joined: Thu May 10, 2018 10:21 am

Re: Convert hh:dd:ss to seconds for timer

Post by LosWochos » Thu May 10, 2018 11:35 am

Awesome! Thank you very much! :)

Works like a charm.

The hours are always below 10 without a leading 0, so I adapted accordingly.

Again, thanks a lot!

//EDIT:

Actually, I will have to take that back.

If I run it standalone and change the numbers to anything within that format, it works fine.

But as soon as I try to implement it into my code, it no longer works and outputs something like 7599743 with an input value of 1:30 or so. I have displayed the output of "theText" to see if it looks funny at all, but it's the same format. Either something like 1:30 or 1:22:43.

Here's the complete code (this has only been put together rudimentary for testing purposes, don't hate on it :lol: ):

Code: Select all

SRT>GetTime
//Capture specific area of window
SetFocus>Screen1
GetWindowPos>Screen1,X1,Y1
GetWindowSize>Screen1,w,h
ScreenCapture>{%X1%+50},{%Y1%+222},{%X1%+185},{%Y1%+250},%SCRIPT_DIR%\screen.bmp

//run tesseract on the screen grab and output to temporary file
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run>"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" "%SCRIPT_DIR%\screen.bmp" "%SCRIPT_DIR%\tmp"

//read temporary file into memory and delete it
ReadFile>%SCRIPT_DIR%\tmp.txt,theText
DeleteFile>%SCRIPT_DIR%\tmp.txt

Gosub>Get_secs,theText
MDL>res_secs
MDL>theText
END>GetTime

SRT>Get_secs
  // Get the input variable into a variable str
  Let>str=Get_secs_VAR_1
  
  // Remove any initial or trailing spaces
  Trim>str,str
  
  // Append 00: in the beginning
  Let>str=00:%str%
  
  // Get the ending 7 characters
  Length>str,nLength
  MidStr>str,{%nLength%-6},7,res
  // Get the time parts
  TimePart>res,H,resH
  TimePart>res,M,resM
  TimePart>res,S,resS

  // Calculate seconds
  Let>res_secs={%resH%*60*60+%resM%*60+%resS%}
END>Get_secs
I'm using Tesseract, as the Macro Scheduler OCR won't work on the window I'm using.

//EDIT2:

Also I have found, that the hours will always consist of 1 digit (with all testing so far), while the minutes may be two or one digit. Seconds are always two.

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Convert hh:dd:ss to seconds for timer

Post by hagchr » Thu May 10, 2018 1:34 pm

Hi, I changed it so that it does not matter if one or two characters are used.

Code: Select all

Let>str1=04:33:20
Let>str2=7:21

Gosub>Get_secs,str1
MDL>res_secs

Gosub>Get_secs,str2
MDL>res_secs


SRT>Get_secs
  // Get the input variable into a variable str
  Let>str=Get_secs_VAR_1
  
  // Remove any initial or trailing spaces
  Trim>str,str
  
  // Append 00: in the beginning
  Let>str=00:%str%

  // Split the string into its part, using : as separator => last part is seconds, then before that, minutes, then hours.  
  Separate>str,:,res
  
  // Get the array number for each item
  Let>tmpS=%res_COUNT%
  Let>tmpM=%tmpS%-1
  Let>tmpH=%tmpS%-2
  
  // Get each item
  Let>resS=res_%tmpS%
  Let>resM=res_%tmpM%
  Let>resH=res_%tmpH%

  // Calculate seconds
  Let>res_secs={%resH%*60*60+%resM%*60+%resS%}
END>Get_secs

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts