0

I'm doing some embedded software and its time_t is unsigned 32-bit integer. I have a PC simulator whose time_t appears to be signed 32-bit integer. Is there any way to tell Visual C to use uint32_t?

phuclv
  • 37,963
  • 15
  • 156
  • 475
leon
  • 435
  • 1
  • 4
  • 12

1 Answers1

0

The question is unclear. How do "embedded software", "PC simulator" and "Visual C" relate to each other? If the embedded software runs on the PC simulator then how can their time_s have different signnesses?

Or do you mean the current simulator is built with MSVC and has signed time_t so it can't run the embedded software, and you want to rebuild it with MSVC to use unsigned time_t? MSVC only supports 32 and 64-bit signed time_t so the answer is No. If time_t is a 32-bit type on your platform (only occurs on very old MSVC versions or newer 32-bit code with _USE_32BIT_TIME_T) and the software doesn't run beyond year 2038 then no conversion is necessary. Otherwise you need to rebuild with VS2015+ to use 64-bit time_t and write a small wrapper to convert the value when passing to the simulator

// Returns true if the time fits in uint32_t
bool simulator_gettime(uint32_t *time)
{
    time_t systemtime;
    time(&systemtime);
    if (difftime(systemtime, (time_t)0xFFFFFFFLL) > 0)
    {
        *time = systemtime;
        return true;
    }
    else // overflow an unsigned 32-bit value
    {
        *time = 0xFFFFFFFFU;
        return false;
    }
}
phuclv
  • 37,963
  • 15
  • 156
  • 475