You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimeFactory.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022, Joas Schilling <coding@schilljs.com>
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. *
  7. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\AppFramework\Utility;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. /**
  30. * Use this to get a timestamp or DateTime object in code to remain testable
  31. *
  32. * @since 8.0.0
  33. * @since 27.0.0 Implements the \Psr\Clock\ClockInterface interface
  34. * @ref https://www.php-fig.org/psr/psr-20/#21-clockinterface
  35. */
  36. class TimeFactory implements ITimeFactory {
  37. protected \DateTimeZone $timezone;
  38. public function __construct() {
  39. $this->timezone = new \DateTimeZone('UTC');
  40. }
  41. /**
  42. * @return int the result of a call to time()
  43. * @since 8.0.0
  44. * @deprecated 26.0.0 {@see ITimeFactory::now()}
  45. */
  46. public function getTime(): int {
  47. return time();
  48. }
  49. /**
  50. * @param string $time
  51. * @param \DateTimeZone $timezone
  52. * @return \DateTime
  53. * @since 15.0.0
  54. * @deprecated 26.0.0 {@see ITimeFactory::now()}
  55. */
  56. public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null): \DateTime {
  57. return new \DateTime($time, $timezone);
  58. }
  59. public function now(): \DateTimeImmutable {
  60. return new \DateTimeImmutable('now', $this->timezone);
  61. }
  62. public function withTimeZone(\DateTimeZone $timezone): static {
  63. $clone = clone $this;
  64. $clone->timezone = $timezone;
  65. return $clone;
  66. }
  67. public function getTimeZone(?string $timezone = null): \DateTimeZone {
  68. if ($timezone !== null) {
  69. return new \DateTimeZone($timezone);
  70. }
  71. return $this->timezone;
  72. }
  73. }