forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateAndTime.class.php
More file actions
347 lines (287 loc) · 11 KB
/
Copy pathDateAndTime.class.php
File metadata and controls
347 lines (287 loc) · 11 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
// +----------------------------------------------------------------------+
// | Anuko Time Tracker
// +----------------------------------------------------------------------+
// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
// +----------------------------------------------------------------------+
// | LIBERAL FREEWARE LICENSE: This source code document may be used
// | by anyone for any purpose, and freely redistributed alone or in
// | combination with other software, provided that the license is obeyed.
// |
// | There are only two ways to violate the license:
// |
// | 1. To redistribute this code in source form, with the copyright
// | notice or license removed or altered. (Distributing in compiled
// | forms without embedded copyright notices is permitted).
// |
// | 2. To redistribute modified versions of this code in *any* form
// | that bears insufficient indications that the modifications are
// | not the work of the original author(s).
// |
// | This license applies to this document only, not any other software
// | that it may be combined with.
// |
// +----------------------------------------------------------------------+
// | Contributors:
// | https://www.anuko.com/time_tracker/credits.htm
// +----------------------------------------------------------------------+
/**
* Parse a time/date generated with strftime().
*
* This function is the same as the original one defined by PHP (Linux/Unix only),
* but now you can use it on Windows too.
* Limitation : Only this format can be parsed %S, %M, %H, %d, %m, %Y
*
* @author Lionel SAURON
* @version 1.0
* @public
*
* @param $sDate(string) The string to parse (e.g. returned from strftime()).
* @param $sFormat(string) The format used in date (e.g. the same as used in strftime()).
* @return (array) Returns an array with the <code>$sDate</code> parsed, or <code>false</code> on error.
*/
function my_strptime($sDate, $sFormat)
{
$aResult = array
(
'tm_sec' => 0,
'tm_min' => 0,
'tm_hour' => 0,
'tm_mday' => 1,
'tm_mon' => 0,
'tm_year' => 0,
'tm_wday' => 0,
'tm_yday' => 0,
'unparsed' => $sDate,
);
while($sFormat != "")
{
// ===== Search a %x element, Check the static string before the %x =====
$nIdxFound = strpos($sFormat, '%');
if($nIdxFound === false)
{
// There is no more format. Check the last static string.
$aResult['unparsed'] = ($sFormat == $sDate) ? "" : $sDate;
break;
}
$sFormatBefore = mb_substr($sFormat, 0, $nIdxFound);
$sDateBefore = mb_substr($sDate, 0, $nIdxFound);
if($sFormatBefore != $sDateBefore) break;
// ===== Read the value of the %x found =====
$sFormat = mb_substr($sFormat, $nIdxFound);
$sDate = mb_substr($sDate, $nIdxFound);
$aResult['unparsed'] = $sDate;
$sFormatCurrent = mb_substr($sFormat, 0, 2);
$sFormatAfter = mb_substr($sFormat, 2);
$nValue = -1;
$sDateAfter = "";
switch($sFormatCurrent)
{
case '%S': // Seconds after the minute (0-59)
sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
if(($nValue < 0) || ($nValue > 59)) return false;
$aResult['tm_sec'] = $nValue;
break;
// ----------
case '%M': // Minutes after the hour (0-59)
sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
if(($nValue < 0) || ($nValue > 59)) return false;
$aResult['tm_min'] = $nValue;
break;
// ----------
case '%H': // Hour since midnight (0-23)
sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
if(($nValue < 0) || ($nValue > 23)) return false;
$aResult['tm_hour'] = $nValue;
break;
// ----------
case '%d': // Day of the month (1-31)
sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
if(($nValue < 1) || ($nValue > 31)) return false;
$aResult['tm_mday'] = $nValue;
break;
// ----------
case '%m': // Months since January (0-11)
sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
if(($nValue < 1) || ($nValue > 12)) return false;
$aResult['tm_mon'] = ($nValue - 1);
break;
// ----------
case '%Y': // Years since 1900
sscanf($sDate, "%4d%[^\\n]", $nValue, $sDateAfter);
if($nValue < 1900) return false;
$aResult['tm_year'] = ($nValue - 1900);
break;
// ----------
default:
//sscanf($sDate, "%s%[^\\n]", $skip, $sDateAfter);
preg_match('/^(.+)(\s|$)/uU', $sDate, $matches);
if (isset($matches[1])) {
$sDateAfter = mb_substr($sDate, mb_strlen($matches[1]));
} else {
$sDateAfter = '';
}
//break 2; // Break Switch and while
break;
}
// ===== Next please =====
$sFormat = $sFormatAfter;
$sDate = $sDateAfter;
$aResult['unparsed'] = $sDate;
} // END while($sFormat != "")
// ===== Create the other value of the result array =====
$nParsedDateTimestamp = mktime($aResult['tm_hour'], $aResult['tm_min'], $aResult['tm_sec'],
$aResult['tm_mon'] + 1, $aResult['tm_mday'], $aResult['tm_year'] + 1900);
// Before PHP 5.1 return -1 when error
if(($nParsedDateTimestamp === false)
||($nParsedDateTimestamp === -1)) return false;
$aResult['tm_wday'] = (int) strftime("%w", $nParsedDateTimestamp); // Days since Sunday (0-6)
$aResult['tm_yday'] = (strftime("%j", $nParsedDateTimestamp) - 1); // Days since January 1 (0-365)
return $aResult;
} // END of function
class DateAndTime {
var $mHour = 0;
var $mMinute = 0;
var $mSecond = 0;
var $mMonth;
var $mDay; // day of week
var $mDate; // day of month
var $mYear;
var $mIntrFormat = "%d.%m.%Y %H:%M:%S"; //29.02.2004 16:21:42 internal format date
var $mLocalFormat;
var $mParseResult = 0;
var $mAutoComplete = true;
/**
* Constructor
*
* @param String $format
* @param String $strfDateTime
* @return DateAndTime
*/
function __construct($format="",$strfDateTime="") {
$this->mLocalFormat = ($format ? $format : $this->mIntrFormat);
$d = ($strfDateTime ? $strfDateTime : $this->do_strftime($this->mLocalFormat));
$this->parseVal($d);
}
function setFormat($format) {
$this->mLocalFormat = $format;
}
function getFormat() {
return $this->mLocalFormat;
}
//01 to 31
function getDate() { return $this->mDate; }
//0 (for Sunday) through 6 (for Saturday)
function getDay() { return $this->mDay; }
//01 through 12
function getMonth() { return $this->mMonth; }
//1999 or 2003
function getYear() { return $this->mYear; }
function setDate($value) { $this->mDate = $value; }
function setMonth($value) { $this->mMonth = $value; }
function setYear($value) { $this->mYear = $value; }
function setTimestamp($ts) {
$this->mDate = date("d",$ts);
$this->mDay = date("w",$ts);
$this->mMonth = date("m",$ts);
$this->mYear = date("Y",$ts);
$this->mHour = date("H",$ts);
$this->mMinute = date("i",$ts);
$this->mSecond = date("s",$ts);
}
/**
* Return UNIX timestamp
*/
function getTimestamp() {
return @mktime($this->mHour, $this->mMinute, $this->mSecond, $this->mMonth, $this->mDate, $this->mYear);
}
function compare($datetime) {
$ts1 = $this->getTimestamp();
$ts2 = $datetime->getTimestamp();
if ($ts1<$ts2) return -1;
if ($ts1==$ts2) return 0;
if ($ts1>$ts2) return 1;
}
function toString($format="") {
if ($this->mParseResult==0) {
if ($format) {
return $this->do_strftime($format, $this->getTimestamp());
} else {
return $this->do_strftime($this->mLocalFormat, $this->getTimestamp());
}
} else {
if ($format) {
return $this->do_strftime($format);
} else {
return $this->do_strftime($this->mLocalFormat);
}
}
}
function parseVal($szDate, $format="") {
$useformat = ($format ? $format : $this->mLocalFormat);
$res = my_strptime($szDate, $useformat);
if ($res !== false) {
$this->mDate = $res['tm_mday'];
$this->mDay = $res['tm_wday'];
$this->mMonth = $res['tm_mon'] + 1; // tm_mon - Months since January (0-11)
$this->mYear = 1900 + $res['tm_year']; // tm_year - Years since 1900
$this->mHour = $res['tm_hour'];
$this->mMinute = $res['tm_min'];
$this->mSecond = $res['tm_sec'];
$this->mParseResult = 0;
} elseif ($this->mAutoComplete) {
$this->setTimestamp(time());
$this->mParseResult = 1;
}
}
function isError() {
if ($this->mParseResult != 0) return true;
return false;
}
function before(/*DateAndTime*/ $obj) {
if ($this->getTimestamp() < $obj->getTimestamp()) return true;
return false;
}
function after(/*DateAndTime*/ $obj) {
if ($this->getTimestamp() > $obj->getTimestamp()) return true;
return false;
}
function equals(/*DateAndTime*/ $obj) {
if ($this->getTimestamp() == $obj->getTimestamp()) return true;
return false;
}
function decDay(/*int*/$days=1) {
$this->setTimestamp(@mktime($this->mHour, $this->mMinute, $this->mSecond, $this->mMonth, $this->mDate - $days, $this->mYear));
}
function incDay(/*int*/$days=1) {
$this->setTimestamp(@mktime($this->mHour, $this->mMinute, $this->mSecond, $this->mMonth, $this->mDate + $days, $this->mYear));
}
/**
* @param $format string Datetime format string
* @return string Preprocessed string with all locale-depended format
* characters replaced by localized i18n strings.
*/
function preprocessFormatString($format) {
global $i18n;
// replace locale-dependent strings
$format = str_replace('%a', mb_substr($i18n->getWeekDayName($this->mDay), 0, 3, 'utf-8'), $format);
$format = str_replace('%A', $i18n->getWeekDayName($this->mDay), $format);
$abbrev_month = mb_substr($i18n->monthNames[$this->mMonth], 0, 3, 'utf-8');
$format = str_replace('%b', $abbrev_month, $format);
$format = str_replace('%h', $abbrev_month, $format);
$format = str_replace('%z', date('O'), $format);
$format = str_replace('%Z', date('O'), $format); // format as 'O' for consistency with JS strftime
if (strpos($format, '%c') !== false) {
$format = str_replace('%c', $this->preprocessFormatString('%a %d %b %Y %T %Z'), $format);
}
return $format;
}
function do_strftime($format, $timestamp = null)
{
if (!is_null($timestamp)) {
return strftime($this->preprocessFormatString($format), $timestamp);
} else {
return strftime($this->preprocessFormatString($format));
}
}
}