簡體   English   中英

直接設置struct tm屬性的值不起作用

[英]Directly setting values of struct tm's attributes not working

為什么asctime(ptr)什么也不返回? 結構的所有變量都有值。 有人可以解釋為什么會這樣嗎?

我也嘗試使用strftime但是結果是相同的。

#include <iostream>
#include <ctime>
#include <new>
//#include <cstdio>

using namespace std;

int main(int argc,char *argv[])
{
    struct tm *ptr=new struct tm;
    //char buf[50];

    ptr->tm_hour=0;
    ptr->tm_mon=0;
    ptr->tm_year=0;
    ptr->tm_mday=0;
    ptr->tm_sec=0;
    ptr->tm_yday=0;
    ptr->tm_isdst=0;
    ptr->tm_min=0;
    ptr->tm_wday=0;

    cout << asctime(ptr);
    //strftime(buf,sizeof(char)*50,"%D",ptr);
    //printf("%s",buf);

    return 0;
}

以下程序有效。 用1刪除零,它將起作用。

    struct tm *ptr = new struct tm();
char buf[50];

ptr->tm_hour = 1;
ptr->tm_mon = 1;
ptr->tm_year = 1;
ptr->tm_mday = 1;
ptr->tm_sec = 1;
ptr->tm_yday = 1;
ptr->tm_isdst = 1;
ptr->tm_min = 1;
ptr->tm_wday = 1;
cout << asctime(ptr)

這也適用:

 ptr->tm_hour = 0;
ptr->tm_mon = 0;
ptr->tm_year = 0;
ptr->tm_mday = 1;
ptr->tm_sec = 0;
ptr->tm_yday = 0;
ptr->tm_isdst = 0;
ptr->tm_min = 0;
ptr->tm_wday = 0;

cout << asctime(ptr);

如果struct tm任何成員超出其正常范圍,則asctime的行為是不確定的。

尤其是如果日歷日小於0,則行為是不確定的(某些實現將tm_mday==0視為上個月的最后一天)。

有關詳細信息,請訪問http://en.cppreference.com/w/cpp/chrono/c/asctimehttp://en.cppreference.com/w/cpp/chrono/c/tm

暫無
暫無

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

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