PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/libs/clearbricks/common/lib.date.php

https://bitbucket.org/dotclear/dotclear/
PHP | 260 lines | 118 code | 33 blank | 109 comment | 12 complexity | 570ad3fc526ff54e2f76eaef8121af2d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. # -- BEGIN LICENSE BLOCK ---------------------------------------
  3. #
  4. # This file is part of Clearbricks.
  5. #
  6. # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
  7. # Licensed under the GPL version 2.0 license.
  8. # See LICENSE file or
  9. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  10. #
  11. # -- END LICENSE BLOCK -----------------------------------------
  12. /**
  13. * Date/time utilities
  14. *
  15. * @package Clearbricks
  16. * @subpackage Common
  17. */
  18. class dt
  19. {
  20. /**
  21. * Timestamp formating
  22. *
  23. * Returns a date formated like PHP
  24. * {@link http://www.php.net/manual/en/function.strftime.php strftime}
  25. * function.
  26. * Special cases %a, %A, %b and %B are handled by {@link l10n} library.
  27. *
  28. * @param string $p Format pattern
  29. * @param integer $ts Timestamp
  30. * @param string $tz Timezone
  31. * @return string
  32. */
  33. public static function str($p,$ts=null,$tz=null)
  34. {
  35. if ($ts === null) { $ts = time(); }
  36. $hash = '799b4e471dc78154865706469d23d512';
  37. $p = preg_replace('/(?<!%)%(a|A)/','{{'.$hash.'__$1%w__}}',$p);
  38. $p = preg_replace('/(?<!%)%(b|B)/','{{'.$hash.'__$1%m__}}',$p);
  39. if ($tz) {
  40. $T = self::getTZ();
  41. self::setTZ($tz);
  42. }
  43. $res = strftime($p,$ts);
  44. if ($tz) {
  45. self::setTZ($T);
  46. }
  47. $res = preg_replace_callback('/{{'.$hash.'__(a|A|b|B)([0-9]{1,2})__}}/',array('self','_callback'),$res);
  48. return $res;
  49. }
  50. /**
  51. * Date to date
  52. *
  53. * Format a literal date to another literal date.
  54. *
  55. * @param string $p Format pattern
  56. * @param string $dt Date
  57. * @param string $tz Timezone
  58. * @return string
  59. */
  60. public static function dt2str($p,$dt,$tz=null)
  61. {
  62. return dt::str($p,strtotime($dt),$tz);
  63. }
  64. /**
  65. * ISO-8601 formatting
  66. *
  67. * Returns a timestamp converted to ISO-8601 format.
  68. *
  69. * @param integer $ts Timestamp
  70. * @param string $tz Timezone
  71. * @return string
  72. */
  73. public static function iso8601($ts,$tz='UTC')
  74. {
  75. $o = self::getTimeOffset($tz,$ts);
  76. $of = sprintf('%02u:%02u',abs($o)/3600,(abs($o)%3600)/60);
  77. return date('Y-m-d\\TH:i:s',$ts).($o < 0 ? '-' : '+').$of;
  78. }
  79. /**
  80. * RFC-822 formatting
  81. *
  82. * Returns a timestamp converted to RFC-822 format.
  83. *
  84. * @param integer $ts Timestamp
  85. * @param string $tz Timezone
  86. * @return string
  87. */
  88. public static function rfc822($ts,$tz='UTC')
  89. {
  90. # Get offset
  91. $o = self::getTimeOffset($tz,$ts);
  92. $of = sprintf('%02u%02u',abs($o)/3600,(abs($o)%3600)/60);
  93. return strftime('%a, %d %b %Y %H:%M:%S '.($o < 0 ? '-' : '+').$of,$ts);
  94. }
  95. /**
  96. * Timezone set
  97. *
  98. * Set timezone during script execution.
  99. *
  100. * @param string $tz Timezone
  101. */
  102. public static function setTZ($tz)
  103. {
  104. if (function_exists('date_default_timezone_set')) {
  105. date_default_timezone_set($tz);
  106. return;
  107. }
  108. if (!ini_get('safe_mode')) {
  109. putenv('TZ='.$tz);
  110. }
  111. }
  112. /**
  113. * Current timezone
  114. *
  115. * Returns current timezone.
  116. *
  117. * @return string
  118. */
  119. public static function getTZ()
  120. {
  121. if (function_exists('date_default_timezone_get')) {
  122. return date_default_timezone_get();
  123. }
  124. return date('T');
  125. }
  126. /**
  127. * Time offset
  128. *
  129. * Get time offset for a timezone and an optionnal $ts timestamp.
  130. *
  131. * @param string $tz Timezone
  132. * @param integer $ts Timestamp
  133. * @return integer
  134. */
  135. public static function getTimeOffset($tz,$ts=false)
  136. {
  137. if (!$ts) {
  138. $ts = time();
  139. }
  140. $server_tz = self::getTZ();
  141. $server_offset = date('Z',$ts);
  142. self::setTZ($tz);
  143. $cur_offset = date('Z',$ts);
  144. self::setTZ($server_tz);
  145. return $cur_offset-$server_offset;
  146. }
  147. /**
  148. * UTC conversion
  149. *
  150. * Returns any timestamp from current timezone to UTC timestamp.
  151. *
  152. * @param integer $ts Timestamp
  153. * @return integer
  154. */
  155. public static function toUTC($ts)
  156. {
  157. return $ts + self::getTimeOffset('UTC',$ts);
  158. }
  159. /**
  160. * Add timezone
  161. *
  162. * Returns a timestamp with its timezone offset.
  163. *
  164. * @param string $tz Timezone
  165. * @param integer $ts Timestamp
  166. * @return integer
  167. */
  168. public static function addTimeZone($tz,$ts=false)
  169. {
  170. if (!$ts) {
  171. $ts = time();
  172. }
  173. return $ts + self::getTimeOffset($tz,$ts);
  174. }
  175. /**
  176. * Timzones
  177. *
  178. * Returns an array of supported timezones, codes are keys and names are values.
  179. *
  180. * @todo Store timzones in a static variable at the first time.
  181. *
  182. * @param boolean $flip Names are keys and codes are values
  183. * @param boolean $groups Return timezones in arrays of continents
  184. * @return array
  185. */
  186. public static function getZones($flip=false,$groups=false)
  187. {
  188. if (!is_readable($f = dirname(__FILE__).'/tz.dat')) {
  189. return array();
  190. }
  191. $tz = file(dirname(__FILE__).'/tz.dat');
  192. $res = array();
  193. foreach ($tz as $v)
  194. {
  195. $v = trim($v);
  196. if ($v) {
  197. $res[$v] = str_replace('_',' ',$v);
  198. }
  199. }
  200. if ($flip) {
  201. $res = array_flip($res);
  202. if ($groups) {
  203. $tmp = array();
  204. foreach ($res as $k => $v) {
  205. $g = explode('/',$k);
  206. $tmp[$g[0]][$k] = $v;
  207. }
  208. $res = $tmp;
  209. }
  210. }
  211. return $res;
  212. }
  213. private static function _callback($args)
  214. {
  215. $b = array(1=>'_Jan',2=>'_Feb',3=>'_Mar',4=>'_Apr',5=>'_May',6=>'_Jun',
  216. 7=>'_Jul',8=>'_Aug',9=>'_Sep',10=>'_Oct',11=>'_Nov',12=>'_Dec');
  217. $B = array(1=>'January',2=>'February',3=>'March',4=>'April',
  218. 5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',
  219. 10=>'October',11=>'November',12=>'December');
  220. $a = array(1=>'_Mon',2=>'_Tue',3=>'_Wed',4=>'_Thu',5=>'_Fri',
  221. 6=>'_Sat',0=>'_Sun');
  222. $A = array(1=>'Monday',2=>'Tuesday',3=>'Wednesday',4=>'Thursday',
  223. 5=>'Friday',6=>'Saturday',0=>'Sunday');
  224. return __(${$args[1]}[(integer) $args[2]]);
  225. }
  226. }
  227. ?>