2017-03-17 5 views
-2

文字列形式はUTC時間です。現在のタイムゾーンと文字列形式に変換する必要があります。C++ - 与えられたUTC時間文字列を現地時間帯に変換する

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

上記の値を同じ文字列形式の現地時間に変換する必要があります。それは"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- point-or-similar – Edd

答えて

0

ガット解決策になるISTで よう...

1)

2をtime_t型に文字列フォーマットされた時間に変換)は現地時間にUTCに変換する "localtime_s" を使用します。

3) "strftime"を使用して現地時間(struct 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); 
} 
関連する問題