My countdown timer is all messed up. Need help

My timer counts down perfectly but, the hours are not at “00:” and the microseconds have too many numbers instead of “00” format.
It should all read “00:00:00:00” but, it doesn’t.

countdown timer.blend (566.3 KB)

I can’t get it to run.
In case you can’t get it to work either, here’s what I wrote. Use and modify as you please.

#---------------------------------------------------------------------------
class time:
	#--------
    def convert_format(self):
        if isinstance(self.sec,float):
            from math import modf
            x = math.modf(self.sec)
            self.sec,self.mili = x[0],int(x[1])
		#--------
        min_num = self.sec/60
        for i in range(0,int(min_num)):
            if int(self.sec) >= 60:
                self.sec = self.sec - 60
                self.min += 1
		#--------
        hour_num = self.min/60
        for i in range(0,int(hour_num)):
            if self.min >= 60:
                self.min = self.min - 60
                self.hour += 1
	#--------
    def __init__(self,dir):
        if dir == 'up':
            self.mili = 0.0
        else:
            self.mili = 1.0
        self.sec = 0
        self.min = 0
        self.hour = 0
        self.subdiv = 1
        self.dir = dir
        self.convert_format()
	#--------
    def count_up(self,timescale):
        self.mili += ((1/60)*(self.subdiv**timescale))
        if self.mili >= 1.0:
            self.mili = self.mili - 1.0
            self.sec += 1
	#--------
    def count_down(self,timescale):
        self.mili -= ((1/60)*(self.subdiv**timescale))
        if self.min <= 0 and self.hour > 0:
            self.min = 59
            self.hour -= 1
		#--------
        if int(self.sec) <= 0 and self.min > 0:
            self.sec = 59
            self.min -= 1
		#--------
        if self.mili <= 0.0 and self.sec > 0:
            self.mili = self.mili + 1.0
            self.sec -= 1
        if (self.mili < 0):
            return False
		#--------
        return True
	#--------
    def convert_read(self):
        read = (
        str(self.hour)+':'+str(self.min)+':'+str(int(self.sec)))
        return read
   	#--------
    def update(self,timescale=1):
        return self.timeDict[self.dir](self,timescale)
    
    timeDict = {'up':count_up,'down':count_down}
#--------------------------------------------------------------------------- 

To use:

import time as counter
cooldown = counter("down") #this counter will count down when update is called
#now you can
cooldown.sec = 3600 #automatically turns this into an hour
#or just
cooldown.hour = 1 #it's the exact same
counterStillTicking = cooldown.update()
#this returns False when count down reaches zero
#if counting up, returns none
#you can also input a timescale as an optional argument

my_time = counter("up") #this counts up.
my_time.mili = 0.75
my_time.sec = 1
my_time.min = 3
my_time.hour = 6

my_time.update(0.5) #the clock ticks at half normal speed
my_time_to_string = my_time.convert_read() #returns H:M:S as a string

PS: Whiskey and wimmen.

change you script to this and it works.

import bge 
from datetime import datetime

textObject = bge.logic.getCurrentController().owner
if textObject["time"] < 0:
   time = datetime.fromtimestamp(-textObject["time"])
   Microsecond = "{:%f}".format(time)
   textObject.text = "{:%H:%M:%S}:{}".format(time,Microsecond[:2])

i missed the "hours are not at “00:” part, looking into it.

now it works

here is the result http://15b.dk/blendfiles/countdown_timer.blend

Wow! I’m impressed!
Thanks a lot for fixing my project @edderkop!

no problem :wink: