Passed
Push — develop ( c341e4...5dd870 )
by Greg
05:58
created

Carbon::local()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees;
21
22
use Carbon\CarbonImmutable;
23
use Fisharebest\Webtrees\Contracts\UserInterface;
24
25
use Fisharebest\Webtrees\Contracts\UserInterface;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use Fisharebest\Webtrees\Contracts\UserInterface as UserInterface because the name is already in use
Loading history...
26
27
use function gregoriantojd;
28
29
/**
30
 * A wrapper around CarbonImmutable dates.
31
 *
32
 * The future of nesbot/carbon seems uncertain.  We can always swap it out
33
 * for carbondate/carbon, cake-php/chronos or some other datetime class.
34
 */
35
class Carbon extends CarbonImmutable
36
{
37
    /**
38
     * We use julian days extensively, so a helper function to convert.
39
     */
40
    public function julianDay(): int
41
    {
42
        $local = $this->local();
43
44
        return gregoriantojd($local->month, $local->day, $local->year);
45
    }
46
47
    /**
48
     * Create a local timestamp for the current user.
49
     *
50
     * @return Carbon
51
     */
52
    public function local(): Carbon
53
    {
54
        $timezone = Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE', 'UTC'));
55
56
        // Changing locale does not create a new immutable object.
57
        $this->locale(I18N::locale()->code());
58
59
        return $this->setTimezone($timezone);
60
    }
61
}
62