timezone.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "wlib/xtime.h"
  19. #include "timezone.h"
  20. void GetTimezoneInfo(const char * &timezone_str, int &timezone_offset) {
  21. timezone_str = "Unknown Timezone";
  22. timezone_offset = 0;
  23. #ifdef _WINDOWS
  24. struct _timeb wintime;
  25. _ftime(&wintime);
  26. if (wintime.dstflag) {
  27. // Daylight savings time
  28. if (_daylight) {
  29. timezone_str = _tzname[1];
  30. }
  31. } else {
  32. timezone_str = _tzname[0];
  33. }
  34. timezone_offset = wintime.timezone * 60; // its in minutes...
  35. #endif
  36. #ifndef _WINDOWS
  37. struct timeval unixtime;
  38. struct timezone unixtzone;
  39. gettimeofday(&unixtime,&unixtzone);
  40. struct tm unixtm;
  41. localtime_r(&unixtime.tv_sec, &unixtm);
  42. if (unixtm.tm_isdst) {
  43. // Daylight savings time
  44. if (daylight) timezone_str = tzname[1];
  45. timezone_offset = altzone;
  46. } else {
  47. timezone_str = tzname[0];
  48. timezone_offset = timezone;
  49. }
  50. #endif
  51. }
  52. const char * TimezoneString(void) {
  53. const char *timezone_str;
  54. int timezone_offset;
  55. GetTimezoneInfo(timezone_str, timezone_offset);
  56. return timezone_str;
  57. }
  58. int TimezoneOffset(void) {
  59. const char *timezone_str;
  60. int timezone_offset;
  61. GetTimezoneInfo(timezone_str, timezone_offset);
  62. return timezone_offset;
  63. }