简体   繁体   English

将 time_t 设置为无符号 32 位 int

[英]Set time_t to unsigned 32-bit int

I'm doing some embedded software and its time_t is unsigned 32-bit integer.我正在做一些嵌入式软件,它的time_t是无符号 32 位整数。 I have a PC simulator whose time_t appears to be signed 32-bit integer.我有一个 PC 模拟器,其time_t似乎是有符号的 32 位整数。 Is there any way to tell Visual C to use uint32_t ?有什么方法可以告诉 Visual C 使用uint32_t吗?

The question is unclear.问题不清楚。 How do "embedded software", "PC simulator" and "Visual C" relate to each other? “嵌入式软件”、“PC 模拟器”和“Visual C”如何相互关联? If the embedded software runs on the PC simulator then how can their time_ s have different signnesses ?如果嵌入式软件在PC模拟器上运行,那么它们的time_怎么会有不同的符号呢?

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 构建的并且已经签署了time_t所以它不能运行嵌入式软件,并且你想用 MSVC 重建它以使用 unsigned time_t MSVC only supports 32 and 64-bit signed time_t so the answer is No . MSVC 仅支持32 位和 64 位有符号time_t所以答案是否定的 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.如果time_t在您的平台上是 32 位类型(仅发生在非常旧的 MSVC 版本或带有_USE_32BIT_TIME_T较新 32 位代码上)并且软件不会在2038 年之后运行,则不需要转换。 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否则你需要用 VS2015+ 重建以使用 64 位time_t并编写一个小包装器在传递给模拟器时转换值

// 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM