Time of clock is not updating in c++ - c++

#include <ctime>
#include <iostream>
using namespace std;
int main() {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << now->tm_mday << '-'//day
<< (now->tm_mon +1 ) << '-'//month
<< (now->tm_year +1900 )//year
<<endl
<<now->tm_hour//hour
<<'-'<<now->tm_min//min
<<'-'<< now->tm_sec//sec
<< endl;
return 0;
}
This piece of code gives me the system date and time,the only problem i have is that the time is not updating.
For example:= the time is not moving forward and is stuck at a fixed time like 1.33.10

You need to clear the console and display date again every a given amount of time.
Consider following example (compile with -std=c++11):
#include <iostream> // IO
#include <ctime> // time
#include <thread> // threads
#include <chrono> // chrono
int main(){
while(true){
system("clear"); // clear console
time_t t = time(0); // get time now
struct tm* now = localtime(&t);
std::cout << now->tm_mday << '-' // day
<< (now->tm_mon + 1) << '-' // month
<< (now->tm_year + 1900) << std::endl // year
<< now->tm_hour << '-' // hour
<< now->tm_min << '-' // min
<< now->tm_sec << std::endl // sec
;
// sleep for 1000 ms (=1s)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}
In given code, we infinitelly loop the code that:
Clears the console
Recout the date
Waits 1000ms (1s)

Related

why when using while the var is not updated and print after start var only?

I wanna make a code to sync clock now and clock targeted, but the code print the same clock as first start when using while.
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;
int main(){
time_t now = time(0);
tm *ltm = localtime(&now);
do{
if(ltm->tm_sec > 60){
cout << "ok : " << ltm->tm_sec << endl;
break;
} else {
cout << "ok : " << ltm->tm_sec << endl;
continue;
}
}while (ltm->tm_sec <= 60);
}
I expect it will print the new clock now but it print as same as when start the code time.
You need to update now & ltm variable with current time inside do-while loop.
You may change your code as below:
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0);
tm *ltm;
do
{
now = time(0);
ltm = localtime(&now);
if(ltm->tm_sec > 60)
{
cout << "ok : " << ltm->tm_sec << endl;
break;
}
else
{
cout << "ok : " << ltm->tm_sec << endl;
continue;
}
}while (ltm->tm_sec <= 60);
return 0;
}
I hope it works!

How can I get current date in c++?

I did not find any solution which give the only date. I found the solution but all are complex and we have to parse the array and separate the date from time
Try to this
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}
If you run the following code, today then you will find the
current date in following format.
04/14/15
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
char c[9];
_strdate_s(c);
cout<<c<<endl;
return 0;
}
ideone code
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "Year: "<< 1900 + ltm->tm_year << endl;
cout << "Month: "<< 1 + ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
}

Strange mktime behaviour - Change if return value is assigned to a variable

I'm experimenting a strange mktime() function behaviour. When I assign the value returned by the function, the value of the input parameter is one and when I doesn't the value is different.
I already know that mktime() adjust the values of the struct tm input parameter but what's happening it's different, lets see the code with the corresponding output:
First code
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char** argv) {
struct tm cT;
strptime("31/07/2014 16:54:00", "%d/%m/%Y%n%T", &cT);
mktime(&cT);
cout << "Current Time: " << cT.tm_mday << "/" << cT.tm_mon + 1 << "/" << cT.tm_year + 1900 << " " << cT.tm_hour << ":" << cT.tm_min << ":" << cT.tm_sec << endl;
}
Output:
Current Time: 31/7/2014 16:54:0
Second code
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char** argv) {
struct tm cT;
strptime("31/07/2014 16:54:00", "%d/%m/%Y%n%T", &cT);
time_t t = mktime(&cT);
cout << "Current Time: " << cT.tm_mday << "/" << cT.tm_mon + 1 << "/" << cT.tm_year + 1900 << " " << cT.tm_hour << ":" << cT.tm_min << ":" << cT.tm_sec << endl;
}
Output:
Current Time: 31/7/2014 15:54:0
Any help is welcome. :)
This is a classic case of forgetting to initialize a variable. Specifically, you need to initialize the cT variable with appropriate values for at least all fields that won't be touched by strptime (strptime will only set those fields corresponding to the input field descriptors in the format string).
Eg. :
struct tm cT = { 0 };
cT.tm_isdst = -1;
strptime("31/07/2014 16:54:00", "%d/%m/%Y%n%T", &cT);

Get time difference in milliseconds in c++

I'm trying to get the time difference between two time stamps. Thread is sleeping between the two time stamps. but when i get the difference, it doesn't give me the time which the thread was sleeping. My code is below.
#include <iostream>
#include <locale>
#include <sys/time.h>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int timeInMilli();
int main()
{
timeval t;
timeval t2;
gettimeofday(&t, NULL);
gettimeofday(&t2, NULL);
std::string buf(20, '\0');
std::strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
std::string hr = buf.substr(0, 2);
std::string min = buf.substr(3, 2);
std::string sec = buf.substr(6, 2);
/*std::cout << hr << '\n';
std::cout << min << '\n';
std::cout << std::atoi(sec.c_str()) << '\n';*/
int a = timeInMilli();
usleep(10);
int b = timeInMilli();
cout << b-a << endl;
}
int timeInMilli()
{
timeval t;
gettimeofday(&t, NULL);
string buf(20, '\0');
strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
string str_hr = buf.substr(0, 2);
string str_min = buf.substr(3, 2);
string str_sec = buf.substr(6, 2);
int hr = atoi(str_hr.c_str());
int min = atoi(str_min.c_str());
int sec = atoi(str_sec.c_str());
int milli = t.tv_usec/1000;
/*cout << hr << endl;
cout << min << endl;
cout << sec << endl;
cout << milli << endl;*/
int timeInMilli = (((hr * 60) + min) * 60 + sec) * 1000 + milli;
return timeInMilli;
}
usleep(10);
means that you pause for 10 µsec and not 10 msec since usleep works with microseconds. try with
usleep(10000);

get future time value

#include <time.h>
#include <iostream>
using namespace std;
int main()
{
time_t current = time(0);
cout << ctime(&current) << endl;
return 0;
}
How can I get the future time, say 1 hour later, from the current time?
time(2) returns the number of seconds since 1970-01-01 00:00:00 +0000 (UTC). One hour later would be current + 3600.
time_t current = time(0);
time_t inOneHour = current + (60*60); // 60 minutes of 60 sec.
cout << "Now: " << ctime(&current) << "\n"
<< "In 1 hour: " << ctime(&inOneHour)
<< "\n";