2017-03-17 345 views
-2

我们有字符串格式UTC时间,我们需要将字符串格式转换为您当前的时区。C++ - 将给定的UTC时间字符串转换为本地时区

string strUTCTime = "2017-03-17T10:00:00Z"; 

需要转换输入的值,以本地时间相同字符串格式。 像IST这将是"2017-03-17T15:30:00Z"

+1

你试过什么? – UnholySheep

+0

尝试使用localtime_s..working .. –

+0

尝试检出http://stackoverflow.com/questions/21021388/how-to-parse-a-date-string-into-a-c11-stdchrono-time-指向或类似的 – Edd

回答

0

得到解决......

1)转换的格式字符串时间time_t的

2)使用 “localtime_s” 以UTC转换为本地时间。 3)使用“strftime”将本地时间(结构tm格式)转换为字符串格式。

int main() 
{ 
    std::string strUTCTime = "2017-03-17T13:20:00Z"; 
    std::wstring wstrUTCTime = std::wstring(strUTCTime.begin(),strUTCTime.end()); 
    time_t utctime = getEpochTime(wstrUTCTime.c_str()); 
    struct tm tm; 
    /*Convert UTC TIME To Local TIme*/ 
    localtime_s(&tm, &utctime); 
    char CharLocalTimeofUTCTime[30]; 
    strftime(CharLocalTimeofUTCTime, 30, "%Y-%m-%dT%H:%M:%SZ", &tm); 
    string strLocalTimeofUTCTime(CharLocalTimeofUTCTime); 
    std::cout << "\n\nLocal To UTC Time Conversion:::" << strLocalTimeofUTCTime; 
} 

std::time_t getEpochTime(const std::wstring& dateTime) 
{ 

    /* Standard UTC Format*/ 
    static const std::wstring dateTimeFormat{ L"%Y-%m-%dT%H:%M:%SZ" }; 

    std::wistringstream ss{ dateTime }; 
    std::tm dt; 
    ss >> std::get_time(&dt, dateTimeFormat.c_str()); 

    /* Convert the tm structure to time_t value and return Epoch. */ 
    return _mkgmtime(&dt); 
} 
相关问题