DateTimeFormatter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. if ($timestamp > $baseTimestamp) {
  149. return (string) $l->t('tomorrow');
  150. } else {
  151. return (string) $l->t('yesterday');
  152. }
  153. } else if ($dateInterval->y == 0 && $dateInterval->m == 0) {
  154. if ($timestamp > $baseTimestamp) {
  155. return (string) $l->n('in %n day', 'in %n days', $dateInterval->d);
  156. } else {
  157. return (string) $l->n('%n day ago', '%n days ago', $dateInterval->d);
  158. }
  159. } else if ($dateInterval->y == 0 && $dateInterval->m == 1) {
  160. if ($timestamp > $baseTimestamp) {
  161. return (string) $l->t('next month');
  162. } else {
  163. return (string) $l->t('last month');
  164. }
  165. } else if ($dateInterval->y == 0) {
  166. if ($timestamp > $baseTimestamp) {
  167. return (string) $l->n('in %n month', 'in %n months', $dateInterval->m);
  168. } else {
  169. return (string) $l->n('%n month ago', '%n months ago', $dateInterval->m);
  170. }
  171. } else if ($dateInterval->y == 1) {
  172. if ($timestamp > $baseTimestamp) {
  173. return (string) $l->t('next year');
  174. } else {
  175. return (string) $l->t('last year');
  176. }
  177. }
  178. if ($timestamp > $baseTimestamp) {
  179. return (string) $l->n('in %n year', 'in %n years', $dateInterval->y);
  180. } else {
  181. return (string) $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. * @return string Dates returned are:
  207. * < 60 sec => seconds ago
  208. * < 1 hour => n minutes ago
  209. * < 1 day => n hours ago
  210. * < 1 month => Yesterday, n days ago
  211. * < 13 month => last month, n months ago
  212. * >= 13 month => last year, n years ago
  213. * @param \OCP\IL10N $l The locale to use
  214. * @return string Formatted time span
  215. */
  216. public function formatTimeSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) {
  217. $l = $this->getLocale($l);
  218. $timestamp = $this->getDateTime($timestamp);
  219. if ($baseTimestamp === null) {
  220. $baseTimestamp = time();
  221. }
  222. $baseTimestamp = $this->getDateTime($baseTimestamp);
  223. $diff = $timestamp->diff($baseTimestamp);
  224. if ($diff->y > 0 || $diff->m > 0 || $diff->d > 0) {
  225. return (string) $this->formatDateSpan($timestamp, $baseTimestamp, $l);
  226. }
  227. if ($diff->h > 0) {
  228. if ($timestamp > $baseTimestamp) {
  229. return (string) $l->n('in %n hour', 'in %n hours', $diff->h);
  230. } else {
  231. return (string) $l->n('%n hour ago', '%n hours ago', $diff->h);
  232. }
  233. } else if ($diff->i > 0) {
  234. if ($timestamp > $baseTimestamp) {
  235. return (string) $l->n('in %n minute', 'in %n minutes', $diff->i);
  236. } else {
  237. return (string) $l->n('%n minute ago', '%n minutes ago', $diff->i);
  238. }
  239. }
  240. if ($timestamp > $baseTimestamp) {
  241. return (string) $l->t('in a few seconds');
  242. } else {
  243. return (string) $l->t('seconds ago');
  244. }
  245. }
  246. /**
  247. * Formats the date and time of the given timestamp
  248. *
  249. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  250. * @param string $formatDate See formatDate() for description
  251. * @param string $formatTime See formatTime() for description
  252. * @param \DateTimeZone $timeZone The timezone to use
  253. * @param \OCP\IL10N $l The locale to use
  254. * @return string Formatted date and time string
  255. */
  256. public function formatDateTime($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  257. return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l);
  258. }
  259. /**
  260. * Formats the date and time of the given timestamp
  261. *
  262. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  263. * @param string $formatDate See formatDate() for description
  264. * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable
  265. * @param string $formatTime See formatTime() for description
  266. * @param \DateTimeZone $timeZone The timezone to use
  267. * @param \OCP\IL10N $l The locale to use
  268. * @return string Formatted relative date and time string
  269. */
  270. public function formatDateTimeRelativeDay($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  271. if (substr($formatDate, -1) !== '^' && substr($formatDate, -1) !== '*') {
  272. $formatDate .= '^';
  273. }
  274. return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l);
  275. }
  276. /**
  277. * Formats the date and time of the given timestamp
  278. *
  279. * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object
  280. * @param string $type One of 'date', 'datetime' or 'time'
  281. * @param string $format Format string
  282. * @param \DateTimeZone $timeZone The timezone to use
  283. * @param \OCP\IL10N $l The locale to use
  284. * @return string Formatted date and time string
  285. */
  286. protected function format($timestamp, $type, $format, \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) {
  287. $l = $this->getLocale($l);
  288. $timeZone = $this->getTimeZone($timeZone);
  289. $timestamp = $this->getDateTime($timestamp, $timeZone);
  290. return (string) $l->l($type, $timestamp, array(
  291. 'width' => $format,
  292. ));
  293. }
  294. }