Convert timestamp to epoctime with milisecond

Hi , this is my first question.

I would like convert @timestamp to epoch-time with millisecond
Because I want millisecond not second , I try check the latency through the messaging queue.

Here is my code,


ruby {
code => "event.set('epoc', event.get('@timestamp').to_i)"
}


Is there the other method ? ( to_i method convert to second)

You could convert to float, multiply by 1000 and convert the result to int. (But maybe there is a more elegant way.)

ruby {
    code => "event.set('epoc', (event.get('@timestamp').to_f*1000).to_i)"
  }

Thank you quick response. Jenni.
I didn't know to_f method.

Though I try it , millisecond part is all zero (like below)

  a)my old code (to_i method)
   - code  
               code => "event.set('epoc', event.get('@timestamp').to_i)"
   -  output
               "epoc" => 1522983522,
  b)new code (you suggested)
   - code  
               code => "event.set('epoc', (event.get('@timestamp').to_f * 1000).to_i)"
   -  output
                "epoc" => 1522983522000,

I want accurate value of milli second ( "000" of 21522983522000").
Could you have more idea ?

Mh. That's strange. I had tried that before posting and it seemed to work for me:

"@timestamp" => 2018-04-06T07:59:21.130Z,
"epoc" => 1523001561130

"@timestamp" => 2018-04-06T08:01:36.846Z,
"epoc" => 1523001696846

Is there a chance that you are losing that level of precision one step earlier when you are creating the timestamp? What does the original timestamp look like in the rubydebug output?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.