【问题标题】:Variable Interpolation changes the result of Delorean functions变量插值改变了 Delorean 函数的结果
【发布时间】: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


    【解决方案1】:

    没关系,看起来它是格式不佳的 CSV 的混合体(使用 dd/mm/yy 而不是 dd/mm/yyyy 和复杂的代码。我现在正在使用它,它似乎正在工作:

    def ref_date_epoch():
            parseDate = parse(row[2])
            getEpoch = parseDate.epoch
            shiftEpoch = epoch(getEpoch).shift("US/Eastern")
            convertEpoch = shiftEpoch.epoch
            testing = int(convertEpoch)
            return "{testing}000".format(testing=testing)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多