lundi 22 décembre 2014

Compare launch time of EC2 instance and current time in python

I extract the launch_time from EC2 instance, it returns a unicode string like this:



2014-12-22T08:46:10.000Z



I use dateutil parser to convert it to datetime with



launch_time = parser.parse(instance.launch_time)


so I get lunch_time after converted like this:



2014-12-22 08:46:10+00:00



And I want to compare this launchtime with current time to see how long this instance has been running.


I get current_time with:



current_time = datetime.datetime.now()


and I get it like this:



2014-12-22 11:46:10.527010



Now I have two timestamps, I have this function



def timeDiff(launch_time, current_time):
running_time = current_time - launch_time
return running_time.seconds/60


I expect the result would be 180 minutes (3 hours). But I got this error:



TypeError: can't subtract offset-naive and offset-aware datetimes



I think there's obvious difference between these two timestamps. I need to compare exactly date and time to see how long it has been running. I couldn't find a proper way to solve this. Any thoughts appreciated!





Aucun commentaire:

Enregistrer un commentaire