Search code examples
dictionarygoogle-cloud-platformtypesgoogle-cloud-logging

"Value 0:00:00.416559 has unexpected type <class 'datetime.timedelta'>" when diff of two timestamps as value in jsonPayload gcloud logger.log_struct()


Code:

from datetime import datetime
start = datetime.now()
sleep(1)
diff = datetime.now() - start

When saving the diff variable as a value of a dictionary "my_dict" that will then be written to a jsonPayload output by the google cloud logging module with its function log_struct(my_dict), I get:

Value 0:00:00.416559 has unexpected type <class 'datetime.timedelta'>

How to get the format as seconds instead to get rid of the datatype error?


Solution

  • Change to:

    diff = (datetime.now() - start).total_seconds()
    

    Taken from Python finding difference between two time stamps in minutes.