PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/I18n/Time.php

https://gitlab.com/ViniciusP/project-games
PHP | 353 lines | 149 code | 23 blank | 181 comment | 18 complexity | fd4bee5b7f3677e404fd2d1e8cf38d94 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\I18n;
  16. use Cake\Chronos\MutableDateTime;
  17. use DateTimeInterface;
  18. use DateTimeZone;
  19. use IntlDateFormatter;
  20. use JsonSerializable;
  21. /**
  22. * Extends the built-in DateTime class to provide handy methods and locale-aware
  23. * formatting helpers
  24. *
  25. */
  26. class Time extends MutableDateTime implements JsonSerializable
  27. {
  28. use DateFormatTrait;
  29. /**
  30. * The format to use when formatting a time using `Cake\I18n\Time::i18nFormat()`
  31. * and `__toString`
  32. *
  33. * The format should be either the formatting constants from IntlDateFormatter as
  34. * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
  35. * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
  36. *
  37. * It is possible to provide an array of 2 constants. In this case, the first position
  38. * will be used for formatting the date part of the object and the second position
  39. * will be used to format the time part.
  40. *
  41. * @var string|array|int
  42. * @see \Cake\I18n\Time::i18nFormat()
  43. */
  44. protected static $_toStringFormat = [IntlDateFormatter::SHORT, IntlDateFormatter::SHORT];
  45. /**
  46. * The format to use when formatting a time using `Cake\I18n\Time::nice()`
  47. *
  48. * The format should be either the formatting constants from IntlDateFormatter as
  49. * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
  50. * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
  51. *
  52. * It is possible to provide an array of 2 constants. In this case, the first position
  53. * will be used for formatting the date part of the object and the second position
  54. * will be used to format the time part.
  55. *
  56. * @var string|array|int
  57. * @see \Cake\I18n\Time::nice()
  58. */
  59. public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT];
  60. /**
  61. * The format to use when formatting a time using `Cake\I18n\Time::timeAgoInWords()`
  62. * and the difference is more than `Cake\I18n\Time::$wordEnd`
  63. *
  64. * @var string
  65. * @see \Cake\I18n\Time::timeAgoInWords()
  66. */
  67. public static $wordFormat = [IntlDateFormatter::SHORT, -1];
  68. /**
  69. * The format to use when formatting a time using `Time::timeAgoInWords()`
  70. * and the difference is less than `Time::$wordEnd`
  71. *
  72. * @var array
  73. * @see \Cake\I18n\Time::timeAgoInWords()
  74. */
  75. public static $wordAccuracy = [
  76. 'year' => "day",
  77. 'month' => "day",
  78. 'week' => "day",
  79. 'day' => "hour",
  80. 'hour' => "minute",
  81. 'minute' => "minute",
  82. 'second' => "second",
  83. ];
  84. /**
  85. * The end of relative time telling
  86. *
  87. * @var string
  88. * @see \Cake\I18n\Time::timeAgoInWords()
  89. */
  90. public static $wordEnd = '+1 month';
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function __construct($time = null, $tz = null)
  95. {
  96. if ($time instanceof DateTimeInterface) {
  97. $tz = $time->getTimeZone();
  98. $time = $time->format('Y-m-d H:i:s');
  99. }
  100. if (is_numeric($time)) {
  101. $time = '@' . $time;
  102. }
  103. parent::__construct($time, $tz);
  104. }
  105. /**
  106. * Returns a nicely formatted date string for this object.
  107. *
  108. * The format to be used is stored in the static property `Time::niceFormat`.
  109. *
  110. * @param string|\DateTimeZone|null $timezone Timezone string or DateTimeZone object
  111. * in which the date will be displayed. The timezone stored for this object will not
  112. * be changed.
  113. * @param string|null $locale The locale name in which the date should be displayed (e.g. pt-BR)
  114. * @return string Formatted date string
  115. */
  116. public function nice($timezone = null, $locale = null)
  117. {
  118. return $this->i18nFormat(static::$niceFormat, $timezone, $locale);
  119. }
  120. /**
  121. * Returns true if this object represents a date within the current week
  122. *
  123. * @return bool
  124. */
  125. public function isThisWeek()
  126. {
  127. return static::now($this->getTimezone())->format('W o') == $this->format('W o');
  128. }
  129. /**
  130. * Returns true if this object represents a date within the current month
  131. *
  132. * @return bool
  133. */
  134. public function isThisMonth()
  135. {
  136. return static::now($this->getTimezone())->format('m Y') == $this->format('m Y');
  137. }
  138. /**
  139. * Returns true if this object represents a date within the current year
  140. *
  141. * @return bool
  142. */
  143. public function isThisYear()
  144. {
  145. return static::now($this->getTimezone())->format('Y') == $this->format('Y');
  146. }
  147. /**
  148. * Returns the quarter
  149. *
  150. * @param bool $range Range.
  151. * @return int|array 1, 2, 3, or 4 quarter of year, or array if $range true
  152. */
  153. public function toQuarter($range = false)
  154. {
  155. $quarter = ceil($this->format('m') / 3);
  156. if ($range === false) {
  157. return $quarter;
  158. }
  159. $year = $this->format('Y');
  160. switch ($quarter) {
  161. case 1:
  162. return [$year . '-01-01', $year . '-03-31'];
  163. case 2:
  164. return [$year . '-04-01', $year . '-06-30'];
  165. case 3:
  166. return [$year . '-07-01', $year . '-09-30'];
  167. case 4:
  168. return [$year . '-10-01', $year . '-12-31'];
  169. }
  170. }
  171. /**
  172. * Returns a UNIX timestamp.
  173. *
  174. * @return string UNIX timestamp
  175. */
  176. public function toUnixString()
  177. {
  178. return $this->format('U');
  179. }
  180. /**
  181. * Returns either a relative or a formatted absolute date depending
  182. * on the difference between the current time and this object.
  183. *
  184. * ### Options:
  185. *
  186. * - `from` => another Time object representing the "now" time
  187. * - `format` => a fall back format if the relative time is longer than the duration specified by end
  188. * - `accuracy` => Specifies how accurate the date should be described (array)
  189. * - year => The format if years > 0 (default "day")
  190. * - month => The format if months > 0 (default "day")
  191. * - week => The format if weeks > 0 (default "day")
  192. * - day => The format if weeks > 0 (default "hour")
  193. * - hour => The format if hours > 0 (default "minute")
  194. * - minute => The format if minutes > 0 (default "minute")
  195. * - second => The format if seconds > 0 (default "second")
  196. * - `end` => The end of relative time telling
  197. * - `relativeString` => The `printf` compatible string when outputting relative time
  198. * - `absoluteString` => The `printf` compatible string when outputting absolute time
  199. * - `timezone` => The user timezone the timestamp should be formatted in.
  200. *
  201. * Relative dates look something like this:
  202. *
  203. * - 3 weeks, 4 days ago
  204. * - 15 seconds ago
  205. *
  206. * Default date formatting is d/M/YY e.g: on 18/2/09. Formatting is done internally using
  207. * `i18nFormat`, see the method for the valid formatting strings
  208. *
  209. * The returned string includes 'ago' or 'on' and assumes you'll properly add a word
  210. * like 'Posted ' before the function output.
  211. *
  212. * NOTE: If the difference is one week or more, the lowest level of accuracy is day
  213. *
  214. * @param array $options Array of options.
  215. * @return string Relative time string.
  216. */
  217. public function timeAgoInWords(array $options = [])
  218. {
  219. return $this->diffFormatter()->timeAgoInWords($this, $options);
  220. }
  221. /**
  222. * Get list of timezone identifiers
  223. *
  224. * @param int|string|null $filter A regex to filter identifier
  225. * Or one of DateTimeZone class constants
  226. * @param string|null $country A two-letter ISO 3166-1 compatible country code.
  227. * This option is only used when $filter is set to DateTimeZone::PER_COUNTRY
  228. * @param bool|array $options If true (default value) groups the identifiers list by primary region.
  229. * Otherwise, an array containing `group`, `abbr`, `before`, and `after`
  230. * keys. Setting `group` and `abbr` to true will group results and append
  231. * timezone abbreviation in the display value. Set `before` and `after`
  232. * to customize the abbreviation wrapper.
  233. * @return array List of timezone identifiers
  234. * @since 2.2
  235. */
  236. public static function listTimezones($filter = null, $country = null, $options = [])
  237. {
  238. if (is_bool($options)) {
  239. $options = [
  240. 'group' => $options,
  241. ];
  242. }
  243. $defaults = [
  244. 'group' => true,
  245. 'abbr' => false,
  246. 'before' => ' - ',
  247. 'after' => null,
  248. ];
  249. $options += $defaults;
  250. $group = $options['group'];
  251. $regex = null;
  252. if (is_string($filter)) {
  253. $regex = $filter;
  254. $filter = null;
  255. }
  256. if ($filter === null) {
  257. $filter = DateTimeZone::ALL;
  258. }
  259. $identifiers = DateTimeZone::listIdentifiers($filter, $country);
  260. if ($regex) {
  261. foreach ($identifiers as $key => $tz) {
  262. if (!preg_match($regex, $tz)) {
  263. unset($identifiers[$key]);
  264. }
  265. }
  266. }
  267. if ($group) {
  268. $groupedIdentifiers = [];
  269. $now = time();
  270. $before = $options['before'];
  271. $after = $options['after'];
  272. foreach ($identifiers as $key => $tz) {
  273. $abbr = null;
  274. if ($options['abbr']) {
  275. $dateTimeZone = new DateTimeZone($tz);
  276. $trans = $dateTimeZone->getTransitions($now, $now);
  277. $abbr = isset($trans[0]['abbr']) ?
  278. $before . $trans[0]['abbr'] . $after :
  279. null;
  280. }
  281. $item = explode('/', $tz, 2);
  282. if (isset($item[1])) {
  283. $groupedIdentifiers[$item[0]][$tz] = $item[1] . $abbr;
  284. } else {
  285. $groupedIdentifiers[$item[0]] = [$tz => $item[0] . $abbr];
  286. }
  287. }
  288. return $groupedIdentifiers;
  289. }
  290. return array_combine($identifiers, $identifiers);
  291. }
  292. /**
  293. * Returns true this instance will happen within the specified interval
  294. *
  295. * This overridden method provides backwards compatible behavior for integers,
  296. * or strings with trailing spaces. This behavior is *deprecated* and will be
  297. * removed in future versions of CakePHP.
  298. *
  299. * @param string|int $timeInterval the numeric value with space then time type.
  300. * Example of valid types: 6 hours, 2 days, 1 minute.
  301. * @return bool
  302. */
  303. public function wasWithinLast($timeInterval)
  304. {
  305. $tmp = trim($timeInterval);
  306. if (is_numeric($tmp)) {
  307. $timeInterval = $tmp . ' days';
  308. }
  309. return parent::wasWithinLast($timeInterval);
  310. }
  311. /**
  312. * Returns true this instance happened within the specified interval
  313. *
  314. * This overridden method provides backwards compatible behavior for integers,
  315. * or strings with trailing spaces. This behavior is *deprecated* and will be
  316. * removed in future versions of CakePHP.
  317. *
  318. * @param string|int $timeInterval the numeric value with space then time type.
  319. * Example of valid types: 6 hours, 2 days, 1 minute.
  320. * @return bool
  321. */
  322. public function isWithinNext($timeInterval)
  323. {
  324. $tmp = trim($timeInterval);
  325. if (is_numeric($tmp)) {
  326. $timeInterval = $tmp . ' days';
  327. }
  328. return parent::isWithinNext($timeInterval);
  329. }
  330. }