Passed
Push — task/priority_bucket ( 56fe70...7361cc )
by Yonathan
07:47 queued 10s
created

ptDayStartToUtcTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 1
ccs 5
cts 5
cp 1
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('ptDayStartToUtcTime')) {
112
    /**
113
     * Given a date, creates a datetime object representing the start of day
114
     * in the most Western timezone in Canada (America/Vancouver).
115
     *
116
     * @param string $date ISO standard date YYYY-MM-DD.
117
     *
118
     * @return Date
119
     */
120
    function ptDayStartToUtcTime(string $date) : Date
121
    {
122 29
        $jobTimezone = Config::get('app.job_timezone');
123 29
        $dbTimezone = Config::get('app.timezone');
124
        // Create a new date that's the beginning of the day in
125
        // Pacific Daylight/Standard Time.
126 29
        $date = new Date("$date 00:00:00", new \DateTimeZone($jobTimezone));
127
        // Convert to UTC for correct offset.
128 29
        $date->setTimezone($dbTimezone);
129
130 29
        return $date;
131
    }
132
}
133
134
if (!function_exists('ptDayEndToUtcTime')) {
135
    /**
136
     * Given a date, creates a datetime object representing the start of day
137
     * in the most Western timezone in Canada (America/Vancouver).
138
     *
139
     * @param string $date ISO standard date YYYY-MM-DD.
140
     *
141
     * @return Date
142
     */
143
    function ptDayEndToUtcTime(string $date) : Date
144
    {
145 29
        $jobTimezone = Config::get('app.job_timezone');
146 29
        $dbTimezone = Config::get('app.timezone');
147
        // Create a new date that's the beginning of the day in
148
        // Pacific Daylight/Standard Time.
149 29
        $date = new Date("$date 23:59:59", new \DateTimeZone($jobTimezone));
150
        // Convert to UTC for correct offset.
151 29
        $date->setTimezone($dbTimezone);
152
153 29
        return $date;
154
    }
155
}
156