summaryrefslogtreecommitdiff
path: root/classes/timehelper.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2020-09-23 13:05:00 +0300
committerAndrew Dolgov <[email protected]>2020-09-23 13:05:00 +0300
commitd0ed7890df949669261f52235151c93cf324714e (patch)
tree95d9473e29b7d882404169915430f95b4e08f4a5 /classes/timehelper.php
parent215f3889924973ad138cfce309744cb1abf38cf0 (diff)
prev: add missing class
Diffstat (limited to 'classes/timehelper.php')
-rw-r--r--classes/timehelper.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/classes/timehelper.php b/classes/timehelper.php
new file mode 100644
index 000000000..ce9e35f3e
--- /dev/null
+++ b/classes/timehelper.php
@@ -0,0 +1,88 @@
+<?php
+class TimeHelper {
+
+ static function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) {
+ if (!$owner_uid) $owner_uid = $_SESSION['uid'];
+
+ if ($eta_min && time() + $tz_offset - $timestamp < 3600) {
+ return T_sprintf("%d min", date("i", time() + $tz_offset - $timestamp));
+ } else if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) {
+ $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
+ if (strpos((strtolower($format)), "a") === false)
+ return date("G:i", $timestamp);
+ else
+ return date("g:i a", $timestamp);
+ } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) {
+ $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
+ return date($format, $timestamp);
+ } else {
+ $format = get_pref('LONG_DATE_FORMAT', $owner_uid);
+ return date($format, $timestamp);
+ }
+ }
+
+ static function make_local_datetime($timestamp, $long, $owner_uid = false,
+ $no_smart_dt = false, $eta_min = false) {
+
+ if (!$owner_uid) $owner_uid = $_SESSION['uid'];
+ if (!$timestamp) $timestamp = '1970-01-01 0:00';
+
+ global $utc_tz;
+ global $user_tz;
+
+ if (!$utc_tz) $utc_tz = new DateTimeZone('UTC');
+
+ $timestamp = substr($timestamp, 0, 19);
+
+ # We store date in UTC internally
+ $dt = new DateTime($timestamp, $utc_tz);
+
+ $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid);
+
+ if ($user_tz_string != 'Automatic') {
+
+ try {
+ if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string);
+ } catch (Exception $e) {
+ $user_tz = $utc_tz;
+ }
+
+ $tz_offset = $user_tz->getOffset($dt);
+ } else {
+ $tz_offset = (int) -$_SESSION["clientTzOffset"];
+ }
+
+ $user_timestamp = $dt->format('U') + $tz_offset;
+
+ if (!$no_smart_dt) {
+ return self::smart_date_time($user_timestamp,
+ $tz_offset, $owner_uid, $eta_min);
+ } else {
+ if ($long)
+ $format = get_pref('LONG_DATE_FORMAT', $owner_uid);
+ else
+ $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
+
+ return date($format, $user_timestamp);
+ }
+ }
+
+ static function convert_timestamp($timestamp, $source_tz, $dest_tz) {
+
+ try {
+ $source_tz = new DateTimeZone($source_tz);
+ } catch (Exception $e) {
+ $source_tz = new DateTimeZone('UTC');
+ }
+
+ try {
+ $dest_tz = new DateTimeZone($dest_tz);
+ } catch (Exception $e) {
+ $dest_tz = new DateTimeZone('UTC');
+ }
+
+ $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz);
+ return $dt->format('U') + $dest_tz->getOffset($dt);
+ }
+
+}