Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing timestamp with Python2.4

I want to parse a timestamp from a log file that has been written via

datetime.datetime.now().strftime('%Y%m%d%H%M%S')

and then compute the number of seconds that have passed since this timestamp.

I know I could do it with datetime.datetime.strptime to get back a datetime object and then compute a timedelta. Problem is, the strptime function has been introduced with Python 2.5 and I'm using Python2.4.4 (an upgrade is not possible in my context).

Any easy way to do this?

like image 671
Johannes Charra Avatar asked Dec 04 '25 19:12

Johannes Charra


1 Answers

>>> ts = time.mktime(time.strptime('20040412234551', '%Y%m%d%H%M%S'))
>>> ts
1081809951.0
>>> datetime.datetime.fromtimestamp(ts)
datetime.datetime(2004, 4, 12, 23, 45, 51)
like image 102
SilentGhost Avatar answered Dec 06 '25 10:12

SilentGhost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!