tz.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-FileCopyrightText: 1996 Arthur David Olson
  3. // SPDX-License-Identifier: BSD-2-Clause
  4. #pragma once
  5. #include <cstdint>
  6. #include <limits>
  7. #include <span>
  8. #include <array>
  9. #include <time.h>
  10. namespace Tz {
  11. using u8 = uint8_t;
  12. using s8 = int8_t;
  13. using u16 = uint16_t;
  14. using s16 = int16_t;
  15. using u32 = uint32_t;
  16. using s32 = int32_t;
  17. using u64 = uint64_t;
  18. using s64 = int64_t;
  19. constexpr size_t TZ_MAX_TIMES = 1000;
  20. constexpr size_t TZ_MAX_TYPES = 128;
  21. constexpr size_t TZ_MAX_CHARS = 50;
  22. constexpr size_t MY_TZNAME_MAX = 255;
  23. constexpr size_t TZNAME_MAXIMUM = 255;
  24. constexpr size_t TZ_MAX_LEAPS = 50;
  25. constexpr s64 TIME_T_MAX = std::numeric_limits<s64>::max();
  26. constexpr s64 TIME_T_MIN = std::numeric_limits<s64>::min();
  27. constexpr size_t CHARS_EXTRA = 3;
  28. constexpr size_t MAX_ZONE_CHARS = std::max(TZ_MAX_CHARS + CHARS_EXTRA, sizeof("UTC"));
  29. constexpr size_t MAX_TZNAME_CHARS = 2 * (MY_TZNAME_MAX + 1);
  30. struct ttinfo {
  31. s32 tt_utoff;
  32. bool tt_isdst;
  33. s32 tt_desigidx;
  34. bool tt_ttisstd;
  35. bool tt_ttisut;
  36. };
  37. static_assert(sizeof(ttinfo) == 0x10, "ttinfo has the wrong size!");
  38. struct Rule {
  39. s32 timecnt;
  40. s32 typecnt;
  41. s32 charcnt;
  42. bool goback;
  43. bool goahead;
  44. std::array <u8, 0x2> padding0;
  45. std::array<s64, TZ_MAX_TIMES> ats;
  46. std::array<u8, TZ_MAX_TIMES> types;
  47. std::array<ttinfo, TZ_MAX_TYPES> ttis;
  48. std::array<char, std::max(MAX_ZONE_CHARS, MAX_TZNAME_CHARS)> chars;
  49. s32 defaulttype;
  50. std::array <u8, 0x12C4> padding1;
  51. };
  52. static_assert(sizeof(Rule) == 0x4000, "Rule has the wrong size!");
  53. struct CalendarTimeInternal {
  54. s32 tm_sec;
  55. s32 tm_min;
  56. s32 tm_hour;
  57. s32 tm_mday;
  58. s32 tm_mon;
  59. s32 tm_year;
  60. s32 tm_wday;
  61. s32 tm_yday;
  62. s32 tm_isdst;
  63. std::array<char, 16> tm_zone;
  64. s32 tm_utoff;
  65. s32 time_index;
  66. };
  67. static_assert(sizeof(CalendarTimeInternal) == 0x3C, "CalendarTimeInternal has the wrong size!");
  68. s32 ParseTimeZoneBinary(Rule& out_rule, std::span<const u8> binary);
  69. bool localtime_rz(CalendarTimeInternal* tmp, Rule const* sp, time_t* timep);
  70. u32 mktime_tzname(time_t* out_time, Rule const* sp, CalendarTimeInternal* tmp);
  71. } // namespace Tz