【C++】获取系统时间

DateTimeData::DateTimeData()
{
    m_strDate = "";
    m_longDate = 0l;
    time_t t = time(0);
    char temp[64];
    m_longDate = t;
    strftime(temp, sizeof(temp), "%Y-%m-%d %H:%M:%S", localtime(&t));
    m_strDate = temp;
}

DateTimeData::DateTimeData(string strDate)
{
    if (strDate == "" || strDate.length() != 19) {
        m_longDate = 0l;
        m_strDate = "";
        return;
    }
    
    m_strDate = strDate;
    
    tm t;
    t.tm_year = atoi(m_strDate.substr(0,4).c_str());
    t.tm_mon = atoi(m_strDate.substr(5,7).c_str()) - 1;
    t.tm_mday = atoi(m_strDate.substr(8,10).c_str());
    t.tm_hour = atoi(m_strDate.substr(11,13).c_str());
    t.tm_min = atoi(m_strDate.substr(14,16).c_str());
    t.tm_sec = atoi(m_strDate.substr(17,19).c_str());
    t.tm_isdst = 0;
    
    time_t t_ = mktime(&t);
    m_longDate = t_;
}

你可能感兴趣的:(获取系统时间)