Get time object from nano second? - ruby

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)

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

Related

How to measure time elapsed in Ballerina?

I am currently trying to get the execution time for a ballerina function. For that I need to get the timestamp before and after calling the function.
How to get the timestamp in milliseconds using ballerina?
I have tried time module and I did not find a direct way to get it.
For the common use case to calculate the elapsed time, it is better to use the following API instead of time:UtcNow()
decimal now = time: monotonicNow();
Do note, time:monotonicNow does not guarantee accurate reading of the utc timestamp. It only guarantees continuity i.e it guarantees consistent value increase in subsequent calls with nanoseconds precision.
The time:utcNow() function provides all needed details to get the accurate timestamp in milliseconds. This returns a tuple of length 2. The first member of the tuple is int representing an integral number of seconds from the epoch. The second member of the tuple is a decimal giving the fraction of a second. Default precision is in nanoseconds. You can manipulate the time:Utc tuple as follows to get the time in milliseconds.
time:Utc now = time:utcNow();
int timeInMills = <int>(<decimal>now[0] + now[1]) * 1000;
Do note, this can result in negative interval due to the clock synchronisation

How to convert QueryPerformanceCounter to Unix nanoseconds (epoch)

I am using QueryPerformanceCounter windows syscall in order to get high-precision timestamp.
I need to convert it to unix epoch (in nanoseconds) as I am going to pass the value to an API that needs it in this format
Could anybody help me understanding how to accomplish this?
QueryPerformanceCounter does not return a timestamp with a fixed offset to the current time (as in UTC, or, the time shown by a wall clock), so you cannot convert it to UNIX time.
However, time differences measured using QueryPerformanceCounter can be converted to nanoseconds (or any time unit) by dividing by the result of QueryPerformanceFrequency (to get seconds) and multiplying by, e.g., 10^9 to get nanoseconds.
As per comments above, QueryPerformanceCounter is not the right way to go.
I have found GetSystemTimePreciseAsFileTime that suits my needs

what is this elasticsearch timestamp format?

I'm reverse engineering a query from a kibana board and it has timestamp values in it like '1408884022624'.
In reading over the elastic search date mapping docs I don't see anything in there regarding what (appears to be) some sort of millisecond or tick format. Could someone tell me what the number above represents in my query? (I'm pretty sure we're not using a custom date format.)
It's the number of milliseconds since the beginning of Unix Epoch time, 00:00:00 UTC January 1 1970. Sometimes referred to as Java Epoch time. Technically it's not Unix Epoch time as that's tracked as the number of seconds since the above date, but many tools/converters handle both seconds and milliseconds.
Care should be taken, though, as it's quite easy to accidentally get the time in one format (let's say seconds) and pass it to a function or method expecting it in the other.
http://en.wikipedia.org/wiki/Unix_time
http://www.javaworld.com/article/2074293/core-java/groovy--java--and-the-unix-epoch-time.html

Alternative to Unix Time for transactional record keeping

I am planning on starting a project that will need to record timestamps of incoming transactions. I appreciate that Unix Time is an integer value and I can use this type of functionality to my advantage. However, Unix Time only measures in seconds. As a minimal requirement I need to record transaction times at the millisecond level.
I know that there are ways that I could get around this issue, but I was wondering if there was another standardized way of representing time data that also represented milliseconds (or, some factor of sub-milliseconds) in the time value that is fully expressed as an integer value since epoch.
Does such a time format exist? FYI, so long as the date data-type is standardized, I don't care what system this is native in. I can code my own implementation, however, I would like to use an existing date/time format, rather than create my own.
One place where such a standard is used is ECMAScript / Javascript. Javascript date objects use milliseconds since January 1, 1970, midnight UTC for their numerical integer representation. This is detailed here.
You can test this using your browser's console:
var d = new Date();
console.log(d.getTime()); // yields integer milliseconds since epoch
So yes, there is prior art for such a use.
date +%s
outputs timestamp in seconds
date +%s%N
returns timestamp in nanoseconds
To get milliseconds divide the nanoseconds by 1 000 000
UNIX time is not appropriate for time stamping transactions because it does some weird stuff, inserting leap seconds on occasion, thus making it so that you won't be able to add and subtract time stamps reliably, nor sort transactions by timestamp.
A more appropriate standard for timestamps is TAI https://www.nist.gov/pml/time-and-frequency-division/nist-time-frequently-asked-questions-faq#tai . TAI is stored in the same way as UNIX time as a number of seconds and or microseconds and or nanoseconds since the UNIX epoch, however, it is the true number, no leap seconds are added or removed. This means that you can actually add and subtract TAI timestamps to get elapsed time and TAI timestamps are always sortable. Unfortunately, support for TAI timestamps is somewhat limited. For example, linux added support for TAI timestamps only recently in version 3.10 python added this support only in version 3.9/time.html?highlight=time#module-time

can i increment date using xpath?

i need to increment current date by 7 days and i'm wondering if it's possible to do that using a xpath function.
thanks !!
It is possible in XPATH 2.0. There are a number of date functions.
current-date() + xs:dayTimeDuration('P7D')
If your date is in some numeric format (CTIME, seconds since 1970 (32 bit int), or FILETIME, 100 nanosecond counts since 1601 (64 bit)) then incrementing time is easy. Just add the correct number of seconds (or 100 nanosecond intervals) to the time.
You could convert your format to CTIME via the Java time function library, add the correct number of seconds (86,400 seconds in a day), and then convert it back to your string format, I suppose. Probably not the worlds most efficient approach though.

Resources