Turn numbers into time-code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

each second, my RichTextLabel adds +1.

extends RichTextLabel

var hour = 0
var minute = 0
var second = 0
var last_second = OS.get_time().second

func _process(delta):
	var time = OS.get_time()
	if time.second != last_second:
		print("start of a new second")
		text = str(int(text) + 1)
	last_second = time.second

But instead of showing that increasing number, I would like it to be displayed as a timecode like hh:mm:ss
I believe I’d need to somehow translate that number into something like this:

set_text("( %02d:%02d:%02d )" % [hour, minute, second])

or maybe this:

set_text("( %02d:%02d:%02d )" % [fmod(hour/3600, 24), fmod(minute/60, 60), fmod(second, 60)])

but I can’t quite find a way to achieve this. Any ideas?

:bust_in_silhouette: Reply From: jgodfrey

The return value of that OS.get_time() call is a Dictionary. So, you can reference the independent time elemens as the keys in your dict, such as…

time.hour
time.minute
time.second

Just realized that I probably misunderstood your question. The solution in this post is likely what you’re after…

jgodfrey | 2022-09-28 02:02

Thank you, that led the way.

I shaped it up like this:

var last_second = OS.get_time().second

func _process(delta):
	var time = OS.get_time()
	
	if time.second != last_second:
		
		var seconds = # get some integers from anywhere
		var minutes = seconds / 60
		var hours = seconds / 3600
		set_text("( %02d:%02d:%02d )" % [hours%24, minutes%60, seconds%60])
		
	last_second = time.second

pferft | 2022-09-30 11:06