0

Trying to use datetime to get an age of ec2 instance by comparing launch_time to current time. All working fine by using format below:

datetime.datetime.strptime(instance.launch_time, "%Y-%m-%dT%H:%M:%S.%fZ")

Unfortunately I've got an one with 0 microseconds, so getting an error about not matching format:

time data '2015-03-16T03:21:05Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ')

2015-03-16T03:02:12.910Z

2015-03-16T03:21:05Z - this one is problematic

2015-03-25T09:19:34.018Z

Any idea how to get around this? It looks like datetime is the easiest way to get this sorted but if there are quicker way of doing that, happy to see other options. FYI, comparision has to be done up to hour, so don't care about seconds. Thanks.

ZdaR
  • 22,343
  • 7
  • 66
  • 87
andre
  • 1
  • 1
  • If you only want up to the hour, why not e.g. `strptime(instance.launch_time[:13], "%Y-%m-%dT%H")`? – jonrsharpe Apr 28 '15 at 07:27
  • Or you catch that exception... – filmor Apr 28 '15 at 07:37
  • possible duplicate of [Compare launch time of EC2 instance and current time in python](http://stackoverflow.com/questions/27601551/compare-launch-time-of-ec2-instance-and-current-time-in-python) – rafaelc Apr 28 '15 at 07:41
  • @jonrsharpe - thanks for quick response, working perfectly :) – andre Apr 28 '15 at 08:08

0 Answers0