1

I'm trying to retrive xml data from a url using python request.get(url) method. But, all the dates are in epochdate format is there a way to get the date in actual datetime format?

I tried changing the headers = {'accept': 'application/xml') and headers = {'accept': 'application/json') and request content in raw format, json,content text formats

headers = {'accept': 'application/xml;odata=verbose; q=0.9, */*;q=0.8'}

val = requests.get(url, auth=HttpNtlmAuth(username, password),headers=headers, verify=False ,headers= headers)

val.text or val.content or val.json return the same date value in epoch format

how date is in xml:

2010-06-11T11:57:55

request.get(url) returned:

("Modified": "/Date(1276257475000)/")

any help here is appreciated.

bharatk
  • 4,202
  • 5
  • 16
  • 30
Praneeth
  • 313
  • 4
  • 9

3 Answers3

0

I think you could use time.strftime if you convert the epoch time into a number, maybe something like this? https://stackoverflow.com/a/12400584/9032733

You could change the format of the resulting date string.

Lee Garcon
  • 182
  • 10
  • Thanks for your reply @lee. But, im looking a solution to get the date in right format thorugh request.get(url) method. As the xml documents is huge one can't interate over all the variables and apply time.strftime function over them. – Praneeth Jun 20 '19 at 00:46
  • I don't think there exists a way to do so, besides performing any operations through `requests.get(url)` shouldn't take any less time than iterating through the xml. – Lee Garcon Jun 20 '19 at 00:51
0

Just convert it to timestamp: more details of utcfromtimestamp

import datetime
date_time = datetime.datetime.utcfromtimestamp(1276257475000//1000.0)
print(date_time)

O/P:

2010-06-11 11:57:55
bharatk
  • 4,202
  • 5
  • 16
  • 30
0

Thanks for your suggestions wrote a function and iterated over the json document as @LeeGarcon suggested. It worked !

import re
import datetime

def _epoch_to_datetime(_dict_element):
   """ This function expects a dictionary (k,v) """
   pattern = "^/Date..............."

   for k,v in _dict_element.items():
       if re.match(pattern,str(v)):
            epoch_time = v[6:-5]
            _dict_element[k] = datetime.datetime.fromtimestamp(int(epoch_time)).strftime('%Y-%m-%d %H:%M:%S')
Praneeth
  • 313
  • 4
  • 9