Game Development tutorials
Game Development  journey
Beginner "how to" tutorials , programming, software usage,  artwork, news  and more
For TIPS and SHORT TUTORIALS
 FOLLOW 
100+

Time extracting function in Godot engine - transforming seconds into days, hours, minutes, seconds and milliseconds

Posted by Vix Mark on July 21No Comments

While I was working on my Stopwatch Android app I had to build several functions that would use time the app was counting as an argument and return some values I needed, and which were time dependent. Well it's a stopwatch, so almost everything depends on time passed.

The app has analog style display with arrows, digits, milliseconds scale, etc... But I also wanted to show days, hours, minutes, seconds and milliseconds with digits. Because you know... Believe or not but analog display might be confusing for some, people say 🙂

I used Label Node to show that data. If there's change in the time value Label's text property gets updated every frame as well as the rotation of arrows.

As displaying plain seconds (12 456.594 seconds make me wanna use calculator) wouldn't do the job, I wrote several functions to get an array with 5 values out of any number of seconds given. I named it time_extract()

Godot and GDScritp: Converting float seconds into DHMS.s

'DHMS.s' is something I've just made up to simplify this writing. Anyway, the logic is quite straightforward:

  • time_extract(arg) receives float seconds as an argument
  • there's an array created at the beginning of the function with 5 values of the variables representing DHMS.s. All of them equal to 0:
#========TIME EXTRACTION========
func time_extract(my_time):
	var days = 0
	var hours = 0
	var minutes = 0
	var seconds = 0
	var mill_seconds = 0
	
	var time_array = [days, hours, minutes, seconds, mill_seconds]
  • then using if...elif the argument (time) gets evaluated to find out in what range it happens to be and therefore which array members have to get new values
  • there are also 5 additional functions, each calculating separate value for DHMS.s. Which of them are called within if... elif blocks depends on the time argument:
	if my_time < 1: # less than a second
		mill_seconds = get_mill_seconds(my_time)
	elif int(my_time) in range(1, 60): # less than a minute
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
	elif int(my_time) in range(60, 3600): # less than an hour
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
		minutes = get_minutes(my_time)
	elif int(my_time) in range(3600, 86400): # less than a day
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
		minutes = get_minutes(my_time)
		hours = get_hours(my_time)
	elif int(my_time) >= 86400: # more than one day
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
		minutes = get_minutes(my_time)
		hours = get_hours(my_time)
		days = get_days(my_time)
  • After new values get assigned to the appropriate variables, the array again gets all 5 variables assigned to its members. So, if one of them was 0 at the beginning and hasn't changed - it's still equals to 0 but if it's changed within any of if... elif block, then a member gets a new value:
	time_array[0] = days
	time_array[1] = hours
	time_array[2] = minutes
	time_array[3] = seconds
	time_array[4] = mill_seconds
	
	for i in range(time_array.size()):
		if time_array[i] < 10:
			time_array[i] = "0" + str(time_array[i])
		else:
			time_array[i] = str(time_array[i])
	
	return time_array

The for loop above converts array members into strings and keeps them consistent, with 2 digits adding "0" before numbers less than 10. Finally, I return my time array for farther use.

Additional functions

Here's 5 helper functions - each gets data for every array member if there's data to get. For example, if the number of seconds equals to or exceeds 24 or more hours get_days() function is called and it returns a value which is then assigned to days variable. These helpers also use time for calculations:

func get_mill_seconds(t):
	return int((t - floor(t)) * 100)

func get_seconds(t):
	t = floor(t)
	var mnts = floor(t/60)
	return t - (mnts * 60)

func get_minutes(t):
	t = floor(t)
	var hrs = floor(t/3600)
	return floor((t - (hrs * 3600))/60)

func get_hours(t):
	t = floor(t)
	var dys = floor(t/86400)
	return floor((t - (dys * 86400))/3600)

func get_days(t):
	return floor(t/86400)
#========TIME EXTRACTION END========

The overall script looks like this:

#========TIME EXTRACTION========
func time_extract(my_time):
	var days = 0
	var hours = 0
	var minutes = 0
	var seconds = 0
	var mill_seconds = 0
	
	var time_array = [days, hours, minutes, seconds, mill_seconds]
	
	if my_time < 1: # less than a second
		mill_seconds = get_mill_seconds(my_time)
	elif int(my_time) in range(1, 60): # less than a minute
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
	elif int(my_time) in range(60, 3600): # less than an hour
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
		minutes = get_minutes(my_time)
	elif int(my_time) in range(3600, 86400): # less than a day
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
		minutes = get_minutes(my_time)
		hours = get_hours(my_time)
	elif int(my_time) >= 86400: # more than one day
		mill_seconds = get_mill_seconds(my_time)
		seconds = get_seconds(my_time)
		minutes = get_minutes(my_time)
		hours = get_hours(my_time)
		days = get_days(my_time)
	
	time_array[0] = days
	time_array[1] = hours
	time_array[2] = minutes
	time_array[3] = seconds
	time_array[4] = mill_seconds
	
	for i in range(time_array.size()):
		if time_array[i] < 10:
			time_array[i] = "0" + str(time_array[i])
		else:
			time_array[i] = str(time_array[i])
	
	return time_array

func get_mill_seconds(t):
	return int((t - floor(t)) * 100)

func get_seconds(t):
	t = floor(t)
	var mnts = floor(t/60)
	return t - (mnts * 60)

func get_minutes(t):
	t = floor(t)
	var hrs = floor(t/3600)
	return floor((t - (hrs * 3600))/60)

func get_hours(t):
	t = floor(t)
	var dys = floor(t/86400)
	return floor((t - (dys * 86400))/3600)

func get_days(t):
	return floor(t/86400)
#========TIME EXTRACTION END========

How do I use returned time array - an example from my app

After making the function to do exactly what I needed I also had to code looping through that returned array to line up all its members into one string with spaces and a dot just before the milliseconds. Here it is:

func tablo_output(source):
	var result = ""
	for i in range (source.size()):
		if i == source.size() - 1:
			result += "." + source[i]
		elif i == source.size() - 2:
			result += source[i]
		else:
			result += source[i] + " " + ""
	return result

This loop makes a one line string out of any array of strings. It separates the members with spaces but if they are the last two - with the dot.

The string is assigned to Label Node's text property and there you go - after some theme customization the final result is this:

RoundTimer Android App
time_extract() function result demonstration

By the way, that glowing trail on the top of the seconds arrow is a Particles2D Node. I used a PNG image of white circle as a texture and 'played' with colors and curves of the flow to make what I wanted out it. You can check out this article to learn how to make exhaust and random explosions with the Particles Generator.

If you find this article useful, follow me on Instagram for more tips and learning materials.

Share this
Pin it
Conversation
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
©2026 GamaDevFcups - how to make Indie Games.

Game Development: Godot Engine, Krita, Blender, programming, tutorials

GameDevFcups

Desidned and built by VixStudio
 | 
VixMark@protonmail.com
pointer-down
0
Would love your thoughts, please comment.x
()
x