shell函数from_unixtime,将日期时间字符串转换为毫秒UNIX时间戳

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值