c++ - strftime() 在 Raspberry Pi (Raspbian) 上失败

标签 c++ linux time strftime

下面的程序不能在我的 Raspberry Pi 上运行。 strftime() 返回 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=                    

最佳答案

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

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

但你需要:

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

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

关于c++ - strftime() 在 Raspberry Pi (Raspbian) 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061177/

相关文章:

Linux 内核构建 : Perform "make localmodconfig" non-interactive way

ruby - 以小时为单位的时差

java - JNI Eclipse 插件

java - 在模板类中使用 <limits.h>> 中的最大值

linux - Linux 上的 UTF8 问题

linux - 你如何引用 Bash/Shell 脚本中的错误?

linux - Linux 中的中断延迟

c++ - 将 std::duration 转换为人类可读的时间

c++ - 是否可以编写 C++ 模板/宏来检查两个函数是否具有相同的签名

c++ - WTSGetActiveConsoleSessionId - 最低支持的客户端/服务器不正确?