【问题标题】:tm struct time.h not normalizingtm struct time.h 未标准化
【发布时间】:2016-05-25 23:06:57
【问题描述】:

我正在向我的 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

我在 Windows 7 机器上使用 Eclipse,用 Cygwin 编译。

【问题讨论】:

  • 您可以从阅读strftime() 的手册页开始,这将表明您使用的格式字符串已知存在问题。提供一个有效的格式字符串,事情应该会好得多。请注意,要使 %c 正常工作,必须正确设置本地语言环境。
  • 这种对printf() 的调用会导致编译器发出警告消息:printf(buffer); 建议使用:printf("%s\n", buffer);

标签: c time mktime time.h normalizing


【解决方案1】:

这里是更正后的代码:

#include <stdio.h>
#include <time.h>

struct tm timeStruct;
char buffer[80];

int main( void )
{
    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");
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror( "first call to mktime failed due to: ");
    }

    if( 0 == strftime(buffer, sizeof(buffer), "%c", &timeStruct) )
    {
        perror( "first call to strftime failed due to: ");
    }

    printf( "\n\n%s\n", 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;
    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 after adding first interval: \n");
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror( "second call to mktime failed due to: ");
    }

    if( 0 == strftime(buffer, sizeof(buffer), "%c", &timeStruct) )
    {
        perror( "second call to strftime failed due to: ");
    }

    printf("\n\n%s\n", 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 second interval: \n");
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror( "third call to mktime failed due to: ");
    }

    if( 0 == strftime(buffer, sizeof(buffer), "%c", &timeStruct) )
    {
        perror( "third call to strftime failed due to: ");
    }

    printf("\n\n%s\n", 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 );
}

这是当前/更正后的代码输出:

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 first 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 second interval: 


Tue May  3 05:02:01 2016

the year is 116
the month is 4
the day is 3
the hours are 5
the minutes are 2
the seconds are 1

【讨论】:

    【解决方案2】:

    在您的代码中,mktime() 仅在添加间隔之前被调用。您需要在添加间隔后调用它(以便它可以标准化更新的timeStruct):

    printf( "Date after adding interval: \n");
    mktime(&timeStruct);    // <--- here
    strftime(buffer, sizeof(buffer), "%c", &timeStruct);
    

    输出:

    Tue May  3 05:02:01 2016
    the year is 116
    the month is 4
    the day is 3
    the hours are 5
    the minutes are 2
    the seconds are 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2022-01-11
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      相关资源
      最近更新 更多