Blame view

src/InputOutput/IHMImpl/Tools/CommonClass.php 4.48 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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)
	{
703f403f   Benjamin Renard   Fix some bugs wit...
26
		$mls = intval($timeStamp*1000)-intval($timeStamp)*1000;
22521f1c   Benjamin Renard   First commit
27
28
29
30
31
32
33
34
		$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;
703f403f   Benjamin Renard   Fix some bugs wit...
35
		$t = date("His",$timeStamp).str_pad($mls, 3, '0', STR_PAD_LEFT);
22521f1c   Benjamin Renard   First commit
36
37
		return $y.$d.$t;
	}
703f403f   Benjamin Renard   Fix some bugs wit...
38
39
40
41

	public static function isoToTimeStampWithMls($iso)
	{
		date_default_timezone_set('UTC');
6bea1021   Myriam Bouchemit   Parse partial ISO...
42
43
44
45
46
47
48
		$iso = trim($iso);
		if (empty($iso)) {
			return 0.;
		}
		else if ($iso[strlen($iso)-1] == 'Z') {
			$iso = substr($iso, 0, -1);
		}
703f403f   Benjamin Renard   Fix some bugs wit...
49
50
51
52
53
		$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);
6bea1021   Myriam Bouchemit   Parse partial ISO...
54
55
56
57
58
59
60
		if ($date !== FALSE) {
			return round(floatval($date->getTimestamp()),3);
		}
		$date = DateTime::createFromFormat('Y-m-d\TH:i', $iso);
		if ($date === FALSE) {
			return 0.;
		}
703f403f   Benjamin Renard   Fix some bugs wit...
61
62
		return round(floatval($date->getTimestamp()),3);
	}
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
63
        
703f403f   Benjamin Renard   Fix some bugs wit...
64
        public static function getDurationDDTime($strStop, $strStart)
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
65
	{
703f403f   Benjamin Renard   Fix some bugs wit...
66
67
		$timeStamp = round(CommonClass::isoToTimeStampWithMls($strStop) - CommonClass::isoToTimeStampWithMls($strStart),3);
		return CommonClass::timeStampToDDTime($timeStamp);
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	}
        
                    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;
	}
        
ffc5cb81   Elena.Budnik   temporary commit
103
104
105
106
107
	/*
	 * @brief DDTime to timestamp conversion
	*/
	public static function DDTimeToTimeStamp($DDTime)
	{
703f403f   Benjamin Renard   Fix some bugs wit...
108
		date_default_timezone_set('UTC');
ffc5cb81   Elena.Budnik   temporary commit
109
110
		$date = DateTime::createFromFormat('YzHisu', $DDTime);
		
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
111
		return strtotime($date->format("Y-m-d\TH:i:s.u"));
ffc5cb81   Elena.Budnik   temporary commit
112
113
114
115
116
117
118
119
120
	}	
	
	/*
	 * @brief DDTime to ISO conversion
	*/
	public static function DDTimeToIso($DDTime)
	{
		$date = DateTime::createFromFormat('YzHisu', $DDTime);
		
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
121
		return $date->format("Y-m-d\TH:i:s.u");
ffc5cb81   Elena.Budnik   temporary commit
122
123
124
125
126
127
128
129
130
131
	}
	
	/*
	 * @brief DDTime to ISO conversion
	*/
	public static function DDStartIntervalToStopIso($DDStart, $DDInterval)
	{		
		$startStamp = self::DDTimeToTimeStamp($DDStart);
		$intStamp = self::DDTimeToTimeStamp($DDInterval);
		
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
132
		return date("Y-m-d\TH:i:s.u", $startStamp + $intStamp);
ffc5cb81   Elena.Budnik   temporary commit
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	}
	
	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);
		}
	}
22521f1c   Benjamin Renard   First commit
154
155
}

703f403f   Benjamin Renard   Fix some bugs wit...
156
?>