Skip to content

Instantly share code, notes, and snippets.

@gianmaria
Last active November 6, 2022 12:08
Show Gist options
  • Save gianmaria/51dd2ec0de9b5ec856f466218b388307 to your computer and use it in GitHub Desktop.
Save gianmaria/51dd2ec0de9b5ec856f466218b388307 to your computer and use it in GitHub Desktop.
Howard Hinnant date
/*
https://github.com/HowardHinnant/date
https://github.com/HowardHinnant/date/wiki
https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
https://github.com/HowardHinnant/date/wiki/Boost-datetime-Examples-Translated
https://howardhinnant.github.io/date/date.html
https://howardhinnant.github.io/date/tz.html
*/
string UNIX_epoch_to_local_time(size_t unix_time)
{
auto sec = std::chrono::seconds{unix_time};
auto time = date::sys_time<std::chrono::seconds>{sec};
auto ita_zone = date::make_zoned("Europe/Rome", time);
std::ostringstream oss;
oss.imbue(std::locale("it-ch.utf8"));
date::to_stream(oss,
"%A, %d %B %Y - %H:%M:%S %Z",
ita_zone);
return oss.str();
}
std::tuple<string, string, string> UTC_to_excel(const std::string& input)
{
std::istringstream ss(input);
date::sys_seconds utc_tp; // sys_time associated with UTC timezone
string timezone_name;
[[maybe_unused]] const auto& stream = date::from_stream(ss, "%FT%T%Z", utc_tp, &timezone_name);
assert(not stream.fail());
assert(timezone_name == "Z"sv);
auto current_zone_time = date::make_zoned(date::current_zone(), utc_tp);
/*
cout << "utc_time: " << input << endl;
cout << "rome_time: " << current_zone_time << endl;
*/
/*{
using namespace std::literals::chrono_literals;
auto t_local = current_zone_time.get_local_time();
auto today_local = std::chrono::floor<date::days>(t_local);
date::hh_mm_ss time_local{t_local - today_local};
date::year_month_day date_local{today_local};
auto min = time_local.minutes().count();
min = (min / 10) * 10;
time_local.minutes() = std::chrono::minutes(min);
int stoip = 0;
}*/
auto res = std::make_tuple(
date::format("%d/%m/%Y", current_zone_time),
date::format("%H:%M", current_zone_time),
date::format("%Z", current_zone_time)
);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment