Python中的Timedeltas

时间:2015-09-07 13:57:14

标签: python timedelta python-datetime

我知道在Python中有一些关于使用WITH CTE_YourTable --My version of your table AS ( SELECT row_num = ROW_NUMBER() OVER (PARTITION BY Batch#,[Event] ORDER BY Event#), --This numbers the events for each batch and type. * FROM ( VALUES (1, 1, 'Start', '2015-07-01 12:31:31.000'), (1, 2, 'Start', '2015-07-01 12:31:31.000'), (1, 3, 'Start', '2015-07-01 12:31:31.000'), (1, 4, 'End', '2015-07-01 12:33:32.000'), (1, 5, 'End', '2015-07-01 12:33:32.000'), (1, 6, 'End', '2015-07-01 12:33:32.000'), (2, 1, 'Start', '2015-07-01 12:35:32.000'), (2, 2, 'End', '2015-07-01 12:36:32.000') ) AS A(Batch#,Event#,[Event],[Timestamp]) ) SELECT A.Batch#, A.Event#, A.[Event], A.[Timestamp], B.Event#, B.[Event], B.[Timestamp] FROM CTE_YourTable AS A LEFT JOIN CTE_YourTable AS B ON A.Batch# = B.Batch# --Only join matching batches AND A.row_num = B.row_num --Match the rows so the first start goes with the first end, second start to second end etc... AND B.[Event] = 'End' --Only end events on the right table aliased "B" WHERE A.[Event] = 'Start' --Only start events on the left table aliased "A" ORDER BY A.Batch#,A.Event# 对象的帖子,Python文档足够清楚,可以理解所有内容。但我无法弄清楚一件事。我们假设我们有两个日期:

timedelta

我使用下面的代码解析了t1 = 'Fri 11 Feb 2078 00:05:21 +0400' t2 = 'Mon 29 Dec 2064 03:33:48 -1100' t1,以秒为单位找出它们之间的差异:

t2

正确的答案是def offset(arg): return timedelta(hours = arg / 100, minutes = arg % 100) def normalize(time, offset, sign): return time+offset if sign == '-' else time-offset def main(): t1offset = offset(int(t1[-5:])) t2offset = offset(int(t2[-5:])) t1 = normalize(datetime.strptime(t1[:-6], "%a %d %b %Y %H:%M:%S"), t1offset, t1[-5]) t2 = normalize(datetime.strptime(t2[:-6], "%a %d %b %Y %H:%M:%S"), t2offset, t2[-5]) if t1>t2: print (t1-t2).total_seconds() elif t2>t1: print (t2-t1).total_seconds() else: print 0 ,而我的结果是|t1-t2| = 413962293。它的差异为79200秒 - > 22个小时我究竟做错了什么?我跳过了什么或者我应该考虑什么才能解决这个问题?

3 个答案:

答案 0 :(得分:0)

您将偏移的符号放入offset函数。

'Fri 11 Feb 2078 00:05:21 +0400'[-5:] == '+0400'

我认为它应该只是数字:

'Fri 11 Feb 2078 00:05:21 +0400'[-4:] == '0400'

这样您的偏移量就会有负值,而在normalize函数中,您将再次应用该符号。试试这个:

def main():
    t1offset = offset(int(t1[-4:]))
    t2offset = offset(int(t2[-4:]))

    t1 = normalize(datetime.strptime(t1[:-6], 
           "%a %d %b %Y %H:%M:%S"), t1offset, t1[-5])
    t2 = normalize(datetime.strptime(t2[:-6], 
                   "%a %d %b %Y %H:%M:%S"), t2offset, t2[-5])

    if t1>t2:
        print (t1-t2).total_seconds()
    elif t2>t1:
        print (t2-t1).total_seconds()
    else:
        print 0

但是,您真的不应该使用硬编码字符串索引来解析这样的字符串时间戳。

答案 1 :(得分:0)

时间格式类似于the date/time format used in emails。您可以使用email模块来解析它:

>>> from email.utils import parsedate_tz, mktime_tz
>>> ts1 = mktime_tz(parsedate_tz('Fri 11 Feb 2078 00:05:21 +0400'))
>>> ts2 = mktime_tz(parsedate_tz('Mon 29 Dec 2064 03:33:48 -1100'))
>>> ts1 - ts2
413962293

答案 2 :(得分:-1)

这不是用时区解析日期的最佳方法,但如果你想这样就是你的错误:

def normalize(time, offset, sign):
    return time+offset if sign == '+' else time-offset
                                   ^

归一化应该是另一种方式。

如果您使用的是Python 3+(您应该使用),您可以这样做:

datetime.strptime(t1, "%a %d %b %Y %H:%M:%S %z")

时区说明符在Python 2.x

中不起作用

或使用dateutils lib:

import dateutil
dateutil.parser.parse('Fri 11 Feb 2078 00:05:21 +0400')

编辑:以原始方式执行此操作的更好方法是完全删除符号比较并依赖解析后的数字符号:

def offset(arg):
    return -timedelta(hours = arg / 100, minutes = arg % 100)

def normalize(time, offset):
    return time+offset