473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Reading date stored in binary fomrat

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;
Nov 14 '05 #1
4 4746
Ekong, Samuel Akpan wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2
On Sun, 04 Jan 2004 18:48:35 GMT, "Ekong, Samuel Akpan"
<sa**********@sbcglobal.net> wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
On Sun, 04 Jan 2004 13:48:35 -0500, Ekong, Samuel Akpan wrote:
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
Nov 14 '05 #4
On Sun, 04 Jan 2004 18:48:35 GMT, "Ekong, Samuel Akpan"
<sa**********@sbcglobal.net> wrote:
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 <snip>
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

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
3
by: Tanuki | last post by:
Hi All: I encounter a programming problem recently. I need to read a binary file. I need to translate the binary data into useful information. I have the format at hand, like 1st byte = ID,...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
3
by: KevinD | last post by:
thank you for your helpful explanations. In my first note I forgot to mention that my simple flatfile is a text file with a newline character at the end thus I able to get an entire record . ...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.