ZP-685 Date/Time function changes. Released under the Affero GNU General...

ZP-685 Date/Time function changes. Released under the Affero GNU General Public License (AGPL) version 3.
parent f3bb3702
...@@ -1100,25 +1100,42 @@ class TimezoneUtil { ...@@ -1100,25 +1100,42 @@ class TimezoneUtil {
ZLog::Write(LOGLEVEL_DEBUG, "TimezoneUtil::GetFullTZ() for ". $phptimezone); ZLog::Write(LOGLEVEL_DEBUG, "TimezoneUtil::GetFullTZ() for ". $phptimezone);
$servertzname = self::guessTZNameFromPHPName($phptimezone); $servertzname = self::guessTZNameFromPHPName($phptimezone);
$offset = self::$tzonesoffsets[$servertzname]; return self::GetFullTZFromTZName($servertzname);
}
/**
* Returns a full timezone array
*
* @param string $tzname a TZID value
*
* @access public
* @return array
*/
static public function GetFullTZFromTZName($tzname) {
if (!array_key_exists($tzname, self::$tzonesoffsets)) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("TimezoneUtil::GetFullTZFromTZName('%s'): Is a PHP TimeZone, converting", $tzname));
$tzname = self::guessTZNameFromPHPName($tzname);
}
$offset = self::$tzonesoffsets[$tzname];
$tz = array( $tz = array(
"bias" => $offset[0], "bias" => $offset[0],
"tzname" => self::encodeTZName(self::getMSTZnameFromTZName($servertzname)), "tzname" => self::encodeTZName(self::getMSTZnameFromTZName($tzname)),
"dstendyear" => $offset[3], "dstendyear" => $offset[3],
"dstendmonth" => $offset[4], "dstendmonth" => $offset[4],
"dstendday" => $offset[6], "dstendday" => $offset[5],
"dstendweek" => $offset[5], "dstendweek" => $offset[6],
"dstendhour" => $offset[7], "dstendhour" => $offset[7],
"dstendminute" => $offset[8], "dstendminute" => $offset[8],
"dstendsecond" => $offset[9], "dstendsecond" => $offset[9],
"dstendmillis" => $offset[10], "dstendmillis" => $offset[10],
"stdbias" => $offset[1], "stdbias" => $offset[1],
"tznamedst" => self::encodeTZName(self::getMSTZnameFromTZName($servertzname)), "tznamedst" => self::encodeTZName(self::getMSTZnameFromTZName($tzname)),
"dststartyear" => $offset[11], "dststartyear" => $offset[11],
"dststartmonth" => $offset[12], "dststartmonth" => $offset[12],
"dststartday" => $offset[14], "dststartday" => $offset[13],
"dststartweek" => $offset[13], "dststartweek" => $offset[14],
"dststarthour" => $offset[15], "dststarthour" => $offset[15],
"dststartminute" => $offset[16], "dststartminute" => $offset[16],
"dststartsecond" => $offset[17], "dststartsecond" => $offset[17],
...@@ -1210,7 +1227,7 @@ class TimezoneUtil { ...@@ -1210,7 +1227,7 @@ class TimezoneUtil {
* @access public * @access public
* @return string * @return string
*/ */
static private function getMSTZnameFromTZName($name) { static public function getMSTZnameFromTZName($name) {
foreach (self::$mstzones as $mskey => $msdefs) { foreach (self::$mstzones as $mskey => $msdefs) {
if ($name == $msdefs[0]) if ($name == $msdefs[0])
return $msdefs[1]; return $msdefs[1];
......
...@@ -933,6 +933,62 @@ class Utils { ...@@ -933,6 +933,62 @@ class Utils {
} }
return substr($email, 0, $pos); return substr($email, 0, $pos);
} }
/**
* Generate date object from string and timezone.
*
* @param string $value
* @param string $timezone
*
* @access public
* @return int epoch
*/
public static function MakeUTCDate($value, $timezone = null) {
$tz = null;
if ($timezone) {
$tz = timezone_open($timezone);
}
if (!$tz) {
//If there is no timezone set, we use the default timezone
$tz = timezone_open(date_default_timezone_get());
}
//20110930T090000Z
$date = date_create_from_format('Ymd\THis\Z', $value, timezone_open("UTC"));
if (!$date) {
//20110930T090000
$date = date_create_from_format('Ymd\THis', $value, $tz);
}
if (!$date) {
//20110930 (Append T000000Z to the date, so it starts at midnight)
$date = date_create_from_format('Ymd\THis\Z', $value . "T000000Z", $tz);
}
return date_timestamp_get($date);
}
/**
* Generate a tzid from various formats
*
* @param str $timezone
*
* @access public
* @return timezone id
*/
public static function ParseTimezone($timezone) {
//(GMT+01.00) Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna
if (preg_match('/GMT(\\+|\\-)0(\d)/', $timezone, $matches)) {
return "Etc/GMT" . $matches[1] . $matches[2];
}
//(GMT+10.00) XXX / XXX / XXX / XXX
if (preg_match('/GMT(\\+|\\-)1(\d)/', $timezone, $matches)) {
return "Etc/GMT" . $matches[1] . "1" . $matches[2];
}
///inverse.ca/20101018_1/Europe/Amsterdam or /inverse.ca/20101018_1/America/Argentina/Buenos_Aires
if (preg_match('/\/[.[:word:]]+\/\w+\/(\w+)\/([\w\/]+)/', $timezone, $matches)) {
return $matches[1] . "/" . $matches[2];
}
return TimezoneUtil::getMSTZnameFromTZName(trim($timezone, '"'));
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment