Fix time conversion.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2021-04-21 12:05:36 +02:00
parent 1e52e8edac
commit f869ed9df2
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 22 additions and 2 deletions

View File

@ -15,7 +15,10 @@
*/
#include "gitea2rss.hpp"
#include <ctime>
#include <iomanip>
#include <string>
namespace gitea2rss
{
@ -32,11 +35,28 @@ string strtime(const system_clock::time_point &timepoint)
string strtime(const string &time)
{
// FIXME: Do this more elegantly.
std::tm tm = {};
tm.tm_isdst = -1; // Detect daylight saving time.
std::stringstream ss(time);
ss >> std::get_time(&tm, "%Y-%m-%dT%T"); // Assume time is UTC.
return strtime(system_clock::from_time_t(timegm(&tm)));
ss >> std::get_time(&tm, "%Y-%m-%dT%T");
std::time_t time_tmp = timegm(&tm);
constexpr size_t zone_start = 19;
if (time[zone_start] == '+')
{
string zone = time.substr(zone_start + 1);
time_tmp -= std::stol(zone.substr(0, 2)) * 60 * 60;
time_tmp -= std::stol(zone.substr(3)) * 60;
}
else if (time[zone_start] == '+')
{
string zone = time.substr(zone_start + 1);
time_tmp += std::stol(zone.substr(0, 2)) * 60 * 60;
time_tmp += std::stol(zone.substr(3)) * 60;
}
return strtime(system_clock::from_time_t(time_tmp));
}
} // namespace gitea2rss