Konkretes Beispiel für Zeitausgabe



  • Hallo, ich habe jetzt schon einige Beiträge über Zeitausgabe gelesen. Kann mir da jemand mal ein allgemeines Beispiel geben?



  • Diese Funktion hat ich noch inner txt stehen, hoffe das is ungefähr das was du meinst? 😕

    const std::string GetLocalTime(){
            time_t *currentTime = new time_t;
            time(currentTime);
            tm *newtime = localtime(currentTime);
        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 << " - ";
    
        sstream << 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();
    }
    


  • Hallo

    Ich habe dazu eine Frage:

    effe_eichelt schrieb:

    Diese Funktion hat ich noch inner txt stehen, hoffe das is ungefähr das was du meinst? 😕

    const std::string GetLocalTime(){
            time_t *currentTime = new time_t;
            time(currentTime);
            tm *newtime = localtime(currentTime);
        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 << " - ";
    
        sstream << 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();
    }
    

    Fehlt da nicht noch ein:

    delete currentTime;
    

    chrische



  • chrische5 schrieb:

    Fehlt da nicht noch ein:

    delete currentTime;
    

    Aber unbedingt. Noch besser wäre es natürlich, wenn man komplett auf die new-Allokation verzichten und einfach ein lokales Objekt verwenden würde.


Anmelden zum Antworten