DateTimeFormatter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 (!str_ends_with($format, '^') && !str_ends_with($format, '*')) {
  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. * @param \OCP\IL10N $l The locale to use
  130. * @return string Formatted date span. Dates returned are:
  131. * < 1 month => Today, Yesterday, n days ago
  132. * < 13 month => last month, n months ago
  133. * >= 13 month => last year, n years ago
  134. */
  135. public function formatDateSpan($timestamp, $baseTimestamp = null, ?\OCP\IL10N $l = null) {
  136. $l = $this->getLocale($l);
  137. $timestamp = $this->getDateTime($timestamp);
  138. $timestamp->setTime(0, 0, 0);
  139. if ($baseTimestamp === null) {
  140. $baseTimestamp = time();
  141. }
  142. $baseTimestamp = $this->getDateTime($baseTimestamp);
  143. $baseTimestamp->setTime(0, 0, 0);
  144. $dateInterval = $timestamp->diff($baseTimestamp);
  145. if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 0) {
  146. return $l->t('today');
  147. } elseif ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 1) {
  148. if ($timestamp > $baseTimestamp) {
  149. return $l->t('tomorrow');
  150. } else {
  151. return $l->t('yesterday');
  152. }
  153. } elseif ($dateInterval->y == 0 && $dateInterval->m == 0) {
  154. if ($timestamp > $baseTimestamp) {
  155. return $l->n('in %n day', 'in %n days', $dateInterval->d);
  156. } else {
  157. return $l->n('%n day ago', '%n days ago', $dateInterval->d);
  158. }
  159. } elseif ($dateInterval->y == 0 && $dateInterval->m == 1) {
  160. if ($timestamp > $baseTimestamp) {
  161. return $l->t('next month');
  162. } else {
  163. return $l->t('last month');
  164. }
  165. } elseif ($dateInterval->y == 0) {
  166. if ($timestamp > $baseTimestamp) {
  167. return $l->n('in %n month', 'in %n months', $dateInterval->m);
  168. } else {
  169. return $l->n('%n month ago', '%n months ago', $dateInterval->m);
  170. }
  171. } elseif ($dateInterval->y == 1) {
  172. if ($timestamp > $baseTimestamp) {
  173. return $l->t('next year');
  174. } else {
  175. return $l->t('last year');
  176. }
  177. }
  178. if ($timestamp > $baseTimestamp) {
  179. return $l->n('in %n year', 'in %n years', $dateInterval->y);
  180. } else {
  181. return $l->n('%n year ago', '%n years ago', $dateInterval->y);
  182. }
  183. }
  184. /**
  185. * Formats the time of the given timestamp
  186. *
  187. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  188. * @param string $format Either 'full', 'long', 'medium' or 'short'
  189. * full: e.g. 'h:mm:ss a zzzz' => '11:42:13 AM GMT+0:00'
  190. * long: e.g. 'h:mm:ss a z' => '11:42:13 AM GMT'
  191. * medium: e.g. 'h:mm:ss a' => '11:42:13 AM'
  192. * short: e.g. 'h:mm a' => '11:42 AM'
  193. * The exact format is dependent on the language
  194. * @param \DateTimeZone $timeZone The timezone to use
  195. * @param \OCP\IL10N $l The locale to use
  196. * @return string Formatted time string
  197. */
  198. public function formatTime($timestamp, $format = 'medium', ?\DateTimeZone $timeZone = null, ?\OCP\IL10N $l = null) {
  199. return $this->format($timestamp, 'time', $format, $timeZone, $l);
  200. }
  201. /**
  202. * Gives the relative past time of the timestamp
  203. *
  204. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  205. * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time
  206. * @param \OCP\IL10N $l The locale to use
  207. * @return string Formatted time span. 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. */
  215. public function formatTimeSpan($timestamp, $baseTimestamp = null, ?\OCP\IL10N $l = null) {
  216. $l = $this->getLocale($l);
  217. $timestamp = $this->getDateTime($timestamp);
  218. if ($baseTimestamp === null) {
  219. $baseTimestamp = time();
  220. }
  221. $baseTimestamp = $this->getDateTime($baseTimestamp);
  222. $diff = $timestamp->diff($baseTimestamp);
  223. if ($diff->y > 0 || $diff->m > 0 || $diff->d > 0) {
  224. return $this->formatDateSpan($timestamp, $baseTimestamp, $l);
  225. }
  226. if ($diff->h > 0) {
  227. if ($timestamp > $baseTimestamp) {
  228. return $l->n('in %n hour', 'in %n hours', $diff->h);
  229. } else {
  230. return $l->n('%n hour ago', '%n hours ago', $diff->h);
  231. }
  232. } elseif ($diff->i > 0) {
  233. if ($timestamp > $baseTimestamp) {
  234. return $l->n('in %n minute', 'in %n minutes', $diff->i);
  235. } else {
  236. return $l->n('%n minute ago', '%n minutes ago', $diff->i);
  237. }
  238. }
  239. if ($timestamp > $baseTimestamp) {
  240. return $l->t('in a few seconds');
  241. } else {
  242. return $l->t('seconds ago');
  243. }
  244. }
  245. /**
  246. * Formats the date and time of the given timestamp
  247. *
  248. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  249. * @param string $formatDate See formatDate() for description
  250. * @param string $formatTime See formatTime() for description
  251. * @param \DateTimeZone $timeZone The timezone to use
  252. * @param \OCP\IL10N $l The locale to use
  253. * @return string Formatted date and time string
  254. */
  255. public function formatDateTime($timestamp, $formatDate = 'long', $formatTime = 'medium', ?\DateTimeZone $timeZone = null, ?\OCP\IL10N $l = null) {
  256. return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l);
  257. }
  258. /**
  259. * Formats the date and time of the given timestamp
  260. *
  261. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  262. * @param string $formatDate See formatDate() for description
  263. * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable
  264. * @param string $formatTime See formatTime() for description
  265. * @param \DateTimeZone $timeZone The timezone to use
  266. * @param \OCP\IL10N $l The locale to use
  267. * @return string Formatted relative date and time string
  268. */
  269. public function formatDateTimeRelativeDay($timestamp, $formatDate = 'long', $formatTime = 'medium', ?\DateTimeZone $timeZone = null, ?\OCP\IL10N $l = null) {
  270. if (!str_ends_with($formatDate, '^') && !str_ends_with($formatDate, '*')) {
  271. $formatDate .= '^';
  272. }
  273. return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l);
  274. }
  275. /**
  276. * Formats the date and time of the given timestamp
  277. *
  278. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  279. * @param string $type One of 'date', 'datetime' or 'time'
  280. * @param string $format Format string
  281. * @param \DateTimeZone $timeZone The timezone to use
  282. * @param \OCP\IL10N $l The locale to use
  283. * @return string Formatted date and time string
  284. */
  285. protected function format($timestamp, $type, $format, ?\DateTimeZone $timeZone = null, ?\OCP\IL10N $l = null) {
  286. $l = $this->getLocale($l);
  287. $timeZone = $this->getTimeZone($timeZone);
  288. $timestamp = $this->getDateTime($timestamp, $timeZone);
  289. return $l->l($type, $timestamp, [
  290. 'width' => $format,
  291. ]);
  292. }
  293. }