PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/runtime/base/time/datetime.h

https://github.com/kevlund/hiphop-php
C Header | 304 lines | 119 code | 38 blank | 147 comment | 0 complexity | e30ed06784a3a2a859f20680f3a25384 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef __HPHP_DATETIME_H__
  17. #define __HPHP_DATETIME_H__
  18. #include <runtime/base/types.h>
  19. #include <runtime/base/time/timezone.h>
  20. namespace HPHP {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. /**
  23. * Encapsulating all date/time manipulations, conversions, input and output
  24. * into this one single class.
  25. */
  26. class DateTime : public SweepableResourceData {
  27. public:
  28. DECLARE_OBJECT_ALLOCATION(DateTime);
  29. /**
  30. * Different RFC/ISO date/time formats for toString(DateFormat).
  31. */
  32. enum DateFormat {
  33. InvalidFormat,
  34. /**
  35. * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
  36. * date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
  37. * day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
  38. * date = 1*2DIGIT month 2DIGIT ; day month year e.g. 20 Jun 82
  39. * month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
  40. * "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
  41. * time = hour zone ; ANSI and Military
  42. * hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
  43. * zone = "UT" / "GMT" / "EST" / "EDT" / "CST" / "CDT" / "MST" /
  44. * "MDT" / "PST" / "PDT" / 1ALPHA / (("+" / "-") 4DIGIT)
  45. */
  46. RFC822,
  47. /**
  48. * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
  49. * Format must be acceptable both to the ARPANET and to the getdate
  50. * routine. One format that is acceptable to both is Weekday,
  51. * DD-Mon-YY HH:MM:SS TIMEZONE
  52. * TIMEZONE can be any timezone name (3 or more letters)
  53. */
  54. RFC850,
  55. /**
  56. * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
  57. * Its format must be acceptable both in RFC-822 and to the getdate(3)
  58. * Wdy, DD Mon YY HH:MM:SS TIMEZONE
  59. * There is no hope of having a complete list of timezones. Universal
  60. * Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
  61. * CDT, EST, EDT) and the +/-hhmm offset specifed in RFC-822 should be
  62. * supported.
  63. */
  64. RFC1036,
  65. /**
  66. * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
  67. * RFC-822 Date and Time Specification: RFC-822 Section 5
  68. * The syntax for the date is hereby changed to:
  69. * date = 1*2DIGIT month 2*4DIGIT
  70. */
  71. RFC1123,
  72. /**
  73. * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
  74. * FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
  75. * CFWS = *([FWS] comment) (([FWS] comment) / FWS)
  76. *
  77. * date-time = [ day-of-week "," ] date FWS time [CFWS]
  78. * day-of-week = ([FWS] day-name)
  79. * day-name = "Mon"/"Tue"/"Wed"/"Thu"/"Fri"/"Sat"/"Sun"
  80. * date = day month year
  81. * year = 4*DIGIT
  82. * month = (FWS month-name FWS)
  83. * month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
  84. * "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
  85. * day = ([FWS] 1*2DIGIT)
  86. * time = time-of-day FWS zone
  87. * time-of-day = hour ":" minute [ ":" second ]
  88. * hour = 2DIGIT
  89. * minute = 2DIGIT
  90. * second = 2DIGIT
  91. * zone = (( "+" / "-" ) 4DIGIT)
  92. */
  93. RFC2822,
  94. /**
  95. * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
  96. * date-fullyear = 4DIGIT
  97. * date-month = 2DIGIT ; 01-12
  98. * date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
  99. * ; month/year
  100. *
  101. * time-hour = 2DIGIT ; 00-23
  102. * time-minute = 2DIGIT ; 00-59
  103. * time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on
  104. * ; leap second rules
  105. *
  106. * time-secfrac = "." 1*DIGIT
  107. * time-numoffset = ("+" / "-") time-hour ":" time-minute
  108. * time-offset = "Z" / time-numoffset
  109. *
  110. * partial-time = time-hour ":" time-minute ":" time-second
  111. * [time-secfrac]
  112. * full-date = date-fullyear "-" date-month "-" date-mday
  113. * full-time = partial-time time-offset
  114. *
  115. * date-time = full-date "T" full-time
  116. */
  117. RFC3339,
  118. ISO8601,
  119. /**
  120. * Preliminary specification:
  121. * http://wp.netscape.com/newsref/std/cookie_spec.html
  122. * "This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123,
  123. * with the variations that the only legal time zone is GMT
  124. * and the separators between the elements of the date must be dashes."
  125. */
  126. Cookie,
  127. /**
  128. * Similar to Cookie without dashes.
  129. */
  130. HttpHeader,
  131. /**
  132. * RFC4287, Section 3.3: http://www.ietf.org/rfc/rfc4287.txt
  133. * A Date construct is an element whose content MUST conform to the
  134. * "date-time" production in [RFC3339]. In addition, an uppercase "T"
  135. * character MUST be used to separate date and time, and an uppercase
  136. * "Z" character MUST be present in the absence of a numeric time zone
  137. * offset.
  138. */
  139. Atom = RFC3339,
  140. /**
  141. * RSS 2.0 Specification: http://blogs.law.harvard.edu/tech/rss
  142. * "All date-times in RSS conform to the Date and Time Specification of
  143. * RFC 822, with the exception that the year may be expressed with two
  144. * characters or four characters (four preferred)"
  145. */
  146. RSS = RFC1123,
  147. W3C = RFC3339,
  148. };
  149. /**
  150. * Different array formats for toArray().
  151. */
  152. enum ArrayFormat {
  153. TimeMap,
  154. TmMap,
  155. TmVector
  156. };
  157. /**
  158. * Different formats for getSunInfo().
  159. */
  160. enum SunInfoFormat {
  161. ReturnTimeStamp,
  162. ReturnString,
  163. ReturnDouble
  164. };
  165. public:
  166. static const char *DateFormatRFC822;
  167. static const char *DateFormatRFC850;
  168. static const char *DateFormatRFC1036;
  169. static const char *DateFormatRFC1123;
  170. static const char *DateFormatRFC2822;
  171. static const char *DateFormatRFC3339;
  172. static const char *DateFormatISO8601;
  173. static const char *DateFormatCookie;
  174. static const char *DateFormatHttpHeader;
  175. static const char *MonthNames[];
  176. static const char *ShortMonthNames[];
  177. static const char *WeekdayNames[];
  178. static const char *ShortWeekdayNames[];
  179. static const char *GetWeekdayName(int y, int m, int d);
  180. static const char *GetShortWeekdayName(int y, int m, int d);
  181. static const char *OrdinalSuffix(int number);
  182. static bool IsLeap(int year);
  183. static int DaysInMonth(int y, int m);
  184. static bool IsValid(int y, int m, int d);
  185. /**
  186. * What time is it?
  187. */
  188. static SmartObject<DateTime> Current(bool utc = false);
  189. /**
  190. * Returns are really in special PHP formats, and please read datetime.cpp
  191. * for details.
  192. */
  193. static Array Parse(CStrRef datetime);
  194. static Array Parse(CStrRef ts, CStrRef format);
  195. public:
  196. // constructor
  197. DateTime();
  198. DateTime(int64 timestamp, bool utc = false); // from a timestamp
  199. static StaticString s_class_name;
  200. // overriding ResourceData
  201. CStrRef o_getClassNameHook() const { return s_class_name; }
  202. // informational
  203. bool local() const { return m_time->is_localtime;}
  204. bool utc() const { return !m_time->is_localtime;}
  205. int year() const { return m_time->y;}
  206. int month() const { return m_time->m;}
  207. int day() const { return m_time->d;}
  208. int hour() const { return m_time->h;}
  209. int hour12() const { return (m_time->h % 12) ? (int) m_time->h % 12 : 12;}
  210. int minute() const { return m_time->i;}
  211. int second() const { return m_time->s;}
  212. double fraction() const { return m_time->f;}
  213. int beat() const; // Swatch Beat a.k.a. Internet Time
  214. int dow() const; // day of week
  215. int doy() const; // day of year
  216. int isoWeek() const;
  217. int isoYear() const;
  218. int isoDow() const;
  219. int offset() const; // timezone offset from UTC
  220. SmartObject<TimeZone> timezone() const { return m_tz->cloneTimeZone();}
  221. const char *weekdayName() const;
  222. const char *shortWeekdayName() const;
  223. const char *monthName() const;
  224. const char *shortMonthName() const;
  225. // modifications
  226. void set(int hour, int minute, int second, int month, int day, int year);
  227. void setDate(int year, int month, int day);
  228. void setISODate(int year, int week, int day = 1);
  229. void setTime(int hour, int minute, int second = 0);
  230. void setTimezone(SmartObject<TimeZone> tz);
  231. void modify(CStrRef diff); // PHP's date_modify() function, very powerful
  232. // conversions
  233. void toTm(struct tm &ta) const;
  234. int64 toTimeStamp(bool &err) const;
  235. int64 toInteger(char format) const;
  236. String toString(CStrRef format, bool stdc = false) const;
  237. String toString(DateFormat format) const;
  238. Array toArray(ArrayFormat format) const;
  239. void fromTimeStamp(int64 timestamp, bool utc = false);
  240. bool fromString(CStrRef input, SmartObject<TimeZone> tz);
  241. // cloning
  242. SmartObject<DateTime> cloneDateTime() const;
  243. // sun info
  244. Array getSunInfo(double latitude, double longitude) const;
  245. Variant getSunInfo(SunInfoFormat retformat,
  246. double latitude, double longitude,
  247. double zenith, double utc_offset, bool calc_sunset) const;
  248. private:
  249. struct time_deleter {
  250. void operator()(timelib_time *t) {
  251. timelib_time_dtor(t);
  252. }
  253. };
  254. typedef boost::shared_ptr<timelib_time> TimePtr;
  255. TimePtr m_time;
  256. SmartObject<TimeZone> m_tz;
  257. mutable int64 m_timestamp;
  258. mutable bool m_timestampSet;
  259. // helpers
  260. void update();
  261. String rfcFormat(CStrRef format) const;
  262. String stdcFormat(CStrRef format) const;
  263. };
  264. ///////////////////////////////////////////////////////////////////////////////
  265. }
  266. #endif // __HPHP_DATETIME_H__