<?php
/*+***********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 *************************************************************************************/
/**
* @author IT Sapiens
*/
class ITSapiensDateTimeUtils{
	/**
	* @param string $timeZone - timezone to convert (Ex. 'europe/berlin')
	* @return UTC offset of timezone (Ex. +01:00, -03:00)
	* @example covertTimeZoneStringToUTCOffset('europe/riga'); //+02:00
	* @static
	* 
	*/
	public static function covertTimeZoneStringToUTCOffset($timeZone){
		$timeZone = new DateTimeZone($timeZone);
		$offset   = $timeZone->getOffset(new DateTime);
		$negativeOffset = $offset < 0 ? TRUE : FALSE;
		$offset = abs($offset);
		$offset = $negativeOffset ? "-".gmdate("H:i", $offset) : "+".gmdate("H:i", $offset);
		return $offset;
	}

	/**
	* The above function assumes times in DB are stored in UTC, this is not true as they are stored in $default_timezone
	* @param string $timeZone - timezone to convert (Ex. 'europe/berlin')
	* @return Offset from $default_timezone (global config variable) of timezone (Ex. +01:00, -03:00)
	* @static
	* 
	*/
	public static function covertTimeZoneStringToDefaultOffset($timeZone){
		global $default_timezone;

		$timeZone = new DateTimeZone($timeZone);
		$offset   = $timeZone->getOffset(new DateTime);

		if(isset($default_timezone)){
			$defaultTZ = $default_timezone;
		} elseif(function_exists('date_default_timezone_get')) {
			$defaultTZ = date_default_timezone_get();
		} else {
			$defaultTZ = 'UTC';
		}
		
		$defaultTZ = new DateTimeZone($defaultTZ);
		$defaultOffset = $defaultTZ->getOffset(new DateTime);

		$offset = $offset - $defaultOffset;
		$negativeOffset = $offset < 0 ? TRUE : FALSE;
		$offset = abs($offset);
		$offset = $negativeOffset ? "-".gmdate("H:i", $offset) : "+".gmdate("H:i", $offset);
		return $offset;
	}

}
