Kleines Problem mit SYSTEMTIME



  • Hi,

    habe ein kleines Problem mit SYSTEMTIME 😡

    void CSystemzeitDlg::OnButton1() 
    {
    	SYSTEMTIME st;
    
    	GetSystemTime (&st);
    	//i = st.wMinute;
    	i = st.wHour;
    	//i = st.wSecond;
    	//i = st.wMonth;
    	//i = st.wDay;
    
    	UpdateData(FALSE);
    }
    

    Geht alles, außer st.wHour gibt mir immer 2St. weniger zurück als meine Uhr anzeigt. Also 15Uhr statt 17Uhr.

    Warum 😕

    Gruss Sven


  • Administrator

    womöglich wegen dem:

    MSDN schrieb:

    The GetSystemTime function retrieves the current system date and time. The system time is expressed in Coordinated Universal Time (UTC).

    benutze doch folgendes:

    void CSystemzeitDlg::OnButton1() 
    {
        CTime time = CTime::GetCurrentTime();
    
        //i = st.wMinute;
        i = time.GetHour();
        //i = st.wSecond;
        //i = st.wMonth;
        //i = st.wDay;
    
        UpdateData(FALSE);
    }
    

    Grüssli



  • Du solltest vielleicht "GetLocalTime" verwenden...
    und es schadet *nie* wenn man doch mal in die Doku schaut 😉



  • Ich hab´s mir ja schon fast gedacht dass es daran liegt. 😃

    Grüsse



  • Klassische Alternative mittels time.h:

    #include <ctime>
    #include <iostream>
    #include <string>
    #include <conio.h>
    using namespace std;
    
    int main()
    {
    	/*
        midnight, January 1, 1970, to January 18, 19:14:07, 2038. 
    
    	time function returns the number of seconds elapsed since 
    	midnight (00:00:00), January 1, 1970, coordinated universal time, 
    	according to the system clock. The return value is stored in the location given by timer. 
    	This parameter may be NULL, in which case the return value is not stored.
    
    	localtime function converts a time stored as a time_t value 
    	and stores the result in a structure of type tm. 
    	*/
    
        time_t long_time;
    	struct tm *newtime;
    	time( &long_time );                /* Get time as long integer. */
    	newtime = localtime( &long_time );
    
    	int year    = 1900 + (newtime->tm_year);
    	int month   = 1 + (newtime->tm_mon);
    	int day     = newtime->tm_mday;
    
    	int hour    = newtime->tm_hour;
    	int min     = newtime->tm_min;
    	int sec     = newtime->tm_sec;
    
    	int weekday = newtime->tm_wday;
    
        string str;
    	switch(weekday)
    	{
    		case 0:   str = "Sonntag"; break; 
    		case 1:   str = "Montag";  break; 
    		case 2:   str = "Dienstag"; break; 
    		case 3:   str = "Mittwoch"; break; 
    		case 4:   str = "Donnerstag"; break; 
    		case 5:   str = "Freitag"; break; 
    		case 6:   str = "Samstag"; break; 
    	}
    
    	cout << str << ", " << year << "-" << month << "-" << day << ", " << hour << ":" << min << ":" << sec;
    	getch();
    }
    

  • Administrator

    @Erhard Henkes

    Ufff wieso bitte so kompliziert? Wir sind hier im MFC Forum! Das was du das machst, kann man alles mit CTime::Format machen. Viel einfacher und viel kürzer. Bzw. wenn du keine MFC verwenden willst, dann benütze doch strftime. Schau es dir in der MSDN mal an, sehr empfehlenswert um eine Zeit/ein Datum in ein string, char* oder CString zu konvertieren!

    Grüssli



  • Ganz abgesehen davon würde ich heute nicht mehr "time" verwenden, da Deine Kunden spätestens in ca. 30 Jahren anrufen, warum das Programm dauernd abstürzt...



  • strftime ist selbstverständlich o.k., ich wollte nur den elementaren Weg zeigen, da man damit völlig eigene Dinge hinsichtlich Berechnung und Darstellung machen kann. Aber wir nehmen strftime (auch time.h) der Vollständigkeit halber dazu:

    #include <ctime>
    #include <iostream>
    #include <string>
    #include <conio.h>
    using namespace std;
    
    int main()
    {
    	/*
        midnight, January 1, 1970, to January 18, 19:14:07, 2038. 
    
    	time function returns the number of seconds elapsed since 
    	midnight (00:00:00), January 1, 1970, coordinated universal time, 
    	according to the system clock. The return value is stored in the location given by timer. 
    	This parameter may be NULL, in which case the return value is not stored.
    
    	localtime function converts a time stored as a time_t value 
    	and stores the result in a structure of type tm. 
    	*/
    
        time_t long_time;
    	struct tm *newtime;
    	time( &long_time );                /* Get time as long integer. */
    	newtime = localtime( &long_time );
    
        char szTime[200];
        strftime(szTime, 200, "%#c",newtime);
        cout << szTime << endl;  
    
    	int year	= 1900 + (newtime->tm_year);
    	int month	= 1 + (newtime->tm_mon);
    	int day		= newtime->tm_mday;
    
    	int hour    = newtime->tm_hour;
    	int min     = newtime->tm_min;
    	int sec     = newtime->tm_sec;
    
    	int weekday = newtime->tm_wday;
    
        string str;
    	switch(weekday)
    	{
    		case 0:   str = "Sonntag"; break; 
    		case 1:   str = "Montag";  break; 
    		case 2:   str = "Dienstag"; break; 
    		case 3:   str = "Mittwoch"; break; 
    		case 4:   str = "Donnerstag"; break; 
    		case 5:   str = "Freitag"; break; 
    		case 6:   str = "Samstag"; break; 
    	}
    
    	cout << str << ", " << year << "-" << month << "-" << day << ", " << hour << ":" << min << ":" << sec;
    	getch();
    }
    


  • Ganz abgesehen davon würde ich heute nicht mehr "time" verwenden, da Deine Kunden spätestens in ca. 30 Jahren anrufen, warum das Programm dauernd abstürzt...

    Um exakt zu sein: January 18, 19:14:07, 2038 ⚠



  • Danke !!!

    Das Problem habe ich gelöst. 👍
    Und wie solls anders sein steh ich vorm nächsten 😃

    http://www.c-plusplus.net/forum/viewtopic-var-t-is-149363.html

    Grüsse


  • Administrator

    Jochen Kalmbach schrieb:

    Ganz abgesehen davon würde ich heute nicht mehr "time" verwenden, da Deine Kunden spätestens in ca. 30 Jahren anrufen, warum das Programm dauernd abstürzt...

    LOL 30 Jahren? Da sind wir bei Windows Version 30 (klar übertrieben), da wird mein Programm mit Sicherheit nicht mehr verwendent! Die Entwicklung geht viel zu schnell vorran. Wenn du ein Programm entwickelst, dass in 30 Jahren noch läuft musst du sowieso Updates liefern und irgendwann kannst ja dann auch das CTime ersetzen oder was auch immer. Wobei ich definitiv nicht daran glaube, dass ein und dasselbe Programm über 30 Jahren benutzt wird. Nenn mir mal nur ein Programm, dass nicht bereits in einer neuen Version ist und seit 30 Jahren existiert und verwendet wird!!

    Grüssli


Anmelden zum Antworten