Skip to content

Commit 5a73f86

Browse files
committed
chore: refactor.
separate functions for timestamp.
1 parent e956ed5 commit 5a73f86

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/QueryBuilder.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Closure;
66

7+
use DateTime;
8+
use DateTimeZone;
79
use Exception;
810

911
class QueryBuilder
@@ -1226,6 +1228,32 @@ protected function getValueType($value)
12261228
return $placeHolder;
12271229
}
12281230

1231+
/**
1232+
* Helper function, to get current timestamp
1233+
*
1234+
* @return string
1235+
*/
1236+
protected function currentTimestamp()
1237+
{
1238+
if (\function_exists('wp_timezone_string')) {
1239+
$timezoneString = wp_timezone_string();
1240+
} elseif (!($timezoneString = get_option('timezone_string'))) {
1241+
$offset = (float) get_option('gmt_offset');
1242+
$hours = (int) $offset;
1243+
$minutes = ($offset - $hours);
1244+
1245+
$sign = ($offset < 0) ? '-' : '+';
1246+
$absHour = abs($hours);
1247+
$absMins = abs($minutes * 60);
1248+
1249+
$timezoneString = sprintf('%s%02d:%02d', $sign, $absHour, $absMins);
1250+
}
1251+
1252+
$dateTime = new DateTime('now', new DateTimeZone($timezoneString));
1253+
1254+
return $dateTime->format(self::TIME_FORMAT);
1255+
}
1256+
12291257
/**
12301258
* Run bulk insert query
12311259
*
@@ -1252,7 +1280,7 @@ private function bulkInsert($attributes)
12521280
foreach ($attributes as $row) {
12531281
ksort($row);
12541282
if ($createdAt) {
1255-
$row['created_at'] = date(self::TIME_FORMAT);
1283+
$row['created_at'] = $this->currentTimestamp();
12561284
}
12571285

12581286
$rowValues = array_values($row);
@@ -1388,11 +1416,11 @@ private function prepareAttributeForSaveOrUpdate($isUpdate = false)
13881416

13891417
if (property_exists($this->_model, 'timestamps') && $this->_model->timestamps) {
13901418
if (!$isUpdate) {
1391-
$this->_model->setAttribute('created_at', date(self::TIME_FORMAT));
1419+
$this->_model->setAttribute('created_at', $this->currentTimestamp());
13921420
$columnsToPrepare[] = 'created_at';
13931421
}
13941422

1395-
$this->_model->setAttribute('updated_at', date(self::TIME_FORMAT));
1423+
$this->_model->setAttribute('updated_at', $this->currentTimestamp());
13961424
$columnsToPrepare[] = 'updated_at';
13971425
}
13981426

@@ -1487,7 +1515,7 @@ private function prepareUpdate()
14871515
private function prepareDelete()
14881516
{
14891517
if (property_exists($this->_model, 'soft_deletes') && $this->_model->soft_deletes) {
1490-
return $this->update(['deleted_at' => date(self::TIME_FORMAT)])->prepareUpdate();
1518+
return $this->update(['deleted_at' => $this->currentTimestamp()])->prepareUpdate();
14911519
}
14921520

14931521
$sql = 'DELETE FROM ' . $this->table;

0 commit comments

Comments
 (0)