1
0

DateTimeFormatter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author dartcafe <github@dartcafe.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC;
  26. class DateTimeFormatter implements \OCP\IDateTimeFormatter {
  27. /** @var \DateTimeZone */
  28. protected $defaultTimeZone;
  29. /** @var \OCP\IL10N */
  30. protected $defaultL10N;
  31. /**
  32. * Constructor
  33. *
  34. * @param \DateTimeZone $defaultTimeZone Set the timezone for the format
  35. * @param \OCP\IL10N $defaultL10N Set the language for the format
  36. */
  37. public function __construct(\DateTimeZone $defaultTimeZone, \OCP\IL10N $defaultL10N) {
  38. $this->defaultTimeZone = $defaultTimeZone;
  39. $this->defaultL10N = $defaultL10N;
  40. }
  41. /**
  42. * Get TimeZone to use
  43. *
  44. * @param \DateTimeZone $timeZone The timezone to use
  45. * @return \DateTimeZone The timezone to use, falling back to the current user's timezone
  46. */
  47. protected function getTimeZone($timeZone = null) {
  48. if ($timeZone === null) {
  49. $timeZone = $this->defaultTimeZone;
  50. }
  51. return $timeZone;
  52. }
  53. /**
  54. * Get \OCP\IL10N to use
  55. *
  56. * @param \OCP\IL10N $l The locale to use
  57. * @return \OCP\IL10N The locale to use, falling back to the current user's locale
  58. */
  59. protected function getLocale($l = null) {
  60. if ($l === null) {
  61. $l = $this->defaultL10N;
  62. }
  63. return $l;
  64. }
  65. /**
  66. * Generates a DateTime object with the given timestamp and TimeZone
  67. *
  68. * @param mixed $timestamp
  69. * @param \DateTimeZone $timeZone The timezone to use
  70. * @return \DateTime
  71. */
  72. protected function getDateTime($timestamp, \DateTimeZone $timeZone = null) {
  73. if ($timestamp === null) {
  74. return new \DateTime('now', $timeZone);
  75. } elseif (!$timestamp instanceof \DateTime) {
  76. $dateTime = new \DateTime('now', $timeZone);
  77. $dateTime->setTimestamp($timestamp);
  78. return $dateTime;
  79. }
  80. if ($timeZone) {
  81. $timestamp->setTimezone($timeZone);
  82. }
  83. return $timestamp;
  84. }
  85. /**
  86. * Formats the date of the given timestamp
  87. *
  88. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  89. * @param string $format Either 'full', 'long', 'medium' or 'short'
  90. * full: e.g. 'EEEE, MMMM d, y' => 'Wednesday, August 20, 2014'
  91. * long: e.g. 'MMMM d, y' => 'August 20, 2014'
  92. * medium: e.g. 'MMM d, y' => 'Aug 20, 2014'
  93. * short: e.g. 'M/d/yy' => '8/20/14'
  94. * The exact format is dependent on the language
  95. * @param \DateTimeZone $timeZone The timezone to use
  96. * @param \OCP\IL10N $l The locale to use
  97. * @return string Formatted date string
  98. */
  99. public function formatDate($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  100. return $this->format($timestamp, 'date', $format, $timeZone, $l);
  101. }
  102. /**
  103. * Formats the date of the given timestamp
  104. *
  105. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  106. * @param string $format Either 'full', 'long', 'medium' or 'short'
  107. * full: e.g. 'EEEE, MMMM d, y' => 'Wednesday, August 20, 2014'
  108. * long: e.g. 'MMMM d, y' => 'August 20, 2014'
  109. * medium: e.g. 'MMM d, y' => 'Aug 20, 2014'
  110. * short: e.g. 'M/d/yy' => '8/20/14'
  111. * The exact format is dependent on the language
  112. * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable
  113. * @param \DateTimeZone $timeZone The timezone to use
  114. * @param \OCP\IL10N $l The locale to use
  115. * @return string Formatted relative date string
  116. */
  117. public function formatDateRelativeDay($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  118. if (substr($format, -1) !== '*' && substr($format, -1) !== '*') {
  119. $format .= '^';
  120. }
  121. return $this->format($timestamp, 'date', $format, $timeZone, $l);
  122. }
  123. /**
  124. * Gives the relative date of the timestamp
  125. * Only works for past dates
  126. *
  127. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  128. * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time
  129. * @return string Dates returned are:
  130. * < 1 month => Today, Yesterday, n days ago
  131. * < 13 month => last month, n months ago
  132. * >= 13 month => last year, n years ago
  133. * @param \OCP\IL10N $l The locale to use
  134. * @return string Formatted date span
  135. */
  136. public function formatDateSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) {
  137. $l = $this->getLocale($l);
  138. $timestamp = $this->getDateTime($timestamp);
  139. $timestamp->setTime(0, 0, 0);
  140. if ($baseTimestamp === null) {
  141. $baseTimestamp = time();
  142. }
  143. $baseTimestamp = $this->getDateTime($baseTimestamp);
  144. $baseTimestamp->setTime(0, 0, 0);
  145. $dateInterval = $timestamp->diff($baseTimestamp);
  146. if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 0) {
  147. return $l->t('today');
  148. } elseif ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 1) {
  149. if ($timestamp > $baseTimestamp) {
  150. return $l->t('tomorrow');
  151. } else {
  152. return $l->t('yesterday');
  153. }
  154. } elseif ($dateInterval->y == 0 && $dateInterval->m == 0) {
  155. if ($timestamp > $baseTimestamp) {
  156. return $l->n('in %n day', 'in %n days', $dateInterval->d);
  157. } else {
  158. return $l->n('%n day ago', '%n days ago', $dateInterval->d);
  159. }
  160. } elseif ($dateInterval->y == 0 && $dateInterval->m == 1) {
  161. if ($timestamp > $baseTimestamp) {
  162. return $l->t('next month');
  163. } else {
  164. return $l->t('last month');
  165. }
  166. } elseif ($dateInterval->y == 0) {
  167. if ($timestamp > $baseTimestamp) {
  168. return $l->n('in %n month', 'in %n months', $dateInterval->m);
  169. } else {
  170. return $l->n('%n month ago', '%n months ago', $dateInterval->m);
  171. }
  172. } elseif ($dateInterval->y == 1) {
  173. if ($timestamp > $baseTimestamp) {
  174. return $l->t('next year');
  175. } else {
  176. return $l->t('last year');
  177. }
  178. }
  179. if ($timestamp > $baseTimestamp) {
  180. return $l->n('in %n year', 'in %n years', $dateInterval->y);
  181. } else {
  182. return $l->n('%n year ago', '%n years ago', $dateInterval->y);
  183. }
  184. }
  185. /**
  186. * Formats the time of the given timestamp
  187. *
  188. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  189. * @param string $format Either 'full', 'long', 'medium' or 'short'
  190. * full: e.g. 'h:mm:ss a zzzz' => '11:42:13 AM GMT+0:00'
  191. * long: e.g. 'h:mm:ss a z' => '11:42:13 AM GMT'
  192. * medium: e.g. 'h:mm:ss a' => '11:42:13 AM'
  193. * short: e.g. 'h:mm a' => '11:42 AM'
  194. * The exact format is dependent on the language
  195. * @param \DateTimeZone $timeZone The timezone to use
  196. * @param \OCP\IL10N $l The locale to use
  197. * @return string Formatted time string
  198. */
  199. public function formatTime($timestamp, $format = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  200. return $this->format($timestamp, 'time', $format, $timeZone, $l);
  201. }
  202. /**
  203. * Gives the relative past time of the timestamp
  204. *
  205. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  206. * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time
  207. * @return string Dates returned are:
  208. * < 60 sec => seconds ago
  209. * < 1 hour => n minutes ago
  210. * < 1 day => n hours ago
  211. * < 1 month => Yesterday, n days ago
  212. * < 13 month => last month, n months ago
  213. * >= 13 month => last year, n years ago
  214. * @param \OCP\IL10N $l The locale to use
  215. * @return string Formatted time span
  216. */
  217. public function formatTimeSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) {
  218. $l = $this->getLocale($l);
  219. $timestamp = $this->getDateTime($timestamp);
  220. if ($baseTimestamp === null) {
  221. $baseTimestamp = time();
  222. }
  223. $baseTimestamp = $this->getDateTime($baseTimestamp);
  224. $diff = $timestamp->diff($baseTimestamp);
  225. if ($diff->y > 0 || $diff->m > 0 || $diff->d > 0) {
  226. return $this->formatDateSpan($timestamp, $baseTimestamp, $l);
  227. }
  228. if ($diff->h > 0) {
  229. if ($timestamp > $baseTimestamp) {
  230. return $l->n('in %n hour', 'in %n hours', $diff->h);
  231. } else {
  232. return $l->n('%n hour ago', '%n hours ago', $diff->h);
  233. }
  234. } elseif ($diff->i > 0) {
  235. if ($timestamp > $baseTimestamp) {
  236. return $l->n('in %n minute', 'in %n minutes', $diff->i);
  237. } else {
  238. return $l->n('%n minute ago', '%n minutes ago', $diff->i);
  239. }
  240. }
  241. if ($timestamp > $baseTimestamp) {
  242. return $l->t('in a few seconds');
  243. } else {
  244. return $l->t('seconds ago');
  245. }
  246. }
  247. /**
  248. * Formats the date and time of the given timestamp
  249. *
  250. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  251. * @param string $formatDate See formatDate() for description
  252. * @param string $formatTime See formatTime() for description
  253. * @param \DateTimeZone $timeZone The timezone to use
  254. * @param \OCP\IL10N $l The locale to use
  255. * @return string Formatted date and time string
  256. */
  257. public function formatDateTime($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  258. return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l);
  259. }
  260. /**
  261. * Formats the date and time of the given timestamp
  262. *
  263. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  264. * @param string $formatDate See formatDate() for description
  265. * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable
  266. * @param string $formatTime See formatTime() for description
  267. * @param \DateTimeZone $timeZone The timezone to use
  268. * @param \OCP\IL10N $l The locale to use
  269. * @return string Formatted relative date and time string
  270. */
  271. public function formatDateTimeRelativeDay($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  272. if (substr($formatDate, -1) !== '^' && substr($formatDate, -1) !== '*') {
  273. $formatDate .= '^';
  274. }
  275. return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l);
  276. }
  277. /**
  278. * Formats the date and time of the given timestamp
  279. *
  280. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  281. * @param string $type One of 'date', 'datetime' or 'time'
  282. * @param string $format Format string
  283. * @param \DateTimeZone $timeZone The timezone to use
  284. * @param \OCP\IL10N $l The locale to use
  285. * @return string Formatted date and time string
  286. */
  287. protected function format($timestamp, $type, $format, \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  288. $l = $this->getLocale($l);
  289. $timeZone = $this->getTimeZone($timeZone);
  290. $timestamp = $this->getDateTime($timestamp, $timeZone);
  291. return $l->l($type, $timestamp, [
  292. 'width' => $format,
  293. ]);
  294. }
  295. }