1

I have a time_t and Struct tm being used in my code. I am not able to initiaize the struct tm like the way i am doing. If this is initialized in the function then it works fine. Please help

#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;
}
Akshay
  • 181
  • 1
  • 6
  • 17

1 Answers1

1

For these three lines:

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

You can't initialize a global instance like that (line by line assignment statements) at global scope. That has to be done within a function:

You could try this instead:

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

That might work assuming that the members of strtime are declared in the expected order of tm_sec, tm_min, followed by tm_hour. But I can't guarantee if that the member order of a struct tm is standard on every platform.

It's honestly just better to just do the explicit initialization as you have it early on in main.

selbie
  • 100,020
  • 15
  • 103
  • 173