python datetime relativedelta给出不同的值

时间:2015-02-22 21:50:29

标签: python timedelta python-dateutil

我有以下代码让星期五总是晚于基准日期。

def get_next_settlement_date(base_date):
    settlement_date = base_date + relativedelta(days=1, weekday=FR)

我有以下单元测试:

for i in range(7):
    base_date = localtime(now() + timedelta(days=i))
    settlement_date = Settlement.objects.next_settlement_date(base_date)
    logger.debug("{} - {}".format(base_date, settlement_date))
    self.assertTrue(settlement_date > localtime(now()), '{} > {}'.format(settlement_date, localtime(now())))
    self.assertTrue(settlement_date > base_date)

当我的同事运行时,它出现了错误:

self.assertTrue(settlement_date > localtime(now()), '{} > {}'.format(settlement_date, localtime(now())))
AssertionError: 2015-02-20 00:00:00+13:00 > 2015-02-20 12:13:34.954536+13:00

该错误表示基于2015-02-20 12:13:34.954536+13:00结算日期为2015-02-20 00:00:00+13:00

当我运行测试时,它运行良好,所以当我的同事运行它时,我试图模仿相同的日期时间:

for i in range(7):
    import pytz
    from datetime import datetime

    auckland = pytz.timezone('Pacific/Auckland')
    base_date = auckland.localize(datetime.strptime('2015-02-20 12:13:34.954536', '%Y-%m-%d %H:%M:%S.%f') + timedelta(days=i))
    # base_date = localtime(now() + timedelta(days=i))
    settlement_date = Settlement.objects.next_settlement_date(base_date)
    logger.debug("{} - {}".format(base_date, settlement_date))
    self.assertTrue(settlement_date > localtime(now()), '{} > {}'.format(settlement_date, localtime(now())))
    self.assertTrue(settlement_date > base_date)

单位测试通过并基于2015-02-20 12:13:34.954536+13:00生成的结算日期为2015-02-27 00:00:00+13:00

输出:

2015-02-20 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00
2015-02-21 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00
2015-02-22 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00
2015-02-23 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00
2015-02-24 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00
2015-02-25 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00
2015-02-26 12:13:34.954536+13:00 - 2015-02-27 00:00:00+13:00

我很难理解为什么它在那个时间点为我的同事产生了错误的结算日期。

0 个答案:

没有答案