从字符串中标记/提取信息的最佳方法

发布于 2024-10-13 02:36:01 字数 1545 浏览 4 评论 0原文

我正在尝试将收到的日期时间转换为特定格式以插入到 MySQL 数据库中。该程序是用 C++ 编写的,下面的解决方案可以工作,但我觉得它的效率非常低。

输入为:Mon Nov 08 17:41:23 +0000 2010
所需的输出格式为:YYYY-MM-DD HH:MM:SS
因此,对于这个示例,输出将是: 2010-11-08 17:41:23

我已经包含了代码的相关部分。

    //class variable
    std::map<std::string, std::string> monthMap;

void processor::initializeMonthMap(){
    monthMap["Jan"] = "01";
    monthMap["Feb"] = "02";
    monthMap["Mar"] = "03";
    monthMap["Apr"] = "04";
    monthMap["May"] = "05";
    monthMap["Jun"] = "06";
    monthMap["June"] = "06";
    monthMap["Jul"] = "07";
    monthMap["July"] = "07";
    monthMap["Aug"] = "08";
    monthMap["Sept"] = "09";
    monthMap["Sep"] = "09";
    monthMap["Oct"] = "10";
    monthMap["Nov"] = "11";
    monthMap["Dec"] = "12";
}

inline std::string processor::convertDate(std::string input) {
    //Format: Mon Nov 08 17:41:23 +0000 2010
    //To get to YYYY-MM-DD HH:MM:SS

    std::stringstream  newString(input);
    std::string temp1;
    std::string temp2;

    // Read Day in txt, discard
    newString >> temp1;

    //Read month, convert to number
    newString >> temp1;
    temp2 = "-" + monthMap[temp1] + "-";

    //Read Day in number
    newString >> temp1;
    temp2.append(temp1 + " ");

    //Read TimeStamp
    newString >> temp1;
    temp2.append(temp1);

    //Discard UTM adjustment
    newString >> temp1;

    //Read year
    newString >> temp1;

    //Add year to beginning of input
    temp1.append(temp2);

    return temp1;
}

I am trying to convert date-times that i am receiving into a particular format to insert into a MySQL database. The program is written in C++, and the following solution works but I feel it is horribly inefficient.

The input is : Mon Nov 08 17:41:23 +0000 2010
The desired output format is: YYYY-MM-DD HH:MM:SS
So for this example the output would be: 2010-11-08 17:41:23

I have included the relevant parts of the code.

    //class variable
    std::map<std::string, std::string> monthMap;

void processor::initializeMonthMap(){
    monthMap["Jan"] = "01";
    monthMap["Feb"] = "02";
    monthMap["Mar"] = "03";
    monthMap["Apr"] = "04";
    monthMap["May"] = "05";
    monthMap["Jun"] = "06";
    monthMap["June"] = "06";
    monthMap["Jul"] = "07";
    monthMap["July"] = "07";
    monthMap["Aug"] = "08";
    monthMap["Sept"] = "09";
    monthMap["Sep"] = "09";
    monthMap["Oct"] = "10";
    monthMap["Nov"] = "11";
    monthMap["Dec"] = "12";
}

inline std::string processor::convertDate(std::string input) {
    //Format: Mon Nov 08 17:41:23 +0000 2010
    //To get to YYYY-MM-DD HH:MM:SS

    std::stringstream  newString(input);
    std::string temp1;
    std::string temp2;

    // Read Day in txt, discard
    newString >> temp1;

    //Read month, convert to number
    newString >> temp1;
    temp2 = "-" + monthMap[temp1] + "-";

    //Read Day in number
    newString >> temp1;
    temp2.append(temp1 + " ");

    //Read TimeStamp
    newString >> temp1;
    temp2.append(temp1);

    //Discard UTM adjustment
    newString >> temp1;

    //Read year
    newString >> temp1;

    //Add year to beginning of input
    temp1.append(temp2);

    return temp1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话 2024-10-20 02:36:01

您正在解析字符串,幸运的是您可以通过简单的附加来构建输出。我不认为这会变得更有效率。

但是,您似乎可以使用eekg 将光标定位到一周中的某一天并进行UTM 调整。

http://www.cplusplus.com/reference/iostream/istream/seekg/

You're parsing the string and fortunately you can build the output by simple appends. I don't see this getting more efficient.

However, it appears as if you can use seekg to position the cursor past day of the week and UTM adjustment.

http://www.cplusplus.com/reference/iostream/istream/seekg/

放肆 2024-10-20 02:36:01

使用 boost::date_time。您可以使用格式字符串以多种方式格式化输入和输出。

Use boost::date_time. You can format your input and output in a wide variety of ways with a format string.

场罚期间 2024-10-20 02:36:01

如果您使用的是 POSIX 系统(即不是 Windows),则可以使用 strptime:
http://www.mkssoftware.com/docs/man3/strptime.3。 asp

这需要一个字符串并构建一个 struct tm,您可以通过 strftime 以任何所需的格式输出它。

有一些 Windows 实现,但默认情况下不可用。在此处查看更多信息:将字符串转换为 C++ 中的日期

If you're on a POSIX system (i.e. not windows), you can use strptime:
http://www.mkssoftware.com/docs/man3/strptime.3.asp

This takes a string and builds a struct tm, which you can output in any desired format via strftime.

There are some windows implementations of this, but it's not available by default. See more here: Convert a string to a date in C++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文