Reading date stored in binary fomrat

Discussion in 'C Programming' started by Ekong, Samuel Akpan, Jan 4, 2004.

  1. 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;
     
    Ekong, Samuel Akpan, Jan 4, 2004
    #1
    1. Advertisements

  2. 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.
    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.
    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.
    _putenv is not a standard function, and if it's one of yours then it
    violates the implementation's namespace.
    Call of a variadic function possibly without a prototype in scope.
    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.
    Non-standard, violates implementation namespace.
    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).
    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.
    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
     
    Kevin Goodsell, Jan 4, 2004
    #2
    1. Advertisements

  3. Ekong, Samuel Akpan

    Jack Klein Guest

    Encoded by whom, using what algorithm?
    Whoops, no _putenv() in the C library...
    Whoops, no _tzset() function in the C library...
    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, Jan 4, 2004
    #3
  4. 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
     
    Michael B Allen, Jan 5, 2004
    #4
  5. Ekong, Samuel Akpan

    Bill Latvin Guest

    <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
     
    Bill Latvin, Jan 6, 2004
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.