Passed
Push — feature/job_builder_auto_close... ( eb1c97 )
by Tristan
09:08
created

pstDayEndToUtcTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.9666
c 1
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
use Jenssegers\Date\Date;
4
use Illuminate\Support\Facades\App;
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Facades\Lang;
7
8
if (!function_exists('humanizeDate')) {
9
    /**
10
     * Computes a human readable localized date.
11
     *
12
     * @param Date $datetime DateTime object to translate.
13
     *
14
     * @return string
15
     */
16
    function humanizeDate(Date $datetime) : string
17
    {
18 7
        $dateFormat = Config::get('app.date_format');
19 7
        $locale = App::getLocale();
20 7
        $timezone = Config::get('app.local_timezone');
21
22 7
        $datetime->setTimezone(new \DateTimeZone($timezone));
23
24 7
        return $datetime->format($dateFormat[$locale]);
25
    }
26
}
27
28
if (!function_exists('humanizeTime')) {
29
    /**
30
     * Computes a human readable localized time.
31
     *
32
     * @param Date $datetime DateTime object to translate.
33
     *
34
     * @return string
35
     */
36
    function humanizeTime(Date $datetime) : string
37
    {
38 2
        $timeFormat = Config::get('app.time_format');
39 2
        $locale = App::getLocale();
40 2
        $timezone = Config::get('app.local_timezone');
41
42 2
        $datetime->setTimezone(new \DateTimeZone($timezone));
43
44 2
        $displayTime = $datetime->format($timeFormat[$locale]);
45
46 2
        if ($locale == 'fr') {
47
            $displayTime = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayTime);
48
        }
49
50 2
        return $displayTime;
51
    }
52
}
53
54
if (!function_exists('humanizeDateDiff')) {
55
    /**
56
     * Computes a human readable time difference between two dates.
57
     *
58
     * @param Date $datetime DateTime object to measure.
59
     * @param Date $origin   DateTime object to use as a reference.
60
     *
61
     * @return string
62
     */
63
    function humanizeDateDiff(Date $datetime, Date $origin = null) : string
64
    {
65 3
        if (!isset($origin)) {
66 3
            $origin = new Date();
67
        }
68 3
        $interval = $datetime->diff($origin);
69
70 3
        $d = $interval->d;
71 3
        $h = $interval->h;
72 3
        $m = $interval->i;
73 3
        $s = $interval->s;
74
75 3
        if ($d > 0) {
76 3
            $unit = 'day';
77 3
            $count = $d;
78 2
        } elseif ($h > 0) {
79 1
            $unit = 'hour';
80 1
            $count = $h;
81 2
        } elseif ($m > 0) {
82 1
            $unit = 'minute';
83 1
            $count = $m;
84
        } else {
85 1
            $unit = 'second';
86 1
            $count = $s;
87
        }
88
89 3
        $key = "common/time.$unit";
90
91 3
        return Lang::choice($key, $count);
92
    }
93
}
94
95
if (!function_exists('humanizeLastDay')) {
96
    /**
97
     * Returns the date of the last full day a person has before a deadline time.
98
     *
99
     * @param Date $datetime DateTime object to transform to date of last day.
100
     *
101
     * @return string
102
     */
103
    function humanizeLastDay(Date $datetime) : string
104
    {
105 3
        $lastday = $datetime->sub("1 day");
106
107 3
        return humanizeDate($lastday);
108
    }
109
}
110
111
if (!function_exists('pstDayStartToUtcTime')) {
112
    /**
113
     * Given a date, creates a datetime object representing the start of day Pacific Standard Time (12:00), but converted to UTC.
114
     *
115
     * @param integer $year
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
116
     * @param integer $month
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
117
     * @param integer $day
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
118
     * @return Date
119
     */
120
    function pstDayStartToUtcTime(int $year, int $month, int $day): Date
121
    {
122 24
        $date = Date::now();
123 24
        $date->year = $year;
124 24
        $date->month = $month;
125 24
        $date->day = $day;
126 24
        $date->hour = 8;
127 24
        $date->minute = 0;
128 24
        $date->second = 0;
129 24
        return $date;
130
    }
131
}
132
133
if (!function_exists('pstDayEndToUtcTime')) {
134
    /**
135
     * Given a date, creates a datetime object representing end of day Pacific Standard Time (23:59:59), but converted to UTC.
136
     *
137
     * @param integer $year
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
138
     * @param integer $month
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
139
     * @param integer $day
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
140
     * @return Date
141
     */
142
    function pstDayEndToUtcTime(int $year, int $month, int $day): Date
143
    {
144 24
        $date = Date::now();
145 24
        $date->year = $year;
146 24
        $date->month = $month;
147 24
        $date->day = $day;
148
149
        //23:59 March 1 PST is 7:59 March 2 UTC, so add a day
150 24
        $date->addDay();
151
152 24
        $date->hour = 7;
153 24
        $date->minute = 59;
154 24
        $date->second = 59;
155 24
        return $date;
156
    }
157
}
158