Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create datetime from a negative epoch in Python

Tags:

python

arcpy

First timer on 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. The JSON response looks like this:

    {
  "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. 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".

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. 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? I have looked at similar posts, but could not find one that explains working with negative epochs.

like image 1000
Anand Avatar asked Jun 21 '13 07:06

Anand


People also ask

How do I convert epoch time to local time?

=(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number). For other time zones: =((A1 +/- time zone adjustment) / 86400) + 25569.

What is negative epoch time?

The Unix epoch is at the beginning of 1970 meaning that any timestamp prior to 1970 needs to be represented as a negative number representing the number of seconds until January 1st, 1970 00:00:00 UTC.


1 Answers

This works for me:

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

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.

like image 142
Jason Scheirer Avatar answered Sep 23 '22 00:09

Jason Scheirer