How to convert time value read from binary file
to some formated value? I read this localtime - C++ Reference but they use different typeCode:uint32_t dwLowDateTime=3675431915;
tm * and time_t . But how to do it with uint32_t?
How to convert time value read from binary file
to some formated value? I read this localtime - C++ Reference but they use different typeCode:uint32_t dwLowDateTime=3675431915;
tm * and time_t . But how to do it with uint32_t?
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?
Yeah. And ... ?
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?
No, I cannot:
1) program will crash on last lineCode:uint32_t rawtime=3675431915; struct tm * timeinfo; // time (&rawtime); timeinfo = localtime (&rawtime); printf ("Time and date: %s", asctime(timeinfo));
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.
how? The input type is uint32_t!
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
Once again complete code here:
warning: this decimal constant is unsigned only in ISO C90Code:#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));
(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.
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:
Also this little program …Code:$ export TZ="UTC" $ date -d @3675431915 Thu Jun 20 17:18:35 UTC 2086
… compile without error and warnings and give outCode:#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; }
Code:$ export TZ="UTC" $ ./timetest Time and date: Thu Jun 20 17:18:35 2086
Other have classes, we are class
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.
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
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);
Now, i have a little bit tested under linux and came to this program.
But the result is different of that you are mean.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; }
On the other side i don't wonder if Microsoft calculate the calendar wrong.
Other have classes, we are class