date - Get timezone where it's 10AM (or any arbitrary hour)? -
i've been stumped trying solve while, logic keeps escaping me. on given language, logic calculate timezones it's given time?
my first answer had bug pointed out matt johnson. such major design flaw in answer instead of editing answer, decided start on new answer.
to assure myself had right time, looped on 24 "requested" hours (00 - 23), , output places requested hours. output rather lengthy, , i'm giving link it:
note in answer, there times 6am (for example), different dates:
time 06:00 etc/gmt+11 2016-01-27 06:00:00 gmt+11 etc/gmt-13 2016-01-28 06:00:00 gmt-13 pacific/auckland 2016-01-28 06:00:00 nzdt pacific/chatham 2016-01-28 06:45:00 chadt pacific/enderbury 2016-01-28 06:00:00 phot pacific/fakaofo 2016-01-28 06:00:00 tkt pacific/niue 2016-01-27 06:00:00 nut pacific/pago_pago 2016-01-27 06:00:00 sst pacific/tongatapu 2016-01-28 06:00:00 tot
the algorithm doing loop on time zones, current local time, , compare hour of time "requested" hour. here code:
#include "tz.h" #include <chrono> #include <iomanip> #include <iostream> int main() { (int hh = 0; hh < 24; ++hh) { using namespace std::chrono; using namespace date; auto& db = get_tzdb(); auto = floor<hours>(system_clock::now()); auto target = hours{hh}; std::cout << "time " << make_time(target + 0min) << '\n'; (auto& z : db.zones) { auto zt = make_zoned(&z, now); auto local_time = zt.get_local_time(); auto dp = floor<days>(local_time); auto time = make_time(local_time - dp); if (time.hours() == target) { std::cout << std::setw(30) << std::left << z.name() << " " << zt << '\n'; } } std::cout << '\n'; } }
note time comparison i'm using comparing hour, , ignoring both finer (minutes, seconds) , courser (days).
this uses free, open source parser of iana timezone database:
Comments
Post a Comment