Home > Mobile >  Count the duration of a variable in a state (true/false)
Count the duration of a variable in a state (true/false)

Time:02-18

I am trying to count the duration of a variable in a specific state (true or false) in seconds then store it in a mongo database, but I can't figure out how to proceed.

Example : if "my_var" is true for 60 seconds, then "my_var_time" = 60 on the database.

I was thinking about running a script that would check "my_var" value each second, but that would not be reliable. It can't work if the program doesn't end in less than one second, which is likely if multiple objects are checked during the process.

CodePudding user response:

There must be some event that causes my_var to change. So you could, after the line that causes the change, add a line that captures a timestamp. That way, you could get timestamps for the moment when it became true and for the moment when it became false, and calculate the difference between those two stamps.

  • Related