2011-02-05 00:22:15 +01:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 23:45:28 +02:00
|
|
|
class Application_Common_DateHelper
|
2011-02-05 00:22:15 +01:00
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
private $_dateTime;
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function __construct()
|
2011-03-22 14:55:33 +01:00
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->_dateTime = date('U');
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
2021-10-11 16:10:47 +02:00
|
|
|
* YYYY-MM-DD HH:mm:ss.
|
2011-03-22 14:55:33 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function getTimestamp()
|
2011-03-22 14:55:33 +01:00
|
|
|
{
|
2015-06-26 20:42:52 +02:00
|
|
|
return date(DEFAULT_TIMESTAMP_FORMAT, $this->_dateTime);
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-08-15 22:40:24 +02:00
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
2021-10-11 16:10:47 +02:00
|
|
|
* YYYY-MM-DD HH:mm:ss.
|
2011-08-15 22:40:24 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function getUtcTimestamp()
|
2011-08-15 22:40:24 +02:00
|
|
|
{
|
2015-06-26 20:42:52 +02:00
|
|
|
return gmdate(DEFAULT_TIMESTAMP_FORMAT, $this->_dateTime);
|
2011-08-15 22:40:24 +02:00
|
|
|
}
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-05-31 00:12:57 +02:00
|
|
|
/**
|
|
|
|
* Get date of object construction in the format
|
2021-10-11 16:10:47 +02:00
|
|
|
* YYYY-MM-DD.
|
2011-05-31 00:12:57 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function getDate()
|
2011-05-31 00:12:57 +02:00
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
return gmdate('Y-m-d', $this->_dateTime);
|
2011-05-31 00:12:57 +02:00
|
|
|
}
|
|
|
|
|
2011-03-22 14:55:33 +01:00
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
2021-10-11 16:10:47 +02:00
|
|
|
* HH:mm:ss.
|
2011-03-22 14:55:33 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function getTime()
|
2011-03-22 14:55:33 +01:00
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
return gmdate('H:i:s', $this->_dateTime);
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
/** Get the abbreviated timezone for the currently logged in user.
|
2023-07-01 16:55:43 +02:00
|
|
|
* @return string A string containing the short form of the timezone set in the preferences for the current user (eg. EST, CEST, etc.)
|
2013-12-12 19:28:51 +01:00
|
|
|
*/
|
|
|
|
public static function getUserTimezoneAbbreviation()
|
|
|
|
{
|
|
|
|
return self::getTimezoneAbbreviation(Application_Model_Preference::GetUserTimezone());
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-12 19:28:51 +01:00
|
|
|
/** Get the abbreviated timezone string of the timezone the station is set to.
|
2023-07-01 16:55:43 +02:00
|
|
|
* @return string A string containing the short form of the station's timezone (eg. EST, CEST, etc.)
|
2013-12-12 19:28:51 +01:00
|
|
|
*/
|
|
|
|
public static function getStationTimezoneAbbreviation()
|
|
|
|
{
|
|
|
|
return self::getTimezoneAbbreviation(Application_Model_Preference::GetDefaultTimezone());
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-12 19:28:51 +01:00
|
|
|
private static function getTimezoneAbbreviation($fullTimeZoneName)
|
|
|
|
{
|
|
|
|
$timeZone = new DateTimeZone($fullTimeZoneName);
|
2021-10-11 16:10:47 +02:00
|
|
|
$now = new DateTime('now', $timeZone);
|
|
|
|
|
|
|
|
return $now->format('T');
|
2013-12-12 19:28:51 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-11 21:20:19 +01:00
|
|
|
public static function getUserTimezoneOffset()
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$now = new DateTime('now', $userTimezone);
|
|
|
|
|
|
|
|
return $now->format('Z');
|
2013-12-11 21:20:19 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-11 21:20:19 +01:00
|
|
|
public static function getStationTimezoneOffset()
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$stationTimezone = new DateTimeZone(Application_Model_Preference::GetDefaultTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$now = new DateTime('now', $stationTimezone);
|
|
|
|
|
|
|
|
return $now->format('Z');
|
2013-12-11 21:20:19 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 23:41:59 +01:00
|
|
|
/**
|
|
|
|
* @return DateTime - YYYY-MM-DD 00:00 in station timezone of today
|
2011-11-22 00:03:56 +01:00
|
|
|
*/
|
2013-12-10 23:41:59 +01:00
|
|
|
public static function getTodayStationStartDateTime()
|
2011-11-22 00:03:56 +01:00
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$stationTimezone = new DateTimeZone(Application_Model_Preference::GetDefaultTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$now = new DateTime('now', $stationTimezone);
|
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
$now->setTime(0, 0, 0);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $now;
|
2013-12-10 23:41:59 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 23:41:59 +01:00
|
|
|
/**
|
|
|
|
* @return DateTime - YYYY-MM-DD 00:00 in station timezone of tomorrow
|
|
|
|
*/
|
|
|
|
public static function getTodayStationEndDateTime()
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$stationTimezone = new DateTimeZone(Application_Model_Preference::GetDefaultTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$now = new DateTime('now', $stationTimezone);
|
|
|
|
|
|
|
|
$now->add(new DateInterval('P1D'));
|
2014-10-24 21:11:27 +02:00
|
|
|
$now->setTime(0, 0, 0);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $now;
|
2013-12-10 23:41:59 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
/**
|
2013-12-10 23:41:59 +01:00
|
|
|
* @return DateTime - YYYY-MM-DD 00:00 in station timezone
|
|
|
|
*/
|
|
|
|
public static function getWeekStartDateTime()
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$now = self::getTodayStationStartDateTime();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-07-03 19:17:48 +02:00
|
|
|
// our week starts on monday, but php week starts on sunday.
|
2013-12-10 22:45:05 +01:00
|
|
|
$day = $now->format('w');
|
|
|
|
if ($day == 0) {
|
2014-10-24 21:11:27 +02:00
|
|
|
$day = 7;
|
2013-12-10 22:45:05 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 22:45:05 +01:00
|
|
|
$dayDiff = $day - 1;
|
|
|
|
if ($dayDiff > 0) {
|
2014-10-24 21:11:27 +02:00
|
|
|
$now->sub(new DateInterval("P{$dayDiff}D"));
|
2013-12-10 22:45:05 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 22:45:05 +01:00
|
|
|
return $now;
|
2011-11-22 00:03:56 +01:00
|
|
|
}
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-05-30 18:24:39 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* This function formats a time by removing seconds.
|
2011-05-30 18:24:39 +02:00
|
|
|
*
|
|
|
|
* When we receive a time from the database we get the
|
|
|
|
* format "hh:mm:ss". But when dealing with show times, we
|
|
|
|
* do not care about the seconds.
|
|
|
|
*
|
2011-08-16 21:04:41 +02:00
|
|
|
* @param int $p_dateTime
|
2021-10-11 16:10:47 +02:00
|
|
|
* The value which to format
|
|
|
|
*
|
2011-05-30 18:24:39 +02:00
|
|
|
* @return int
|
2021-10-11 16:10:47 +02:00
|
|
|
* The timestamp with the new format "hh:mm", or
|
|
|
|
* the original input parameter, if it does not have
|
|
|
|
* the correct format
|
2011-05-30 18:24:39 +02:00
|
|
|
*/
|
2011-08-16 21:04:41 +02:00
|
|
|
public static function removeSecondsFromTime($p_dateTime)
|
2011-05-30 18:24:39 +02:00
|
|
|
{
|
2022-03-14 11:15:04 +01:00
|
|
|
// Format is in hh:mm:ss. We want hh:mm
|
2021-10-11 16:10:47 +02:00
|
|
|
$timeExplode = explode(':', $p_dateTime);
|
|
|
|
|
|
|
|
if (count($timeExplode) == 3) {
|
|
|
|
return $timeExplode[0] . ':' . $timeExplode[1];
|
|
|
|
}
|
2011-05-30 18:24:39 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
return $p_dateTime;
|
2011-05-30 18:24:39 +02:00
|
|
|
}
|
|
|
|
|
2011-11-15 16:32:07 +01:00
|
|
|
/* Given a track length in the format HH:MM:SS.mm, we want to
|
2011-06-17 00:59:15 +02:00
|
|
|
* convert this to seconds. This is useful for Liquidsoap which
|
2011-11-15 16:32:07 +01:00
|
|
|
* likes input parameters give in seconds.
|
|
|
|
* For example, 00:06:31.444, should be converted to 391.444 seconds
|
2011-06-17 00:59:15 +02:00
|
|
|
* @param int $p_time
|
|
|
|
* The time interval in format HH:MM:SS.mm we wish to
|
|
|
|
* convert to seconds.
|
2011-12-19 21:45:44 +01:00
|
|
|
* @return float
|
2011-06-17 00:59:15 +02:00
|
|
|
* The input parameter converted to seconds.
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function calculateLengthInSeconds($p_time)
|
|
|
|
{
|
|
|
|
if (2 !== substr_count($p_time, ':')) {
|
2013-03-05 00:52:51 +01:00
|
|
|
return false;
|
2011-06-17 00:59:15 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
if (1 === substr_count($p_time, '.')) {
|
Feature: Support php7.4 (#1354)
* Run CI tests against php 7.4
* Sort composer dependencies
* Remove unused Aws S3 php library
* Pin simplepie dependency to ^1.5
* Pin getid3 dependency to ^1.9
* Pin composer semver to ^3.2
* Pin php-amqplib to ^2.12
* Drop sentry logging support
* Update composer dependencies
* Move propel regenerate to Makefile
* Regenerate propel files with v1.7.0
* Pin propel orm to ^1.7
* Regenerate propel files with v1.7.2
* fix: generator_version in airtime-conf-production.php
* Replace propel/propel1 with jooola/propel1
* Regenerate propel files with v1.7.3-dev
* Fix php7.4 compatibility
Using php-cs-fixer:
'@PhpCsFixer' => true,
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => false,
'yoda_style' => false,
'@PHP74Migration' => true,
'assign_null_coalescing_to_coalesce_equal' => false,
'ternary_to_null_coalescing' => false,
'heredoc_indentation' => false,
'@PHP74Migration:risky' => true,
'declare_strict_types' => false,
'void_return' => false,
'use_arrow_functions' => false,
* Fix pre-commit
2021-10-17 17:19:53 +02:00
|
|
|
[$hhmmss, $ms] = explode('.', $p_time);
|
2011-06-17 00:59:15 +02:00
|
|
|
} else {
|
|
|
|
$hhmmss = $p_time;
|
|
|
|
$ms = 0;
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
Feature: Support php7.4 (#1354)
* Run CI tests against php 7.4
* Sort composer dependencies
* Remove unused Aws S3 php library
* Pin simplepie dependency to ^1.5
* Pin getid3 dependency to ^1.9
* Pin composer semver to ^3.2
* Pin php-amqplib to ^2.12
* Drop sentry logging support
* Update composer dependencies
* Move propel regenerate to Makefile
* Regenerate propel files with v1.7.0
* Pin propel orm to ^1.7
* Regenerate propel files with v1.7.2
* fix: generator_version in airtime-conf-production.php
* Replace propel/propel1 with jooola/propel1
* Regenerate propel files with v1.7.3-dev
* Fix php7.4 compatibility
Using php-cs-fixer:
'@PhpCsFixer' => true,
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => false,
'yoda_style' => false,
'@PHP74Migration' => true,
'assign_null_coalescing_to_coalesce_equal' => false,
'ternary_to_null_coalescing' => false,
'heredoc_indentation' => false,
'@PHP74Migration:risky' => true,
'declare_strict_types' => false,
'void_return' => false,
'use_arrow_functions' => false,
* Fix pre-commit
2021-10-17 17:19:53 +02:00
|
|
|
[$hours, $minutes, $seconds] = explode(':', $hhmmss);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
$totalSeconds = ($hours * 3600 + $minutes * 60 + $seconds) . ".{$ms}";
|
|
|
|
|
2013-03-05 00:52:51 +01:00
|
|
|
return round($totalSeconds, 3);
|
2011-06-17 00:59:15 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-07-19 22:57:24 +02:00
|
|
|
/**
|
|
|
|
* returns true or false depending on input is wether in
|
2021-10-11 16:10:47 +02:00
|
|
|
* valid range of SQL date/time.
|
|
|
|
*
|
2012-07-19 22:57:24 +02:00
|
|
|
* @param string $p_datetime
|
2021-10-11 16:10:47 +02:00
|
|
|
* should be in format of '0000-00-00 00:00:00'
|
2012-07-19 22:57:24 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function checkDateTimeRangeForSQL($p_datetime)
|
|
|
|
{
|
2012-07-19 22:57:24 +02:00
|
|
|
$info = explode(' ', $p_datetime);
|
|
|
|
$dateInfo = explode('-', $info[0]);
|
2012-08-07 22:17:24 +02:00
|
|
|
if (isset($info[1])) {
|
|
|
|
$timeInfo = explode(':', $info[1]);
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
$retVal = [];
|
|
|
|
$retVal['success'] = true;
|
|
|
|
|
2012-07-19 22:57:24 +02:00
|
|
|
$year = $dateInfo[0];
|
|
|
|
$month = $dateInfo[1];
|
|
|
|
$day = $dateInfo[2];
|
|
|
|
// if year is < 1753 or > 9999 it's out of range
|
2012-08-07 22:17:24 +02:00
|
|
|
if ($year < 1753) {
|
|
|
|
$retVal['success'] = false;
|
2021-10-11 16:10:47 +02:00
|
|
|
$retVal['errMsg'] = sprintf(_('The year %s must be within the range of 1753 - 9999'), $year);
|
|
|
|
} elseif (!checkdate($month, $day, $year)) {
|
2012-08-07 22:17:24 +02:00
|
|
|
$retVal['success'] = false;
|
2021-10-11 16:10:47 +02:00
|
|
|
$retVal['errMsg'] = sprintf(_('%s-%s-%s is not a valid date'), $year, $month, $day);
|
2012-07-19 22:57:24 +02:00
|
|
|
} else {
|
|
|
|
// check time
|
2012-08-07 22:27:18 +02:00
|
|
|
if (isset($timeInfo)) {
|
2021-10-11 16:10:47 +02:00
|
|
|
if (isset($timeInfo[0]) && $timeInfo[0] != '') {
|
2012-08-07 22:27:18 +02:00
|
|
|
$hour = intval($timeInfo[0]);
|
|
|
|
} else {
|
|
|
|
$hour = -1;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
if (isset($timeInfo[1]) && $timeInfo[1] != '') {
|
2012-08-07 22:27:18 +02:00
|
|
|
$min = intval($timeInfo[1]);
|
|
|
|
} else {
|
|
|
|
$min = -1;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
if (isset($timeInfo[2]) && $timeInfo[2] != '') {
|
2012-08-07 22:27:18 +02:00
|
|
|
$sec = intval($timeInfo[2]);
|
|
|
|
} else {
|
|
|
|
$sec = -1;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
if (($hour < 0 || $hour > 23) || ($min < 0 || $min > 59) || ($sec < 0 || $sec > 59)) {
|
2012-08-07 22:27:18 +02:00
|
|
|
$retVal['success'] = false;
|
2021-10-11 16:10:47 +02:00
|
|
|
$retVal['errMsg'] = sprintf(_('%s:%s:%s is not a valid time'), $timeInfo[0], $timeInfo[1], $timeInfo[2]);
|
2012-08-07 22:27:18 +02:00
|
|
|
}
|
2012-07-19 22:57:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-08-07 22:17:24 +02:00
|
|
|
return $retVal;
|
2012-07-19 22:57:24 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 22:45:05 +01:00
|
|
|
/*
|
|
|
|
* @param $datetime string Y-m-d H:i:s in UTC timezone
|
2021-10-11 16:10:47 +02:00
|
|
|
*
|
2013-12-10 22:45:05 +01:00
|
|
|
* @return string in $format default Y-m-d H:i:s in station timezone
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function UTCStringToStationTimezoneString($datetime, $format = DEFAULT_TIMESTAMP_FORMAT)
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$stationTimezone = new DateTimeZone(Application_Model_Preference::GetDefaultTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$utcTimezone = new DateTimeZone('UTC');
|
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
$d = new DateTime($datetime, $utcTimezone);
|
|
|
|
$d->setTimezone($stationTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $d->format($format);
|
2013-12-10 22:45:05 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 22:45:05 +01:00
|
|
|
/*
|
|
|
|
* @param $datetime string Y-m-d H:i:s in UTC timezone
|
|
|
|
*
|
|
|
|
* @return string Y-m-d H:i:s in user's timezone
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function UTCStringToUserTimezoneString($datetime, $format = DEFAULT_TIMESTAMP_FORMAT)
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$utcTimezone = new DateTimeZone('UTC');
|
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
$d = new DateTime($datetime, $utcTimezone);
|
|
|
|
$d->setTimezone($userTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $d->format($format);
|
2013-12-10 22:45:05 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-11 21:20:19 +01:00
|
|
|
/*
|
|
|
|
* @param $datetime string Y-m-d H:i:s in user timezone
|
|
|
|
*
|
|
|
|
* @return string Y-m-d H:i:s in UTC timezone
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function UserTimezoneStringToUTCString($datetime, $format = DEFAULT_TIMESTAMP_FORMAT)
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone());
|
2021-10-11 16:10:47 +02:00
|
|
|
$utcTimezone = new DateTimeZone('UTC');
|
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
$d = new DateTime($datetime, $userTimezone);
|
|
|
|
$d->setTimezone($utcTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $d->format($format);
|
2013-12-11 21:20:19 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2013-12-10 22:45:05 +01:00
|
|
|
/**
|
|
|
|
* Convert the columns given in the array $columnsToConvert in the
|
|
|
|
* database result $rows to local timezone.
|
|
|
|
*
|
|
|
|
* @param array $rows arrays of arrays containing database query result
|
|
|
|
* @param array $columnsToConvert array of column names to convert
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param string (station|user) convert to either station or user timezone
|
|
|
|
* @param mixed $domain
|
2013-12-10 22:45:05 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function convertTimestamps(&$rows, $columnsToConvert, $domain = 'station')
|
2013-12-10 22:45:05 +01:00
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
if (!is_array($rows)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
$converter = 'UTCStringTo' . ucfirst($domain) . 'TimezoneString';
|
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
foreach ($rows as &$row) {
|
|
|
|
foreach ($columnsToConvert as $column) {
|
|
|
|
$row[$column] = self::$converter($row[$column]);
|
|
|
|
}
|
|
|
|
}
|
2013-12-10 22:45:05 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
/**
|
|
|
|
* Convert the columns given in the array $columnsToConvert in the
|
|
|
|
* database result $rows to local timezone.
|
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param array $rows arrays of arrays containing database query result
|
|
|
|
* @param array $columnsToConvert array of column names to convert
|
|
|
|
* @param string $timezone convert to the given timezone
|
2014-10-24 21:11:27 +02:00
|
|
|
* @param string $format time format to convert to
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function convertTimestampsToTimezone(&$rows, $columnsToConvert, $timezone, $format = DEFAULT_TIMESTAMP_FORMAT)
|
2014-10-24 21:11:27 +02:00
|
|
|
{
|
|
|
|
$timezone = strtolower($timezone);
|
|
|
|
// Check that the timezone is valid and rows is an array
|
|
|
|
if (!is_array($rows)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
foreach ($rows as &$row) {
|
|
|
|
if (is_array($row)) {
|
|
|
|
foreach ($columnsToConvert as $column) {
|
|
|
|
if (array_key_exists($column, $row)) {
|
|
|
|
$newTimezone = new DateTimeZone($timezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
$utcTimezone = new DateTimeZone('UTC');
|
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
$d = new DateTime($row[$column], $utcTimezone);
|
|
|
|
$d->setTimezone($newTimezone);
|
|
|
|
$row[$column] = $d->format($format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::convertTimestampsToTimezone($row, $columnsToConvert, $timezone, $format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Return the end date time in the given timezone.
|
|
|
|
*
|
|
|
|
* @param mixed $timezoneString
|
|
|
|
* @param mixed $days
|
2014-10-24 21:11:27 +02:00
|
|
|
*
|
|
|
|
* @return DateTime
|
|
|
|
*/
|
|
|
|
public static function getEndDateTime($timezoneString, $days)
|
|
|
|
{
|
|
|
|
$timezone = new DateTimeZone($timezoneString);
|
2021-10-11 16:10:47 +02:00
|
|
|
$now = new DateTime('now', $timezone);
|
|
|
|
|
|
|
|
$now->add(new DateInterval('P' . $days . 'D'));
|
2014-10-24 21:11:27 +02:00
|
|
|
$now->setTime(0, 0, 0);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $now;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
/**
|
|
|
|
* Return a formatted string representing the
|
2021-10-11 16:10:47 +02:00
|
|
|
* given datetime in the given timezone.
|
2014-10-24 21:11:27 +02:00
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param unknown $datetime the time to convert
|
|
|
|
* @param unknown $timezone the timezone to convert to
|
|
|
|
* @param string $format the formatted string
|
2014-10-24 21:11:27 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function UTCStringToTimezoneString($datetime, $timezone, $format = DEFAULT_TIMESTAMP_FORMAT)
|
|
|
|
{
|
|
|
|
$d = new DateTime($datetime, new DateTimeZone('UTC'));
|
2014-10-24 21:11:27 +02:00
|
|
|
$timezone = strtolower($timezone);
|
|
|
|
$newTimezone = new DateTimeZone($timezone);
|
|
|
|
$d->setTimezone($newTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
return $d->format($format);
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-24 21:11:27 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Return the timezone offset in seconds for the given timezone.
|
2014-10-24 21:11:27 +02:00
|
|
|
*
|
|
|
|
* @param unknown $userDefinedTimezone the timezone used to determine the offset
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function getTimezoneOffset($userDefinedTimezone)
|
|
|
|
{
|
2014-10-24 21:11:27 +02:00
|
|
|
$now = new DateTimeZone($userDefinedTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
$d = new DateTime('now', $now);
|
|
|
|
|
|
|
|
return $d->format('Z');
|
2014-10-24 21:11:27 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-09-17 22:46:51 +02:00
|
|
|
/**
|
|
|
|
* This function is used for calculations! Don't modify for display purposes!
|
|
|
|
*
|
|
|
|
* Convert playlist time value to float seconds
|
|
|
|
*
|
|
|
|
* @param string $plt
|
2021-10-11 16:10:47 +02:00
|
|
|
* playlist interval value (HH:mm:ss.dddddd)
|
|
|
|
*
|
2012-09-17 22:46:51 +02:00
|
|
|
* @return int
|
2021-10-11 16:10:47 +02:00
|
|
|
* seconds
|
2012-09-17 22:46:51 +02:00
|
|
|
*/
|
|
|
|
public static function playlistTimeToSeconds($plt)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
$arr = preg_split('/:/', $plt);
|
2012-09-17 22:46:51 +02:00
|
|
|
if (isset($arr[2])) {
|
2021-10-11 16:10:47 +02:00
|
|
|
return (intval($arr[0]) * 60 + intval($arr[1])) * 60 + floatval($arr[2]);
|
2012-09-17 22:46:51 +02:00
|
|
|
}
|
|
|
|
if (isset($arr[1])) {
|
2021-10-11 16:10:47 +02:00
|
|
|
return intval($arr[0]) * 60 + floatval($arr[1]);
|
2012-09-17 22:46:51 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-09-17 22:46:51 +02:00
|
|
|
return floatval($arr[0]);
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-09-17 22:46:51 +02:00
|
|
|
/**
|
|
|
|
* This function is used for calculations! Don't modify for display purposes!
|
|
|
|
*
|
|
|
|
* Convert float seconds value to playlist time format
|
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param mixed $p_seconds
|
|
|
|
*
|
2012-09-17 22:46:51 +02:00
|
|
|
* @return string
|
2021-10-11 16:10:47 +02:00
|
|
|
* interval in playlist time format (HH:mm:ss.d)
|
2012-09-17 22:46:51 +02:00
|
|
|
*/
|
|
|
|
public static function secondsToPlaylistTime($p_seconds)
|
|
|
|
{
|
|
|
|
$info = explode('.', $p_seconds);
|
|
|
|
$seconds = $info[0];
|
|
|
|
if (!isset($info[1])) {
|
|
|
|
$milliStr = 0;
|
|
|
|
} else {
|
|
|
|
$milliStr = $info[1];
|
|
|
|
}
|
|
|
|
$hours = floor($seconds / 3600);
|
|
|
|
$seconds -= $hours * 3600;
|
|
|
|
$minutes = floor($seconds / 60);
|
|
|
|
$seconds -= $minutes * 60;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
return sprintf('%02d:%02d:%02d.%s', $hours, $minutes, $seconds, $milliStr);
|
2012-07-25 18:44:37 +02:00
|
|
|
}
|
2014-11-20 17:22:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns date fields from give start and end teimstamp strings
|
2021-10-11 16:10:47 +02:00
|
|
|
* if no start or end parameter is passed start will be set to 1
|
|
|
|
* in the past and end to now.
|
2014-11-20 17:22:53 +01:00
|
|
|
*
|
2023-12-27 13:40:15 +01:00
|
|
|
* @param string startTimestamp Y-m-d H:i:s
|
|
|
|
* @param string endTImestamp Y-m-d H:i:s
|
|
|
|
* @param string timezone (ex UTC) of the start and end parameters
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param mixed $startTimestamp
|
|
|
|
* @param mixed $endTimestamp
|
|
|
|
* @param mixed $timezone
|
|
|
|
*
|
2014-11-20 17:22:53 +01:00
|
|
|
* @return array (start DateTime, end DateTime) in UTC timezone
|
|
|
|
*/
|
|
|
|
public static function getStartEnd($startTimestamp, $endTimestamp, $timezone)
|
|
|
|
{
|
|
|
|
$prefTimezone = Application_Model_Preference::GetTimezone();
|
2021-10-11 16:10:47 +02:00
|
|
|
$utcTimezone = new DateTimeZone('UTC');
|
|
|
|
$utcNow = new DateTime('now', $utcTimezone);
|
2014-11-20 17:22:53 +01:00
|
|
|
|
|
|
|
if (empty($timezone)) {
|
|
|
|
$userTimezone = new DateTimeZone($prefTimezone);
|
|
|
|
} else {
|
|
|
|
$userTimezone = new DateTimeZone($timezone);
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to 1 day
|
|
|
|
if (empty($startTimestamp) || empty($endTimestamp)) {
|
|
|
|
$startsDT = clone $utcNow;
|
2021-10-11 16:10:47 +02:00
|
|
|
$startsDT->sub(new DateInterval('P1D'));
|
2014-11-20 17:22:53 +01:00
|
|
|
$endsDT = clone $utcNow;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$startsDT = new DateTime($startTimestamp, $userTimezone);
|
|
|
|
$startsDT->setTimezone($utcTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-11-20 17:22:53 +01:00
|
|
|
$endsDT = new DateTime($endTimestamp, $userTimezone);
|
|
|
|
$endsDT->setTimezone($utcTimezone);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-11-20 17:22:53 +01:00
|
|
|
if ($startsDT > $endsDT) {
|
2021-10-11 16:10:47 +02:00
|
|
|
throw new Exception('start greater than end');
|
2014-11-20 17:22:53 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (Exception $e) {
|
2014-11-20 17:22:53 +01:00
|
|
|
Logging::info($e);
|
|
|
|
Logging::info($e->getMessage());
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-11-20 17:22:53 +01:00
|
|
|
$startsDT = clone $utcNow;
|
2021-10-11 16:10:47 +02:00
|
|
|
$startsDT->sub(new DateInterval('P1D'));
|
2014-11-20 17:22:53 +01:00
|
|
|
$endsDT = clone $utcNow;
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
return [$startsDT, $endsDT];
|
2014-11-20 17:22:53 +01:00
|
|
|
}
|
2011-02-05 00:22:15 +01:00
|
|
|
}
|