/framework/Kolab_Format/lib/Horde/Kolab/Format/Date.php

https://github.com/ewandor/horde · PHP · 258 lines · 97 code · 17 blank · 144 comment · 11 complexity · 9b4d191147fd242fe549ddd0f31580b7 MD5 · raw file

  1. <?php
  2. /**
  3. * Helper functions to handle format conversions.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Kolab
  8. * @package Kolab_Format
  9. * @author Stuart Binge <omicron@mighty.co.za>
  10. * @author Thomas Jarosch <thomas.jarosch@intra2net.com>
  11. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  12. * @link http://www.horde.org/libraries/Horde_Kolab_Format
  13. */
  14. /**
  15. * Kolab date handling functions. Based upon Kolab.php from Stuart Binge.
  16. *
  17. * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
  18. *
  19. * See the enclosed file COPYING for license information (LGPL). If you did not
  20. * receive this file, see
  21. * http://www.horde.org/licenses/lgpl21.
  22. *
  23. * @category Kolab
  24. * @package Kolab_Format
  25. * @author Stuart Binge <omicron@mighty.co.za>
  26. * @author Thomas Jarosch <thomas.jarosch@intra2net.com>
  27. * @license http://www.horde.org/licenses/lgpl21 LGPL
  28. * @link http://www.horde.org/libraries/Horde_Kolab_Format
  29. */
  30. class Horde_Kolab_Format_Date
  31. {
  32. /**
  33. * Returns a UNIX timestamp corresponding the given date string which is in
  34. * the format prescribed by the Kolab Format Specification.
  35. *
  36. * @param string $date The string representation of the date.
  37. *
  38. * @return integer The unix timestamp corresponding to $date.
  39. */
  40. static public function decodeDate($date)
  41. {
  42. if (empty($date)) {
  43. return 0;
  44. }
  45. list($year, $month, $day) = explode('-', $date);
  46. return mktime(0, 0, 0, $month, $day, $year);
  47. }
  48. /**
  49. * Returns a UNIX timestamp corresponding the given date-time string which
  50. * is in the format prescribed by the Kolab Format Specification.
  51. *
  52. * @param string $datetime The string representation of the date & time.
  53. *
  54. * @return integer The unix timestamp corresponding to $datetime.
  55. */
  56. static public function decodeDateTime($datetime)
  57. {
  58. if (empty($datetime)) {
  59. return 0;
  60. }
  61. list($year, $month, $day, $hour, $minute, $second) = sscanf($datetime,
  62. '%d-%d-%dT%d:%d:%dZ');
  63. return gmmktime($hour, $minute, $second, $month, $day, $year);
  64. }
  65. /**
  66. * Returns a UNIX timestamp corresponding the given date or date-time
  67. * string which is in either format prescribed by the Kolab Format
  68. * Specification.
  69. *
  70. * @param string $date The string representation of the date (& time).
  71. *
  72. * @return integer The unix timestamp corresponding to $date.
  73. */
  74. static public function decodeDateOrDateTime($date)
  75. {
  76. if (empty($date)) {
  77. return 0;
  78. }
  79. return (strlen($date) == 10 ? self::decodeDate($date) : self::decodeDateTime($date));
  80. }
  81. /**
  82. * Returns a string containing the current UTC date in the format
  83. * prescribed by the Kolab Format Specification.
  84. *
  85. * @param int $date The integer representation of the date.
  86. *
  87. * @return string The current UTC date in the format 'YYYY-MM-DD'.
  88. */
  89. static public function encodeDate($date = false)
  90. {
  91. if ($date === false) {
  92. $date = time();
  93. }
  94. return strftime('%Y-%m-%d', $date);
  95. }
  96. /**
  97. * Returns a string containing the current UTC date and time in the format
  98. * prescribed by the Kolab Format Specification.
  99. *
  100. * @param int $datetime The integer representation of the date.
  101. *
  102. * @return string The current UTC date and time in the format
  103. * 'YYYY-MM-DDThh:mm:ssZ', where the T and Z are literal
  104. * characters.
  105. */
  106. static public function encodeDateTime($datetime = false)
  107. {
  108. if ($datetime === false) {
  109. $datetime = time();
  110. }
  111. return gmstrftime('%Y-%m-%dT%H:%M:%SZ', $datetime);
  112. }
  113. /**
  114. * Parse the provided string into a PHP DateTime object.
  115. *
  116. * @param string $date_time The Kolab date-time value.
  117. *
  118. * @since Horde_Kolab_Format 1.1.0
  119. *
  120. * @return DateTime The date-time value represented as PHP DateTime object.
  121. */
  122. static public function readUtcDateTime($date_time)
  123. {
  124. if ($date = DateTime::createFromFormat(
  125. 'Y-m-d\TH:i:s\Z', $date_time, new DateTimeZone('UTC')
  126. )) {
  127. return $date;
  128. }
  129. /**
  130. * No need to support fractions of a second yet. So lets just try to
  131. * remove a potential microseconds part and attempt parsing again.
  132. */
  133. $date_time = preg_replace(
  134. '/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).\d+Z/',
  135. '\1Z',
  136. $date_time
  137. );
  138. return DateTime::createFromFormat(
  139. 'Y-m-d\TH:i:s\Z', $date_time, new DateTimeZone('UTC')
  140. );
  141. }
  142. /**
  143. * Parse the provided string into a PHP DateTime object.
  144. *
  145. * @param string $date The Kolab date value.
  146. * @param string $timezone The associated timezone.
  147. *
  148. * @since Horde_Kolab_Format 1.1.0
  149. *
  150. * @return DateTime The date-time value represented as PHP DateTime object.
  151. */
  152. static public function readDate($date, $timezone)
  153. {
  154. return DateTime::createFromFormat(
  155. '!Y-m-d', $date, new DateTimeZone($timezone)
  156. );
  157. }
  158. /**
  159. * Parse the provided string into a PHP DateTime object.
  160. *
  161. * @param string $date_time The Kolab date-time value.
  162. * @param string $timezone The associated timezone.
  163. *
  164. * @since Horde_Kolab_Format 1.1.0
  165. *
  166. * @return DateTime The date-time value represented as PHP DateTime object.
  167. */
  168. static public function readDateTime($date_time, $timezone)
  169. {
  170. /**
  171. * The trailing "Z" for UTC times holds no relevant information. The
  172. * authoritative timezone information is the "tz" attribute. If that one
  173. * is missing we will assume to have a UTC date-time in any case - with
  174. * or without "Z".
  175. */
  176. $date_time = preg_replace('/Z$/','', $date_time);
  177. if ($date = DateTime::createFromFormat(
  178. 'Y-m-d\TH:i:s', $date_time, new DateTimeZone($timezone)
  179. )) {
  180. return $date;
  181. }
  182. /**
  183. * No need to support fractions of a second yet. So lets just try to
  184. * remove a potential microseconds part and attempt parsing again.
  185. */
  186. $date_time = preg_replace(
  187. '/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).\d+Z/',
  188. '\1Z',
  189. $date_time
  190. );
  191. return DateTime::createFromFormat(
  192. 'Y-m-d\TH:i:s\Z', $date_time, new DateTimeZone($timezone)
  193. );
  194. }
  195. /**
  196. * Write the provided PHP DateTime object into a Kolab format UTC date-time
  197. * representation.
  198. *
  199. * @param DateTime $date_time The PHP DateTime object.
  200. *
  201. * @since Horde_Kolab_Format 1.1.0
  202. *
  203. * @return string The Kolab format UTC date-time string.
  204. */
  205. static public function writeUtcDateTime(DateTime $date_time)
  206. {
  207. return $date_time->format('Y-m-d\TH:i:s\Z');
  208. }
  209. /**
  210. * Write the provided PHP DateTime object into a Kolab format date-time
  211. * representation.
  212. *
  213. * @param DateTime $date_time The PHP DateTime object.
  214. *
  215. * @since Horde_Kolab_Format 1.1.0
  216. *
  217. * @return string The Kolab format date-time string.
  218. */
  219. static public function writeDateTime(DateTime $date_time)
  220. {
  221. if ($date_time->getTimezone()->getName() == 'UTC') {
  222. return $date_time->format('Y-m-d\TH:i:s\Z');
  223. } else {
  224. return $date_time->format('Y-m-d\TH:i:s');
  225. }
  226. }
  227. /**
  228. * Write the provided PHP DateTime object into a Kolab format date
  229. * representation.
  230. *
  231. * @param DateTime $date The PHP DateTime object.
  232. *
  233. * @since Horde_Kolab_Format 1.1.0
  234. *
  235. * @return string The Kolab format UTC date string.
  236. */
  237. static public function writeDate(DateTime $date)
  238. {
  239. return $date->format('Y-m-d');
  240. }
  241. }