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

Kel*_*ard 0 c++ linux time strftime

以下程序不适用于我的Raspberry Pi.strftime()返回0并且未设置变量.这个确切的代码在AS/400上编译并运行良好.有趣,但红帽也有问题.我得到的输出是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                                 
Run Code Online (Sandbox Code Playgroud)

在覆盆子上:

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=
Run Code Online (Sandbox Code Playgroud)

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

我不知道在哪里看,但我应该说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=
Run Code Online (Sandbox Code Playgroud)

在AS/400上:

LANG=/QSYS.LIB/EN_US.LOCALE
LC_COLLATE=                
LC_CTYPE=                  
LC_MESSAGES=               
LC_MONETARY=               
LC_NUMERIC=                
LC_TIME=                   
LC_ALL=                    
Run Code Online (Sandbox Code Playgroud)

Dan*_*rey 5

缓冲区可能太小了 - 或者这就是strftime想法.你写了:

strftime(ctimeStr, sizeof(timeStr) - 1, "%FT%TZ", tm);
Run Code Online (Sandbox Code Playgroud)

但你需要:

strftime(ctimeStr, sizeof(ctimeStr) - 1, "%FT%TZ", tm);
Run Code Online (Sandbox Code Playgroud)

它似乎可以在某些平台上运行,具体取决于std::string- 但只是为了创建可能的缓冲区溢出.