Is there an easy "beginner" way to take the current time using <ctime> to a Date object that has
int month
int day
int year
for it's member variables? Thanks.
time_t tt = time(NULL); // get current time as time_t
struct tm* t = localtime(&tt) // convert t_time to a struct tm
cout << "Month " << t->tm_mon
<< ", Day " << t->tm_mday
<< ", Year " << t->tm_year
<< endl
The tm struct ints are all 0-based (0 = Jan, 1 = Feb) and you can get various day measures, day in month (tm_mday), week (tm_wday) and year(tm_yday).
If there is localtime_r then you should use localtime_r rather than localtime since this is the reentrant version of localtime.
#include <ctime>
#include <iostream>
int main()
{
time_t tt = time(NULL); // get current time as time_t
tm tm_buf;
tm* t = localtime_r(&tt, &tm_buf); // convert t_time to a struct tm
std::cout << "Month " << t->tm_mon
<< ", Day " << t->tm_mday
<< ", Year " << t->tm_year
<< std::endl;
return 0;
}
Related
I am writing a program that takes input a date from the user, uses it to initialize a tm struct, then using chrono::time_points performs some chrono::duration operation, such as getting the age.
Here is my code:
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
using namespace std;
int main(){
//representing a date
tm *birthday = new tm{00, 30, 00, 07, 11, 97};
//convert to time t
time_t bt = mktime(birthday);
//convert time_t to time_point
chrono::system_clock::time_point t = chrono::system_clock::from_time_t(bt);
chrono::system_clock::time_point now = chrono::system_clock::now();
/*.. Testing
time_t nn = chrono::system_clock::to_time_t(now);
time_t tnn = chrono::system_clock::to_time_t(t);
*/
chrono::system_clock::duration lft = now - t;
//convert to timepoint
chrono::system_clock::time_point tlft{lft};
time_t lifetime = chrono::system_clock::to_time_t(tlft);
cout << put_time(localtime(&lifetime), "%F %T") << endl;
return 0;
}
And my output is something like this:
$> 1990-02-10 09:42:46
So, according to my understanding, it performs a plain mathematical subtraction on the ticks and using localtime, converts it to a date since EPOCH that is why it is giving me 1990. I want to know, is there any way, to convert the duration straight into struct tm, so that I should get something like 20 years?
Here is how you extract a duration in the unit of your choice:
std::chrono::duration<double> lft = now - t;
using year = std::chrono::duration<int, std::ratio<31557600>>;
auto nby = std::chrono::duration_cast<year>(lft);
std::cout << nby.count() << "\n";
With this in mind, I'll suggest an implementation of the taste of:
struct Age
{
using year = std::chrono::duration<int, std::ratio<31'557'600>>;
using month = std::chrono::duration<int, std::ratio< 2'592'000>>;
using day = std::chrono::duration<int, std::ratio< 86'400>>;
using hour = std::chrono::hours;
using minute = std::chrono::minutes;
using second = std::chrono::seconds;
Age(std::chrono::system_clock::time_point birth)
: _age(std::chrono::system_clock::now() - birth)
{}
template<class Duration>
auto extract()
{
const auto result = std::chrono::duration_cast<Duration>(_age);
_age -= result;
return result;
}
friend std::ostream& operator<<(std::ostream& os, Age age)
{
const auto years = age.extract<year>();
const auto monthes = age.extract<month>();
const auto days = age.extract<day>();
const auto hours = age.extract<hour>();
const auto minutes = age.extract<minute>();
const auto seconds = age.extract<second>();
return os << years.count()
<< ":" << std::setw(2) << std::setfill('0') << monthes.count()
<< ":" << std::setw(2) << std::setfill('0') << days.count()
<< " " << std::setw(2) << std::setfill('0') << hours.count()
<< ":" << std::setw(2) << std::setfill('0') << minutes.count()
<< ":" << std::setw(2) << std::setfill('0') << seconds.count()
;
}
private:
std::chrono::duration<double> _age;
};
Prints 20:01:10 12:43:40 with your example date (live demo).
I need to get the current time (hour,minutes and seconds) and add 30 minutes. In final i need to save the result to an integer variable.
How it's possible to do?
My code at the moment:
int main()
{
time_t currentTime;
struct tm *localTime;
time( ¤tTime );
localTime = localtime( ¤tTime );
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
std::cout << "Execute at: " << Hour << ":" << Min << ":" << Sec << std::endl;
return 0;
}
Thanks
you can do the following to get time 30 minutes ahead of local time:
time_t currentTime;
struct tm *localTime;
time( ¤tTime );
currentTime += 1*30*60;
localTime= localtime(&utc);
The localTime structure now has 30 minutes ahead of current local time.You can get the values in integers as you were doing before.
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
In case you need string out of it,you can do
std::string strAheadTime = asctime(localTime);
You can achieve the same like this -
#include <iostream>
#include <ctime>
int main()
{
time_t currentTime;
struct tm *localTime;
time( ¤tTime ); // Get the current time
localTime = localtime( ¤tTime ); // Convert the current time to the local time
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
std::cout << "The current time is : " << Hour << ":" << Min << ":" << Sec << std::endl;
//increase local time by 30 minutes and adjust minutes and hour.
int IncMin = Min + 30;
if(IncMin >= 60){
int res = IncMin/60;
int newMin = IncMin % 60;
Hour += res;
if(Hour > 23){
Hour = 00;
}
std::cout << "The updated time is : " << Hour << ":" << newMin << ":" << Sec << std::endl;
}
return 0;
}
I have strings of unixtime and I want to compare them by hours and minutes.
For example:
unixtime1 = "1327418725"
unixtime2 = "1327511595"
time_t convertUnixToTime(const char* unixtime){
time_t time = (time_t)atol(unixtime);
return time;
}
I use the function as such:
time_t time1 = convertUnixToTime("1327418725");
struct tm *timeinfo1;
timeinfo1 = localtime(&time1);
time_t time2 = convertUnixToTime("1327511595");
struct tm *timeinfo2;
timeinfo2 = localtime(&time2);
cout << "time: " << asctime(timeinfo1);// << endl;
cout << "time: " << asctime(timeinfo2);// << endl;
and I get the output:
time: Wed Jan 25 09:13:15 2012
time: Wed Jan 25 09:13:15 2012
I can't figure out why I'm getting the same time and also I want to be able to difftime() after to see how many HOURS and MINUTES they are apart.
Any suggestions/insight?
localtime() retuns a pointer to an object that is integral to the library. You are storing this pointer twice in timeinfo1 and timeinfo2.
You need to copy the struct tm
#include <ctime>
#include <cstdlib>
#include <iostream>
time_t convertUnixToTime(const char* unixtime){
time_t time = (time_t)atol(unixtime);
return time;
}
using namespace std;
int main()
{
time_t time1 = convertUnixToTime("1327418725");
struct tm timeinfo1;
timeinfo1 = *localtime(&time1);
time_t time2 = convertUnixToTime("1327511595");
struct tm timeinfo2;
timeinfo2 = *localtime(&time2);
cout << "time: " << asctime(&timeinfo1);// << endl;
cout << "time: " << asctime(&timeinfo2);// << endl;
return 0;
}
What my program does is that it first prints the current time, then the user presses enter. Afterwards it prints out the time again and calculates how long the user waited to press enter.
I can't get my time to subtract. I got the code for printing the local time from another question in stackoverflow.
#include <iostream>
#include <ctime>
using namespace std;
void main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
cout << "Press enter" << endl;
cin.ignore();
time_t rawtime2;
struct tm * timeinfo2;
time ( &rawtime2 );
timeinfo2 = localtime ( &rawtime2 );
printf ( "Later local time and date: %s", asctime (timeinfo2) );
printf ( "You took %s", ( asctime (timeinfo2) - asctime (timeinfo) ) ); //math won't work here
printf ( " seconds to press enter. ");
cout << endl;
}
cout << "Elapsed time: " << rawtime2 - rawtime << " seconds" << endl;
printf ( "You took %s", ( asctime (timeinfo2) - asctime (timeinfo) ) ); //math won't work here
No. The difference between two char*s doesn't make any sense in this context. You really just need to take the difference of rawtime and rawtime2, which are already in whole numbers of seconds.
Also, you should not mix printf() and std::cout in c++ code as it is not idiomatic and makes code harder to read. As such, perhaps something like this would be better...
#include <ctime>
#include <iostream>
int main()
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << "Current local time and date: " << asctime(timeinfo);
std::cout << "Press enter\n";
std::cin.ignore();
time_t rawtime2;
struct tm* timeinfo2;
time(&rawtime2);
timeinfo2 = localtime(&rawtime2);
std::cout << "Later local time and date: " << asctime(timeinfo2);
time_t const timediff = rawtime2 - rawtime;
std::cout << "You took " << timediff << " seconds to press enter.\n";
}
Note:
main() should return int, not void.
This is a somewhat cleaner solution, the main difference is using int main() and return 0; at the end, main should never return void, it's a bad programming practice.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
cout << "Current local time and date: " << asctime (timeinfo) ;
cout << "Press enter" << endl;
cin.ignore();
time_t rawtime2;
struct tm * timeinfo2;
time ( &rawtime2 );
timeinfo2 = localtime ( &rawtime2 );
cout << "Later local time and date: " << asctime (timeinfo2) << endl;
int prinAll = rawtime2 - rawtime;
cout << "You took " << prinAll << " seconds to press enter" << endl; //math won't work here
cout << endl;
return 0;
}
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
time_t current = time(0);
cout << ctime(¤t) << 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(¤t) << "\n"
<< "In 1 hour: " << ctime(&inOneHour)
<< "\n";