jtime.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JTIME_HPP
  14. #define JTIME_HPP
  15. #include "jlib.hpp"
  16. #include "jiface.hpp"
  17. interface IJlibConstDateTime : extends IInterface
  18. {
  19. virtual IStringVal & getString(IStringVal & str) const = 0;
  20. virtual IStringVal & getDateString(IStringVal & str) const = 0;
  21. virtual IStringVal & getTimeString(IStringVal & str) const = 0;
  22. virtual void getDate(unsigned & _year, unsigned & _month, unsigned & _day) const = 0;
  23. virtual void getTime(unsigned & _hour, unsigned & _min, unsigned & _sec, unsigned & _nanosec, int & localToGmtDelta) const = 0;
  24. virtual IStringVal & getGmtString(IStringVal & str) const = 0;
  25. virtual IStringVal & getGmtDateString(IStringVal & str) const = 0;
  26. virtual IStringVal & getGmtTimeString(IStringVal & str) const = 0;
  27. virtual void getGmtDate(unsigned & _year, unsigned & _month, unsigned & _day) const = 0;
  28. virtual void getGmtTime(unsigned & _hour, unsigned & _min, unsigned & _sec, unsigned & _nanosec) const = 0;
  29. virtual IStringVal & getLocalString(IStringVal & str) const = 0;
  30. virtual IStringVal & getLocalDateString(IStringVal & str) const = 0;
  31. virtual IStringVal & getLocalTimeString(IStringVal & str) const = 0;
  32. virtual void getLocalDate(unsigned & _year, unsigned & _month, unsigned & _day) const = 0;
  33. virtual void getLocalTime(unsigned & _hour, unsigned & _min, unsigned & _sec, unsigned & _nanosec) const = 0;
  34. virtual int compare(const IJlibConstDateTime & other) const = 0;
  35. };
  36. interface IJlibDateTime : extends IJlibConstDateTime
  37. {
  38. virtual void setString(const char * pstr) = 0;
  39. virtual void setDateTime(unsigned _year, unsigned _month, unsigned _day, unsigned _hour, unsigned _min, unsigned _sec, unsigned _nanosec, int localToGmtDelta) = 0;
  40. virtual void setGmtString(const char * pstr) = 0;
  41. virtual void setGmtDateString(const char * pstr) = 0;
  42. virtual void setGmtTimeString(const char * pstr) = 0;
  43. virtual void setGmtDate(unsigned _year, unsigned _month, unsigned _day) = 0;
  44. virtual void setGmtTime(unsigned _hour, unsigned _min, unsigned _sec, unsigned _nanosec) = 0;
  45. virtual void setLocalString(const char * pstr) = 0;
  46. virtual void setLocalDateString(const char * pstr) = 0;
  47. virtual void setLocalTimeString(const char * pstr) = 0;
  48. virtual void setLocalDate(unsigned _year, unsigned _month, unsigned _day) = 0;
  49. virtual void setLocalTime(unsigned _hour, unsigned _min, unsigned _sec, unsigned _nanosec) = 0;
  50. };
  51. class jlib_decl CDateTime
  52. {
  53. public:
  54. CDateTime() { clear(); }
  55. CDateTime(CDateTime const & other) { set(other); }
  56. CDateTime(MemoryBuffer & src) { deserialize(src); }
  57. void deserialize(MemoryBuffer & src);
  58. void serialize(MemoryBuffer & dst) const;
  59. hash64_t getHash(hash64_t init) const;
  60. CDateTime & operator=(CDateTime const & other) { set(other); return *this; }
  61. void clear();
  62. void set(CDateTime const & other);
  63. void set(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second, unsigned nano = 0, bool local = false);
  64. void setDate(unsigned year, unsigned month, unsigned day); // Sets to midnight UTC on date given
  65. void setTime(unsigned hour, unsigned minute, unsigned second, unsigned nano = 0, bool local = false); // Leaves the date along, set to the time given
  66. void set(time_t simple);
  67. void setFromFILETIME(__int64 fileTime);
  68. void setTimeStamp(timestamp_type ts);
  69. void setString(char const * str, char const * * end = NULL, bool local = false); // Sets to date and time given as yyyy-mm-ddThh:mm:ss[.nnnnnnnnn]
  70. void setDateString(char const * str, char const * * end = NULL); // Sets to midnight UTC on date given as yyyy-mm-dd
  71. void setTimeString(char const * str, char const * * end = NULL, bool local = false); // Leaves the date alone, sets to the time given as hh:mm:ss[.nnnnnnnnn]
  72. void setNow();
  73. void adjustTime(int deltaMins);
  74. void adjustTimeSecs(int deltaSecs);
  75. void getDate(unsigned & year, unsigned & month, unsigned & day, bool local = false) const;
  76. void getTime(unsigned & hour, unsigned & minute, unsigned & second, unsigned & nano, bool local = false) const;
  77. time_t getSimple() const;
  78. unsigned __int64 getTimeStamp() const;
  79. StringBuffer & getString(StringBuffer & str, bool local = false) const;
  80. StringBuffer & getDateString(StringBuffer & str, bool local = false) const;
  81. StringBuffer & getTimeString(StringBuffer & str, bool local = false) const;
  82. bool isNull() const;
  83. bool equals(CDateTime const & cdt, bool compareNanosec = true) const;
  84. int compare(CDateTime const & cdt, bool compareNanosec = true) const;
  85. int compareDate(CDateTime const & cdt) const;
  86. //NB operator forms ignore nanoseconds, use named methods for other behaviour
  87. bool operator==(CDateTime const & cdt) const { return equals(cdt, false); }
  88. bool operator!=(CDateTime const & cdt) const { return !equals(cdt, false); }
  89. bool operator<(CDateTime const & cdt) const { return (compare(cdt, false) < 0); }
  90. bool operator<=(CDateTime const & cdt) const { return (compare(cdt, false) <= 0); }
  91. bool operator>(CDateTime const & cdt) const { return (compare(cdt, false) > 0); }
  92. bool operator>=(CDateTime const & cdt) const { return (compare(cdt, false) >= 0); }
  93. int queryUtcToLocalDelta() const;
  94. private:
  95. void setFromUtcTm(struct tm const & ts);
  96. void getToUtcTm(struct tm & ts) const;
  97. private:
  98. short utc_year;
  99. byte utc_mon;
  100. byte utc_mday;
  101. byte utc_hour;
  102. byte utc_min;
  103. byte utc_sec;
  104. unsigned nanosec;
  105. };
  106. class jlib_decl CCronAtSchedule
  107. {
  108. // all arrays are in order
  109. UnsignedArray minutes; // 0-59
  110. UnsignedArray hours; // 0-23
  111. UnsignedArray days; // 1-31
  112. UnsignedArray months; // 1-12
  113. UnsignedArray dows; // 0-6 0=sunday, 6=saturday
  114. bool match(UnsignedArray &a,unsigned v,unsigned &next);
  115. bool matchDay(unsigned yr, unsigned mon, unsigned dy, unsigned &nextdy);
  116. public:
  117. const char *set(const char *spec); // returns past final param
  118. void next(const CDateTime &fromdt, CDateTime &nextdt, bool greater=false);
  119. };
  120. interface ICronTable : public IInterface
  121. {
  122. interface Transaction : public IInterface
  123. {
  124. //for safety, will rollback on destruction, so need explicit commit()
  125. virtual void add(char const * spec, char const * tag) = 0;
  126. virtual unsigned remove(char const * tag) = 0;
  127. virtual unsigned unremove(char const * tag) = 0;
  128. virtual void removeall() = 0;
  129. virtual void commit() = 0;
  130. virtual void rollback() = 0;
  131. };
  132. virtual void add(char const * spec, char const * tag) = 0;
  133. virtual unsigned remove(char const * tag) = 0;
  134. virtual void removeall() = 0;
  135. virtual Transaction * getTransactionFrame() = 0; //can only have one frame at a time, subsequent attempts return NULL
  136. virtual unsigned next(CDateTime const & fromdt, CDateTime & nextdt, StringArray & tags) = 0;
  137. };
  138. extern jlib_decl ICronTable * createCronTable();
  139. class CTimeMon
  140. {
  141. public:
  142. unsigned t;
  143. unsigned timeout = 0;
  144. CTimeMon() { t = msTick(); }
  145. CTimeMon(unsigned _timeout) { reset(_timeout); }
  146. void reset(unsigned _timeout) { timeout=_timeout; t = msTick(); }
  147. unsigned elapsed()
  148. {
  149. return msTick()-t;
  150. }
  151. bool timedout(unsigned *remaining=NULL)
  152. {
  153. if ((int)timeout<0) {
  154. if (remaining)
  155. *remaining = (unsigned)-1;
  156. return false;
  157. }
  158. unsigned e = elapsed();
  159. if (e>=timeout) {
  160. if (remaining)
  161. *remaining = 0;
  162. return true;
  163. }
  164. if (remaining)
  165. *remaining = timeout-e;
  166. return false;
  167. }
  168. };
  169. extern jlib_decl IJlibDateTime * createDateTime();
  170. extern jlib_decl IJlibDateTime * createDateTimeNow();
  171. extern jlib_decl void serializeDateTime(MemoryBuffer & tgt);
  172. extern jlib_decl IJlibDateTime * deserializeDateTime(MemoryBuffer & src);
  173. extern jlib_decl void testTiming();
  174. extern jlib_decl void testCronTable();
  175. extern jlib_decl IJlibDateTime * createDateTimeFromLocal(time_t lt);
  176. extern jlib_decl time_t createLocalFromDateTime(IJlibDateTime const * dt);
  177. extern jlib_decl void timetToIDateTime(CDateTime * target, time_t time);
  178. extern jlib_decl time_t timetFromIDateTime(const CDateTime * source);
  179. #ifndef __GNUC__ //in GNU C, all these functions are defined --- elsewhere, we'll have to fake them
  180. extern jlib_decl struct tm * gmtime_r(time_t const * simple, struct tm * utc);
  181. extern jlib_decl struct tm * localtime_r(time_t const * simple, struct tm * local);
  182. extern jlib_decl time_t timegm(struct tm * utc);
  183. extern jlib_decl time_t timelocal(struct tm * local);
  184. #endif
  185. #endif