How to create a Timer based on World Time at the point when the event got triggered minus Current World Time?

Hello everyone!

I am trying to create a timer. My understanding was that if I saved the world time of the event trigger and subtracted that from the ongoing World Time it would give me the timer I was looking for. While this is happening it has the problem of negative seconds or minutes like :

Current time y2021/mo12/d30/h16/min34/sec1
Minus Initial Time y2021/mo12/d30/h16/min33/sec21
Timer Outcome y0/ mo0/ d0/ h0/ min1/ sec-20

Thank you all in advance for any help you will be able to provide!

23

To answer my own question after some hours I figured it out. I am sure there are better ways to do it and I would be glad to know about them if someone can enlighten me.

I think that subtracting the struct will not give you an accurate time. You have to convert it down to something like seconds, do the operation and convert it back again to d/h/m/s.

Could you explain what kind of miscalculation might be occurring in this? I run some tests with various dates as fast as 20 years back and it all seemed to be working as intended. Perhaps I am missing something.

Try subtracting after - before midnight new years with 4 second difference → (00, 00, 00, 00, 02) - (12, 31, 23, 59, 58)

That should prove if I’m right or wrong. :grimacing:

Why wouldn’t you set set a timer, and ask it how much time has elapsed, or is left?

If that’s not what you want for some reason, try Get Game Time in Seconds | Unreal Engine Documentation which gives you a number that you can easily do math with – and that will be easier to deal with when saving/reloading saved games.

I just tried what you suggested and got the correct result with one difference. Instead of (00, 00, 00, 00, 02) - (12, 31, 23, 59, 58) I used (2022, 01, 01, 00, 00, 02, 00) - (2021, 12, 31, 23, 59, 58, 00).

I used 01 for months and days since there really couldn’t be a month or day 0 (unless there could program-wise, since I am a beginner I really don’t know).

Anyway, the results were accurate in the Print String.

time

What I am trying to achieve is a timer that keeps track of time even when someone closes the application.

Much like fasting applications that keep your progress and can show it again after you open the app again or games with cooldowns that are unrelated to whether the app is running or not. The easiest method in my mind was Current Time - Time of Event Creation.

Is there a more sophisticated way to do this with the tools you have mentioned or perhaps something else now that I have given you some more context?

Nice! Then I was mistaken :rofl:

Gotcha – yes, if you need real time, you need real time!

The only other way I can think of, is to write a blueprint function library and call the C library time() function, and return that value, if you want “number of seconds since some random time in the past.”

1 Like

Thank you very much for your time and knowledge! You’ve given me some functions to mess with and gain some familiarity. Best of luck to you!