2

ive seen so many examples using a time_t, but timestamp_t is baffling me... Im doing an assignment where we need to print out GPS data, and the gps device returns a type timestamp_t for its time stamp and its an epoch time. Ive tried using gmtime() to convert and strftime() but nothing is working on that type. It keeps telling me it cannot convert timestamp_t* to time_t if i try using them.

Anyone know of any function that can convert a timestamp_t to human readable time?

thanks

  • 1
    What library are you getting timestamp_t from? – D'Nabre Nov 03 '14 at 06:52
  • You need to describe `timestamp_t` properly as D'Nabre said, I would suggest you to see documentation for respective library – twid Nov 03 '14 at 07:02
  • 1
    If it's timestamp_t from gpsd, it's just a time_t represented as double in UTC (yes, that is crazy as hell, but it's what it documented as). Should be a simple (if inaccurate) conversion to a time_t, and easy going from there. – D'Nabre Nov 03 '14 at 07:18
  • I was getting the timestamps from gpsd and yes this seems to work that's alot! –  Nov 03 '14 at 08:50

1 Answers1

2

This site enter link description here Indicates it is a unix timestamp with a fractional part. Assuming this means the numbers to the left of the decimal are the number of seconds since 1/1/1970 and the 'decimal' component is a fractional part of one second, you could strip off the the integer component and use ctime():

#include <time.h>

time_t seconds;
timestamp_t ts = 1415000462.999000;

seconds = (time_t) timestamp_t;

printf("Time: %s\n", ctime(&seconds));

This of course ignores the fractional part of a second, but you should be able to handle that easily.

parsley72
  • 8,449
  • 8
  • 65
  • 98
TonyB
  • 927
  • 6
  • 13
  • Using your example seems to make the program hang on the ctime() call –  Nov 03 '14 at 09:26