用Python写一个时间类
本帖最后由 KeyError 于 2023-11-12 17:43 编辑效果:
>>> a = Time(2023, 1, 22, 0 ,59, 4)
>>> a
2023年1月22日0时59分4秒
>>> x = a + Time(0, 0, 0, 0, 1, 1)
>>> x
2023年1月22日1时1分5秒
>>> x - Time(0, 0, 0, 0, 1, 1)
2023年1月22日0时59分4秒
>>>
使用说明:
初始化方法:
>>> [变量名称] = Time([年份], [月份], [日期], [时], [分], [秒])
支持+, -运算,
有什么不足请各位指出{:10_254:}
import datetime
class TimeError(Exception):
def __init__(self, msg=''):
self.msg = msg
def __repr__(self):
return self.msg
class Time(object):
def __init__(self, nian: int=1, yue: int=1, ri: int=1, h: int=0, m: int=0, s: float=0.0) -> None:
self.gui = ['年','月','日','时','分','秒']
self.tian = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:31,12:30}
if nian % 4 == 0 and (not nian % 400 ==0): #判断是否是闰年
self.tian = 29
if yue == 0:
yue = 12
nian -= 1
self.error = 0
while True:
try:
assert 0 <= yue <= 12
self.error = 1
assert 0 <= ri <= self.tian
self.error = 2
assert 0 <= h <= 23
self.error = 3
assert 0 <= m <= 59
self.error = 4
assert 0 <= s <= 59
self.error = 5
except AssertionError:
if self.error == 0: #如果月无效
if yue:
if yue > 0:
nian += (yue // 12)
yue %= 12
else:
nian -= (((0 - yue) // 12) + 1)
yue = 12 - (yue % 12)
else:
yue = 12
nian - = 1
elif self.error == 1: #如果日无效
while ri < 0:
if yue == 0:
nian -= 1
yue = 12
else:
yue -= 1
if yue == 0:
yue = 12
ri += self.tian
while ri > self.tian:
if yue == 12:
nian += 1
yue = yue % 12 + 1
ri -= self.tian
if ri == 0:
yue -= 1
if yue == 0:
yue = 12
ri = self.tian
elif self.error == 2: #如果时无效
ri += (h // 24)
h %= 24
elif self.error == 3: #如果分无效
h += (m // 60)
m %= 60
else: #如果秒无效
m += (s // 60)
s %= 60
else:
break
self.__nian=str(nian)
self.__yue=str(yue)
self.__ri=str(ri)
self.__h=str(h)
self.__m=str(m)
self.__s=str(s)
self.init()
pass
def init(self):
self.time = self.__nian + '年'
self.time += self.yue + '月'
self.time += self.__ri+'日'
self.time += self.__h+'时'
self.time += self.time+self.__m+'分'
self.time += self.time+self.__s+'秒'
pass
def __repr__(self):
return self.time
__str__ = __repr__
def __add__(self, other):
try:
num = Time(int(self.__nian) + int(other.__nian), int(self.__yue) + int(other.__yue), int(self.__ri) + int(other.__ri), int(self.__h) + int(other.__h), int(self.__m) + int(other.__m), int(self.__s) + int(other.__s))
return num
except:
raise TimeError(f'the type Time and \'{type(other)}\' not cloud \'+\'')
def __sub__(self,other):
try:
num = Time(int(self.__nian) - int(other.__nian), int(self.__yue) - int(other.__yue), int(self.__ri) - int(other.__ri), int(self.__h) - int(other.__h), int(self.__m) - int(other.__m), int(self.__s) - int(other.__s))
return num
except:
raise TimeError(f'the type Time and \'{type(other)}\' not cloud \'-\'')
def getTime() -> Time:
num = list(datetime.datetime.now())
return Time(num, num, num, num, num, num)
看看 本帖最后由 歌者文明清理员 于 2023-3-9 18:25 编辑
容错原理做的很好,必须评分
不过楼主,这个素材
https://xxx.ilovefishc.com/forum/202303/05/145337y4ul39xzjdlxdjum.gif
我推荐给你 1
一般不在__init__里写具体的逻辑,while true那里可以优化成函数抽出来 看看 import time
Time = time.struct_time{:10_256:} tommyyu 发表于 2023-3-9 19:03
请不要灌水,我快被灌吐了 支持一下 诶,看看 更新日志:
2023/11/12 gettime函数更新 学习一下 感觉很有意思喔
可以试试写一个用于计时的秒表
页:
[1]