Timestamp for the API request

Question:

I am asking if I can link an API request with a timestamp that it sent so I can sort them later because I am sending them concurrently and sometimes they come out of order.

here is my code and I am appending the results I wounder if I can use some function or method to add a timestamp

def get_details(lat, lon):
try:
    time.sleep(1)
    url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+ str(lat) + ',' + str(lon)+'&key='+ API_KEY
    response = requests.get(url)
    data = json.loads(response.text)
    print("/n",response.text)
    ids.append(data['results'][0]['place_id'])
Asked By: Hisham Ibrahim

||

Answers:

Generally servers provide their current time in response header Date for example

import requests
r = requests.get("http://www.example.com")
print(r.headers["Date"])

has given output

Mon, 01 Aug 2022 07:24:16 GMT

format used is compliant with RFC822, you can parse it as follows

from email.utils import parsedate_to_datetime
dt = parsedate_to_datetime(r.headers["Date"])
print(dt)

output

2022-08-01 07:24:16+00:00
Answered By: Daweo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.