Skip to content

Instantly share code, notes, and snippets.

@mitsutaka-takeda
Created August 9, 2018 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsutaka-takeda/95ff723ec481ce34326ad6e3654a0ec9 to your computer and use it in GitHub Desktop.
Save mitsutaka-takeda/95ff723ec481ce34326ad6e3654a0ec9 to your computer and use it in GitHub Desktop.
C++20でサマータイムを謳歌する
#include <iostream>
#include <date/date.h>
#include <date/tz.h>
#include <chrono>
void basic_calendar() {
using namespace std::literals;
using namespace date;
constexpr year y{ 2018_y };
constexpr month m{ aug };
constexpr day d{ 9_d };
constexpr year_month_day ymd{ 2018_y / aug / 9_d };
static_assert(ymd == y / m / d);
static_assert(2020_y / aug / 9_d != ymd);
static_assert(2018_y <= 2020_y);
}
void basic_sys_or_local_time() {
using namespace std::literals;
using namespace date;
constexpr auto hour_precision = sys_days{ 2018_y / aug / 9_d } +2h; // hour単位の精度。
std::cout << hour_precision << "\n"; // 2018-08-09 0200
constexpr auto second_precision = sys_days{ 2018_y / aug / 9_d } +2h + 2s; // second単位の精度。
std::cout << second_precision << "\n"; // 2018-08-09 02:00:02
constexpr auto hour_precision_local = local_days{ 2018_y / aug / 9_d } +2h;
std::cout << hour_precision_local << "\n"; // 2018-08-09 0200
constexpr auto second_precision_local = local_days{ 2018_y / aug / 9_d } +2h + 2s;
std::cout << second_precision_local << "\n"; // 2018-08-09 02:00:02
static_assert(hour_precision != second_precision);
// static_assert(hour_precision != second_precision_local); コンパイル・エラー。
}
void basic_time_zone() {
date::tzdb const& tz = date::get_tzdb(); // Timezone database
date::time_zone const* const japan_time_zone = date::locate_zone("Asia/Tokyo"); // 日本の標準タイムゾーン。
}
void basic_zoned_time() {
using namespace std::literals;
using namespace date;
auto z0 = make_zoned("Asia/Tokyo", sys_days{ 2018_y / aug / 9_d }); // Unix時刻の2018年8月9日00:00 == 日本時刻の2018年8月9日09:00
auto z1 = make_zoned("Asia/Tokyo", local_days{ 2018_y / aug / 9_d }); // 日本時刻の2018年8月9日00:00。
assert(z0 != z1);
std::cout << z0 << "\n"; // 2018-08-09 09:00:00 JST
std::cout << z1 << "\n"; // 2018-08-09 00:00:00 JST
}
void begining_day_light_saving_time() {
using namespace std::literals;
using namespace date;
auto z = make_zoned("Asia/Tokyo", local_days{ 1948_y / may / 1 } +23h);
for (int i = 0; i < 4; ++i) {
std::cout << z << " = " << format("%F %T %Z\n", z.get_sys_time());
z = z.get_local_time() + 1h;
}
}
void begining_day_light_saving_time_with_sys_time() {
using namespace std::literals;
using namespace date;
auto z = make_zoned("Asia/Tokyo", local_days{ 1948_y / may / 1 } +23h);
for (int i = 0; i < 4; ++i) {
std::cout << z << " = " << format("%F %T %Z\n", z.get_sys_time());
z = z.get_sys_time() + 1h; // Unix時間を進める。
}
}
void finishing_day_light_saving_time() {
using namespace std::literals;
using namespace date;
auto z = make_zoned("Asia/Tokyo", local_days{ 1948_y / sep / sat[2] } +22h);
for (int i = 0; i < 4; ++i) {
std::cout << z << " = " << format("%F %T %Z\n", z.get_sys_time());
z = z.get_local_time() + 1h;
}
}
void finishing_day_light_saving_time_with_sys_time() {
using namespace std::literals;
using namespace date;
auto z = make_zoned("Asia/Tokyo", local_days{ 1948_y / sep / sat[2] } +22h);
for (int i = 0; i < 4; ++i) {
std::cout << z << " = " << format("%F %T %Z\n", z.get_sys_time());
z = z.get_sys_time() + 1h;
}
}
void diff_across_day_light_saving() {
using namespace std::literals;
using namespace date;
auto beforeJDT = make_zoned("Asia/Tokyo", local_days{ 1948_y / may / 1 } + 23h);
auto afterJDT = make_zoned("Asia/Tokyo", local_days{ 1948_y / may / 2 } + 1h);
std::cout << date::format("%H:%M\n", afterJDT.get_sys_time() - beforeJDT.get_sys_time()); // 1時間差。
std::cout << date::format("%H:%M\n", afterJDT.get_local_time() - beforeJDT.get_local_time()); // 2時間差。
}
int main()
try
{
diff_across_day_light_saving();
int x;
std::cin >> x;
return 0;
}
catch (date::nonexistent_local_time const& n) {
std::cerr << n.what() << "\n";
}
catch (date::ambiguous_local_time const& a) {
std::cerr << a.what() << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment