PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/qCal/lib/qCal/Time.php

https://github.com/pschuele/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 257 lines | 109 code | 34 blank | 114 comment | 21 complexity | c654d0df68a8e1a0b036f77458854c16 MD5 | raw file
  1. <?php
  2. /**
  3. * qCal_Time
  4. *
  5. * This class is used to represent a time that is not associated with any specific date.
  6. *
  7. * @package qCal
  8. * @subpackage qCal_DateTime
  9. * @copyright Luke Visinoni (luke.visinoni@gmail.com)
  10. * @author Luke Visinoni (luke.visinoni@gmail.com)
  11. * @license GNU Lesser General Public License
  12. */
  13. class qCal_Time {
  14. /**
  15. * @var integer Timestamp (represents time at GMT, so must have timezone's offset
  16. * applied before it will be accurate for your specified timezone)
  17. */
  18. protected $time;
  19. /**
  20. * @var string The default format that time is output as
  21. */
  22. protected $format = "H:i:s";
  23. /**
  24. * @var qCal_Timezone The timezone
  25. */
  26. protected $timezone;
  27. /**
  28. * @var array Time array (contains hour, minute, second, etc.)
  29. */
  30. protected $timeArray = array();
  31. /**
  32. * Class constructor
  33. * This component is immutable. It can only be created, not modified.
  34. * @param integer $hour
  35. * @param integer $minute
  36. * @param integer $second
  37. * @param mixed $timezone Either a qCal_Timezone or a string representing one
  38. * @param integer $rollover Set this to true if you want to be able to use "rollover" time intervals.
  39. * For instance, you could specify 100 seconds, which would translate to 1 minute, 40 seconds.
  40. * @access public
  41. */
  42. public function __construct($hour = null, $minute = null, $second = null, $timezone = null, $rollover = null) {
  43. $this->setTimezone($timezone)
  44. ->setTime($hour, $minute, $second, $rollover);
  45. }
  46. /**
  47. * Set the time
  48. * This class is immutable, so this is protected. Only the constructor calls it.
  49. * @param integer $hour
  50. * @param integer $minute
  51. * @param integer $second
  52. * @param integer $rollover (see above)
  53. * @access protected
  54. * @return $this
  55. */
  56. protected function setTime($hour = null, $minute = null, $second = null, $rollover = null) {
  57. if (is_null($hour)) {
  58. $hour = gmdate("H");
  59. }
  60. if (is_null($minute)) {
  61. $minute = gmdate("i");
  62. }
  63. if (is_null($second)) {
  64. $second = gmdate("s");
  65. }
  66. if (is_null($rollover)) $rollover = false;
  67. if (!$rollover) {
  68. if ($hour > 23 || $minute > 59 || $second > 59) {
  69. throw new qCal_DateTime_Exception_InvalidTime(sprintf("Invalid time specified for qCal_Time: \"%02d:%02d:%02d\"", $hour, $minute, $second));
  70. }
  71. }
  72. // since PHP is incapable of storing a time without a date, we use the first day of
  73. // the unix epoch so that we only have the amount of seconds since the zero of unix epoch
  74. // we only use gm here because we don't want the server's timezone to interfere
  75. $time = gmmktime($hour, $minute, $second, 1, 1, 1970);
  76. $this->time = $time;
  77. $formatString = "a|A|B|g|G|h|H|i|s|u";
  78. $keys = explode("|", $formatString);
  79. $vals = explode("|", gmdate($formatString, $this->getTimestamp(false)));
  80. $this->timeArray = array_merge($this->timeArray, array_combine($keys, $vals));
  81. return $this;
  82. }
  83. /**
  84. * Set the timezone
  85. * @param mixed $timezone Either a qCal_Timezone object or a string representing one
  86. * @return $this
  87. * @access protected
  88. */
  89. protected function setTimezone($timezone) {
  90. if (is_null($timezone) || !($timezone instanceof qCal_Timezone)) {
  91. $timezone = qCal_Timezone::factory($timezone);
  92. }
  93. $this->timezone = $timezone;
  94. return $this;
  95. }
  96. /**
  97. * Get the timezone
  98. * @return qCal_Timezone
  99. * @access public
  100. */
  101. public function getTimezone() {
  102. return $this->timezone;
  103. }
  104. /**
  105. * Generate a qCal_Time object via a string or a number of other methods
  106. * @param string A time string to convert into a qCal_Time object (ex: 4:00)
  107. * @param mixed Either a qCal_Timezone object or a string representing one
  108. * @access public
  109. * @static
  110. */
  111. public static function factory($time, $timezone = null) {
  112. if (is_null($timezone) || !($timezone instanceof qCal_Timezone)) {
  113. $timezone = qCal_Timezone::factory($timezone);
  114. }
  115. // get the default timezone so we can set it back to it later
  116. $tz = date_default_timezone_get();
  117. // set the timezone to GMT temporarily
  118. date_default_timezone_set("GMT");
  119. if (is_integer($time)) {
  120. // @todo Handle timestamps
  121. // @maybe not...
  122. }
  123. if (is_string($time)) {
  124. if ($time == "now") {
  125. $time = new qCal_Time(null, null, null, $timezone);
  126. } else {
  127. $tstring = "01/01/1970 $time";
  128. if (!$timestamp = strtotime($tstring)) {
  129. // if unix timestamp can't be created throw an exception
  130. throw new qCal_DateTime_Exception_InvalidTime("Invalid or ambiguous time string passed to qCal_Time::factory()");
  131. }
  132. list($hour, $minute, $second) = explode(":", gmdate("H:i:s", $timestamp));
  133. $time = new qCal_Time($hour, $minute, $second, $timezone);
  134. }
  135. }
  136. // set the timezone back to what it was
  137. date_default_timezone_set($tz);
  138. return $time;
  139. }
  140. /**
  141. * Get the hour
  142. * @return integer The hour
  143. * @access public
  144. */
  145. public function getHour() {
  146. return $this->timeArray['G'];
  147. }
  148. /**
  149. * Get the minute
  150. * @return integer The minute
  151. * @access public
  152. */
  153. public function getMinute() {
  154. return $this->timeArray['i'];
  155. }
  156. /**
  157. * Get the second
  158. * @return integer The second
  159. * @access public
  160. */
  161. public function getSecond() {
  162. return $this->timeArray['s'];
  163. }
  164. /**
  165. * Get the timestamp
  166. * @param boolean $useOffset Set to true to get a timestamp that takes the
  167. * timezone offset into consideration
  168. * @return integer This is not a unix timestamp because there is no date
  169. * associated with this time. It is a timestamp from the beginning of the day
  170. * @access public
  171. */
  172. public function getTimestamp($useOffset = true) {
  173. $time = ($useOffset) ?
  174. $this->time - $this->getTimezone()->getOffsetSeconds() :
  175. $this->time;
  176. return $time;
  177. }
  178. /**
  179. * Set the format to use when outputting as a string
  180. * @param string $format Use PHP's date() function's time-related
  181. * metacharacters to set the format used when this object is printed
  182. * @return $this
  183. * @access public
  184. */
  185. public function setFormat($format) {
  186. $this->format = (string) $format;
  187. return $this;
  188. }
  189. /**
  190. * Output the object using PHP's date() function's meta-characters
  191. * @param string $format Use PHP's date() function's time-related
  192. * metacharacters to set the format that this returns
  193. * @return string This object formatted as a string
  194. * @access public
  195. */
  196. public function format($format) {
  197. $escape = false;
  198. $meta = str_split($format);
  199. $output = array();
  200. foreach($meta as $char) {
  201. if ($char == '\\') {
  202. $escape = true;
  203. continue;
  204. }
  205. if (!$escape && array_key_exists($char, $this->timeArray)) {
  206. $output[] = $this->timeArray[$char];
  207. } else {
  208. $output[] = $char;
  209. }
  210. // reset this to false after every iteration that wasn't "continued"
  211. $escape = false;
  212. }
  213. return implode($output);
  214. }
  215. /**
  216. * Output the object as a string
  217. * @return string This object formatted as a string
  218. * @access public
  219. */
  220. public function __toString() {
  221. return $this->format($this->format);
  222. }
  223. }