DateTimeFormatterTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. class DateTimeFormatterTest extends TestCase {
  9. /** @var \OC\DateTimeFormatter */
  10. protected $formatter;
  11. protected static $oneMinute = 60;
  12. protected static $oneHour = 3600;
  13. protected static $oneDay;
  14. protected static $oneYear;
  15. protected static $defaultTimeZone;
  16. public static function setUpBeforeClass(): void {
  17. parent::setUpBeforeClass();
  18. self::$defaultTimeZone = date_default_timezone_get();
  19. date_default_timezone_set('UTC');
  20. self::$oneDay = self::$oneHour * 24;
  21. self::$oneYear = self::$oneDay * 365;
  22. }
  23. public static function tearDownAfterClass(): void {
  24. date_default_timezone_set(self::$defaultTimeZone);
  25. parent::tearDownAfterClass();
  26. }
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->formatter = new \OC\DateTimeFormatter(new \DateTimeZone('UTC'), \OCP\Util::getL10N('lib', 'en'));
  30. }
  31. protected function getTimestampAgo($time, $seconds = 0, $minutes = 0, $hours = 0, $days = 0, $years = 0) {
  32. return $time - $seconds - $minutes * 60 - $hours * 3600 - $days * 24 * 3600 - $years * 365 * 24 * 3600;
  33. }
  34. public function formatTimeSpanData() {
  35. $time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
  36. $deL10N = \OCP\Util::getL10N('lib', 'de');
  37. return [
  38. ['seconds ago', $time, $time],
  39. ['in a few seconds', $time + 5 , $time],
  40. ['1 minute ago', $this->getTimestampAgo($time, 30, 1), $time],
  41. ['15 minutes ago', $this->getTimestampAgo($time, 30, 15), $time],
  42. ['in 15 minutes', $time, $this->getTimestampAgo($time, 30, 15)],
  43. ['1 hour ago', $this->getTimestampAgo($time, 30, 15, 1), $time],
  44. ['3 hours ago', $this->getTimestampAgo($time, 30, 15, 3), $time],
  45. ['in 3 hours', $time, $this->getTimestampAgo($time, 30, 15, 3)],
  46. ['4 days ago', $this->getTimestampAgo($time, 30, 15, 3, 4), $time],
  47. ['seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  48. ['seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  49. ['1 minute ago', new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  50. ['3 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  51. ['59 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  52. ['1 hour ago', new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  53. ['3 hours ago', new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  54. ['yesterday', new \DateTime('Tue, 01 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  55. ['2 days ago', new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  56. [$deL10N->t('seconds ago'), new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  57. [$deL10N->n('%n minute ago', '%n minutes ago', 1), new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  58. [$deL10N->n('%n minute ago', '%n minutes ago', 3), new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  59. [$deL10N->n('%n hour ago', '%n hours ago', 1), new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  60. [$deL10N->n('%n hour ago', '%n hours ago', 3), new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  61. [$deL10N->n('%n day ago', '%n days ago', 2), new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  62. ];
  63. }
  64. /**
  65. * @dataProvider formatTimeSpanData
  66. */
  67. public function testFormatTimeSpan($expected, $timestamp, $compare, $locale = null): void {
  68. $this->assertEquals((string)$expected, (string)$this->formatter->formatTimeSpan($timestamp, $compare, $locale));
  69. }
  70. public function formatDateSpanData() {
  71. $time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
  72. $deL10N = \OCP\Util::getL10N('lib', 'de');
  73. return [
  74. // Normal testing
  75. ['today', $this->getTimestampAgo($time, 30, 15), $time],
  76. ['yesterday', $this->getTimestampAgo($time, 0, 0, 0, 1), $time],
  77. ['tomorrow', $time, $this->getTimestampAgo($time, 0, 0, 0, 1)],
  78. ['4 days ago', $this->getTimestampAgo($time, 0, 0, 0, 4), $time],
  79. ['in 4 days', $time, $this->getTimestampAgo($time, 0, 0, 0, 4)],
  80. ['5 months ago', $this->getTimestampAgo($time, 0, 0, 0, 155), $time],
  81. ['next month', $time, $this->getTimestampAgo($time, 0, 0, 0, 32)],
  82. ['in 5 months', $time, $this->getTimestampAgo($time, 0, 0, 0, 155)],
  83. ['2 years ago', $this->getTimestampAgo($time, 0, 0, 0, 0, 2), $time],
  84. ['next year', $time, $this->getTimestampAgo($time, 0, 0, 0, 0, 1)],
  85. ['in 2 years', $time, $this->getTimestampAgo($time, 0, 0, 0, 0, 2)],
  86. // Test with compare timestamp
  87. ['today', $this->getTimestampAgo($time, 0, 0, 0, 0, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)],
  88. ['yesterday', $this->getTimestampAgo($time, 30, 15, 3, 1, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)],
  89. ['4 days ago', $this->getTimestampAgo($time, 30, 15, 3, 4, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)],
  90. ['5 months ago', $this->getTimestampAgo($time, 30, 15, 3, 155, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)],
  91. ['2 years ago', $this->getTimestampAgo($time, 30, 15, 3, 35, 3), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)],
  92. // Test translations
  93. [$deL10N->t('today'), new \DateTime('Wed, 02 Oct 2013 12:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N],
  94. [$deL10N->t('yesterday'), new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), $deL10N],
  95. [$deL10N->n('%n day ago', '%n days ago', 2), new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), $deL10N],
  96. [$deL10N->n('%n month ago', '%n months ago', 9), new \DateTime('Tue, 31 Dec 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000'), $deL10N],
  97. [$deL10N->n('%n year ago', '%n years ago', 2), new \DateTime('Sun, 01 Jan 2012 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000'), $deL10N],
  98. // Test time
  99. ['today', new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  100. ['today', new \DateTime('Wed, 02 Oct 2013 12:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  101. ['today', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  102. // Test some special yesterdays
  103. ['yesterday', new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000')],
  104. ['yesterday', new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  105. ['yesterday', new \DateTime('Tue, 01 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000')],
  106. ['yesterday', new \DateTime('Tue, 01 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')],
  107. ['yesterday', new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000')],
  108. ['yesterday', new \DateTime('Mon, 31 Dec 2012 00:00:00 +0000'), new \DateTime('Tue, 01 Jan 2013 00:00:00 +0000')],
  109. // Test last month
  110. ['2 days ago', new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000')],
  111. ['last month', new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Tue, 31 Oct 2013 00:00:00 +0000')],
  112. ['last month', new \DateTime('Sun, 01 Sep 2013 00:00:00 +0000'), new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000')],
  113. ['last month', new \DateTime('Sun, 01 Sep 2013 00:00:00 +0000'), new \DateTime('Thu, 31 Oct 2013 00:00:00 +0000')],
  114. // Test last year
  115. ['9 months ago', new \DateTime('Tue, 31 Dec 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')],
  116. ['11 months ago', new \DateTime('Thu, 03 Oct 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')],
  117. ['last year', new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')],
  118. ['last year', new \DateTime('Tue, 01 Jan 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')],
  119. ['2 years ago', new \DateTime('Sun, 01 Jan 2012 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')],
  120. ];
  121. }
  122. /**
  123. * @dataProvider formatDateSpanData
  124. */
  125. public function testFormatDateSpan($expected, $timestamp, $compare = null, $locale = null): void {
  126. $this->assertEquals((string)$expected, (string)$this->formatter->formatDateSpan($timestamp, $compare, $locale));
  127. }
  128. public function formatDateData() {
  129. return [
  130. [1102831200, 'December 12, 2004'],
  131. ];
  132. }
  133. /**
  134. * @dataProvider formatDateData
  135. */
  136. public function testFormatDate($timestamp, $expected): void {
  137. $this->assertEquals($expected, (string)$this->formatter->formatDate($timestamp));
  138. }
  139. public function formatDateTimeData() {
  140. return [
  141. [1350129205, null, "October 13, 2012, 11:53:25\xE2\x80\xAFAM UTC"],
  142. [1350129205, new \DateTimeZone('Europe/Berlin'), "October 13, 2012, 1:53:25\xE2\x80\xAFPM GMT+2"],
  143. ];
  144. }
  145. /**
  146. * @dataProvider formatDateTimeData
  147. */
  148. public function testFormatDateTime($timestamp, $timeZone, $expected): void {
  149. $this->assertEquals($expected, (string)$this->formatter->formatDateTime($timestamp, 'long', 'long', $timeZone));
  150. }
  151. public function testFormatDateWithInvalidTZ(): void {
  152. $this->expectException(\Exception::class);
  153. $this->formatter->formatDate(1350129205, 'long', new \DateTimeZone('Mordor/Barad-dûr'));
  154. }
  155. }