在 Bash/Python 中转换 CIM_DATETIME [英] Convert CIM_DATETIME in Bash/Python
问题描述
我有一个从 wmic os get lastbootuptime
获取的 CIM_DATETIME
时间:
I have a CIM_DATETIME
time acquired from wmic os get lastbootuptime
:
20190309221835.234400-480
CIM_DATETIME
的格式如下:
yyyymmddHHMMSS.mmmmmmsUUU
如何在 Bash 或 Python 中将其转换为更人性化的内容?
How can I convert that to something more human-friendly in Bash or Python?
最理想的格式是:
Sunday, March 10th, 2019 @ 16:01:30 UTC
那或者我是否可以将其转换为诸如 Unix 时间戳之类的正常内容.如果您想知道我对非 Windows 语言的需求(Batch 中已经有很多解决方案)是因为我进行计算机取证,但我将 Linux 作为我的主要操作系统运行,所以我希望能够轻松解释我从中获得的日期WMI 变成我可以轻松解释的东西.它太小众了,我什至无法在网上找到一个工具来做到这一点.:/
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. :/
谢谢.
推荐答案
这是我在 Bash 中提出的解决方案.输出似乎是正确的,并且与几天前我启动 Windows 框的时间一致——就在午夜之前.我还删除了@",因为它几乎没有让时间更好看并制作它,因此由于 date
命令,我不得不使用 CIM_DATETIME
在以下脚本中删除它无法解析.
Here is the solution in Bash i came up with. The output seems to be correct and align with the time a couple of days ago I booted up the Windows box -- just before midnight. I also removed the "@" because it barely made the time nicer to look at and made it so I had to remove it in following scripts using CIM_DATETIME
due to the date
command not being able to parse that.
CIM_DATETIME="20190309221835.234400-480"
date="${CIM_DATETIME:0:8}"
date="$(date -d "$date" '+%A, %B%e, %Y')"
time="${CIM_DATETIME:8:6}"
time="$(echo "$time" | sed 's/../&:/g;s/:$//')" # Adds : between hours, minutes and seconds of time so it can be parsed correctly
timezone="${CIM_DATETIME:21}"
time="$time $timezone"
time="$(date -d "$time" '+%r %Z')"
echo "$date $time"
示例输出:美国东部时间 2019 年 3 月 9 日星期六晚上 11:38:35
感谢您提供的所有解决方案.
Thank you for all the solutions.
这篇关于在 Bash/Python 中转换 CIM_DATETIME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!