Hi Guys
I am reading some array records from a device, i know the first 4 bytes i am reading in is the Calender date and time, how can i print this in a time format? Any help will be highly appreciated
Regards
Hi Guys
I am reading some array records from a device, i know the first 4 bytes i am reading in is the Calender date and time, how can i print this in a time format? Any help will be highly appreciated
Regards
Well, you have to know how the data is being stored. Number of seconds since some arbitrary start time? Anyway, check out time.h.
> Anyway, check out time.h.
http://www.cppreference.com/stddate/index.html
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
There are too many ways that those four bytes could represent the date and time. Do you know the data format?
If not, you'll just have to guess. Here's my first guess:
Code:time_t t; FILE *fp; fread(&t, sizeof t, 1, fp); puts(ctime(&t));
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Well, you have to know how the data is represented in the array. How did you read it in, and what kind of thing did you store it in?
This makes the assumption that the array location is such that a time_t object can be read directly from it. This is definitely OK on an x86 machine, but an array of char can be place at any byte boundary, and some processors will not be able to read a multibyte word from an unaligned address.Code:if (sizeof(time_t) != 4) printf("Huh? time_t is not 4 bytes\n"); else { time_t t; t = *(time_t *)&array[0]; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This is what i am tryin to do:
/*pnt2 is of type ubyte*/
pnt2 = &ans.data[8];
time_t t;
for(i=0;i<ans.data[2];i++)
{
if(array_type == 0) /* Analog array, each element is 4 bytes*/
{
ans_f.B[0] = pnt2[0];
ans_f.B[1] = pnt2[1];
ans_f.B[2] = pnt2[2];
ans_f.B[3] = pnt2[3];
pnt2 += 4;
t = ans_f.F;
printf("%s\n",ctime(&t));
This code is printing me the following thing
Wed Dec 31 17:00:00 1969
Does not seem right too me....i hope this helps you guys what i am trying to get.
I'm guessing that means that t==0 (since that's what would be printed for a UNIX timestamp of 0 (midnight 1/1/70 GMT) on the east coast of the USA (GMT -5)).
Am I supposed to know what ans_f.F is and how it relates to ans_f.B? 'Cause I'm guessing that t is not set correctly.
It's possible that time_t is an integer, so that the assignment causes a cast and truncation of your data (interpreted as a float). Often floats are stored exponents first, so if your four-byte integer doesn't have any 1's in the first seven bits or so, you have a subnormal (read: very small) number.
Last edited by tabstop; 12-20-2007 at 06:06 PM. Reason: I can't read
It's definitely wrong to make it a float and then an integer [time_t is definitely not a float value].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.