为什么这两个代码的日期输出格式不同?一个是 datetime 对象,另一个似乎是一个字符串(我更喜欢字符串看起来相似的格式)。我错过了什么?
def generator():
i=0
while (i<50000):
yield random.randint(-1, 1), datetime.datetime.now()
i=i+1
mynumber = 100
for random_number, current_time in generator():
mynumber += random_number
print mynumber,",", current_time
输出:
>>> 100 , 2013-04-04 09:16:55.730000
101 , 2013-04-04 09:16:55.746000
第二个例子:
def test(timestamp,interval = 1*60):
xt = datetime.datetime(2013, 4,4)
#dt = datetime.datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')
dt=timestamp
delta_second =(dt - xt).seconds
normalize_second = (delta_second / interval) * interval
newtime = xt + timedelta(seconds=normalize_second)
print (dt,newtime)
test(datetime.datetime.now())
>>> (datetime.datetime(2013, 4, 4, 9, 21, 12, 386000), datetime.datetime(2013, 4, 4, 9, 21))