将日期时间字符串转换为毫秒UNIX时间戳 [英] Convert a datetime string to millisecond UNIX time stamp
问题描述
我试图关联从Java作业和linux性能监视工具 perf
(特别是 perf stat
)。
I'm trying to correlate the timing information obtain from a java job and linux performance monitoring tool perf
(specifically perf stat
).
使用
String tstamp0 = String.valueOf(System.currentTimeMillis() );
(本质上是从纪元开始的毫秒数)
,而 perf
给出了过程开始的时间,仅记录了随后的记录显示经过的时间。
The timing information from java is obtained using
String tstamp0 = String.valueOf(System.currentTimeMillis());
(This is essentially time in milliseconds from epoch)
whereas perf
gives the time the process has began and the subsequent recording only show the time elapsed.
我想做的是,将从 perf stat
获得的计时信息转换为毫秒,然后在这里是我失败的地方。我正在使用 Python
处理此问题。
What I would like to do is, convert the timing information obtained from the perf stat
to milliseconds, and here is where I'm failing. I'm approaching this problem in Python
.
这段代码为我提供了 perf
tailit = "head -n 1 " + dataset_path
process = subprocess.Popen(tailit, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
date_time = out.split("\n")[0].split(" ")[4:]
date = date_time[3] + "-" + date_time[0] + "-" + date_time[1]
time = date_time[2]
#TIMESTAMP
INIT_TIME = datetime.datetime.strptime(date + ' ' + time, "%Y-%B-%d %H:%M:%S") + datetime.timedelta(seconds=0.01)
#df is pandas data frame
df['STAMPME'] = df['TCOUNT'].apply(lambda x: foobar(datetime.timedelta(seconds=x) + INIT_TIME))
其中foobar是以下将字符串转换为时间戳的毫秒
here foobar is the following to convert a string to timestamp in milliseconds, but it doesn't make sense.
def foobar(INIT_TIME):
d = datetime.datetime.strptime(str(INIT_TIME), "%Y-%m-%d %H:%M:%S.%f").strftime('%s')
d_in_ms = int(d)*1000
return (d_in_ms)
任何帮助将不胜感激。
编辑:先前的问题没有解决将 java时间戳(currentTimeMillis())
与日期时间与毫秒相关的问题。
Prior questions were not addressing the problem of correlating the java timestamp (currentTimeMillis())
to the datetime with milliseconds.
例如:函数 foobar
:
的INIT_TIME设置为 2017-05-11 10: 56:54.203
,则返回值为 1494493014000
,而返回值应为 1494500214203
For instance: with the function foobar
:
with INIT_TIME set as 2017-05-11 10:56:54.203
, the return value is 1494493014000
when it instead should be 1494500214203
推荐答案
我认为已经解决了问题。
好像 foobar
函数在GMT + 2中返回时间,而Java作业在 GMT
。因此,如果时间增量为 +2
,我就可以解决。
I think figured out the problem.
Looks like foobar
function is returning time in GMT+2, whereas the java job was returning time in GMT
. so with a timedelta of +2
, I could solve it.
这篇关于将日期时间字符串转换为毫秒UNIX时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!