Я добавляю значения к элементам времени (часы, минуты, секунды) моей структуры tm
, и они не обновляются/нормализуются, хотя я использую mktime()
Вот код:
struct tm timeStruct;
char buffer[80];
timeStruct.tm_year = 2016 - 1900;
timeStruct.tm_mon = 3;
timeStruct.tm_mday = 32;
timeStruct.tm_hour = 23;
timeStruct.tm_min = 59;
timeStruct.tm_sec = 59;
timeStruct.tm_isdst = -1;
printf( "Date before adding interval: \n");
mktime(&timeStruct);
strftime(buffer, sizeof(buffer), "%c", &timeStruct);
printf(buffer);
printf( "\nthe year is %d\n", timeStruct.tm_year );
printf( "the month is %d\n", timeStruct.tm_mon );
printf( "the day is %d\n", timeStruct.tm_mday );
printf( "the hours are %d\n", timeStruct.tm_hour );
printf( "the minutes are %d\n", timeStruct.tm_min );
printf( "the seconds are %d\n", timeStruct.tm_sec );
/*
* Add intervals to time
*/
timeStruct.tm_sec += 2;
timeStruct.tm_min += 2;
timeStruct.tm_hour += 5;
printf( "Date after adding interval: \n");
strftime(buffer, sizeof(buffer), "%c", &timeStruct);
printf(buffer);
printf( "\nthe year is %d\n", timeStruct.tm_year );
printf( "the month is %d\n", timeStruct.tm_mon );
printf( "the day is %d\n", timeStruct.tm_mday );
printf( "the hours are %d\n", timeStruct.tm_hour );
printf( "the minutes are %d\n", timeStruct.tm_min );
printf( "the seconds are %d\n", timeStruct.tm_sec );
Вывод в консоль:1
Это распечатка вывода консоли:
Date before adding interval:
Mon May 2 23:59:59 2016
the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding interval:
Mon May 2 28:61:61 2016
the year is 116
the month is 4
the day is 2
the hours are 28
the minutes are 61
the seconds are 61
Я использую Eclipse, компилируемый с помощью Cygwin, на машине с Windows 7.
strftime()
, которая покажет, что используемая вами строка формата имеет проблемы. Укажите допустимую строку формата, и все должно пойти намного лучше. Обратите внимание, что для работы %c необходимо правильно установить локаль. - person user3629249   schedule 16.02.2016printf()
приведет к тому, что компилятор выдаст предупреждающее сообщение:printf(buffer);
предложите использовать:printf("%s\n", buffer);
- person user3629249   schedule 16.02.2016