GregorianDateTime.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef WTF_GregorianDateTime_h
  25. #define WTF_GregorianDateTime_h
  26. #include <string.h>
  27. #include <time.h>
  28. #include <wtf/Noncopyable.h>
  29. namespace WTF {
  30. class GregorianDateTime {
  31. WTF_MAKE_NONCOPYABLE(GregorianDateTime);
  32. public:
  33. GregorianDateTime()
  34. : m_year(0)
  35. , m_month(0)
  36. , m_yearDay(0)
  37. , m_monthDay(0)
  38. , m_weekDay(0)
  39. , m_hour(0)
  40. , m_minute(0)
  41. , m_second(0)
  42. , m_utcOffset(0)
  43. , m_isDST(0)
  44. {
  45. }
  46. inline int year() const { return m_year; }
  47. inline int month() const { return m_month; }
  48. inline int yearDay() const { return m_yearDay; }
  49. inline int monthDay() const { return m_monthDay; }
  50. inline int weekDay() const { return m_weekDay; }
  51. inline int hour() const { return m_hour; }
  52. inline int minute() const { return m_minute; }
  53. inline int second() const { return m_second; }
  54. inline int utcOffset() const { return m_utcOffset; }
  55. inline int isDST() const { return m_isDST; }
  56. inline void setYear(int year) { m_year = year; }
  57. inline void setMonth(int month) { m_month = month; }
  58. inline void setYearDay(int yearDay) { m_yearDay = yearDay; }
  59. inline void setMonthDay(int monthDay) { m_monthDay = monthDay; }
  60. inline void setWeekDay(int weekDay) { m_weekDay = weekDay; }
  61. inline void setHour(int hour) { m_hour = hour; }
  62. inline void setMinute(int minute) { m_minute = minute; }
  63. inline void setSecond(int second) { m_second = second; }
  64. inline void setUtcOffset(int utcOffset) { m_utcOffset = utcOffset; }
  65. inline void setIsDST(int isDST) { m_isDST = isDST; }
  66. WTF_EXPORT_PRIVATE void setToCurrentLocalTime();
  67. operator tm() const
  68. {
  69. tm ret;
  70. memset(&ret, 0, sizeof(ret));
  71. ret.tm_year = m_year - 1900;
  72. ret.tm_mon = m_month;
  73. ret.tm_yday = m_yearDay;
  74. ret.tm_mday = m_monthDay;
  75. ret.tm_wday = m_weekDay;
  76. ret.tm_hour = m_hour;
  77. ret.tm_min = m_minute;
  78. ret.tm_sec = m_second;
  79. ret.tm_isdst = m_isDST;
  80. #if HAVE(TM_GMTOFF)
  81. ret.tm_gmtoff = static_cast<long>(m_utcOffset);
  82. #endif
  83. return ret;
  84. }
  85. void copyFrom(const GregorianDateTime& other)
  86. {
  87. m_year = other.m_year;
  88. m_month = other.m_month;
  89. m_yearDay = other.m_yearDay;
  90. m_monthDay = other.m_monthDay;
  91. m_weekDay = other.m_weekDay;
  92. m_hour = other.m_hour;
  93. m_minute = other.m_minute;
  94. m_second = other.m_second;
  95. m_utcOffset = other.m_utcOffset;
  96. m_isDST = other.m_isDST;
  97. }
  98. private:
  99. int m_year;
  100. int m_month;
  101. int m_yearDay;
  102. int m_monthDay;
  103. int m_weekDay;
  104. int m_hour;
  105. int m_minute;
  106. int m_second;
  107. int m_utcOffset;
  108. int m_isDST;
  109. };
  110. } // namespace WTF
  111. using WTF::GregorianDateTime;
  112. #endif // WTF_GregorianDateTime_h