在Python中查找当前时间时出错

2024-10-01 19:16:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现这个时间类来查找当前时间。我走错时间了。有人能看看我做错什么了吗?你知道吗

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()        

Tags: importselfdatetimereturntimedef时间print
1条回答
网友
1楼 · 发布于 2024-10-01 19:16:42

您的代码总是处理UTC时间,但是您正在将结果与时区中的时间进行比较。试试这个:

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

相关问题 更多 >

    热门问题