Reading date stored in binary fomrat

  • Thread starter Ekong, Samuel Akpan
  • Start date
E

Ekong, Samuel Akpan

Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value. It's more accurate to say that my attempts return a date in
the year 1970. I have been able to read the time accurately but I just want
to be able to parse correctly the date. Any assistance will be appreciated.
Skeleton sample code shown below:
Thanks.
Samuel.

//necessary headers assumed to have been included
int main(int argc, char* argv[])
{
if( _putenv( "TZ=PST-PDT" ) == -1 )
{
printf( "Unable to set TZ\n" );
exit( 1 );
}
else
{
_tzset();
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
printf( "_tzname[1] = %s\n", _tzname[1] );
}

time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
time_t _theTempDateTime = time(&_theTempDate);
char* theStringTime = ctime(&_theTempTime);
struct tm* _theTM = gmtime(&_theTempDateTime);
struct tm* _theTML = localtime(&_theTempDate);
return 0;
 
K

Kevin Goodsell

Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value.

Off the top of my head, I don't think any of the standard date or time
functions read a date or time in a binary format, so it's not surprising
that you are having problems.
It's more accurate to say that my attempts return a date in
the year 1970. I have been able to read the time accurately but I just want
to be able to parse correctly the date. Any assistance will be appreciated.
Skeleton sample code shown below:

Write code to parse it. That's the best advice I can give. No standard
function is likely to do it for you, and I have no idea what the format
is, so I can't suggest any particular method of parsing it.
Thanks.
Samuel.

//necessary headers assumed to have been included

Unless you show them, we assume otherwise. Also, this style of comment
is fine if you are using C99, but C99 compilers are very rare at the
moment. This is a syntax error in other versions of C.
int main(int argc, char* argv[])
{
if( _putenv( "TZ=PST-PDT" ) == -1 )

_putenv is not a standard function, and if it's one of yours then it
violates the implementation's namespace.
{
printf( "Unable to set TZ\n" );

Call of a variadic function possibly without a prototype in scope.
exit( 1 );

This is not a portable exit code. The standard gives meaning to exactly
three exit codes: 0, EXIT_SUCCESS, and EXIT_FAILURE. Also, exit() may
not have a prototype in scope.
}
else
{
_tzset();

Non-standard, violates implementation namespace.
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
printf( "_tzname[1] = %s\n", _tzname[1] );

Variadic function used possibly without a prototype, and none of these
names used as arguments have been declared anywhere (and they may
violate the implementation's namespace).
}

time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
time_t _theTempDateTime = time(&_theTempDate);
char* theStringTime = ctime(&_theTempTime);
struct tm* _theTM = gmtime(&_theTempDateTime);
struct tm* _theTML = localtime(&_theTempDate);

Declarations may not follow executable code in C, unless you are using
C99. Also, you may be missing declarations for all these functions and
types. Finally, I strongly recommend never beginning an identifier with
an underscore. It appears to be OK in this particular case (because they
are local variables), but in general such identifiers may be reserved
for the implementation's use.
return 0;

Missing '}'.

Generally, if you want to get help here, you should post code that is 1)
complete, 2) standard, and 3) as short as it can be while still
demonstrating the problem. This is so that we can can copy, paste, and
compile the code. You've failed to meet 1 and 2. Your description of the
problem also leaves something to be desired, since you didn't tell us
anything about the format of the date.

-Kevin
 
J

Jack Klein

Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).

Encoded by whom, using what algorithm?
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value. It's more accurate to say that my attempts return a date in
the year 1970. I have been able to read the time accurately but I just want
to be able to parse correctly the date. Any assistance will be appreciated.
Skeleton sample code shown below:
Thanks.
Samuel.

//necessary headers assumed to have been included
int main(int argc, char* argv[])
{
if( _putenv( "TZ=PST-PDT" ) == -1 )

Whoops, no _putenv() in the C library...
{
printf( "Unable to set TZ\n" );
exit( 1 );
}
else
{
_tzset();

Whoops, no _tzset() function in the C library...
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
printf( "_tzname[1] = %s\n", _tzname[1] );
}

time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
time_t _theTempDateTime = time(&_theTempDate);
char* theStringTime = ctime(&_theTempTime);
struct tm* _theTM = gmtime(&_theTempDateTime);
struct tm* _theTML = localtime(&_theTempDate);
return 0;

There are two problems here, the first is that you haven't said, and
perhaps don't know, what algorithm/language/program was used to write
the binary value into the file. The second is that your code is
littered with calls to non-standard extensions that are specific to
your compiler/operating system combination.

You need to ask this question in a group that supports that particular
compiler/OS combination of yours.
 
M

Michael B Allen

Hi,
I am parsing a binary file and intend to read a date(not time) field
encoded thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the
known datetime functions to return this value either as a tm structure
or as a string value. It's more accurate to say that my attempts return
a date in the year 1970. I have been able to read the time accurately
but I just want to be able to parse correctly the date. Any assistance
will be appreciated. Skeleton sample code shown below: Thanks.

I've tried several combinations of endianess and with known formats but
I think you have to be missing a byte because 24bits of data is very
small for a data format. Otherwise it could be some screwy microsoft
format that breaks down fields into certain bits.

#include <stdlib.h>
#include <stdio.h>
#include <encdec.h>

int
main(int argc, char *argv[])
{
unsigned char data[4];
unsigned long v1 = 0xa1290b;
unsigned long v2 = 0x0b29a1;
time_t t;


enc_uint32le(v1, data);

t = dec_time(data, TIME_1970_SEC_32LE);

printf("%s", ctime(&t));

return EXIT_SUCCESS;
}

Encdec is available here:

http://www.ioplex.com/~miallen/encdec/

Mike
 
B

Bill Latvin

Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value. It's more accurate to say that my attempts return a date in
time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
<snip>

Projecting the current calendar system back to January 1, year 1,
ignoring the calendar reforms that took place, there would have been
719162 days from 1-1-1 thru 12-31-1969, the day before unix day zero.
If you subtract 719162 from 731553, you get 12391. Multiply that by
86400 seconds per day, feed the result into gmtime, and you get Dec 5
2003. I don't know if the one-day difference between that and your
expected result is due to the time zone or the need to subtract one
more day.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top