<?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[mktime - gleiche Eingabe andere Ausgabe]]></title><description><![CDATA[<p>Hey,</p>
<p>mein mktime rechnet immer eine Stunde zu viel aus (im Vergleich zu time(NULL)). Erst hab ich gedacht das es irgendwie an der Zeitzone liegt, also hab ich mir ein paar Testausgaben gleich am Anfang meiner main-Methode geschrieben. Und siehe da: hier wird keine Stunde drauf gerechnet, nur im weiteren Programmverlauf. Hier mal der Code der Main und im Programm:</p>
<pre><code class="language-cpp">//in der main
    char *str = new char[20];
    time_t now = time(NULL);
    struct tm *tm = localtime(&amp;now);	
    sprintf(str, &quot;%02i.%02i.%04i %02i:%02i:%02i&quot;, tm-&gt;tm_mday, tm-&gt;tm_mon+1, tm-&gt;tm_year+1900, tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec);
    cout &lt;&lt; str &lt;&lt; endl;
    if (strptime(str, &quot;%d.%m.%Y %H:%M:%S&quot;, tm) != NULL){
        printf(&quot;%02i.%02i.%04i %02i:%02i:%02i\n&quot;, tm-&gt;tm_mday, tm-&gt;tm_mon+1, tm-&gt;tm_year+1900, tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec);
        time_t now2 = mktime(tm);
        cout &lt;&lt; now2 &lt;&lt; &quot; - &quot; &lt;&lt; now &lt;&lt; &quot; = &quot; &lt;&lt; now2-now &lt;&lt; endl;
    }		
    delete str;
</code></pre>
<p>erzeugt folgendes an Output:</p>
<pre><code>17.07.2011 20:58:40
17.07.2011 20:58:40
1310929120 - 1310929120 = 0
</code></pre>
<pre><code class="language-cpp">//im Programm
void TexifDataInput::inputTime(string display, time_t &amp;timedif){
    bool finished = false;
    while (!finished){	                            
        cout &lt;&lt; endl &lt;&lt; &quot;Zeit für \&quot;&quot; &lt;&lt; display &lt;&lt; &quot;\&quot; eingeben (Format: dd.mm.yyyy hh:mm:ss, x: abbrechen):&quot; &lt;&lt; endl;
        string input;
        getline(cin, input);
        if (input.length() &gt;= 1 &amp;&amp; input[0] == 'x'){
            cout &lt;&lt; &quot;Die Eingabe wurde abgebrochen...&quot; &lt;&lt; endl;
            finished  = true;
        }else{		
            struct tm tm; 
            if (strptime(input.c_str(), &quot;%d.%m.%Y %H:%M:%S&quot;, &amp;tm) != NULL){			
                time_t 
                    t1 = mktime(&amp;tm),
                    t2 = time(NULL);
                timedif = t1 - t2;
                cout &lt;&lt; t1 &lt;&lt; &quot;-&quot; &lt;&lt; t2 &lt;&lt; &quot; = &quot; &lt;&lt; timedif &lt;&lt; endl;
                finished = true;	
            }else{
                cout &lt;&lt; &quot;Die eingegebene Zeit ist ungültig. Bitte erneut eingeben.&quot; &lt;&lt; endl;				
            }
        }
    }
}
</code></pre>
<p>erzeugt folgendes an Output:</p>
<pre><code>17.07.2011 20:58:40
1310932720 - 1310929217 = 3503
</code></pre>
<p>Ich hab dann natürlich gründlich nach einem Fehler gesucht und paar Sachen im Code geändert und auf einma hats funktioniert. Dann hab ich noch ein paar Sachen ergänzt und auf einma ging wieder nix mehr. Ich kann das aber auch nicht wirklich reproduieren. Ich bin grad irgendwie ratlos <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Hätte jmd die Güte mich zu erleuchten?</p>
<p>MfG &amp; Thx Bergmann.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/290011/mktime-gleiche-eingabe-andere-ausgabe</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 18:46:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/290011.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 17 Jul 2011 19:10:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to mktime - gleiche Eingabe andere Ausgabe on Sun, 17 Jul 2011 19:10:38 GMT]]></title><description><![CDATA[<p>Hey,</p>
<p>mein mktime rechnet immer eine Stunde zu viel aus (im Vergleich zu time(NULL)). Erst hab ich gedacht das es irgendwie an der Zeitzone liegt, also hab ich mir ein paar Testausgaben gleich am Anfang meiner main-Methode geschrieben. Und siehe da: hier wird keine Stunde drauf gerechnet, nur im weiteren Programmverlauf. Hier mal der Code der Main und im Programm:</p>
<pre><code class="language-cpp">//in der main
    char *str = new char[20];
    time_t now = time(NULL);
    struct tm *tm = localtime(&amp;now);	
    sprintf(str, &quot;%02i.%02i.%04i %02i:%02i:%02i&quot;, tm-&gt;tm_mday, tm-&gt;tm_mon+1, tm-&gt;tm_year+1900, tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec);
    cout &lt;&lt; str &lt;&lt; endl;
    if (strptime(str, &quot;%d.%m.%Y %H:%M:%S&quot;, tm) != NULL){
        printf(&quot;%02i.%02i.%04i %02i:%02i:%02i\n&quot;, tm-&gt;tm_mday, tm-&gt;tm_mon+1, tm-&gt;tm_year+1900, tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec);
        time_t now2 = mktime(tm);
        cout &lt;&lt; now2 &lt;&lt; &quot; - &quot; &lt;&lt; now &lt;&lt; &quot; = &quot; &lt;&lt; now2-now &lt;&lt; endl;
    }		
    delete str;
</code></pre>
<p>erzeugt folgendes an Output:</p>
<pre><code>17.07.2011 20:58:40
17.07.2011 20:58:40
1310929120 - 1310929120 = 0
</code></pre>
<pre><code class="language-cpp">//im Programm
void TexifDataInput::inputTime(string display, time_t &amp;timedif){
    bool finished = false;
    while (!finished){	                            
        cout &lt;&lt; endl &lt;&lt; &quot;Zeit für \&quot;&quot; &lt;&lt; display &lt;&lt; &quot;\&quot; eingeben (Format: dd.mm.yyyy hh:mm:ss, x: abbrechen):&quot; &lt;&lt; endl;
        string input;
        getline(cin, input);
        if (input.length() &gt;= 1 &amp;&amp; input[0] == 'x'){
            cout &lt;&lt; &quot;Die Eingabe wurde abgebrochen...&quot; &lt;&lt; endl;
            finished  = true;
        }else{		
            struct tm tm; 
            if (strptime(input.c_str(), &quot;%d.%m.%Y %H:%M:%S&quot;, &amp;tm) != NULL){			
                time_t 
                    t1 = mktime(&amp;tm),
                    t2 = time(NULL);
                timedif = t1 - t2;
                cout &lt;&lt; t1 &lt;&lt; &quot;-&quot; &lt;&lt; t2 &lt;&lt; &quot; = &quot; &lt;&lt; timedif &lt;&lt; endl;
                finished = true;	
            }else{
                cout &lt;&lt; &quot;Die eingegebene Zeit ist ungültig. Bitte erneut eingeben.&quot; &lt;&lt; endl;				
            }
        }
    }
}
</code></pre>
<p>erzeugt folgendes an Output:</p>
<pre><code>17.07.2011 20:58:40
1310932720 - 1310929217 = 3503
</code></pre>
<p>Ich hab dann natürlich gründlich nach einem Fehler gesucht und paar Sachen im Code geändert und auf einma hats funktioniert. Dann hab ich noch ein paar Sachen ergänzt und auf einma ging wieder nix mehr. Ich kann das aber auch nicht wirklich reproduieren. Ich bin grad irgendwie ratlos <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Hätte jmd die Güte mich zu erleuchten?</p>
<p>MfG &amp; Thx Bergmann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093997</guid><dc:creator><![CDATA[Bergmann89]]></dc:creator><pubDate>Sun, 17 Jul 2011 19:10:38 GMT</pubDate></item><item><title><![CDATA[Reply to mktime - gleiche Eingabe andere Ausgabe on Sun, 17 Jul 2011 22:03:45 GMT]]></title><description><![CDATA[<p>Anscheinend wird bei dir die Sommerzeit nicht korrekt erkannt (=1h in unserer Zeitzone, bzw. +2 zur UTC-Zeit). Im Unterschied zur main-Funktion solltest du auch in der inputTime-Funktion die aktuelle Zeit über localtime beziehen. Evtl. löst das dein Problem schon.</p>
<pre><code class="language-cpp">time_t t2;
time ( &amp;t2 );
tm* tm2 = localtime ( &amp;t2 );
</code></pre>
<p><a href="http://www.cplusplus.com/reference/clibrary/ctime/tm/" rel="nofollow">http://www.cplusplus.com/reference/clibrary/ctime/tm/</a></p>
<p>So richtig schön sind die C-Klassen leider nicht. Wenn du viel mit Zeiten tun musst, dann solltest du dir lieber einmal die boost::date_time Klassen anschauen. So verfügt boost::date_time::local_date_time zusätzlich die Zeitzone.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2094062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2094062</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Sun, 17 Jul 2011 22:03:45 GMT</pubDate></item><item><title><![CDATA[Reply to mktime - gleiche Eingabe andere Ausgabe on Mon, 18 Jul 2011 06:59:09 GMT]]></title><description><![CDATA[<p>so. jetzt habe ich auch einen Compiler vorliegen (war gestern zu faul dazu es in XCode zu kopieren).</p>
<p>1. Verwende nie den Namen einer Struktur als Variablennamen (struct tm tm)<br />
2. strptime ist POSIX und kein C/C++ Standard (kann ich hier unter Windows bspw. nicht testen)</p>
<p>Okay. Aber der eigentliche Fehler steckt in der fehlenden Sommerzeitinformation bei der manuellen Eingabe (dst-flag, wobei dst=daylight saving time).</p>
<p>Hier das Beispiel reduziert aufs Wesentliche:</p>
<pre><code class="language-cpp">//aktuelle Zeit über Systemfunktion
time_t aktuelleZeit = time(NULL); 
tm*    pTmLocaltime = localtime(&amp;aktuelleZeit);
std::cout &lt;&lt; pTmLocaltime-&gt;tm_isdst &lt;&lt; std::endl;

//aktuelle Zeit über manuelle Eingabe
tm     tmManuell;
memset(&amp;tmManuell, 0, sizeof(tmManuell));  //tmManuell initialisieren
tmManuell.tm_sec   = pTmLocaltime-&gt;tm_sec ;
tmManuell.tm_min   = pTmLocaltime-&gt;tm_min ;
tmManuell.tm_hour  = pTmLocaltime-&gt;tm_hour;
tmManuell.tm_mon   = pTmLocaltime-&gt;tm_mon ;
tmManuell.tm_year  = pTmLocaltime-&gt;tm_year;
tmManuell.tm_wday  = pTmLocaltime-&gt;tm_wday;
tmManuell.tm_yday  = pTmLocaltime-&gt;tm_yday;
tmManuell.tm_mday  = pTmLocaltime-&gt;tm_mday;
tmManuell.tm_isdst = 0; //wird durch strptime nicht initialisert, da die Zeitzone unbekannt ist
std::cout &lt;&lt; tmManuell.tm_isdst &lt;&lt; std::endl;

std::cout &lt;&lt; mktime( pTmLocaltime) - mktime(&amp;tmManuell) &lt;&lt; std::endl;
</code></pre>
<p>Die Ausgabe liefer:</p>
<pre><code>1
0
-3600
</code></pre>
<p>Demzufolge fehlt deiner manuell gesetzten Zeit exakt eine Stunde, also genau die Sommerzeitverschiebung. Im Winter liefert dein Programm also exakte Ergebnisse. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2094099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2094099</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Mon, 18 Jul 2011 06:59:09 GMT</pubDate></item><item><title><![CDATA[Reply to mktime - gleiche Eingabe andere Ausgabe on Tue, 19 Jul 2011 14:44:55 GMT]]></title><description><![CDATA[<p>Super, jetzt funktionierts. Dankeschön <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2094960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2094960</guid><dc:creator><![CDATA[Bergmann89]]></dc:creator><pubDate>Tue, 19 Jul 2011 14:44:55 GMT</pubDate></item></channel></rss>