Completed
Pull Request — master (#203)
by Maxence
16:54
created

TimezoneService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 59
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convertTimeForCurrentUser() 0 3 1
A convertTimeForUserId() 0 7 1
A getUTCDate() 0 8 1
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 <[email protected]>
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
27
namespace OCA\Circles\Service;
28
29
use DateTime;
30
31
class TimezoneService {
32
33
34
	/** @var string */
35
	private $userId;
36
37
	/** @var ConfigService */
38
	private $configService;
39
40
41
	/**
42
	 * TimezoneService constructor.
43
	 *
44
	 * @param string $userId
45
	 * @param ConfigService $configService
46
	 */
47
	public function __construct($userId, ConfigService $configService) {
48
		$this->userId = $userId;
49
		$this->configService = $configService;
50
	}
51
52
53
	/**
54
	 * @param string $time
55
	 *
56
	 * @return string
57
	 */
58
	public function convertTimeForCurrentUser($time) {
59
		return $this->convertTimeForUserId($this->userId, $time);
60
	}
61
62
63
	/**
64
	 * @param string $userId
65
	 * @param string $time
66
	 *
67
	 * @return string
68
	 */
69
	public function convertTimeForUserId($userId, $time) {
70
		$timezone = $this->configService->getCoreValueForUser($userId, 'timezone', 'UTC');
71
		$date = DateTime::createFromFormat('Y-m-d H:i:s', $time);
72
		$date->setTimezone(new \DateTimeZone($timezone));
73
74
		return $date->format('Y-m-d H:i:s');
75
	}
76
77
78
	/**
79
	 * @return string
80
	 */
81
	public function getUTCDate() {
82
		$defaultTimezone = date_default_timezone_get();
83
		date_default_timezone_set('UTC');
84
		$format = date('Y-m-d H:i:s');
85
		date_default_timezone_set($defaultTimezone);
86
87
		return $format;
88
	}
89
}
90
91
92