How to add hours to current time in C++
This tutorial will teach how to add hours to current time in C++.
What we need to do is to first, get the current time (using inbuilt functions ) and then add the number of hours which we take input from the user or mention in the main function.
adding hours to the current time in C++
time_t is an arithmetic datatype. It stores system time.
ctime() function:
It is included in the header file <ctime>. Basically what it does is performs both the functions task asctime()
and localtime().
asctime() makes the time converted to a format understandable by the user and localtime()
makes or checks for the particular time zone according to which time needs to be found.
The ctime()
function takes a pointer to time_t object as its parameter and it gives output in the format:
day month date hh:mm:ss year
the day and month are outputted in a three-character format.
Also check: localtime() function in C++ with an example
Then next we have to add hours to the time that we get using time() This we do by using a user-defined function.
add_hours()
is a function that takes input as a string of time and operates on it.We get substring containing time using {string_name.substring() }and covert to integer.
After that, we will perform regular addition for adding hours.
Code Snippet:
void add_hours(string s,int h) { string hh1,mm1,ss1; int HH; hh1=s.substr(11,2)+""; mm1=s.substr(14,2)+""; ss1=s.substr(17,2)+""; //stringstream is used to convert a string to integer data type stringstream str1(hh1); int hh=0; str1>>hh; stringstream str2(mm1); int mm=0; str2>>mm; stringstream str3(ss1); int ss=0; str3>>ss; cout<<hh<<" "<<mm<<" "<<ss<<endl; //add time and hours*/ ss=ss; mm=mm+(ss/60); HH=hh+h+(mm/60); mm=mm%60; //leftover minutes ss=ss%60; //leftover seconds cout<<HH<<":"<<mm<<":"<<ss; }
#include<bits/stdc++.h> #include <ctime> #include<cstdio> #include<cstdlib> using namespace std; int main() { int h=3; string p; // we firstly declare the argument of time() time_t my_time = time(NULL); // we use the built in function named ctime() to give us the present time //and then print it on console p = ctime(&my_time); cout<<p; add_hours(p,h); return 0; }
Sample Output:
Sun Jun 23 18:04:05 2019 18 4 5 21 4 5
The full code:
#include<bits/stdc++.h> #include <ctime> #include<cstdio> #include<cstdlib> using namespace std; void add_hours(string s,int h) { string hh1,mm1,ss1; int HH; hh1=s.substr(11,2)+""; mm1=s.substr(14,2)+""; ss1=s.substr(17,2)+""; //stringstream is used to convert a string to integer data type stringstream str1(hh1); int hh=0; str1>>hh; stringstream str2(mm1); int mm=0; str2>>mm; stringstream str3(ss1); int ss=0; str3>>ss; cout<<hh<<" "<<mm<<" "<<ss<<endl; //add time and hours*/ ss=ss; mm=mm+(ss/60); HH=hh+h+(mm/60); mm=mm%60; //leftover minutes ss=ss%60; //leftover seconds cout<<HH<<":"<<mm<<":"<<ss; } int main() { int h=3; string p; // we firstly declare the argument of time() time_t my_time = time(NULL); // we use the built in function named ctime() to give us the present time //and then print it on console p = ctime(&my_time); cout<<p; add_hours(p,h); return 0; }
Also read:
Leave a Reply