Skip to content

Commit 8f5f13a

Browse files
committed
Add timezone selector to User class
1 parent e3e9d3e commit 8f5f13a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries;
4+
5+
use \DateTime;
6+
use \DateTimeZone;
7+
8+
class DateTimeZoneTranslator {
9+
10+
public static function translate($datetime, $timezone) {
11+
12+
if ($timezone instanceof DateTimeZone) {
13+
$tz = $timezone;
14+
} else {
15+
$tz = new DateTimeZone((string) $timezone);
16+
}
17+
18+
if ($datetime instanceof DateTime) {
19+
$dt = clone $datetime;
20+
} else {
21+
$dt = new DateTime($datetime, new DateTimeZone("UTC"));
22+
}
23+
24+
$dt->setTimezone($tz);
25+
26+
return $dt;
27+
28+
}
29+
30+
}

src/libraries/User.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class User implements JsonSerializable {
5252
protected $options_bitmask;
5353
protected $password_hash;
5454
protected $password_salt;
55+
protected $timezone;
5556
protected $username;
5657
protected $verified_datetime;
5758

@@ -64,6 +65,7 @@ public function __construct($data) {
6465
$this->options_bitmask = null;
6566
$this->password_hash = null;
6667
$this->password_salt = null;
68+
$this->timezone = null;
6769
$this->username = null;
6870
$this->verified_datetime = null;
6971
$this->refresh();
@@ -76,6 +78,7 @@ public function __construct($data) {
7678
$this->options_bitmask = $data->options_bitmask;
7779
$this->password_hash = $data->password_hash;
7880
$this->password_salt = $data->password_salt;
81+
$this->timezone = $data->timezone;
7982
$this->username = $data->username;
8083
$this->verified_datetime = $data->verified_datetime;
8184
} else {
@@ -146,11 +149,11 @@ public static function create(
146149
INSERT INTO `users` (
147150
`id`, `email`, `username`, `display_name`, `created_datetime`,
148151
`verified_datetime`, `password_hash`, `password_salt`,
149-
`options_bitmask`
152+
`options_bitmask`, `timezone`
150153
) VALUES (
151154
NULL, :email, :username, :display_name, NOW(),
152155
NULL, :password_hash, :password_salt,
153-
:options_bitmask
156+
:options_bitmask, NULL
154157
);
155158
");
156159
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
@@ -286,6 +289,10 @@ public function getURI() {
286289
);
287290
}
288291

292+
public function getTimezone() {
293+
return $this->timezone;
294+
}
295+
289296
public function getUsername() {
290297
return $this->username;
291298
}
@@ -344,6 +351,7 @@ public function jsonSerialize() {
344351
"created_datetime" => $created_datetime,
345352
"id" => $this->getId(),
346353
"name" => $this->getName(),
354+
"timezone" => $this->getTimezone(),
347355
"verified_datetime" => $verified_datetime,
348356
];
349357
}
@@ -364,6 +372,9 @@ protected static function normalize(StdClass &$data) {
364372
if (!is_null($data->password_salt))
365373
$data->password_salt = (string) $data->password_salt;
366374

375+
if (!is_null($data->timezone))
376+
$data->timezone = (string) $data->timezone;
377+
367378
if (!is_null($data->verified_datetime))
368379
$data->verified_datetime = (string) $data->verified_datetime;
369380

@@ -381,6 +392,7 @@ public function refresh() {
381392
$this->options_bitmask = $cache_val->options_bitmask;
382393
$this->password_hash = $cache_val->password_hash;
383394
$this->password_salt = $cache_val->password_salt;
395+
$this->timezone = $cache_val->timezone;
384396
$this->username = $cache_val->username;
385397
$this->verified_datetime = $cache_val->verified_datetime;
386398
return true;
@@ -398,6 +410,7 @@ public function refresh() {
398410
`options_bitmask`,
399411
`password_hash`,
400412
`password_salt`,
413+
`timezone`,
401414
`username`,
402415
`verified_datetime`
403416
FROM `users`
@@ -419,6 +432,7 @@ public function refresh() {
419432
$this->options_bitmask = $row->options_bitmask;
420433
$this->password_hash = $row->password_hash;
421434
$this->password_salt = $row->password_salt;
435+
$this->timezone = $row->timezone;
422436
$this->username = $row->username;
423437
$this->verified_datetime = $row->verified_datetime;
424438
Common::$cache->set($cache_key, serialize($row), 300);

0 commit comments

Comments
 (0)