日付の文字列をtime_tに保存してから元の文字列に戻したいと思います。文字列をtime_tに変換してからtime_tを文字列に変換します
しかし、意志の出力以下のコード出力の"2016-04-25_10:10:05"
そして時間はdate_str
を変更することにより、不正確になります。
コードをstd::string date_str = "1470-04-25_09:10:05";
に変更すると、 の結果が正しくなります。
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
// try changing year, hour will be incorrect
std::string date_str = "2016-04-25_09:10:05";
std::tm tm{};
std::istringstream str_stream(date_str);
str_stream >> std::get_time(&tm, "%Y-%m-%d_%T");
std::time_t time = std::mktime(&tm);
std::stringstream stream;
stream << std::put_time(std::localtime(&time), "%F_%T");
std::cout << stream.str() << std::endl;
}
タイムゾーンの問題?あなたは何タイムゾーンですか?夏時間調整?詳細は 'tm'構造体をチェックしましたか(デバッガなど)? –
1時間のギャップは意味があります...私が得意でないものは、1470年です。 – pah
@JoachimPileborg同じマシンにあります。しかし、時間帯に依存しないようにする方法。 – Christophe