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

/pkg/vtiger/modules/Mobile/modules/Mobile/third-party/qCal/qCal/Time.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 198 lines | 109 code | 29 blank | 60 comment | 21 complexity | e81cb5cd76d1f147f427a96a9662f44c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, LGPL-2.1, GPL-2.0, GPL-3.0
  1. <?php
  2. class qCal_Time {
  3. /**
  4. * Timestamp (represents time at GMT, so must have timezone's offest
  5. * applied before it will be accurate for your specified timezone)
  6. */
  7. protected $time;
  8. /**
  9. * The default format that time is output as
  10. */
  11. protected $format = "H:i:s";
  12. /**
  13. * The timezone
  14. */
  15. protected $timezone;
  16. /**
  17. * Time array (contains hour, minute, second, etc.)
  18. */
  19. protected $timeArray = array();
  20. /**
  21. * Class constructor
  22. * This component is immutable. It can only be created, not modified.
  23. */
  24. public function __construct($hour = null, $minute = null, $second = null, $timezone = null, $rollover = null) {
  25. $this->setTimezone($timezone)
  26. ->setTime($hour, $minute, $second, $rollover);
  27. }
  28. /**
  29. * Set the time
  30. * @access protected This class is immutable, so this is protected. Only the constructor calls it.
  31. */
  32. protected function setTime($hour = null, $minute = null, $second = null, $rollover = null) {
  33. if (is_null($hour)) {
  34. $hour = gmdate("H");
  35. }
  36. if (is_null($minute)) {
  37. $minute = gmdate("i");
  38. }
  39. if (is_null($second)) {
  40. $second = gmdate("s");
  41. }
  42. if (is_null($rollover)) $rollover = false;
  43. if (!$rollover) {
  44. if ($hour > 23 || $minute > 59 || $second > 59) {
  45. throw new qCal_DateTime_Exception_InvalidTime(sprintf("Invalid time specified for qCal_Time: \"%02d:%02d:%02d\"", $hour, $minute, $second));
  46. }
  47. }
  48. // since PHP is incapable of storing a time without a date, we use the first day of
  49. // the unix epoch so that we only have the amount of seconds since the zero of unix epoch
  50. // we only use gm here because we don't want the server's timezone to interfere
  51. $time = gmmktime($hour, $minute, $second, 1, 1, 1970);
  52. $this->time = $time;
  53. $formatString = "a|A|B|g|G|h|H|i|s|u";
  54. $keys = explode("|", $formatString);
  55. $vals = explode("|", gmdate($formatString, $this->getTimestamp(false)));
  56. $this->timeArray = array_merge($this->timeArray, array_combine($keys, $vals));
  57. return $this;
  58. }
  59. /**
  60. * Set the timezone
  61. */
  62. protected function setTimezone($timezone) {
  63. if (is_null($timezone) || !($timezone instanceof qCal_Timezone)) {
  64. $timezone = qCal_Timezone::factory($timezone);
  65. }
  66. $this->timezone = $timezone;
  67. return $this;
  68. }
  69. /**
  70. * Get the timezone
  71. */
  72. public function getTimezone() {
  73. return $this->timezone;
  74. }
  75. /**
  76. * Generate a qCal_Time object via a string or a number of other methods
  77. */
  78. public static function factory($time, $timezone = null) {
  79. if (is_null($timezone) || !($timezone instanceof qCal_Timezone)) {
  80. $timezone = qCal_Timezone::factory($timezone);
  81. }
  82. // get the default timezone so we can set it back to it later
  83. $tz = date_default_timezone_get();
  84. // set the timezone to GMT temporarily
  85. date_default_timezone_set("GMT");
  86. if (is_integer($time)) {
  87. // @todo Handle timestamps
  88. // @maybe not...
  89. }
  90. if (is_string($time)) {
  91. if ($time == "now") {
  92. $time = new qCal_Time(null, null, null, $timezone);
  93. } else {
  94. $tstring = "01/01/1970 $time";
  95. if (!$timestamp = strtotime($tstring)) {
  96. // if unix timestamp can't be created throw an exception
  97. throw new qCal_DateTime_Exception_InvalidTime("Invalid or ambiguous time string passed to qCal_Time::factory()");
  98. }
  99. list($hour, $minute, $second) = explode(":", gmdate("H:i:s", $timestamp));
  100. $time = new qCal_Time($hour, $minute, $second, $timezone);
  101. }
  102. }
  103. // set the timezone back to what it was
  104. date_default_timezone_set($tz);
  105. return $time;
  106. }
  107. /**
  108. * Get the hour
  109. */
  110. public function getHour() {
  111. return $this->timeArray['G'];
  112. }
  113. /**
  114. * Get the minute
  115. */
  116. public function getMinute() {
  117. return $this->timeArray['i'];
  118. }
  119. /**
  120. * Get the second
  121. */
  122. public function getSecond() {
  123. return $this->timeArray['s'];
  124. }
  125. /**
  126. * Get the timestamp
  127. */
  128. public function getTimestamp($useOffset = true) {
  129. $time = ($useOffset) ?
  130. $this->time - $this->getTimezone()->getOffsetSeconds() :
  131. $this->time;
  132. return $time;
  133. }
  134. /**
  135. * Set the format to use when outputting as a string
  136. */
  137. public function setFormat($format) {
  138. $this->format = (string) $format;
  139. return $this;
  140. }
  141. /**
  142. * Output the object using PHP's date() function's meta-characters
  143. */
  144. public function format($format) {
  145. $escape = false;
  146. $meta = str_split($format);
  147. $output = array();
  148. foreach($meta as $char) {
  149. if ($char == '\\') {
  150. $escape = true;
  151. continue;
  152. }
  153. if (!$escape && array_key_exists($char, $this->timeArray)) {
  154. $output[] = $this->timeArray[$char];
  155. } else {
  156. $output[] = $char;
  157. }
  158. // reset this to false after every iteration that wasn't "continued"
  159. $escape = false;
  160. }
  161. return implode($output);
  162. }
  163. /**
  164. * Output the object as a string
  165. */
  166. public function __toString() {
  167. return $this->format($this->format);
  168. }
  169. }