Skip to content

Commit 4d0cfc6

Browse files
committed
Merge pull request #107 from soloweb/tzFeature
Update Timestamp (+BC) + TimestampTZ
2 parents db08f37 + a5db00a commit 4d0cfc6

18 files changed

+568
-208
lines changed

core/Base/Date.class.php

Lines changed: 81 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ class Date implements Stringable, DialectString
2727
const WEEKDAY_FRIDAY = 5;
2828
const WEEKDAY_SATURDAY = 6;
2929
const WEEKDAY_SUNDAY = 0; // because strftime('%w') is 0 on Sunday
30-
31-
protected $string = null;
32-
protected $int = null;
33-
34-
protected $year = null;
35-
protected $month = null;
36-
protected $day = null;
37-
30+
31+
/**
32+
* @var DateTime
33+
*/
34+
protected $dateTime = null;
35+
3836
/**
3937
* @return Date
4038
**/
4139
public static function create($date)
4240
{
43-
return new self($date);
41+
return new static($date);
4442
}
4543

4644
public static function today($delimiter = '-')
@@ -53,7 +51,7 @@ public static function today($delimiter = '-')
5351
**/
5452
public static function makeToday()
5553
{
56-
return new self(self::today());
54+
return new static(static::today());
5755
}
5856

5957
/**
@@ -68,13 +66,13 @@ public static function makeFromWeek($weekNumber, $year = null)
6866

6967
Assert::isTrue(
7068
($weekNumber > 0)
71-
&& ($weekNumber <= self::getWeekCountInYear($year))
69+
&& ($weekNumber <= static::getWeekCountInYear($year))
7270
);
7371

7472
$date =
75-
new self(
73+
new static(
7674
date(
77-
self::getFormat(),
75+
static::getFormat(),
7876
mktime(
7977
0, 0, 0, 1, 1, $year
8078
)
@@ -85,7 +83,7 @@ public static function makeFromWeek($weekNumber, $year = null)
8583
(
8684
(
8785
$weekNumber - 1
88-
+ (self::getWeekCountInYear($year - 1) == 53 ? 1 : 0)
86+
+ (static::getWeekCountInYear($year - 1) == 53 ? 1 : 0)
8987
)
9088
* 7
9189
) + 1 - $date->getWeekDay();
@@ -110,10 +108,10 @@ public static function dayDifference(Date $left, Date $right)
110108

111109
public static function compare(Date $left, Date $right)
112110
{
113-
if ($left->int == $right->int)
111+
if ($left->toStamp() == $right->toStamp())
114112
return 0;
115113
else
116-
return ($left->int > $right->int ? 1 : -1);
114+
return ($left->toStamp() > $right->toStamp() ? 1 : -1);
117115
}
118116

119117
public static function getWeekCountInYear($year)
@@ -129,77 +127,66 @@ public static function getWeekCountInYear($year)
129127

130128
public function __construct($date)
131129
{
132-
if (is_int($date) || is_numeric($date)) { // unix timestamp
133-
$this->string = date($this->getFormat(), $date);
134-
} elseif ($date && is_string($date))
135-
$this->stringImport($date);
136-
137-
if ($this->string === null) {
138-
throw new WrongArgumentException(
139-
"strange input given - '{$date}'"
140-
);
141-
}
142-
143-
$this->import($this->string);
144-
$this->buildInteger();
130+
$this->import($date);
145131
}
146132

147-
public function __sleep()
133+
public function __clone()
148134
{
149-
return array('int');
135+
$this->dateTime = clone $this->dateTime;
150136
}
151-
152-
public function __wakeup()
137+
138+
public function __sleep()
153139
{
154-
$this->import(date($this->getFormat(), $this->int));
140+
return array('dateTime');
155141
}
156-
142+
157143
public function toStamp()
158144
{
159-
return $this->int;
145+
return $this->getDateTime()->getTimestamp();
160146
}
161147

162148
public function toDate($delimiter = '-')
163149
{
164150
return
165-
$this->year
151+
$this->getYear()
166152
.$delimiter
167-
.$this->month
153+
.$this->getMonth()
168154
.$delimiter
169-
.$this->day;
155+
.$this->getDay();
170156
}
171157

172158
public function getYear()
173159
{
174-
return $this->year;
160+
return $this->dateTime->format('Y');
175161
}
176162

177163
public function getMonth()
178164
{
179-
return $this->month;
165+
return $this->dateTime->format('m');
180166
}
181167

182168
public function getDay()
183169
{
184-
return $this->day;
170+
return $this->dateTime->format('d');
185171
}
186172

187173
public function getWeek()
188174
{
189-
return date('W', $this->int);
175+
return date('W', $this->dateTime->getTimestamp());
190176
}
191177

192178
public function getWeekDay()
193179
{
194-
return strftime('%w', $this->int);
180+
return strftime('%w', $this->dateTime->getTimestamp());
195181
}
196182

197183
/**
198184
* @return Date
199185
**/
200186
public function spawn($modification = null)
201187
{
202-
$child = new $this($this->string);
188+
189+
$child = new static($this->toString());
203190

204191
if ($modification)
205192
return $child->modify($modification);
@@ -214,17 +201,8 @@ public function spawn($modification = null)
214201
public function modify($string)
215202
{
216203
try {
217-
$time = strtotime($string, $this->int);
218-
219-
if ($time === false)
220-
throw new WrongArgumentException(
221-
"modification yielded false '{$string}'"
222-
);
223-
224-
$this->int = $time;
225-
$this->string = date($this->getFormat(), $time);
226-
$this->import($this->string);
227-
} catch (BaseException $e) {
204+
$this->dateTime->modify($string);
205+
} catch (Exception $e) {
228206
throw new WrongArgumentException(
229207
"wrong time string '{$string}'"
230208
);
@@ -238,9 +216,9 @@ public function getDayStartStamp()
238216
return
239217
mktime(
240218
0, 0, 0,
241-
$this->month,
242-
$this->day,
243-
$this->year
219+
$this->getMonth(),
220+
$this->getDay(),
221+
$this->getYear()
244222
);
245223
}
246224

@@ -249,9 +227,9 @@ public function getDayEndStamp()
249227
return
250228
mktime(
251229
23, 59, 59,
252-
$this->month,
253-
$this->day,
254-
$this->year
230+
$this->getMonth(),
231+
$this->getDay(),
232+
$this->getYear()
255233
);
256234
}
257235

@@ -277,12 +255,12 @@ public function getLastDayOfWeek($weekStart = Date::WEEKDAY_MONDAY)
277255

278256
public function toString()
279257
{
280-
return $this->string;
258+
return $this->dateTime->format(static::getFormat());
281259
}
282260

283261
public function toFormatString($format)
284262
{
285-
return date($format, $this->toStamp());
263+
return $this->dateTime->format($format);
286264
}
287265

288266
public function toDialectString(Dialect $dialect)
@@ -306,57 +284,53 @@ public function toTimestamp()
306284
{
307285
return Timestamp::create($this->toStamp());
308286
}
287+
288+
/**
289+
* @return DateTime|null
290+
*/
291+
public function getDateTime()
292+
{
293+
return $this->dateTime;
294+
}
309295

310296
protected static function getFormat()
311297
{
312298
return 'Y-m-d';
313299
}
314-
315-
/* void */ protected function import($string)
300+
301+
302+
protected function import($date)
316303
{
317-
list($this->year, $this->month, $this->day) =
318-
explode('-', $string, 3);
319-
320-
if (!$this->month || !$this->day)
304+
try{
305+
if (is_int($date) || is_numeric($date)) { // unix timestamp
306+
$this->dateTime = new DateTime(date(static::getFormat(), $date));
307+
308+
} elseif ($date && is_string($date)) {
309+
310+
if (
311+
preg_match('/^(\d{1,4})[-\.](\d{1,2})[-\.](\d{1,2})/', $date, $matches)
312+
) {
313+
Assert::isTrue(
314+
checkdate($matches[2], $matches[3], $matches[1])
315+
);
316+
} elseif (
317+
preg_match('/^(\d{1,2})[-\.](\d{1,2})[-\.](\d{1,4})/', $date, $matches)
318+
) {
319+
Assert::isTrue(
320+
checkdate($matches[2], $matches[2], $matches[3])
321+
);
322+
}
323+
324+
$this->dateTime = new DateTime($date);
325+
}
326+
327+
328+
} catch(Exception $e) {
321329
throw new WrongArgumentException(
322-
'month and day must not be zero'
323-
);
324-
325-
$this->string =
326-
sprintf(
327-
'%04d-%02d-%02d',
328-
$this->year,
329-
$this->month,
330-
$this->day
331-
);
332-
333-
list($this->year, $this->month, $this->day) =
334-
explode('-', $this->string, 3);
335-
}
336-
337-
/* void */ protected function stringImport($string)
338-
{
339-
$matches = array();
340-
341-
if (
342-
preg_match('/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/', $string, $matches)
343-
) {
344-
if (checkdate($matches[2], $matches[3], $matches[1]))
345-
$this->string = $string;
346-
347-
} elseif (($stamp = strtotime($string)) !== false)
348-
$this->string = date($this->getFormat(), $stamp);
349-
}
350-
351-
/* void */ protected function buildInteger()
352-
{
353-
$this->int =
354-
mktime(
355-
0, 0, 0,
356-
$this->month,
357-
$this->day,
358-
$this->year
330+
"strange input given - '{$date}'"
359331
);
332+
}
333+
360334
}
361335
}
362336
?>

0 commit comments

Comments
 (0)