datetimeformatter.php 10 KB

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