Thread: how convert uint32_t time to human read form of time?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    how convert uint32_t time to human read form of time?

    How to convert time value read from binary file
    Code:
    uint32_t dwLowDateTime=3675431915;
    to some formated value? I read this localtime - C++ Reference but they use different type
    tm * and time_t . But how to do it with uint32_t?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The struct tm type has fields for year, month, day, etc.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Yeah. And ... ?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The link that you posted has an example that will display human readable time. I think the bit that you haven't picked up on is that you can use your dwLowDateTime value where it asks for a time_t.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Quote Originally Posted by Elkvis View Post
    The link that you posted has an example that will display human readable time. I think the bit that you haven't picked up on is that you can use your dwLowDateTime value where it asks for a time_t.
    No, I cannot:
    Code:
      uint32_t rawtime=3675431915;
      struct tm * timeinfo;
    
      // time (&rawtime);
      timeinfo = localtime (&rawtime);
      printf ("Time and date: %s", asctime(timeinfo));
    1) program will crash on last line
    2) here are warnings:
    warning: passing argument 1 of 'localtime' from incompatible pointer type
    time.h|121|note: expected 'const time_t *' but argument is of type 'uint32_t *'

    I think the bit that you haven't picked up on is that the example doesn't use type unsigned integer (or integer) where it asks for a time_t.
    Last edited by barracuda; 03-11-2015 at 01:39 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Use a time_t instead.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    how? The input type is uint32_t!

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by barracuda View Post
    how? The input type is uint32_t!
    Did you define the type of the input, or is it coming from a third party?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I've read that your magic number is coming from a binary file, but what exactly does the documentation for that binary file actually say about that variable?

    What "time" does that magic number represent?

    You can try casting the uint to a time_t or just retrieve the value from the file into a time_t.

    Jim

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Once again complete code here:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdint.h>
      uint32_t rawtime=3675431915; // WARNGING HERE
      struct tm * timeinfo;
      timeinfo = localtime ((const time_t *) &rawtime);
      asctime(timeinfo); // CRASHING HERE
    //  printf ("Time and date: %s", asctime(timeinfo));
    warning: this decimal constant is unsigned only in ISO C90
    (I actually don't know what version of gcc I have }IDK how to find it, gcc --version does not print C90 or anything like that)

    Jim:
    This is value does not come from binary file, it is as it is: 3675431915.

    IDK why I did not check it:
    timeinfo is (struct tm *) 0x0

    Additionally I tried this
    Code:
      time_t raw_time;
      uint32_t rawtime=3675431915;
      struct tm * timeinfo;
      raw_time = (const time_t *) rawtime; // -619535381
      timeinfo = localtime (&raw_time); // (struct tm *) 0x0
      asctime(timeinfo); // CRASH! 
      printf ("Time and date: %s", asctime(timeinfo));
    Last edited by barracuda; 03-11-2015 at 03:00 PM.

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Do you know what for a real time represent the number 3675431915?
    If it represent the seconds since UNIX epoch and you work on an 32bit computer, the time can be wrong, because time_t is defined as int32_t (signed!).
    On 32bit you can only calculate up to 19th January 03:14:07 2038 (2147483647).
    One second more and you jump to 13th December 20:45:52 1901 (-2147483648).

    I work here on an 64bit computer and at the console i receive:
    Code:
    $ export TZ="UTC"
    $ date -d @3675431915
    Thu Jun 20 17:18:35 UTC 2086
    Also this little program …
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdint.h>
    
    
    int main(void) {
        uint32_t rawtime = 3675431915;
        time_t raw_time = (time_t) rawtime;
        struct tm *timeinfo = localtime (&raw_time);
        printf ("Time and date: %s", asctime(timeinfo));
        return 0;
    }
    … compile without error and warnings and give out
    Code:
    $ export TZ="UTC"
    $ ./timetest
    Time and date: Thu Jun 20 17:18:35 2086
    Other have classes, we are class

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I am reading the value from file but probably I did not taken all values needed and not converted them. Maybe the date is encoded in some special way. I will reread documentation:
    There is
    EF 82 DF E2 at offset 0x08 Low = 3806298863 and
    E8 C7 C6 01 at offset 0x0C High = 29804520 => February 27, 2007 I think I must to convert the numbers using some special method. I didn't be aware of it before. But I don't have the method to calculate it.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Were did this file come from? What is the file name?

    Without knowing exactly what the file contains and exactly how that information is laid out you will not be able to properly read the file.

    Jim

  14. #14
    Registered User Sam Jackson's Avatar
    Join Date
    Jan 2015
    Posts
    5
    It looks like a FILETIME Large Integer. Try this. You will need to include windows.h:

    SYSTEMTIME st;
    FILETIME ft;
    memset(&st, 0, sizeof(SYSTEMTIME));
    memset(&ft, 0, sizeof(FILETIME));
    ft.dwLowDateTime = 3806298863;
    ft.dwHighDateTime = 29804520;
    FileTimeToSystemTime(&ft, &st);
    printf("Date = %d/%d/%d", st.wMonth, st.wDay, st.wYear);

  15. #15
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Now, i have a little bit tested under linux and came to this program.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <time.h>
    
    #define DAY_NANO 864000000000
    #define HOUR_NANO 36000000000
    #define MIN_NANO 600000000
    #define SEC_NANO 10000000
    
    typedef struct ms_filetime {
        uint32_t dwHighDateTime;
        uint32_t dwLowDateTime;
    } FILETIME;
    
    
    int leap_year (uint16_t year) {
        if ((year % 4) == 0) {
            if ((year % 100) == 0) {
                if ((year % 400) == 0) return 1;
                else return 0;
            }
            return 1;
        }
        return 0;
    }
    
    
    int filetime2tm (struct tm *timeinfo, FILETIME ftime) {
        uint64_t days;
        uint64_t clock;
        uint8_t day_of_mon[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
        days = ftime.dwHighDateTime;
        days <<= 32;
        days |= ftime.dwLowDateTime;
        clock = days % DAY_NANO;
        days /= DAY_NANO;
    
        uint16_t year = 1601;
        while (days >= (365 + leap_year (year))) {
            days -= (365 + leap_year (year));
            year++;
        }
        if (year < 1900) return 1;
    
        timeinfo->tm_isdst = 0;
        timeinfo->tm_mon = 0;
        timeinfo->tm_mday = 1;
        timeinfo->tm_wday = 0;
        timeinfo->tm_yday = 0;
        timeinfo->tm_year = year - 1900;
    
        while (days >= day_of_mon[timeinfo->tm_mon]) {
            if (timeinfo->tm_mon == 1) {
                if (days >= (day_of_mon[timeinfo->tm_mon] + leap_year (year))) {
                    days -= (day_of_mon[timeinfo->tm_mon] + leap_year (year));
                    timeinfo->tm_mon++;
                }
            }
            else {
                days -= day_of_mon[timeinfo->tm_mon];
                timeinfo->tm_mon++;
            }
        }
        timeinfo->tm_mday += days;
    
        timeinfo->tm_hour = clock / HOUR_NANO;
        clock %= HOUR_NANO;
        timeinfo->tm_min = clock / MIN_NANO;
        clock %= MIN_NANO;
        timeinfo->tm_sec = clock / SEC_NANO;
        clock %= SEC_NANO;
    
        return 0;
    }
    
    
    int main (void) {
        FILETIME ftime;
        ftime.dwHighDateTime = 0x01c6c7e8; // E8 C7 C6 01
        ftime.dwLowDateTime =  0xe2df82ef; // EF 82 DF E2
        struct tm timeinfo;
        if (filetime2tm (&timeinfo, ftime) != 0)
            printf("error!\n");
        else
            printf ( "The current date/time is: %s", asctime(&timeinfo));
    
        return 0;
    }
    But the result is different of that you are mean.
    On the other side i don't wonder if Microsoft calculate the calendar wrong.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  2. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  3. [HELP] Code to Convert Army Time to Normal Time?
    By Kipper DeVera in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2011, 11:50 PM
  4. First time creating a form
    By rocketman50 in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2010, 01:48 PM
  5. Form Order at Run Time
    By peckitt99 in forum C++ Programming
    Replies: 0
    Last Post: 11-12-2007, 06:36 AM