I'm using std::chrono
to add year
, month
, day
etc in turn.
I assume that local time is 2023.2.28
UTC. My timezone is UTC+0800 and the date of my computer is correct.
#include <iostream>
#include <ctime>
#include <chrono>
using namespace std;
using namespace chrono;
int main (void)
{
using years = duration<int, ratio<31556952>>;
using months = duration<int, ratio<2629746>>;
using weeks = duration< int, ratio<3600 * 24 * 7>>;
using days = duration<int, ratio<3600 * 24>>;
using hours = std::chrono::hours;
using minutes = std::chrono::minutes;
using seconds = std::chrono::seconds;
// epoch: 1970.1.1 00:00:00 UTC
//assume that localtime is: 2023.2.28 17:00:00 UTC
system_clock::time_point total (seconds (0));
years year (2023 - 1970);
months month (2 - 1);
days day (28 - 1);
hours hour (17);
minutes minute (0);
seconds second (0);
total += year;
total += month;
total += day;
total += hour;
total += minute;
total += second;
time_t UTC_current_time = system_clock::to_time_t (total);
tm *from_localtime = localtime (&UTC_current_time);
cout << "ctime()\n\t" << ctime (&UTC_current_time);
cout << "localtime() and asctime()\n\t" << asctime (from_localtime);
tm *from_gtime = gmtime (&UTC_current_time);
cout << "gmtime() and asctime()\n\t" << asctime (from_gtime);
}
I think it should print Tue Feb 28 17:00:00 2023
or 8 hours added.
But the output is strange:
ctime()
Tue Feb 28 07:56:42 2023
localtime() and asctime()
Tue Feb 28 07:56:42 2023
gmtime() and asctime()
Mon Feb 27 23:56:42 2023
I wonder why the first version it is not correct.
Is there any secrets in std::chrono
?
Below are my trys:
I used to try another version using struct tm
, it succeeded:
#include <iostream>
#include <ctime>
#include <chrono>
using namespace std;
using namespace chrono;
int main (void)
{
tm total_tm = {};
total_tm.tm_year = 2023 - 1900;
total_tm.tm_mon = 2 - 1;
total_tm.tm_mday = 28;
total_tm.tm_hour = 17;
//I learnt that mktime() won't be affected by timezone and DST.
time_t total = mktime (&total_tm);
cout << "ctime()\n\t" << ctime (&total);
cout << "asctime()\n\t" << asctime (&total_tm);
}
The output is:
ctime()
Tue Feb 28 17:00:00 2023
asctime()
Tue Feb 28 17:00:00 2023
chatGPT told me to add them directly:
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std;
using namespace chrono;
int main()
{
// epoch: 1970.1.1 00:00:00 UTC
// assume that localtime is: 2023.2.28 17:00:00 UTC
system_clock::time_point total = system_clock::from_time_t (0);
total += 31556952s * 53; // 53 years
total += 2629746s * 1; // 1 month
total += 86400s * 27; // 27 days
total += 3600s * 17; // 17 hours
total += 60s * 0; // 0 minutes
total += 0s; // 0 seconds
time_t UTC_current_time = system_clock::to_time_t (total);
cout << "ctime()\n\t" << ctime (&UTC_current_time);
tm *from_gmtime = gmtime (&UTC_current_time);
tm *from_localtime = localtime (&UTC_current_time);
cout << "gmtime() and asctime()\n\t" << asctime (from_gmtime);
cout << "localtime() and asctime()\n\t" << asctime (from_localtime);
return 0;
}
But the output is more strange:
ctime()
Tue Feb 28 07:56:42 2023
gmtime() and asctime()
Tue Feb 28 07:56:42 2023
localtime() and asctime()
Tue Feb 28 07:56:42 2023