【发布时间】:2016-07-11 14:39:54
【问题描述】:
是的,我刚刚通过this HubSpot endpoint 上传了大约 26,000 条笔记,我注意到一堆上传的笔记有非常错误的时间戳(例如,不是回溯或最新的日期,它们被抛到了遥远的未来)。
我已将问题追溯到我的代码的一部分,该代码使用Delorean 模块,以便更轻松地解析时间并将时间转换为纪元时间戳。问题似乎是,当我通过 .format() 函数使用变量插值时 - 它似乎以某种方式改变了一些东西。
示例 1 - 无插值。
def ref_date_epoch():
parseDate = parse("29/04/2014 00:00:00 -0700")
getEpoch = parseDate.epoch
shiftEpoch = epoch(getEpoch).shift("US/Eastern")
convertEpoch = shiftEpoch.epoch
testing = int(convertEpoch)
return "{testing}000".format(testing=testing)
print(ref_date_epoch())
sys.exit()
上面的示例返回1398754800000 作为纪元时间戳,它将转换为正确的日期 - 29/04/2014。
示例 2 - 使用插值。
def ref_date_epoch(datestr):
if len(datestr) > 0:
parseDate = parse("{csvDate} 00:00:00 -0700".format(csvDate=datestr))
getEpoch = parseDate.epoch
shiftEpoch = epoch(getEpoch).shift("US/Eastern")
convertEpoch = shiftEpoch.epoch
testing = int(convertEpoch)
return "{testing}000".format(testing=testing)
else:
None
print(ref_date_epoch(row[2]))
sys.exit()
这一次,上面的示例返回1597302000000 作为纪元时间戳,这真的非常错误——它最终是13/08/2020。详细地说,datestr 参数接受列表 row[2],它引用了包含日期的 csv 中行的索引。
示例 3. 没有 .format() 函数:
def ref_date_epoch(datestr):
if len(datestr) > 0:
parseDate = parse(datestr)
getEpoch = parseDate.epoch
shiftEpoch = epoch(getEpoch).shift("US/Eastern")
convertEpoch = shiftEpoch.epoch
testing = int(convertEpoch)
return "{testing}000".format(testing=testing)
else:
None
print(ref_date_epoch(row[2]))
sys.exit()
这仍然返回1597276800000。似乎仅仅间接引用日期的行为似乎改变了时间。什么给了?
说了这么多 - 如果没有办法让 Delorean 以这种方式工作,那么有人知道将日期字符串转换为毫秒时间戳的方法吗?
编辑:我也忘了提到许多注释也是正确的 - 我运行脚本的计算机会影响纪元时间戳的创建吗?
【问题讨论】:
标签: python-2.7 time epoch hubspot delorian