Passed
Push — feature/profile-skills-finaliz... ( 961836...9d523b )
by Tristan
06:45
created

detectInternetExplorer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
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
use Illuminate\Support\Facades\Request;
8
9
if (!function_exists('humanizeDate')) {
10
    /**
11
     * Computes a human readable localized date.
12
     *
13
     * @param Date $datetime DateTime object to translate.
14
     * @param string $locale Locale the date should be formatted in.
15
     *
16
     * @return string
17
     */
18
    function humanizeDate(Date $datetime, string $locale = null) : string
19
    {
20
        $dateFormat = Config::get('app.date_format');
21
        if ($locale !== null) {
22
            App::setLocale($locale);
23
        }
24
        $locale = App::getLocale();
25
26
        $timezone = Config::get('app.local_timezone');
27
28
        $datetime->setTimezone(new \DateTimeZone($timezone));
29
30
31
        return $datetime->format($dateFormat[$locale]);
32
    }
33
}
34
35
if (!function_exists('humanizeTime')) {
36
    /**
37
     * Computes a human readable localized time.
38
     *
39
     * @param Date $datetime DateTime object to translate.
40
     *
41
     * @return string
42
     */
43
    function humanizeTime(Date $datetime) : string
44
    {
45
        $timeFormat = Config::get('app.time_format');
46
        $locale = App::getLocale();
47
        $timezone = Config::get('app.local_timezone');
48
49
        $datetime->setTimezone(new \DateTimeZone($timezone));
50
51
        $displayTime = $datetime->format($timeFormat[$locale]);
52
53
        if ($locale == 'fr') {
54
            $displayTime = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayTime);
55
        }
56
57
        return $displayTime;
58
    }
59
}
60
61
if (!function_exists('humanizeDateDiff')) {
62
    /**
63
     * Computes a human readable time difference between two dates.
64
     *
65
     * @param Date $datetime DateTime object to measure.
66
     * @param Date $origin   DateTime object to use as a reference.
67
     *
68
     * @return string
69
     */
70
    function humanizeDateDiff(Date $datetime, Date $origin = null) : string
71
    {
72
        if (!isset($origin)) {
73
            $origin = new Date();
74
        }
75
        $interval = $datetime->diff($origin);
76
77
        $d = $interval->d;
78
        $h = $interval->h;
79
        $m = $interval->i;
80
        $s = $interval->s;
81
82
        if ($d > 0) {
83
            $unit = 'day';
84
            $count = $d;
85
        } elseif ($h > 0) {
86
            $unit = 'hour';
87
            $count = $h;
88
        } elseif ($m > 0) {
89
            $unit = 'minute';
90
            $count = $m;
91
        } else {
92
            $unit = 'second';
93
            $count = $s;
94
        }
95
96
        $key = "common/time.$unit";
97
98
        return Lang::choice($key, $count);
99
    }
100
}
101
102
if (!function_exists('humanizeLastDay')) {
103
    /**
104
     * Returns the date of the last full day a person has before a deadline time.
105
     *
106
     * @param Date $datetime DateTime object to transform to date of last day.
107
     * @param string $locale Locale the date should be formatted in.
108
     *
109
     * @return string
110
     */
111
    function humanizeLastDay(Date $datetime, string $locale = null) : string
112
    {
113
        $lastday = $datetime->sub('1 day');
114
115
        return humanizeDate($lastday, $locale);
116
    }
117
}
118
119
if (!function_exists('ptDayStartToUtcTime')) {
120
    /**
121
     * Given a date, creates a datetime object representing the start of day
122
     * in the most Western timezone in Canada (America/Vancouver).
123
     *
124
     * @param string $date ISO standard date YYYY-MM-DD.
125
     *
126
     * @return Date
127
     */
128
    function ptDayStartToUtcTime(string $date) : Date
129
    {
130
        $jobTimezone = Config::get('app.job_timezone');
131
        $dbTimezone = Config::get('app.timezone');
132
        // Create a new date that's the beginning of the day in
133
        // Pacific Daylight/Standard Time.
134
        $date = new Date("$date 00:00:00", new \DateTimeZone($jobTimezone));
135
        // Convert to UTC for correct offset.
136
        $date->setTimezone($dbTimezone);
137
138
        return $date;
139
    }
140
}
141
142
if (!function_exists('ptDayEndToUtcTime')) {
143
    /**
144
     * Given a date, creates a datetime object representing the end of day
145
     * in the most Western timezone in Canada (America/Vancouver).
146
     *
147
     * @param string $date ISO standard date YYYY-MM-DD.
148
     *
149
     * @return Date
150
     */
151
    function ptDayEndToUtcTime(string $date) : Date
152
    {
153
        $jobTimezone = Config::get('app.job_timezone');
154
        $dbTimezone = Config::get('app.timezone');
155
        // Create a new date that's the end of the day in
156
        // Pacific Daylight/Standard Time.
157
        $date = new Date("$date 23:59:59", new \DateTimeZone($jobTimezone));
158
        // Convert to UTC for correct offset.
159
        $date->setTimezone($dbTimezone);
160
161
        return $date;
162
    }
163
}
164
165
if (!function_exists('detectInternetExplorer')) {
166
    /**
167
     * Detect User Agent Internet Explorer
168
     *
169
     * @return boolean
170
     */
171
    function detectInternetExplorer()
172
    {
173
        $agent = Request::server('HTTP_USER_AGENT');
174
        return preg_match('~MSIE|Internet Explorer~i', $agent) || preg_match('~Trident/7.0(.*)?; rv:11.0~', $agent);
175
    }
176
}
177