jeudi 28 novembre 2019

Compare two different times that the user inputs thendisplay it back out C++ using chrono/ctime llibrary's

After I input the time I get very confused on how to display back out my time after it's compared to the system time. Since it gets converted to a double i want see how much remaining time is left, but it's in seconds and I need to convert back to hours minutes and seconds and display out the same date but I'm very lost how to get this done. I'm not very good with working with time in the ctime and chrono library's so a bit of help will go a long way for me.

#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>

int main() {

std::tm now{};

    std::chrono::system_clock::time_point cc;

    std::cout << "enter\nyear month day\n";
    std::cout << "Enter in the hour, min, sec\n";

    std::cin >> now.tm_year >> now.tm_mon >> now.tm_mday >> now.tm_hour >> now.tm_min >> now.tm_sec;

    now.tm_year -= 1900;
    now.tm_mon -= 1;
    now.tm_mday;
    now.tm_hour;
    now.tm_min;
    now.tm_sec;

    std::time_t n = std::mktime(&now);
    cc = std::chrono::system_clock::from_time_t(n);
    n = std::chrono::system_clock::to_time_t(cc);

    std::cout << std::put_time(std::localtime(&n), "Date: %F Time: %T") << "\n";
    std::time_t system_time = time(nullptr);
    std::cout << asctime(localtime(&system_time));

    double fc = difftime(system_time, mktime(&now));
    std::cout << "time diff " << fc;

}

Aucun commentaire:

Enregistrer un commentaire