Skip to content

Commit

Permalink
Add timezone selector to User class
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Aug 22, 2016
1 parent e3e9d3e commit 8f5f13a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/libraries/DateTimeZoneTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace BNETDocs\Libraries;

use \DateTime;
use \DateTimeZone;

class DateTimeZoneTranslator {

public static function translate($datetime, $timezone) {

if ($timezone instanceof DateTimeZone) {
$tz = $timezone;
} else {
$tz = new DateTimeZone((string) $timezone);
}

if ($datetime instanceof DateTime) {
$dt = clone $datetime;
} else {
$dt = new DateTime($datetime, new DateTimeZone("UTC"));
}

$dt->setTimezone($tz);

return $dt;

}

}
18 changes: 16 additions & 2 deletions src/libraries/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class User implements JsonSerializable {
protected $options_bitmask;
protected $password_hash;
protected $password_salt;
protected $timezone;
protected $username;
protected $verified_datetime;

Expand All @@ -64,6 +65,7 @@ public function __construct($data) {
$this->options_bitmask = null;
$this->password_hash = null;
$this->password_salt = null;
$this->timezone = null;
$this->username = null;
$this->verified_datetime = null;
$this->refresh();
Expand All @@ -76,6 +78,7 @@ public function __construct($data) {
$this->options_bitmask = $data->options_bitmask;
$this->password_hash = $data->password_hash;
$this->password_salt = $data->password_salt;
$this->timezone = $data->timezone;
$this->username = $data->username;
$this->verified_datetime = $data->verified_datetime;
} else {
Expand Down Expand Up @@ -146,11 +149,11 @@ public static function create(
INSERT INTO `users` (
`id`, `email`, `username`, `display_name`, `created_datetime`,
`verified_datetime`, `password_hash`, `password_salt`,
`options_bitmask`
`options_bitmask`, `timezone`
) VALUES (
NULL, :email, :username, :display_name, NOW(),
NULL, :password_hash, :password_salt,
:options_bitmask
:options_bitmask, NULL
);
");
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
Expand Down Expand Up @@ -286,6 +289,10 @@ public function getURI() {
);
}

public function getTimezone() {
return $this->timezone;
}

public function getUsername() {
return $this->username;
}
Expand Down Expand Up @@ -344,6 +351,7 @@ public function jsonSerialize() {
"created_datetime" => $created_datetime,
"id" => $this->getId(),
"name" => $this->getName(),
"timezone" => $this->getTimezone(),
"verified_datetime" => $verified_datetime,
];
}
Expand All @@ -364,6 +372,9 @@ protected static function normalize(StdClass &$data) {
if (!is_null($data->password_salt))
$data->password_salt = (string) $data->password_salt;

if (!is_null($data->timezone))
$data->timezone = (string) $data->timezone;

if (!is_null($data->verified_datetime))
$data->verified_datetime = (string) $data->verified_datetime;

Expand All @@ -381,6 +392,7 @@ public function refresh() {
$this->options_bitmask = $cache_val->options_bitmask;
$this->password_hash = $cache_val->password_hash;
$this->password_salt = $cache_val->password_salt;
$this->timezone = $cache_val->timezone;
$this->username = $cache_val->username;
$this->verified_datetime = $cache_val->verified_datetime;
return true;
Expand All @@ -398,6 +410,7 @@ public function refresh() {
`options_bitmask`,
`password_hash`,
`password_salt`,
`timezone`,
`username`,
`verified_datetime`
FROM `users`
Expand All @@ -419,6 +432,7 @@ public function refresh() {
$this->options_bitmask = $row->options_bitmask;
$this->password_hash = $row->password_hash;
$this->password_salt = $row->password_salt;
$this->timezone = $row->timezone;
$this->username = $row->username;
$this->verified_datetime = $row->verified_datetime;
Common::$cache->set($cache_key, serialize($row), 300);
Expand Down

0 comments on commit 8f5f13a

Please sign in to comment.