【问题标题】:time_t declaration issue during compile time编译期间的 time_t 声明问题
【发布时间】:2017-03-25 11:06:36
【问题描述】:

我的代码中使用了 time_t 和 Struct tm。我无法像我正在做的那样初始化 struct tm。如果这是在函数中初始化的,那么它工作正常。请帮忙

#include "time.h"
static struct tm strtime;
struct tm * timeinfo;
time_t startTime;
time_t endTime;
time_t curTime;
time_t candleStartTime;
strtime.tm_hour = 9; //Error here
strtime.tm_min = 15; //Error here
strtime.tm_sec = 00; //Error here
void PrintMarketData(void **args,nsString sPfName)
{
    curTime = time (NULL);
    timeinfo = localtime (&curTime);
    int curYear = timeinfo->tm_year;
    int curMonth = timeinfo->tm_mon;
    int curDay = timeinfo->tm_mday;

    strtime.tm_year = curYear;
    strtime.tm_mon = curMonth;
    strtime.tm_mday = curDay;
}

【问题讨论】:

    标签: c++ struct time-t


    【解决方案1】:

    对于这三行:

    strtime.tm_hour = 9; //Error here
    strtime.tm_min = 15; //Error here
    strtime.tm_sec = 00; //Error here
    

    您不能在全局范围内初始化这样的全局实例(逐行赋值语句)。这必须在一个函数中完成:

    你可以试试这个:

    struct tm strtime = {0, 15, 9};
    

    假设 strtime 的成员按照 tm_sec、tm_min 和 tm_hour 的预期顺序声明,这可能会起作用。但我不能保证struct tm 的成员顺序是否在每个平台上都是标准的。

    老实说,在main 中进行显式初始化会更好。

    【讨论】:

    • struct 会员订单始终是标准化的。
    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多