Error finding current time in Python
Question: I am trying to implement this time class to find the current time. I am
getting wrong time. Can someone please take a look what am I doing wrong here?

    
    
    import time
    import datetime
    
    class Time():
    def __init__(self):
        currentTime = time.time()
        totalSeconds = int(currentTime)    
        self.__second = totalSeconds % 60
        totalMinutes = totalSeconds // 60
        self.__minute = totalMinutes %60
        totalHours = totalMinutes// 60
        self.__hour = totalHours % 12
    
    def getSecond(self):
        return self.__second
    
    def getMinute(self):
        return self.__minute
    
    def getHour(self):
        return self.__hour
    def main():
        t = Time()
        print("Current Time is : ", t.getHour(), ":", t.getMinute(), ":",   t.getSecond())
        print(datetime.datetime.now().time())
        print(time.ctime())
    
    main()        
    


Answer: Your code always deals with UTC times, but you are comparing the result with
time in your time zone. Try this:

    
    
    def main():
        t = Time()
        print("Current Time is : ", t.getHour(), ":", t.getMinute(), ":",   t.getSecond())
        print(datetime.datetime.utcnow().time())
        print(time.asctime(time.gmtime()))
    

