I have a function that takes a date, specified by its day, month and year and creates a std::time_t
, and another function that takes a std::time_t
and converts it to std::string
. The thing is that I'm trying to work only with UTC dates. So, no other timezones must be considered for those two functions.
I have tried so far with:
std::time_t get_timestamp(unsigned day, unsigned month, unsigned year)
{
std::tm t = {};
t.tm_mday = day;
t.tm_mon = month - 1;
t.tm_year = year - 1900;
t.tm_isdst = 0;
return std::mktime(&t);
}
std::string get_date(std::time_t const& ts)
{
std::ostringstream sso;
sso << std::put_time(gmtime(&ts), "%Y-%m-%d %H:%M %Z");
return sso.str();
}
std::time_t sample = get_timestamp("2017-06-01");
std::cout << get_date(sample) << std::endl;
But that piece of code prints 2017-05-31 23:00 GMT
because mktime
uses the current locale.
How can I change the first function definition to get the correct timestamp? I prefer not to work with additional libraries apart from boost
and std
.