这应该可以:
def adjust(h,m,s):
if s > 59: m, s = m+1, s-60
if s < 0: m, s = m-1, s+60
if m > 59: h, m = h+1, m-60
if m < 0: h, m = h-1, m+60
return "%02d:%02d:%02d" % (h, m, s) // old-style formating
# return f"{h:02}:{m:02}:{s:02}" // new-style formating (Python 3.6+)
for t in ('13:48:43.995', '13:59:59.295', '8:00:00'):
h, m, s = [round(float(x)) for x in t.split(':')]
nearest_sec = adjust(h, m, s)
add_one_sec = adjust(h, m, s+1)
sub_one_sec = adjust(h, m, s-1)
print(nearest_sec, '-->', add_one_sec, sub_one_sec)
以下是生成的输出:
13:48:44 --> 13:48:45 13:48:43
13:59:59 --> 14:00:00 13:59:58
08:00:00 --> 08:00:01 07:59:59
请注意,结果不能跨越午夜,因为您的时间戳不包括天。。。