【问题标题】:How can I extract the time from a string using stringstream in C++如何在 C++ 中使用 stringstream 从字符串中提取时间
【发布时间】:2015-07-15 06:20:01
【问题描述】:

如何使用 stringstream 从与此类似的输入中提取小时、分钟、秒和 AM/PM 字符串?

输入:

07:05:45PM

【问题讨论】:

标签: c++ time


【解决方案1】:

类似这样的事情:

unsigned int hour;
unsigned int minute;
unsigned int second;
char colon1;
char colon2;
string AMPM;

if (stream >> hour >> colon1 >>minute >> colon2 >> second >> AMPM)
{
    if (colon1 == ':' && colon2 == ':')
    {
        if ((hour < 12) && (minute < 60) && (second < 60))
        {
            if (AMPM == "AM")
            {
                // do nothing
            }
            else if (AMPM == "PM")
            {
               hour += 12;

            }
            else
            {
                //freak out
            }
        }
        else
        {
            //freak out
        }
    }
    else
    {
        //freak out
    }
}
else
{
    //freak out
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 2011-01-19
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多