CommonClass.php 4.48 KB
<?php

/**
 * @class CommonClass
 * @brief Collection of some common function
 * @details
 */
class CommonClass
{
	/*
	 * @brief generate a random string
	*/
	public static function generateRandomString($length = 10) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randomString = '';
		for ($i = 0; $i < $length; $i++)
			$randomString .= $characters[rand(0, strlen($characters) - 1)];
		return $randomString;
	}

	/*
	 * @brief timestamp to DDTime conversion
	*/
	public static function timeStampToDDTime($timeStamp)
	{
		$mls = intval($timeStamp*1000)-intval($timeStamp)*1000;
		$y = date("Y",$timeStamp);
		$d = date("z",$timeStamp);
		if (strlen($d) == 0)
			$d = "000";
		else if (strlen($d) == 1)
			$d = "00".$d;
		else if (strlen($d) == 2)
			$d = "0".$d;
		$t = date("His",$timeStamp).str_pad($mls, 3, '0', STR_PAD_LEFT);
		return $y.$d.$t;
	}

	public static function isoToTimeStampWithMls($iso)
	{
		date_default_timezone_set('UTC');
		$iso = trim($iso);
		if (empty($iso)) {
			return 0.;
		}
		else if ($iso[strlen($iso)-1] == 'Z') {
			$iso = substr($iso, 0, -1);
		}
		$date = DateTime::createFromFormat('Y-m-d\TH:i:s.v', $iso);
		if ($date !== FALSE) {
			return round(floatval($date->format('v'))/1000. + $date->getTimestamp(),3);
		}
		$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $iso);
		if ($date !== FALSE) {
			return round(floatval($date->getTimestamp()),3);
		}
		$date = DateTime::createFromFormat('Y-m-d\TH:i', $iso);
		if ($date === FALSE) {
			return 0.;
		}
		return round(floatval($date->getTimestamp()),3);
	}
        
        public static function getDurationDDTime($strStop, $strStart)
	{
		$timeStamp = round(CommonClass::isoToTimeStampWithMls($strStop) - CommonClass::isoToTimeStampWithMls($strStart),3);
		return CommonClass::timeStampToDDTime($timeStamp);
	}
        
                    public static function getMsIntFromStrTime($strTime){
                          $ms = end(explode(".", $strTime));
                          if(strlen($ms) == 0 || strlen($ms) > 6 )
                              return 0;
                          if(intval($ms) >= 1000)
                              return (int) intval($ms)/1000;
                          return intval($ms);
                    }
                    
                    public static function getMsStrFromStrTime($strTime){
                         $intMs = CommonClass::getMsIntFromStrTime($strTime);
                         return str_pad($intMs,3, '0', STR_PAD_LEFT);
                    }
                    
        
                public static function strTimeToDDTime($strTime)
	{
                                        $timeStamp = strtotime($strTime);
                                        $ms = end(explode(".", $strTime));
                                        if(strlen($ms) ==0 || strlen($ms) > 6)
                                            $ms= "000";
		$y = date("Y",$timeStamp);
		$d = date("z",$timeStamp);
		if (strlen($d) == 0)
			$d = "000";
		else if (strlen($d) == 1)
			$d = "00".$d;
		else if (strlen($d) == 2)
			$d = "0".$d;
		$t = date("His",$timeStamp).$ms;
		return $y.$d.$t;
	}
        
	/*
	 * @brief DDTime to timestamp conversion
	*/
	public static function DDTimeToTimeStamp($DDTime)
	{
		date_default_timezone_set('UTC');
		$date = DateTime::createFromFormat('YzHisu', $DDTime);
		
		return strtotime($date->format("Y-m-d\TH:i:s.v"));
	}	
	
	/*
	 * @brief DDTime to ISO conversion
	*/
	public static function DDTimeToIso($DDTime)
	{
		$date = DateTime::createFromFormat('YzHisu', $DDTime);
		
		return $date->format("Y-m-d\TH:i:s.v");
	}
	
	/*
	 * @brief DDTime to ISO conversion
	*/
	public static function DDStartIntervalToStopIso($DDStart, $DDInterval)
	{		
		$startStamp = self::DDTimeToTimeStamp($DDStart);
		$intStamp = self::DDTimeToTimeStamp($DDInterval);
		
		return date("Y-m-d\TH:i:s.v", $startStamp + $intStamp);
	}
	
	public static function rrmdir($dir)
	{
		if (is_dir($dir)) 
		{
			$objects = scandir($dir);
			foreach ($objects as $object) 
			{ // Recursively delete a directory that is not empty and directorys in directory 
				if ($object != "." && $object != "..") 
				{  // If object isn't a directory recall recursively this function 
					if (filetype($dir."/".$object) == "dir")
						self::rrmdir($dir."/".$object);
					else
						unlink($dir."/".$object);
				}
			}
			reset($objects);
			rmdir($dir);
		}
	}
}

?>