summaryrefslogtreecommitdiffstats
path: root/web/lib/timezone.inc.php
diff options
context:
space:
mode:
authorMark Weiman <mark.weiman@markzz.com>2017-01-20 07:16:39 +0100
committerLukas Fleischer <lfleischer@archlinux.org>2017-01-20 23:20:40 +0100
commit608c48309084e4048d8226c3f7e363b240248040 (patch)
treeb17b272ae8d5101e239fd65ed75397144981057d /web/lib/timezone.inc.php
parent087b539cbc3f4a24872e340b1aa863c263cd65bd (diff)
downloadaur-608c48309084e4048d8226c3f7e363b240248040.tar.gz
aur-608c48309084e4048d8226c3f7e363b240248040.tar.xz
Add user set timezones
Currently, aurweb displays all dates and times in UTC time. This patch adds a capability for each logged in user to set their preferred timezone. Implements FS#48729. Signed-off-by: Mark Weiman <mark.weiman@markzz.com> Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'web/lib/timezone.inc.php')
-rw-r--r--web/lib/timezone.inc.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/web/lib/timezone.inc.php b/web/lib/timezone.inc.php
new file mode 100644
index 00000000..9fb24331
--- /dev/null
+++ b/web/lib/timezone.inc.php
@@ -0,0 +1,60 @@
+<?php
+set_include_path(get_include_path() . PATH_SEPARATOR . '../lib');
+
+/**
+ * Generate an associative of the PHP timezones and display text.
+ *
+ * @return array PHP Timezone => Displayed Description
+ */
+function generate_timezone_list() {
+ $php_timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
+
+ $offsets = array();
+ foreach ($php_timezones as $timezone) {
+ $tz = new DateTimeZone($timezone);
+ $offset = $tz->getOffset(new DateTime());
+ $offsets[$timezone] = "(UTC" . ($offset < 0 ? "-" : "+") . gmdate("H:i", abs($offset)) .
+ ") " . $timezone;
+ }
+
+ asort($offsets);
+ return $offsets;
+}
+
+/**
+ * Set the timezone for the user.
+ *
+ * @return null
+ */
+function set_tz() {
+ $timezones = generate_timezone_list();
+ $update_cookie = false;
+
+ if (isset($_COOKIE["AURTZ"])) {
+ $timezone = $_COOKIE["AURTZ"];
+ } elseif (isset($_COOKIE["AURSID"])) {
+ $dbh = DB::connect();
+ $q = "SELECT Timezone FROM Users, Sessions ";
+ $q .= "WHERE Users.ID = Sessions.UsersID ";
+ $q .= "AND Sessions.SessionID = ";
+ $q .= $dbh->quote($_COOKIE["AURSID"]);
+ $result = $dbh->query($q);
+
+ if ($result) {
+ $timezone = $result->fetchColumn(0);
+ }
+
+ $update_cookie = true;
+ }
+
+ if (!isset($timezone) || !array_key_exists($timezone, $timezones)) {
+ $timezone = config_get("options", "default_timezone");
+ }
+ date_default_timezone_set($timezone);
+
+ if ($update_cookie) {
+ $timeout = intval(config_get("options", "persistent_cookie_timeout"));
+ $cookie_time = time() + $timeout;
+ setcookie("AURTZ", $timezone, $cookie_time, "/");
+ }
+}