forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.php
More file actions
182 lines (157 loc) · 4.44 KB
/
Time.php
File metadata and controls
182 lines (157 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace kriskbx\gtt;
use ArrayAccess;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use kriskbx\gtt\Helper\ArrayAccessForGetterMethods;
class Time implements ArrayAccess, Arrayable
{
use ArrayAccessForGetterMethods;
protected $seconds;
protected $user;
protected $date;
private $hoursPerDay;
/**
* Time constructor.
*
* @param mixed $date
* @param mixed $input
* @param string $user
* @param int $hoursPerDay
*/
public function __construct($date = null, $input = null, $user = null, $hoursPerDay = 8)
{
if ($date) {
$this->date = Carbon::parse($date);
}
if (is_numeric($input)) {
$this->seconds = $input;
} elseif (is_string($input)) {
$this->seconds = self::parse($input);
}
$this->user = $user;
$this->hoursPerDay = $hoursPerDay;
}
/**
* @param string $user
*/
public function setUser($user)
{
$this->user = $user;
}
/**
* @return string
*/
public function getUser()
{
return $this->user;
}
/**
* @param string $date
*/
public function setDate($date)
{
$this->date = Carbon::parse($date);
}
/**
* @param string $format
*
* @return mixed
*/
public function getDate($format = null)
{
if ($format) {
return $this->date->format($format);
}
return $this->date;
}
/**
* @return int
*/
public function getSeconds()
{
return $this->seconds;
}
/**
* @param int $seconds
*/
public function setSeconds($seconds)
{
$this->seconds = $seconds;
}
/**
* @return string
*/
public function getHumanReadable()
{
return self::humanReadable($this->seconds, $this->hoursPerDay);
}
/**
* @param string $humanReadable
*/
public function setHumanReadable($humanReadable)
{
$this->seconds = self::parse($humanReadable);
}
/**
* Seconds to human readable string.
*
* @param int $seconds
* @param int $hoursPerDay
*
* @return string
*/
static public function humanReadable($seconds, $hoursPerDay = 8)
{
$sign = $seconds < 0 ? '-' : '';
$seconds = abs($seconds);
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = $hoursPerDay * $secondsInAnHour;
$days = floor($seconds / $secondsInADay);
$hourSeconds = $seconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
$string = $days ? (int)$days . 'd ' : '';
$string .= $hours ? (int)$hours . 'h ' : '';
$string .= $minutes ? (int)$minutes . 'm ' : '';
$string .= $seconds ? (int)$seconds . 's' : '';
return $sign . trim($string);
}
/**
* Parse the given human readable string and return seconds.
*
* @param string $humanReadable
* @param int $hoursPerDay
*
* @return int
*/
static public function parse($humanReadable, $hoursPerDay = 8)
{
preg_match('/^(?:(?<sign>[-])\s*)?(?:(?<days>\d+)d\s*)?(?:(?<hours>\d+)h\s*)?(?:(?<minutes>\d+)m\s*)?(?:(?<seconds>\d+)s\s*)?$/',
$humanReadable, $matches);
return
(@$matches['sign'] ? -1 : 1) * (
((@$matches['days'] ?: 0) * $hoursPerDay * 60 * 60) +
((@$matches['hours'] ?: 0) * 60 * 60) +
((@$matches['minutes'] ?: 0) * 60) +
(@$matches['seconds'] ?: 0)
);
}
/**
* @param array $params
*
* @return string
*/
public function toString($params = [])
{
// Make sure things are set
$params['beforeHighlight'] = @$params['beforeHighlight'] ?: '';
$params['afterHighlight'] = @$params['afterHighlight'] ?: '';
$params['dateFormat'] = @$params['dateFormat'] ?: 'Y-m-d H:i:m';
return $params['beforeHighlight'] . $this->getHumanReadable() . $params['afterHighlight'] . " (" . $this->getDate($params['dateFormat']) . ")";
}
}