source: pro-violet-viettel/sourcecode/application/helpers/lockfile_helper.php @ 737

Last change on this file since 737 was 632, checked in by dungnv, 10 years ago
File size: 5.7 KB
Line 
1<?php
2
3if ( ! function_exists('createLockFile'))
4{
5        function createLockFile ($lockFilePrefix, $maxReach, $timeToUnlock, $lockFilePath = '', $checkLockOnly = FALSE, $colSep = '|')
6        {
7                $currentTimeStamp = time();
8                $filePath = $lockFilePath.$lockFilePrefix.'_lock';
9                $fileExisted = file_exists($filePath);
10                $content = '';
11                $lastTimeLock = '';
12                $lockCount = 0;
13                $isLocked = 0;
14                $ready = 0;
15       
16                if ($fileExisted) {
17                        $content = file_get_contents($filePath);
18                }
19       
20                if ($content != '') {
21                        list($lockCount, $lastTimeLock, $isLocked) = explode ($colSep, $content);
22                }
23       
24                $timeDiffMin = $lastTimeLock != '' ? dateDifference(date('Y-m-d H:i:s',$currentTimeStamp), date('Y-m-d H:i:s',$lastTimeLock), '%i') : 0;
25
26                if ($lockCount < $maxReach && $timeDiffMin < $timeToUnlock && !$checkLockOnly) {
27                        $lockCount ++;
28                        $isLocked = $lockCount < $maxReach ? 0:1;
29                        $ready = 1;
30                }
31                else if ($timeDiffMin > $timeToUnlock) {
32                        $lockCount = 1;
33                        $isLocked = 0;
34                        $ready = 1;
35                }
36       
37                $lockTime = ($isLocked == 1) ? $lastTimeLock : $currentTimeStamp;
38                $content = $lockCount . $colSep . $lockTime . $colSep . $isLocked;
39               
40                if($ready == 1 && !$checkLockOnly) {
41                        $fh = fopen ($filePath,'w');
42                        fwrite ($fh, $content);
43                        fclose ($fh);
44                }
45       
46                return (int)$isLocked;
47        }
48}
49
50if ( ! function_exists('releaseLockFile'))
51{
52        function releaseLockFile ($lockFilePrefix, $lockFilePath = '')
53        {
54                $filePath = $lockFilePath.$lockFilePrefix.'_lock';
55                $fileExisted = file_exists($filePath);
56                if ($fileExisted) {
57                        unlink($filePath);
58                }
59        }
60}
61
62/**
63 * dateDifference
64 * Return difference between two dates
65 *
66 * @access      public
67 * @param       date    date_1
68 * @param       date    date_2
69 * @param       string  differenceFormat ()
70 * @return      string
71 *
72 * @example '%y Year %m Month %d Day %h Hours %i Minute %s Seconds', %y Year %m Month %d Day','%m Month %d Day'
73 * @example '%d Day %h Hours', '%d Day', '%h Hours %i Minute %s Seconds', '%i Minute %s Seconds','%h Hours', '%a Days'
74 */
75
76if ( ! function_exists('dateDifference'))
77{
78        function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
79        {
80                $datetime1 = date_create($date_1);
81                $datetime2 = date_create($date_2);
82
83                $diff = date_diff($datetime1, $datetime2);
84                //$diff = DateTime::diff($datetime1, $datetime2);
85                $total = 0;
86                switch( $differenceFormat){
87                        case "%y":
88                                $total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
89                        case "%m":
90                                $total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
91                                break;
92                        case "%a":
93                                $total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
94                                break;
95                        case "%h":
96                                $total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
97                                break;
98                        case "%i":
99                                $total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
100                                break;
101                        case "%s":
102                                $total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
103                                break;
104                }
105               
106                return (int)$total;
107                /* if( $diff->invert)
108                        return -1 * $total;
109                else    return $total; */
110        }
111}
112
113if(!function_exists('date_diff')) {
114  class DateInterval {
115    public $y;
116    public $m;
117    public $d;
118    public $h;
119    public $i;
120    public $s;
121    public $invert;
122    public $days;
123 
124    public function format($format) {
125      $format = str_replace('%R%y',
126        ($this->invert ? '-' : '+') . $this->y, $format);
127      $format = str_replace('%R%m',
128         ($this->invert ? '-' : '+') . $this->m, $format);
129      $format = str_replace('%R%d',
130         ($this->invert ? '-' : '+') . $this->d, $format);
131      $format = str_replace('%R%h',
132         ($this->invert ? '-' : '+') . $this->h, $format);
133      $format = str_replace('%R%i',
134         ($this->invert ? '-' : '+') . $this->i, $format);
135      $format = str_replace('%R%s',
136         ($this->invert ? '-' : '+') . $this->s, $format);
137 
138      $format = str_replace('%y', $this->y, $format);
139      $format = str_replace('%m', $this->m, $format);
140      $format = str_replace('%d', $this->d, $format);
141      $format = str_replace('%h', $this->h, $format);
142      $format = str_replace('%i', $this->i, $format);
143      $format = str_replace('%s', $this->s, $format);
144 
145      return $format;
146    }
147  }
148 
149  function date_diff(DateTime $date1, DateTime $date2) {
150 
151    $diff = new DateInterval();
152 
153    if($date1 > $date2) {
154      $tmp = $date1;
155      $date1 = $date2;
156      $date2 = $tmp;
157      $diff->invert = 1;
158    } else {
159      $diff->invert = 0;
160    }
161 
162    $diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y'));
163    $diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n'));
164    if($diff->m < 0) {
165      $diff->y -= 1;
166      $diff->m = $diff->m + 12;
167    }
168    $diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j'));
169    if($diff->d < 0) {
170      $diff->m -= 1;
171      $diff->d = $diff->d + ((int) $date1->format('t'));
172    }
173    $diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G'));
174    if($diff->h < 0) {
175      $diff->d -= 1;
176      $diff->h = $diff->h + 24;
177    }
178    $diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i'));
179    if($diff->i < 0) {
180      $diff->h -= 1;
181      $diff->i = $diff->i + 60;
182    }
183    $diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s'));
184    if($diff->s < 0) {
185      $diff->i -= 1;
186      $diff->s = $diff->s + 60;
187    }
188 
189    $start_ts   = $date1->format('U');
190    $end_ts   = $date2->format('U');
191    $days     = $end_ts - $start_ts;
192    $diff->days  = round($days / 86400);
193 
194    if (($diff->h > 0 || $diff->i > 0 || $diff->s > 0))
195      $diff->days += ((bool) $diff->invert)
196        ? 1
197        : -1;
198 
199    return $diff;
200 
201  }
202 
203}
Note: See TracBrowser for help on using the repository browser.