Systemzeit in string speichern



  • Ich würde gerne das aktuelle Datum und die Uhrzeit in einem String speichern
    und zwar in diesem Format:

    DD.MM.YYYY - HH:MM:SS
    

    Z.B.:

    09.12.2004 - 20:01:10
    


  • std::string



  • Hi!

    Die Forensuche hätte dir genug Ergebnisse beim Stichwort "Uhrzeit" liefern sollen, wie z.B. diese:
    http://www.c-plusplus.net/forum/viewtopic.php?t=49964&highlight=uhrzeit
    http://www.c-plusplus.net/forum/viewtopic.php?t=64743&highlight=uhrzeit

    Code-Hacker



  • Mein Problem ist, dass ich das Datum nicht ausgeben möchte, sondern in std::string formatiert abspeichern möchte.

    Also in etwa so:

    time_t long_time;
    time(&long_time); // Aktuelle Zeit wird in long_time geseichert
    tm* newtime;
    newtime = localtime(&long_time); 
    
    std::cout
                << std::setfill('0') << std::setw(2) 
                << newtime->tm_mday << "." // Tag	 			
                << std::setfill('0') << std::setw(2)
                << newtime->tm_mon + 1 << "." // Monat, Januar = 0
                << std::setfill('0') << std::setw(2)
                << newtime->tm_year + 1900 << " - "
                << std::setfill('0') << std::setw(2)
                << newtime->tm_hour << ":"
                << std::setfill('0') << std::setw(2)
                << newtime->tm_min  << ":"
                << std::setfill('0') << std::setw(2)
                << newtime->tm_sec  << "  ";
    

    blos wie gesagt in einen std::string...



  • Hi!

    Erstell einen ostringstream, schick alles darin und dann wandel es mit .str() zu einem String um:

    ostringstream oStr("");
    oStr << "text " << 5;
    std::string str(oStr.str());
    

    Nicht vergessen: #include <sstream>

    Code-Hacker



  • Habs gerade gelöst:

    💡

    #include <string>
    #include <ctime>   
    #include <iomanip> 
    #include <sstream>
    
    const std::string GetLocalTime()
    { 
        time_t long_time;
        time(&long_time); 
        tm* newtime;
        newtime = localtime(&long_time); 
    
        std::stringstream sstream;
    
        sstream     << std::setfill('0') << std::setw(2) 
                    << newtime->tm_mday << "." 	 			
                    << std::setfill('0') << std::setw(2)
                    << newtime->tm_mon + 1 << "." 
                    << std::setfill('0') << std::setw(2)
                    << newtime->tm_year + 1900 << " - "
                    << std::setfill('0') << std::setw(2)
                    << newtime->tm_hour << ":"
                    << std::setfill('0') << std::setw(2)
                    << newtime->tm_min  << ":"
                    << std::setfill('0') << std::setw(2)
                    << newtime->tm_sec  << "  ";
    
        return sstream.str();
    }
    

    MfG



  • Hi!

    Sag ich doch. 😉
    Ja, stringstream kannst du sogar als Eingabe- und als Ausgabestrom verwenden. istringstream und ostringstream nur für Eingabe bzw. Ausgabe.

    Code-Hacker



  • mit strftime geht das aber viel schöner



  • sehe auch 'strftime' als gute idee

    #include <time.h>
    #include <stdio.h>
    
    int main()
    {
    struct tm *ptr;
    time_t tm;
    char str[60];
    
    tm = time(NULL);
    ptr = localtime(&tm);
    strftime(str ,100 , "It is %A.\n",ptr);
    /*
    %a  :  abbreviated weekday name
    %A  :  Full weekday name
    %b  :  abbreviated month name
    %B  :  Full month name
    %c  :  standard date and time string
    %C  :  last two digits of the year
    %d  :  day of month as a decimal (e.g 1 to 31)
    %H  :  hour (0 - 23)
    %I  :  hour (1 - 12)
    %j  :  day of year as a decimal ( 1 to 366)
    %m  :  month as a decimal ( 1 to 12)
    %M  :  minute as a decimal ( 0 to 59 )
    %p  :  am or pm
    %S  :  second as a decimal ( 0 to 60 )
    %U  :  week of the year ( 0 to 53 ) Sunday is first day
    %w  :  weekday as a decimal (0 is Sunday)
    %W  :  week of the year ( 0 to 53 ) Monday is first day
    %x  :  standard date string
    %X  :  standard time string
    %Y  :  year including century as a decimal
    %Z  :  time zone name
    %%  :  percent sign
    */
    printf(str);
    
    return 0;
    }
    

Anmelden zum Antworten