1
0

DateTimeFormatter.php 10 KB

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