1

我想要实现的目标很简单:

time.time() 不太可读。如何获得以下内容:

例如

time.time() //say, it's May 15 2013 13:15:46

如何获得以下给定的 time.time() 上面:

2013 年 5月15 日12:15:46

2013年5 月 15 日14:15:46

2013 年 5月15 日13:14:46

2013 年 5月15 日13:16:46

我正在寻找类似的东西:

def back_an_hr(current_time):
    .....
def back_a_min(current_time):
    .....

back_an_hr(time.time()) # this brings time.time() back an hr
back_a_min(time.time()) # this brings time.time() back a min
4

3 回答 3

7

datetime使用该模块可能会更好:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2013, 5, 15, 15, 30, 17, 908152)
>>> onehour = datetime.timedelta(hours=1)
>>> oneminute = datetime.timedelta(minutes=1)
>>> now + onehour
datetime.datetime(2013, 5, 15, 16, 30, 17, 908152)
>>> now + oneminute
datetime.datetime(2013, 5, 15, 15, 31, 17, 908152)
>>> now.strftime("%b %d %Y %H:%M:%S")
'May 15 2013 15:30:17'
>>> (now - onehour).strftime("%b %d %Y %H:%M:%S")
'May 15 2013 14:30:17'
于 2013-05-15T13:31:23.347 回答
0

Python 有一个名为 datetime 和 timedelta 的模块。使用这些模块,您可以定义自己的函数 back_an_hr(current_time) 和 back_a_min(current_time)

timedelta 采用偏移量,您可以将偏移量定义为日、月、年、小时分钟或秒。

于 2013-05-15T13:30:32.003 回答
0

time.time() 以浮点数形式给出秒数。

time.time() - (60 * 60 * 24) # 1 day
于 2013-05-15T13:30:36.903 回答