TimeTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of EasySwoole.
  5. *
  6. * @link https://www.easyswoole.com
  7. * @document https://www.easyswoole.com
  8. * @contact https://www.easyswoole.com/Preface/contact.html
  9. * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
  10. */
  11. namespace EasySwoole\Utility\Tests;
  12. use EasySwoole\Utility\Time;
  13. use PHPUnit\Framework\TestCase;
  14. class TimeTest extends TestCase
  15. {
  16. public function testStartTimestamp()
  17. {
  18. // 传入文本日期
  19. $startDate = '2021-10-05';
  20. $startDateTime = $startDate . ' 01:02:03';
  21. $startTimestamp = Time::startTimestamp($startDateTime);
  22. $startTimestamp2DateTime = date("Y-m-d H:i:s", $startTimestamp);
  23. $this->assertSame($startTimestamp2DateTime, $startDate . ' 00:00:00');
  24. // 传入时间戳
  25. $startDateTimestamp = strtotime('2021-10-05 01:02:03');
  26. $startTimestamp = Time::startTimestamp($startDateTimestamp);
  27. $startTimestamp2DateTime = date("Y-m-d H:i:s", $startTimestamp);
  28. $this->assertSame($startTimestamp2DateTime, $startDate . ' 00:00:00');
  29. }
  30. public function testEndTimestamp()
  31. {
  32. // 传入文本日期
  33. $endDate = '2021-10-05';
  34. $endDateTime = $endDate . ' 01:02:03';
  35. $endTimestamp = Time::endTimestamp($endDateTime);
  36. $endTimestamp2DateTime = date("Y-m-d H:i:s", $endTimestamp);
  37. $this->assertSame($endTimestamp2DateTime, $endDate . ' 23:59:59');
  38. // 传入时间戳
  39. $endDateTimestamp = strtotime('2021-10-05 01:02:03');
  40. $endTimestamp = Time::endTimestamp($endDateTimestamp);
  41. $endTimestamp2DateTime = date("Y-m-d H:i:s", $endTimestamp);
  42. $this->assertSame($endTimestamp2DateTime, $endDate . ' 23:59:59');
  43. }
  44. public function testCreateDateTimeClass()
  45. {
  46. // 传入文本日期
  47. $dateStr = '2021-10-05 01:02:03';
  48. $dateStr2timestamp = strtotime($dateStr);
  49. $dateTimeClass = Time::createDateTimeClass($dateStr);
  50. $this->assertInstanceOf(\DateTime::class, $dateTimeClass);
  51. $this->assertSame($dateStr2timestamp, $dateTimeClass->getTimestamp());
  52. $dateTimezone = $dateTimeClass->getTimezone();
  53. // 当前时区信息
  54. $nowTimezone = new \DateTimeZone(date_default_timezone_get());
  55. $nowTimezoneName = $nowTimezone->getName();
  56. // 断言当前时区
  57. $this->assertInstanceOf(\DateTimeZone::class, $dateTimezone);
  58. $this->assertSame($nowTimezoneName, $dateTimezone->getName());
  59. // 传入时间戳
  60. $timestamp = strtotime('2021-10-05 01:02:03');
  61. $dateTimeClass = Time::createDateTimeClass($timestamp);
  62. $this->assertInstanceOf(\DateTime::class, $dateTimeClass);
  63. $this->assertSame($dateStr2timestamp, $dateTimeClass->getTimestamp());
  64. $dateTimezone = $dateTimeClass->getTimezone();
  65. // 当前时区信息
  66. $nowTimezone = new \DateTimeZone(date_default_timezone_get());
  67. $nowTimezoneName = $nowTimezone->getName();
  68. // 断言当前时区
  69. $this->assertInstanceOf(\DateTimeZone::class, $dateTimezone);
  70. $this->assertSame($nowTimezoneName, $dateTimezone->getName());
  71. }
  72. public function testParserDateTime()
  73. {
  74. // 传入文本日期
  75. $dateStr = '2022-01-13 01:02:03';
  76. $dateTimeArray = Time::parserDateTime($dateStr);
  77. $this->assertSame('01', $dateTimeArray[0]);
  78. $this->assertSame('02', $dateTimeArray[1]);
  79. $this->assertSame('03', $dateTimeArray[2]);
  80. $this->assertSame('1', $dateTimeArray[3]);
  81. $this->assertSame('13', $dateTimeArray[4]);
  82. $this->assertSame('2022', $dateTimeArray[5]);
  83. // 传入时间戳
  84. $timestamp = strtotime('2022-01-13 01:02:03');
  85. $dateTimeArray = Time::parserDateTime($timestamp);
  86. $this->assertSame('01', $dateTimeArray[0]);
  87. $this->assertSame('02', $dateTimeArray[1]);
  88. $this->assertSame('03', $dateTimeArray[2]);
  89. $this->assertSame('1', $dateTimeArray[3]);
  90. $this->assertSame('13', $dateTimeArray[4]);
  91. $this->assertSame('2022', $dateTimeArray[5]);
  92. }
  93. }