Arthur Niu
Arthur Niu

Reputation: 189

Python: how to show timestamp other than datetime function

I get below dictionary data from aws. In python, how can I get it show timestamp instead of datetime.datetime(2020, 10, 26, 10, 57, 19, 215000, tzinfo=tzlocal()) there?

Thanks,

{ "ConfigRuleName": "required-tags", "OrderingTimestamp": datetime.datetime( 2020, 10, 26, 10, 57, 19, 215000, tzinfo=tzlocal() ), "ResourceId": "arn:aws:cloudformation:us-east-1:553763988947:stack/es-edge-security-headers-kells/f1924880-8311-11ea-9a26-0af77bd56d08", "ResourceType": "AWS::CloudFormation::Stack", }

Upvotes: 0

Views: 193

Answers (2)

Johnson Francis
Johnson Francis

Reputation: 271

Not sure what is that you are looking for.. Isn't it already a datetime object ?

from datetime import datetime
from dateutil.tz import *
d=datetime(2020, 10, 26, 10, 57, 19, 215000, tzinfo=tzlocal())
print(d.timestamp())
print(str(d))

Output :

1603690039.215
2020-10-26 10:57:19.215000+05:30

Upvotes: 1

David Oldford
David Oldford

Reputation: 1175

Assuming you are using the normal definition of a timestamp you can just call the datetime timestamp function. This will give you a float representing seconds from the epoch (this is the norm for Python in C it would be an int).

So if you put in the line:

ThatDictionary["OrderingTimeStamp"] = ThatDictionary["OrderingTimeStamp"].timestamp()

You'll permanently convert that value in the dictionary to a timestamp. You can convert it back if you want later or create a copy of the dict and do that to print it out or just run the timestamp() method on that element whenever you use it.

Upvotes: 0

Related Questions