1<?php 2/** 3 * Circles - Bring cloud-users closer together. 4 * 5 * This file is licensed under the Affero General Public License version 3 or 6 * later. See the COPYING file. 7 * 8 * @author Flávio Gomes da Silva Lisboa <flavio.lisboa@fgsl.eti.br> 9 * @copyright 2017 10 * @license GNU AGPL version 3 or any later version 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU AVolu ffero General Public License as 14 * published by the Free Software Foundation, either version 3 of the 15 * License, or (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU Affero General Public License for more details. 21 * 22 * You should have received a copy of the GNU Affero General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. 24 * 25 */ 26 27namespace OCA\Circles\Service; 28 29use DateTime; 30use OC\AppFramework\Utility\TimeFactory; 31 32class TimezoneService { 33 34 35 /** @var string */ 36 private $userId; 37 38 /** @var TimeFactory */ 39 private $timeFactory; 40 41 /** @var ConfigService */ 42 private $configService; 43 44 45 /** 46 * TimezoneService constructor. 47 * 48 * @param string $userId 49 * @param TimeFactory $timeFactory 50 * @param ConfigService $configService 51 */ 52 public function __construct( 53 $userId, 54 TimeFactory $timeFactory, 55 ConfigService $configService 56 ) { 57 $this->userId = $userId; 58 $this->timeFactory = $timeFactory; 59 $this->configService = $configService; 60 } 61 62 63 /** 64 * @param string $time 65 * 66 * @return string 67 */ 68 public function convertTimeForCurrentUser($time) { 69 return $this->convertTimeForUserId($this->userId, $time); 70 } 71 72 73 public function convertToTimestamp($time) { 74 return strtotime($time); 75 } 76 77 78 /** 79 * @param string $userId 80 * @param string $time 81 * 82 * @return string 83 */ 84 public function convertTimeForUserId($userId, $time) { 85 $timezone = $this->configService->getCoreValueForUser($userId, 'timezone', 'UTC'); 86 $date = \DateTime::createFromFormat('Y-m-d H:i:s', $time); 87 if ($date === false) { 88 return $time; 89 } 90 91 $date->setTimezone(new \DateTimeZone($timezone)); 92 93 return $date->format('Y-m-d H:i:s'); 94 } 95 96 97 /** 98 * @param string $time 99 * 100 * @return DateTime 101 */ 102 public function getDateTime(string $time = 'now'): DateTime { 103 return $this->timeFactory->getDateTime($time); 104 } 105 106 107 /** 108 * @return string 109 */ 110 public function getUTCDate(): string { 111 $defaultTimezone = date_default_timezone_get(); 112 date_default_timezone_set('UTC'); 113 $format = date('Y-m-d H:i:s'); 114 date_default_timezone_set($defaultTimezone); 115 116 return $format; 117 } 118} 119