Parsing timestamp with Python2.4

I want to parse a timestamp from a log file that was written through

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

and then calculate the number of seconds elapsed from this timestamp.

I know that I can do this using datetime.datetime.strptimeto return an object datetimeand then calculate timedelta. The problem is that the function strptimewas introduced with Python 2.5 and I am using Python2.4.4 (updating in my context is not possible).

Any easy way to do this?

+3
source share
4 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)
+5
source
now = datetime.datetime.now()
then = datetime.datetime(*time.strptime('20080227034510' ,'%Y%m%d%H%M%S')[0:6])
difference = now - then
+1
source

time python 2.4 strptime time. datetime, , unix, , .

0

There is also mx.DateTime, which is now free to use, and it is quite easy to handle and more flexible than Python's built-in datetime module for anything. Works in python 2.3+ No * and [0: 6] shenanigans required.

Egenix Download

>>> import mx.DateTime as dt
>>> then = dt.DateTimeFrom(dt.strptime('20040412234551', '%Y%m%d%H%M%S'))
>>> delta = dt.now() - then
>>> delta
<DateTimeDelta object for '2247:13:09:22.31' at 2ab37d666b58>
>>> delta.hours
53941.156198977762
>>> delta.days
2247.5481749574069
0
source

Source: https://habr.com/ru/post/1749029/


All Articles