如何使用endDate(ISO8601和durationDays)计算startDate(UTC)

时间:2015-09-16 01:16:44

标签: python

我现在已经使用ISO8601格式和durationDays结束日期了 我想用UTC格式计算startDate 我不确定我的代码是否正确:
当我得到 datetime.datetime.utcfromtimestamp(startDate_Timestamp)
就像没有时区的2015-09-26 05:08:23一样 所以我加一个。但我不确定它是否正确?

import datetime
import dateutil.tz
import dateutil.parser
import time
import pytz


endDate = "2015-10-10T01:08:03-04:00"
durationDays = 13.666435185185186

#convert endDate from ISO8601 to UTC time
pyTime =  dateutil.parser.parse(endDate)
endDateUTC =  pyTime.astimezone(dateutil.tz.tzutc())
print endDateUTC

#convert pyTimeUTC to Timestamp
pyTimeUTC_Timestamp = time.mktime(endDateUTC.timetuple())
print pyTimeUTC_Timestamp


# durationDays * 24 * 60 * 60
durationtimes = durationDays* 24 * 60 * 60

#get startdate Timestamp
startDate_Timestamp = pyTimeUTC_Timestamp - durationtimes
print startDate_Timestamp

# convert startDate_Timestamp back to UTC time
startDateUTC = datetime.datetime.utcfromtimestamp(startDate_Timestamp).replace(tzinfo=pytz.utc)
print startDateUTC

1 个答案:

答案 0 :(得分:0)

您可以像这样测试它,看看它是否给出了您想要的日期:

(endDateUTC-startDateUTC).days
13
(endDateUTC-startDateUTC).seconds
28780