簡體   English   中英

tm struct time.h未規范化

[英]tm struct time.h not normalizing

我正在將值添加到我的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進行編譯。

在您的代碼中,僅在添加間隔之前調用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

這是更正的代碼:

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM