Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert CIM_DATETIME in Bash/Python

I have a CIM_DATETIME time acquired from wmic os get lastbootuptime:

20190309221835.234400-480

CIM_DATETIME is formatted as follows:

yyyymmddHHMMSS.mmmmmmsUUU

How can I convert that to something more human-friendly in Bash or Python?

Format that would be most desierable is:

Sunday, March 10th, 2019 @ 16:01:30 UTC

That or if I could convert it to something normal like a Unix timestamp. If you're wondering my need for this in non-Windows languages (there are already many solutions in Batch) is because I do computer forensics but I run Linux as my main OS so I want to be able to easily interpret dates I get from WMI into something I can interpret easily. It's so niche I'm unable to even find a tool online to do it. :/

Thank you.

like image 437
anti-ansi Avatar asked Sep 20 '25 01:09

anti-ansi


2 Answers

but for computer forensics the timezone really is preferable.

combinatorist's answer already does half of the work. It can easily be expanded to take the time zone offset into account:

import datetime
CIM_DATETIME ='20190309221835.234400-480'
dt = datetime.datetime.strptime(CIM_DATETIME[:-4], '%Y%m%d%H%M%S.%f')
# dt is now datetime.datetime(2019, 3, 9, 22, 18, 35, 234400)
dt -= datetime.timedelta(minutes=int(CIM_DATETIME[-4:]))
# dt is now datetime.datetime(2019, 3, 10, 6, 18, 35, 234400)
dt.strftime("%A, %B %d., %Y @ %X UTC")
like image 147
Armali Avatar answered Sep 21 '25 16:09

Armali


You can get the timestamp without the timezone with this:

from datetime import datetime
datetime.strptime(CIM_DATETIME.split('-')[0], '%Y%m%d%H%M%S.%f')

[Notice, that assumes a negative timezone offset to split the string.] The catch with the timezone is it is expressed in minutes so you have to divide by 60. But do you need to read the timezone? If it will always be the same then you will be able to bring that in separately.

like image 27
combinatorist Avatar answered Sep 21 '25 15:09

combinatorist