strftime() 在 Raspberry Pi (Raspbian) 上失败

strftime() fails on Raspberry Pi (Raspbian)

下面的程序不适用于我的Raspberry Pi。 strftime() returns 0 且变量未设置。这段代码在 AS/400 上编译并运行良好。有趣的是,Red Hat 也有问题。我在 400 上得到的输出是

currentTime=1430852224                                
tm=SPP:0000 :1aefQP0ZSPWT  BEAK      093275 :8840:0:13
tm->year=115                                          
tm->mon=4                                             
tm->mday=5                                            
tm->tm_hour=18                                        
tm->tm_min=57                                         
tm->tm_sec=4                                          
i=20, ctimeStr=2015-05-05T18:57:04Z                   
i=13, ctimeStr=2015 18:57:04                          
timeStr=2015 18:57:04                                 

在覆盆子上:

currentTime=1430852359
tm=0xb6dd9264
tm->year=115
tm->mon=4
tm->mday=5
tm->tm_hour=18
tm->tm_min=59
tm->tm_sec=19
i=0, ctimeStr=
i=0, ctimeStr=
timeStr=

我可能正盯着我的问题看,但我没有看到它。

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <time.h>

using namespace std;

static void GetUTCTimeStr(string& timeStr)
{
    time_t currentTime;
    struct tm *tm;

    currentTime = time(0);
    cout << "currentTime=" << currentTime << endl;

    tm = gmtime(&currentTime);
    cout << "tm=" << tm << endl;
    cout << "tm->year=" << tm->tm_year << endl;
    cout << "tm->mon=" << tm->tm_mon << endl;
    cout << "tm->mday=" << tm->tm_mday << endl;
    cout << "tm->tm_hour=" << tm->tm_hour << endl;
    cout << "tm->tm_min=" << tm->tm_min << endl;
    cout << "tm->tm_sec=" << tm->tm_sec << endl;

    if (tm != 0) {
        char ctimeStr[25];
        size_t i = strftime(ctimeStr, sizeof(timeStr) - 1, "%FT%TZ", tm);
        cout << "i=" << i << ", ctimeStr=" << ctimeStr << endl;
        i = strftime(ctimeStr, sizeof(timeStr) - 1, "%Y %T", tm);
        cout << "i=" << i << ", ctimeStr=" << ctimeStr << endl;
        timeStr = ctimeStr;
    }
    else {
        cout << strerror(errno) << endl;
    }
}


int main(int argc, char* argv[])
{
    string timeStr;
    GetUTCTimeStr(timeStr);
    cout << "timeStr=" << timeStr << endl;
}

我不确定去哪里看,但我应该说 Pi 上的 locale 命令产生了这个:

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8" 
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

在AS/400:

LANG=/QSYS.LIB/EN_US.LOCALE
LC_COLLATE=                
LC_CTYPE=                  
LC_MESSAGES=               
LC_MONETARY=               
LC_NUMERIC=                
LC_TIME=                   
LC_ALL=                    

你打错了:

size_t i = strftime(ctimeStr, sizeof(timeStr) - 1, "%FT%TZ", tm);
                                     ^^^^^^

你想要sizeof(ctimeStr)

缓冲区可能太小 - 或者 strftime 就是这么想的。您写道:

strftime(ctimeStr, sizeof(timeStr) - 1, "%FT%TZ", tm);

但你需要:

strftime(ctimeStr, sizeof(ctimeStr) - 1, "%FT%TZ", tm);

根据 std::string 的大小,它似乎可以在某些平台上工作 - 但只是为了创建可能的缓冲区溢出。