简体   繁体   English

如何从 Python 中的负纪元创建日期时间

[英]how to create datetime from a negative epoch in Python

First timer on StackExchange. StackExchange 上的第一个计时器。

I am working with ArcGIS Server and Python. While trying to execute a query using the REST endpoint to a map service, I am getting the values for a field that is esriFieldTypeDate in negative epoch in the JSON response.我正在使用 ArcGIS Server 和 Python。在尝试使用 REST 端点对 map 服务执行查询时,我在 JSON 响应中获取负纪元 esriFieldTypeDate 字段的值。 The JSON response looks like this: JSON 响应如下所示:

    {
  "feature" :
  {
    "attributes" : {
      "OBJECTID" : 11,
      "BASIN" : "North Atlantic",
      "TRACK_DATE" : -3739996800000,
    }
    ,
    "geometry" :
    {
      "paths" :
      [
        [
          [-99.9999999999999, 30.0000000000001],
          [-100.1, 30.5000000000001]
        ]
      ]
    }
  }
}

The field I am referring to is "TRACK_DATE" in the above JSON. The values returned by ArcGIS Server are always in milliseconds since epoch.我指的字段是上面JSON中的“TRACK_DATE”。ArcGIS Server返回的值总是自纪元以来的毫秒数。 ArcGIS Server also provides a HTML response and the TRACK_DATE field for the same query is displayed as "TRACK_DATE: 1851/06/27 00:00:00 UTC". ArcGIS Server 还提供 HTML 响应,同一查询的 TRACK_DATE 字段显示为“TRACK_DATE:1851/06/27 00:00:00 UTC”。

So, the date is pre 1900 and I understand the Python in-built datetime module is not able to handle dates before 1900. I am using 32-bit Python v2.6.所以,日期是 1900 年之前的,我知道 Python 内置日期时间模块无法处理 1900 年之前的日期。我使用的是 32 位 Python v2.6。 I am trying to convert it to a datetime by using我正在尝试通过使用将其转换为日期时间

datetime.datetime.utcfromtimestamp(float(-3739996800000)/1000)

However, this fails with但是,这失败了

ValueError: timestamp out of range for platform localtime()/gmtime() function

How does one work with epochs that are negative and pre 1900 in Python 2.6?如何在 Python 2.6 中处理负数和 1900 年前的纪元? I have looked at similar posts, but could not find one that explains working with negative epochs.我看过类似的帖子,但找不到解释负时代工作的帖子。

This works for me: 这对我有用:

datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=(-3739996800000/1000))

datetime.datetime(1851, 6, 27, 0, 0) datetime.datetime(1851, 6, 27, 0, 0)

This would have been better asked on StackOverflow since it is more Python specific than it is GIS-specific. 这可以在StackOverflow上得到更好的要求,因为它比特定于GIS的Python更具体。

if timestamp < 0:
    return datetime(1970, 1, 1) + timedelta(seconds=timestamp)
else:
    return datetime.utcfromtimestamp(timestamp)

You can accomplish this using the datetime module's datetime and timedelta functions. 您可以使用datetime模块的datetimetimedelta函数来完成此操作。

The other answers divide the timestamp by 1000 to convert milliseconds to seconds. 其他答案将时间戳除以1000以将毫秒转换为秒。 This is unnecessary, since the timedelta function can take milliseconds directly as a parameter. 这是不必要的,因为timedelta函数可以直接用毫秒作为参数。 It might therefore be cleaner to do something like this: 因此,做这样的事情可能更干净:

datetime.datetime(1970, 1, 1) + datetime.timedelta(milliseconds=-3739996800000)

which gives datetime.datetime(1851, 6, 27, 0, 0) , as you'd expect. 它给出了datetime.datetime(1851, 6, 27, 0, 0) ,正如你所期望的那样。

The fromtimestamp() and the utcfromtimestamp() methods have been updated now to handle negative timestamps. fromtimestamp() 和 utcfromtimestamp() 方法现已更新以处理负时间戳。 You can directly use the fromtimestamp() method in the datetime module to convert the epoch to datetime.您可以直接使用 datetime 模块中的 fromtimestamp() 方法将 epoch 转换为 datetime。 Don't forget to convert the milliseconds to seconds.不要忘记将毫秒转换为秒。 Remember, the fromtimestamp() method will give a datetime object according to your timezone.请记住,fromtimestamp() 方法将根据您的时区给出日期时间 object。

To calculate the datetime object in UTC time, you can use the utcfromtimestamp() method.要以 UTC 时间计算日期时间 object,可以使用 utcfromtimestamp() 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM