mongodb - python: converting unix/posix epoch timestamp to datetime (after monogdb, date_util json export) -


so received data stored in mongodb exported json via bson.json_util.dumps() function. when try reconvert datetime objects, none of usual parsers work. here's sample:

{ u'_group': 0, u'_range': u'', u'canon': 0, u'comment_id': 0, u'created': {u'$date': 1491468607000l}, u'description': u'' } 

and dates this:

[{u'$date': 1491457629000l}, {u'$date': 1491458993000l}, {u'$date': 1491457072000l}, {u'$date': 1491457035000l}, {u'$date': 1491457330000l}, {u'$date': 1491458323000l}, {u'$date': 1491458147000l}, {u'$date': 1491458277000l}, {u'$date': 1491459839000l}, {u'$date': 1491466340000l}, {u'$date': 1491463804000l}, {u'$date': 1491464304000l}, {u'$date': 1491465145000l}, {u'$date': 1492073749000l}, {u'$date': 1492073750000l}, {u'$date': 1492075415000l}, {u'$date': 1492155813000l}, {u'$date': 1492608582000l}, {u'$date': 1492671698000l}, {u'$date': 1493001047000l}, {u'$date': 1493714117000l}] 

my tests:

y = {u'$date': 1491457629000l} >>> y['$date'] 1491457629000l 

and various failed attempts:

print(     datetime.datetime.fromtimestamp(         y['$date']     ).strftime('%y-%m-%d %h:%m:%s') ) 

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

from dateutil import parser parser.parse(str(y['$date'])) 

overflowerror: python int large convert c long

utc_time = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=y['$date']) 

overflowerror: date value out of range

it seems unix timestamp converter should able read this. why isn't working? need year , month, timezone specificity outside scope of demands.

note (to downvoter): not duplicate of converting epoch time milliseconds datetime because solution here use different json parser mirrors way converted out of mongodb, instead of trying reformat unrelated timestamp parser. had researched other link , found didn't solve problem.

have tried removing last 3 zeroes (dividing 1000)?

error seems coming conversion, value in milliseconds, python datetime.datetime.fromtimestamp expects seconds seems.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -