I'm using Snort, which generates timestamps in MM-DD/time format, such as:
06/18-19:31:05.688344
I want to convert this to a Python timestamp, including the current year. What's the most pythonic way?
Use the datetime module's strptime function.
datetime
strptime
>>> import datetime >>> datetime.datetime.strptime(str(datetime.datetime.now().year) + ... '/' + '06/18-19:31:05.688344', '%Y/%m/%d-%H:%M:%S.%f') datetime.datetime(2015, 6, 18, 19, 31, 5, 688344)
Check out snorts configuration. In the output section you can setup "seconds" as an output field. This is the timestamp in unix format which is better to work with. If u still need to convert it :
datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
Use the
datetime
module'sstrptime
function.