LoginSignup
1

C++ コンパイル時間__DATE__をyyyy-MM-ddに変換

Posted at

概要

__DATE__はNov 02 2011(月 日 年)のフォーマットです。

  1. 月の文字列を数値に変換すること
  2. sscanfでyyyy-MM-ddにフォーマットする

コード

    auto getCompileDateTime = []()
        {
            auto ConvertMonthStrToNum = [](const QString& monthStr)
                {
                    if (monthStr == "Jan") return 1;
                    else if (monthStr == "Feb") return 2;
                    else if (monthStr == "Mar") return 3;
                    else if (monthStr == "Apr") return 4;
                    else if (monthStr == "May") return 5;
                    else if (monthStr == "Jun") return 6;
                    else if (monthStr == "Jul") return 7;
                    else if (monthStr == "Aug") return 8;
                    else if (monthStr == "Sep") return 9;
                    else if (monthStr == "Oct") return 10;
                    else if (monthStr == "Nov") return 11;
                    else if (monthStr == "Dec") return 12;
                    else return 0; 
                };

            QString compileDate = __DATE__;
            int day = 0;// 日
            int year = 0;// 年
            char buff[255] = "\0";

            sscanf(compileDate.toLatin1().data(), "%s %d %d", buff, &day, &year);

            QString monthStr(buff); // 月
            int month = ConvertMonthStrToNum(monthStr);

            QDateTime compileDateTime(QDate(year, month, day), QTime::fromString(__TIME__, "hh:mm:ss"));

            return compileDateTime;

        };

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1