Timezone_VX.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Timezone_VXX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Timezone_VX.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: DateTime
  8. // Module: Timezone
  9. //
  10. // Copyright (c) 2004-2011, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Timezone.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/Environment.h"
  18. #include <ctime>
  19. namespace Poco {
  20. int Timezone::utcOffset()
  21. {
  22. std::time_t now = std::time(NULL);
  23. struct std::tm t;
  24. gmtime_r(&now, &t);
  25. std::time_t utc = std::mktime(&t);
  26. return now - utc;
  27. }
  28. int Timezone::dst()
  29. {
  30. std::time_t now = std::time(NULL);
  31. struct std::tm t;
  32. if (localtime_r(&now, &t) != OK)
  33. throw Poco::SystemException("cannot get local time DST offset");
  34. return t.tm_isdst == 1 ? 3600 : 0;
  35. }
  36. bool Timezone::isDst(const Timestamp& timestamp)
  37. {
  38. std::time_t time = timestamp.epochTime();
  39. struct std::tm* tms = std::localtime(&time);
  40. if (!tms) throw Poco::SystemException("cannot get local time DST flag");
  41. return tms->tm_isdst > 0;
  42. }
  43. std::string Timezone::name()
  44. {
  45. // format of TIMEZONE environment variable:
  46. // name_of_zone:<(unused)>:time_in_minutes_from_UTC:daylight_start:daylight_end
  47. std::string tz = Environment::get("TIMEZONE", "UTC");
  48. std::string::size_type pos = tz.find(':');
  49. if (pos != std::string::npos)
  50. return tz.substr(0, pos);
  51. else
  52. return tz;
  53. }
  54. std::string Timezone::standardName()
  55. {
  56. return name();
  57. }
  58. std::string Timezone::dstName()
  59. {
  60. return name();
  61. }
  62. } // namespace Poco