Convert a datetime string to millisecond UNIX time stamp

醉酒当歌 提交于 2020-01-17 06:54:50

问题


I'm trying to correlate the timing information obtain from a java job and linux performance monitoring tool perf (specifically perf stat).

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.

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.

This piece of code is giving me the timing information from 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))

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)

Any help will be appreciated.

EDIT: Prior questions were not addressing the problem of correlating the java timestamp (currentTimeMillis()) to the datetime with milliseconds.

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


回答1:


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.



来源:https://stackoverflow.com/questions/43931906/convert-a-datetime-string-to-millisecond-unix-time-stamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!