0

I have a nanosecond integer need to be formated as Time object in ruby. How can i get Time object from nanosecond in ruby ?

I tried

Time.strptime '2318482693000', '%N'

but the time returned is current time not the exact time given (in nanoseconds)

Chathurika Senani
  • 716
  • 2
  • 9
  • 25
Yana Agun Siswanto
  • 1,932
  • 18
  • 30
  • 2
    If this represents 2318.48 seconds in nanoseconds then this can't be converted back to a `Time` since it's obviously based on some offset that isn't given. – tadman Jan 06 '17 at 07:02
  • 1
    What's your expected result? – Stefan Jan 06 '17 at 10:22
  • @tadman I realized it may not started from 1 january 1970. There was an answer i would like to accept that has been deleted. – Yana Agun Siswanto Jan 06 '17 at 14:50
  • @Stefan I expected ruby to aware that the value i inputed is in nano second. Expected it not to loose any precission to compare it with other time object. But not sure if i do it all right. – Yana Agun Siswanto Jan 06 '17 at 14:51
  • 1
    @bekicot I mean the expected output time. 2,318,482,693,000 nanoseconds are merely 2,318 seconds (as already noted by tadman). What time should that represent? Probably not 1970-01-01 00:38 ... – Stefan Jan 06 '17 at 14:55
  • @Stefan Yes you are right, that was an answer that pointed it out. It was deleted. I believe he was right. Thank you for pointing it out. and sory for my dumbness. I should've aware that it is indeed 2318.48 second. – Yana Agun Siswanto Jan 06 '17 at 15:02
  • 1
    I just wanted to know which result you were inspecting. If you want to get 1970-01-01 00:38, that's fine, but you should state that in your question. If that number is supposed to represent another time, we can probably help you to figure out the correct conversion. But without any further information, you are getting answers based on guesses. – Stefan Jan 06 '17 at 15:12
  • @Stefan Unfortuantely, i dont know the exact time, i do believe it somewhere in 2013, 2014, and 2016. I got the data from http://www.bmi.teicrete.gr/index.php/research/mobiact – Yana Agun Siswanto Jan 06 '17 at 15:28
  • 1
    These values are likely based on the start of some test. You can convert them to hours, minutes and seconds, but to a date is impossible without more information. Ruby's Time class is a representation of a chronological time, not an elapsed time. – tadman Jan 06 '17 at 18:22

1 Answers1

1

You'd better consider use Time#at. But you have to adjust your nano elapse a little bit.

chailong
  • 286
  • 3
  • 11