4

I know we can use clock_gettime(CLOCK_MONOTONIC).

Question i try asking is that if i need the time in nanoseconds say from epoch, it would be a huge number.

For example:

  • Seconds since epoch is 13438461673 so 13438461673 * 1000000000

How do i fit it inside a 64bit integer?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
samairtimer
  • 826
  • 2
  • 12
  • 28
  • 1
    Is your real question how to store an integer larger then 64bit? – alk Mar 15 '15 at 15:25
  • 5
    You *don't*, instead you use e.g. a `timespec` structure, like the one filled in by [`clock_gettime`](http://man7.org/linux/man-pages/man2/clock_gettime.2.html). – Some programmer dude Mar 15 '15 at 15:26
  • So you might like to read here: http://stackoverflow.com/questions/15601430/storing-and-printing-integer-values-greater-than-264 – alk Mar 15 '15 at 15:28
  • @JoachimPileborg , i need to store it to an integer , as to how many nanoseconds have occurred since epoch – samairtimer Mar 15 '15 at 15:28
  • Why? what is the reason you want to do that? you can store the seconds and then the more precise part separately, can't you? That's what `struct timespec` does. – Iharob Al Asimi Mar 15 '15 at 15:29
  • @alk .. actually i am supposed to supply timestamp in nanoseconds to an external API, which is providing me int64_t.. So how would it be possible.. – samairtimer Mar 15 '15 at 15:29
  • @iharob.. I agree with you , but as i said i have an external API which needs me to send timestamp in nanoseconds.. – samairtimer Mar 15 '15 at 15:30
  • 4
    Then you have conflicting requirements and you should bring this up with someone. – Some programmer dude Mar 15 '15 at 15:32
  • @samairtimer that kind of problem is unlikely to be solved by using a larger integer type, instead you need a different approach. – Iharob Al Asimi Mar 15 '15 at 15:32
  • @JoachimPileborg .. I felt the same.. – samairtimer Mar 15 '15 at 15:32
  • @samairtimer: 13438461673 is 400 years after the epoch. `int64_t` nanosecond resolution timestamps can handle a range of about 145 years (before or after the epoch). As of this writing, we are 1426440384 seconds since 1970-01-01 00:00:00 UTC, or 1426440384000000000 = 0x13cbbbf8bd978000 in nanosecond precision. – Nominal Animal Mar 15 '15 at 17:30
  • [link](https://ideone.com/dUceX1 ) I get weird outputs.. is int64_t enough for such a case – samairtimer Mar 15 '15 at 17:52
  • @NominalAnimal Turns out , i was struggling around with wrong problem, there is some problem with implementation of time_t [i suppose it is just long in ideone]. Take a look at this: https://ideone.com/OfEin2 works perfectly fine.cross verified with http://www.epochconverter.com/ – samairtimer Mar 15 '15 at 18:21
  • @samairtimer: `CLOCK_MONOTONIC` is from *arbitrary* epoch, and it actually varies from machine to machine and every boot in Linux. You should use it *only to measure intervals*, i.e. `(int64_t)(after.tv_sec - before.tv_sec) * (int64_t)1000000000UL + (int64_t)(after.tv_nsec - before.tv_nsec)`. For timestamps, use `CLOCK_REALTIME`, as it uses the 1970-01-01 00:00:00 UTC epoch. `int64_t` can handle `CLOCK_REALTIME` timestamps at nanosecond precision (`(int64_t)(t.tv_sec) * (int64_t)1000000000 + (int64_t)(t.tv_nsec)`) dates from year 1679 to 2261 at least; the range is ±292 years, not ±145 years. – Nominal Animal Mar 15 '15 at 19:27
  • 1
    @NominalAnimal Yes, my mistake . I assumed time_t was a long long(64 bit).. It seems i was beating the wrong bush.. – samairtimer Mar 15 '15 at 19:35

1 Answers1

5

CLOCK_MONOTONIC is from arbitrary epoch, and it actually varies from machine to machine and every boot in Linux. You should use it only to measure intervals, i. e.

  (int64_t)(after.tv_sec - before.tv_sec) * (int64_t)1000000000UL
+ (int64_t)(after.tv_nsec - before.tv_nsec)

. For timestamps, use CLOCK_REALTIME, as it uses the 1970-01-01 00:00:00 UTC epoch. int64_t can handle CLOCK_REALTIME timestamps at nanosecond precision –

(int64_t)(t.tv_sec) * (int64_t)1000000000 + (int64_t)(t.tv_nsec)

–, dates from year 1679 to 2261 at least; the range is ±292 years, not ±145 years.

– Nominal Animal

Armali
  • 18,255
  • 14
  • 57
  • 171