/
DBTime.php
192 lines (174 loc) · 4.71 KB
/
DBTime.php
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
183
184
185
186
187
188
189
190
191
192
<?php
namespace SilverStripe\ORM\FieldType;
use IntlDateFormatter;
use InvalidArgumentException;
use SilverStripe\Forms\TimeField;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
/**
* Represents a column in the database with the type 'Time'.
*
* Example definition via {@link DataObject::$db}:
* <code>
* static $db = array(
* "StartTime" => "Time",
* );
* </code>
*/
class DBTime extends DBField
{
/**
* Standard ISO format string for time in CLDR standard format
*/
const ISO_TIME = 'HH:mm:ss';
public function setValue($value, $record = null, $markChanged = true)
{
$value = $this->parseTime($value);
if ($value === false) {
throw new InvalidArgumentException(
'Invalid date passed. Use ' . $this->getISOFormat() . ' to prevent this error.'
);
}
$this->value = $value;
return $this;
}
/**
* Parse timestamp or iso8601-ish date into standard iso8601 format
*
* @param mixed $value
* @return string|null|false Formatted time, null if empty but valid, or false if invalid
*/
protected function parseTime($value)
{
// Skip empty values
if (empty($value) && !is_numeric($value)) {
return null;
}
// Determine value to parse
if (is_array($value)) {
$source = $value; // parse array
} elseif (is_numeric($value)) {
$source = $value; // parse timestamp
} else {
// Convert using strtotime
$source = strtotime($value ?? '');
}
if ($value === false) {
return false;
}
// Format as iso8601
$formatter = $this->getFormatter();
$formatter->setPattern($this->getISOFormat());
return $formatter->format($source);
}
/**
* Get date / time formatter for the current locale
*
* @param int $timeLength
* @return IntlDateFormatter
*/
public function getFormatter($timeLength = IntlDateFormatter::MEDIUM)
{
return IntlDateFormatter::create(i18n::get_locale(), IntlDateFormatter::NONE, $timeLength);
}
/**
* Returns the date in the localised short format
*
* @return string
*/
public function Short()
{
if (!$this->value) {
return null;
}
$formatter = $this->getFormatter(IntlDateFormatter::SHORT);
return $formatter->format($this->getTimestamp());
}
/**
* Returns the standard localised medium time
* e.g. "3:15pm"
*
* @return string
*/
public function Nice()
{
if (!$this->value) {
return null;
}
$formatter = $this->getFormatter();
return $formatter->format($this->getTimestamp());
}
/**
* Return the time using a particular formatting string.
*
* @param string $format Format code string. See https://unicode-org.github.io/icu/userguide/format_parse/datetime
* @return string The time in the requested format
*/
public function Format($format)
{
if (!$this->value) {
return null;
}
$formatter = $this->getFormatter();
$formatter->setPattern($format);
return $formatter->format($this->getTimestamp());
}
public function requireField()
{
$parts = [
'datatype' => 'time',
'arrayValue' => $this->arrayValue
];
$values = [
'type' => 'time',
'parts' => $parts
];
DB::require_field($this->tableName, $this->name, $values);
}
public function scaffoldFormField($title = null, $params = null)
{
return TimeField::create($this->name, $title);
}
/**
* Return a time formatted as per a CMS user's settings.
*
* @param Member $member
* @return string A time formatted as per user-defined settings.
*/
public function FormatFromSettings($member = null)
{
if (!$member) {
$member = Security::getCurrentUser();
}
// Fall back to nice
if (!$member) {
return $this->Nice();
}
// Get user format
$format = $member->getTimeFormat();
return $this->Format($format);
}
/**
* Get standard ISO time format string
*
* @return string
*/
public function getISOFormat()
{
return self::ISO_TIME;
}
/**
* Get unix timestamp for this time
*
* @return int
*/
public function getTimestamp()
{
if ($this->value) {
return strtotime($this->value ?? '');
}
return 0;
}
}