<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Probleme mit std::get_time]]></title><description><![CDATA[<p>Hallo,</p>
<p>Habe Probleme mit <a href="https://en.cppreference.com/w/cpp/io/manip/get_time" rel="nofollow">std::get_time</a>. Versuchte den Fehler auf <a href="https://ideone.com/v0dVMe" rel="nofollow">Ideone</a> zu reproduzieren. Komisch, dort klappt es.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;ctime&gt;
//#include &lt;codecvt&gt;
#include &lt;iomanip&gt;
#include &lt;locale&gt;
#include &lt;boost/date_time.hpp&gt;
//using namespace std;

std::string DateToString(const boost::gregorian::date&amp; date)
{
	std::tm tm = to_tm(date);

	std::ostringstream stream;
	//stream.imbue(std::locale(&quot;en_us.utf-8&quot;));
	stream.imbue(std::locale(&quot;&quot;));

	stream &lt;&lt; std::put_time(&amp;tm, &quot;%x&quot;);
	
	std::cout &lt;&lt; stream.str() &lt;&lt; '\n';
	
	return stream.str();
}


boost::gregorian::date StringToDate(const std::string&amp; string)
{
	std::istringstream stream(string);
	//stream.imbue(std::locale(&quot;en_us.utf-8&quot;));
	stream.imbue(std::locale(&quot;&quot;));
	
	std::tm tm = {};
	stream &gt;&gt; std::get_time(&amp;tm, &quot;%x&quot;);

	boost::gregorian::date ret;

	if (!stream.fail()) 
	{
		ret = boost::gregorian::date_from_tm(tm);
	}
	else 
	{
		ret = boost::gregorian::date(boost::gregorian::not_a_date_time);
	}

	return ret;
}

int main() 
{
	boost::gregorian::date today(2020, 2, 9);
	
	std::string todayString = DateToString(today);
	auto todayFromString = StringToDate(todayString);
	DateToString(todayFromString);
	
	auto yesterdayFromString = StringToDate(&quot;01/09/2020&quot;);
	DateToString(yesterdayFromString);
	
	return 0;
}
</code></pre>
<p>Mache jetzt Denkpause <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f634.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sleeping_face"
      title=":sleeping_face:"
      alt="😴"
    /> , für Tipps bin ich dankbar. Danke im voraus.</p>
<p>Edit: Also das Problem besteht mit msvc immer noch. Anhand eines (identischen) locale<br />
<code>stream.imbue(std::locale(&quot;&quot;))</code><br />
und format strings  (<code>&quot;%x&quot;</code>)<br />
<code>stream &lt;&lt; std::put_time(&amp;tm, &quot;%x&quot;)</code><br />
<code>stream &gt;&gt; std::get_time(&amp;tm, &quot;%x&quot;)</code></p>
<p>Soll ein Datum in einen stringstream ausgegeben werden und umgekehrt wieder eingelesen werden. Das Ausgeben klappt, das Einlesen nicht, obwohl locale und format string identisch sind. (Auf Ideone klappt anscheinend beides, aber ich meine glaube nur mit std::locale(&quot;&quot;), welches bei meinem OS glaube ich so etwas wie de_ch ist).</p>
<p>Edit: Also <code>std::get_time</code> füllt die <code>std::tm</code> struct nur bis <code>tm_mday</code> aus, nachher nicht mehr. Und glaube ein <code>failbit</code> wird gesetzt, also <code>fail()</code> vom <code>stringstream</code>gibt <code>true</code>aus.</p>
<p>Habe versucht es weiter auszubessern und nochmals auf <a href="https://ideone.com/LmpwkT" rel="nofollow">Ideone</a> den Fehler zu reproduzieren. Also eben mit Ideone funktioniert es soweit. Ideone hat aber ein anderes locale mit <code>stream.imbue(std::locale(&quot;&quot;))</code>.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
 
#define BOOST_DATE_TIME_NO_LIB
#include &lt;boost/date_time.hpp&gt;
 
#include &lt;sstream&gt;
#include &lt;locale&gt;
#include &lt;ctime&gt;
#include &lt;iomanip&gt;
 
boost::gregorian::date DateFromString(const std::string&amp; variable);
 
int main() 
{
 
	DateFromString(&quot;01/09/2020&quot;);
 
 
	return 0;
}
 
boost::gregorian::date DateFromString(const std::string&amp; variable)
{
	std::stringstream stream;
	stream.imbue(std::locale(&quot;&quot;));
	stream &lt;&lt; variable;
 
	bool streamCheck = stream.fail();
 
	std::tm datetm = {0};
	stream &gt;&gt; std::get_time(&amp;datetm, &quot;%x&quot;);
 
	std::cout &lt;&lt; datetm.tm_mday &lt;&lt; '\n' &lt;&lt; datetm.tm_mon &lt;&lt; '\n' &lt;&lt; datetm.tm_year &lt;&lt; '\n';
 
	boost::gregorian::date datevalue;
 
	if (!stream.fail()) 
	{
		datevalue = boost::gregorian::date_from_tm(datetm);
	}
	else 
	{
		datevalue = boost::gregorian::date(boost::date_time::not_a_date_time);
	}
 
	return datevalue;
}
</code></pre>
<p>Also in der Referenz <a href="https://en.cppreference.com/w/cpp/locale/locale" rel="nofollow">en.cppreference.com/w/cpp/locale/locale</a> hat es bei den Examples:</p>
<pre><code class="language-cpp"> std::wcout &lt;&lt; &quot;User-preferred locale setting is &quot; &lt;&lt; std::locale(&quot;&quot;).name().c_str() &lt;&lt; '\n';
</code></pre>
<p>wobei <code>User-preferred locale setting is en_US.UTF8</code> als possible output angegeben ist. Drücke ich auf run steht <code>User-preferred locale setting is C</code>. Wähle ich clang (C++14) steht nichts mehr, nichts mehr steht auch, wenn ich dies lokal auf meinem Computer ausprobiere.</p>
<p>Unter <a href="https://en.cppreference.com/w/cpp/locale/time_get/get_date" rel="nofollow">en.cppreference.com/w/cpp/locale/time_get/get_date</a> steht</p>
<blockquote>
<ol start="2">
<li>Reads successive characters from the sequence [beg, end) and parses out the calendar date value using the default format expected by this locale, which is the same format as</li>
</ol>
<p>&quot;%x&quot;</p>
<p>(until C++11)</p>
<p>&quot;%d/%m/%y&quot;, &quot;%m/%d/%y&quot;, &quot;%y/%m/%d&quot;, and &quot;%y/%d/%m&quot;, depending on date_order()</p>
<p>(since C++11)</p>
<p>as used by the functions std::get_time(), get(), and the POSIX function strptime()</p>
</blockquote>
<p>Das &quot;%x&quot; wird seit C++11 überschrieben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f64b-1f3fb-2642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--man_raising_hand_light_skin_tone"
      title=":man_raising_hand_light_skin_tone:"
      alt="🙋🏻♂"
    />  ? Aber auch das Trennzeichen (/) ist ja anders?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/350775/probleme-mit-std-get_time</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 23:44:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/350775.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 09 Feb 2020 15:12:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit std::get_time on Mon, 10 Feb 2020 12:18:15 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Habe Probleme mit <a href="https://en.cppreference.com/w/cpp/io/manip/get_time" rel="nofollow">std::get_time</a>. Versuchte den Fehler auf <a href="https://ideone.com/v0dVMe" rel="nofollow">Ideone</a> zu reproduzieren. Komisch, dort klappt es.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;ctime&gt;
//#include &lt;codecvt&gt;
#include &lt;iomanip&gt;
#include &lt;locale&gt;
#include &lt;boost/date_time.hpp&gt;
//using namespace std;

std::string DateToString(const boost::gregorian::date&amp; date)
{
	std::tm tm = to_tm(date);

	std::ostringstream stream;
	//stream.imbue(std::locale(&quot;en_us.utf-8&quot;));
	stream.imbue(std::locale(&quot;&quot;));

	stream &lt;&lt; std::put_time(&amp;tm, &quot;%x&quot;);
	
	std::cout &lt;&lt; stream.str() &lt;&lt; '\n';
	
	return stream.str();
}


boost::gregorian::date StringToDate(const std::string&amp; string)
{
	std::istringstream stream(string);
	//stream.imbue(std::locale(&quot;en_us.utf-8&quot;));
	stream.imbue(std::locale(&quot;&quot;));
	
	std::tm tm = {};
	stream &gt;&gt; std::get_time(&amp;tm, &quot;%x&quot;);

	boost::gregorian::date ret;

	if (!stream.fail()) 
	{
		ret = boost::gregorian::date_from_tm(tm);
	}
	else 
	{
		ret = boost::gregorian::date(boost::gregorian::not_a_date_time);
	}

	return ret;
}

int main() 
{
	boost::gregorian::date today(2020, 2, 9);
	
	std::string todayString = DateToString(today);
	auto todayFromString = StringToDate(todayString);
	DateToString(todayFromString);
	
	auto yesterdayFromString = StringToDate(&quot;01/09/2020&quot;);
	DateToString(yesterdayFromString);
	
	return 0;
}
</code></pre>
<p>Mache jetzt Denkpause <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f634.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sleeping_face"
      title=":sleeping_face:"
      alt="😴"
    /> , für Tipps bin ich dankbar. Danke im voraus.</p>
<p>Edit: Also das Problem besteht mit msvc immer noch. Anhand eines (identischen) locale<br />
<code>stream.imbue(std::locale(&quot;&quot;))</code><br />
und format strings  (<code>&quot;%x&quot;</code>)<br />
<code>stream &lt;&lt; std::put_time(&amp;tm, &quot;%x&quot;)</code><br />
<code>stream &gt;&gt; std::get_time(&amp;tm, &quot;%x&quot;)</code></p>
<p>Soll ein Datum in einen stringstream ausgegeben werden und umgekehrt wieder eingelesen werden. Das Ausgeben klappt, das Einlesen nicht, obwohl locale und format string identisch sind. (Auf Ideone klappt anscheinend beides, aber ich meine glaube nur mit std::locale(&quot;&quot;), welches bei meinem OS glaube ich so etwas wie de_ch ist).</p>
<p>Edit: Also <code>std::get_time</code> füllt die <code>std::tm</code> struct nur bis <code>tm_mday</code> aus, nachher nicht mehr. Und glaube ein <code>failbit</code> wird gesetzt, also <code>fail()</code> vom <code>stringstream</code>gibt <code>true</code>aus.</p>
<p>Habe versucht es weiter auszubessern und nochmals auf <a href="https://ideone.com/LmpwkT" rel="nofollow">Ideone</a> den Fehler zu reproduzieren. Also eben mit Ideone funktioniert es soweit. Ideone hat aber ein anderes locale mit <code>stream.imbue(std::locale(&quot;&quot;))</code>.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
 
#define BOOST_DATE_TIME_NO_LIB
#include &lt;boost/date_time.hpp&gt;
 
#include &lt;sstream&gt;
#include &lt;locale&gt;
#include &lt;ctime&gt;
#include &lt;iomanip&gt;
 
boost::gregorian::date DateFromString(const std::string&amp; variable);
 
int main() 
{
 
	DateFromString(&quot;01/09/2020&quot;);
 
 
	return 0;
}
 
boost::gregorian::date DateFromString(const std::string&amp; variable)
{
	std::stringstream stream;
	stream.imbue(std::locale(&quot;&quot;));
	stream &lt;&lt; variable;
 
	bool streamCheck = stream.fail();
 
	std::tm datetm = {0};
	stream &gt;&gt; std::get_time(&amp;datetm, &quot;%x&quot;);
 
	std::cout &lt;&lt; datetm.tm_mday &lt;&lt; '\n' &lt;&lt; datetm.tm_mon &lt;&lt; '\n' &lt;&lt; datetm.tm_year &lt;&lt; '\n';
 
	boost::gregorian::date datevalue;
 
	if (!stream.fail()) 
	{
		datevalue = boost::gregorian::date_from_tm(datetm);
	}
	else 
	{
		datevalue = boost::gregorian::date(boost::date_time::not_a_date_time);
	}
 
	return datevalue;
}
</code></pre>
<p>Also in der Referenz <a href="https://en.cppreference.com/w/cpp/locale/locale" rel="nofollow">en.cppreference.com/w/cpp/locale/locale</a> hat es bei den Examples:</p>
<pre><code class="language-cpp"> std::wcout &lt;&lt; &quot;User-preferred locale setting is &quot; &lt;&lt; std::locale(&quot;&quot;).name().c_str() &lt;&lt; '\n';
</code></pre>
<p>wobei <code>User-preferred locale setting is en_US.UTF8</code> als possible output angegeben ist. Drücke ich auf run steht <code>User-preferred locale setting is C</code>. Wähle ich clang (C++14) steht nichts mehr, nichts mehr steht auch, wenn ich dies lokal auf meinem Computer ausprobiere.</p>
<p>Unter <a href="https://en.cppreference.com/w/cpp/locale/time_get/get_date" rel="nofollow">en.cppreference.com/w/cpp/locale/time_get/get_date</a> steht</p>
<blockquote>
<ol start="2">
<li>Reads successive characters from the sequence [beg, end) and parses out the calendar date value using the default format expected by this locale, which is the same format as</li>
</ol>
<p>&quot;%x&quot;</p>
<p>(until C++11)</p>
<p>&quot;%d/%m/%y&quot;, &quot;%m/%d/%y&quot;, &quot;%y/%m/%d&quot;, and &quot;%y/%d/%m&quot;, depending on date_order()</p>
<p>(since C++11)</p>
<p>as used by the functions std::get_time(), get(), and the POSIX function strptime()</p>
</blockquote>
<p>Das &quot;%x&quot; wird seit C++11 überschrieben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f64b-1f3fb-2642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--man_raising_hand_light_skin_tone"
      title=":man_raising_hand_light_skin_tone:"
      alt="🙋🏻♂"
    />  ? Aber auch das Trennzeichen (/) ist ja anders?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2586363</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2586363</guid><dc:creator><![CDATA[titan99_]]></dc:creator><pubDate>Mon, 10 Feb 2020 12:18:15 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::get_time on Tue, 11 Feb 2020 11:47:01 GMT]]></title><description><![CDATA[<p><strong>Tag 3</strong>: Versuchte noch das Trennzeichen zu vereinheitlichen:</p>
<pre><code class="language-cpp">for (size_t index = 0; index &lt; date_string.size(); ++index)
{
	if (!std::isdigit(date_string[index]))
	{
		date_string[index] = '/';
	}
}
</code></pre>
<p>und dann über <a href="https://en.cppreference.com/w/cpp/locale/time_get" rel="nofollow">std::time_get</a> date_order die richtige Reihenfolge der Anordnung von Tag, Monat, Jahr zu finden. Bekam aber falsche Reihenfolgen, obwohl es bei <code>time_put</code> aufgrund desselben locals die Reihenfolge (und eigentlich auch das Trennzeichen) ausgab.</p>
<p>Dafür hat es auf der verlinkten Website bei den Examples ein <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78714" rel="nofollow">Link zu einem Bug</a>. Dort ist eine library <a href="https://github.com/HowardHinnant/date" rel="nofollow">https://github.com/HowardHinnant/date</a> in einem Kommentar verlinkt. Habe es mir noch nicht genau angeschaut, aber ich finde es sieht vielversprechend aus.</p>
<p>Edit: Anscheinend übernimmt die library die bugs von <code>std::time_get</code> <a href="https://github.com/HowardHinnant/date/wiki/FAQ#week_day_parse_bug" rel="nofollow">week_day_parse_bug</a>.</p>
<pre><code class="language-cpp">date::year_month_day date;
std::stringstream istrm(date_string);
istrm &gt;&gt; date::parse(&quot;%x&quot;, date);
std::cout &lt;&lt; &quot;parsed date &quot; &lt;&lt; date &lt;&lt; '\n';
</code></pre>
<p><code>parse</code> hätte es, funktionierte bei mir aber nicht mit <code>std::locale::global(std::locale(&quot;&quot;));</code>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2586455</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2586455</guid><dc:creator><![CDATA[titan99_]]></dc:creator><pubDate>Tue, 11 Feb 2020 11:47:01 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::get_time on Tue, 11 Feb 2020 12:22:43 GMT]]></title><description><![CDATA[<p>Werde einen Trick anwenden. Da <code>std::put_time(&amp;tm, &quot;%x&quot;)</code> funktioniert, übergebe ich ein Datum mit voneinander unterschiedlichen Ziffern, also <code>23.04.1999</code>. Aus dem string kann ich dann Reihenfolge und Trennzeichen auslesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2586466</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2586466</guid><dc:creator><![CDATA[titan99_]]></dc:creator><pubDate>Tue, 11 Feb 2020 12:22:43 GMT</pubDate></item></channel></rss>