HproseCommon.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | hprose |
  5. | |
  6. | Official WebSite: http://www.hprose.com/ |
  7. | http://www.hprose.net/ |
  8. | http://www.hprose.org/ |
  9. | |
  10. \**********************************************************/
  11. /**********************************************************\
  12. * *
  13. * HproseCommon.php *
  14. * *
  15. * hprose common library for php5. *
  16. * *
  17. * LastModified: Nov 15, 2013 *
  18. * Author: Ma Bingyao <andot@hprfc.com> *
  19. * *
  20. \**********************************************************/
  21. class HproseResultMode {
  22. const Normal = 0;
  23. const Serialized = 1;
  24. const Raw = 2;
  25. const RawWithEndTag = 3;
  26. }
  27. class HproseException extends Exception {}
  28. interface HproseFilter {
  29. function inputFilter($data);
  30. function outputFilter($data);
  31. }
  32. class HproseDate {
  33. public $year;
  34. public $month;
  35. public $day;
  36. public $utc = false;
  37. public function __construct() {
  38. $args_num = func_num_args();
  39. $args = func_get_args();
  40. switch ($args_num) {
  41. case 0:
  42. $time = getdate();
  43. $this->year = $time['year'];
  44. $this->month = $time['mon'];
  45. $this->day = $time['mday'];
  46. break;
  47. case 1:
  48. $time = false;
  49. if (is_int($args[0])) {
  50. $time = getdate($args[0]);
  51. }
  52. elseif (is_string($args[0])) {
  53. $time = getdate(strtotime($args[0]));
  54. }
  55. if (is_array($time)) {
  56. $this->year = $time['year'];
  57. $this->month = $time['mon'];
  58. $this->day = $time['mday'];
  59. }
  60. elseif ($args[0] instanceof HproseDate) {
  61. $this->year = $args[0]->year;
  62. $this->month = $args[0]->month;
  63. $this->day = $args[0]->day;
  64. }
  65. else {
  66. throw new HproseException('Unexpected arguments');
  67. }
  68. break;
  69. case 4:
  70. $this->utc = $args[3];
  71. case 3:
  72. if (!self::isValidDate($args[0], $args[1], $args[2])) {
  73. throw new HproseException('Unexpected arguments');
  74. }
  75. $this->year = $args[0];
  76. $this->month = $args[1];
  77. $this->day = $args[2];
  78. break;
  79. default:
  80. throw new HproseException('Unexpected arguments');
  81. }
  82. }
  83. public function addDays($days) {
  84. if (!is_int($days)) return false;
  85. $year = $this->year;
  86. if ($days == 0) return true;
  87. if ($days >= 146097 || $days <= -146097) {
  88. $remainder = $days % 146097;
  89. if ($remainder < 0) {
  90. $remainder += 146097;
  91. }
  92. $years = 400 * (int)(($days - $remainder) / 146097);
  93. $year += $years;
  94. if ($year < 1 || $year > 9999) return false;
  95. $days = $remainder;
  96. }
  97. if ($days >= 36524 || $days <= -36524) {
  98. $remainder = $days % 36524;
  99. if ($remainder < 0) {
  100. $remainder += 36524;
  101. }
  102. $years = 100 * (int)(($days - $remainder) / 36524);
  103. $year += $years;
  104. if ($year < 1 || $year > 9999) return false;
  105. $days = $remainder;
  106. }
  107. if ($days >= 1461 || $days <= -1461) {
  108. $remainder = $days % 1461;
  109. if ($remainder < 0) {
  110. $remainder += 1461;
  111. }
  112. $years = 4 * (int)(($days - $remainder) / 1461);
  113. $year += $years;
  114. if ($year < 1 || $year > 9999) return false;
  115. $days = $remainder;
  116. }
  117. $month = $this->month;
  118. while ($days >= 365) {
  119. if ($year >= 9999) return false;
  120. if ($month <= 2) {
  121. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  122. $days -= 366;
  123. }
  124. else {
  125. $days -= 365;
  126. }
  127. $year++;
  128. }
  129. else {
  130. $year++;
  131. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  132. $days -= 366;
  133. }
  134. else {
  135. $days -= 365;
  136. }
  137. }
  138. }
  139. while ($days < 0) {
  140. if ($year <= 1) return false;
  141. if ($month <= 2) {
  142. $year--;
  143. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  144. $days += 366;
  145. }
  146. else {
  147. $days += 365;
  148. }
  149. }
  150. else {
  151. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  152. $days += 366;
  153. }
  154. else {
  155. $days += 365;
  156. }
  157. $year--;
  158. }
  159. }
  160. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  161. $day = $this->day;
  162. while ($day + $days > $daysInMonth) {
  163. $days -= $daysInMonth - $day + 1;
  164. $month++;
  165. if ($month > 12) {
  166. if ($year >= 9999) return false;
  167. $year++;
  168. $month = 1;
  169. }
  170. $day = 1;
  171. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  172. }
  173. $day += $days;
  174. $this->year = $year;
  175. $this->month = $month;
  176. $this->day = $day;
  177. return true;
  178. }
  179. public function addMonths($months) {
  180. if (!is_int($months)) return false;
  181. if ($months == 0) return true;
  182. $month = $this->month + $months;
  183. $months = ($month - 1) % 12 + 1;
  184. if ($months < 1) {
  185. $months += 12;
  186. }
  187. $years = (int)(($month - $months) / 12);
  188. if ($this->addYears($years)) {
  189. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $months, $this->year);
  190. if ($this->day > $daysInMonth) {
  191. $months++;
  192. $this->day -= $daysInMonth;
  193. }
  194. $this->month = (int)$months;
  195. return true;
  196. }
  197. else {
  198. return false;
  199. }
  200. }
  201. public function addYears($years) {
  202. if (!is_int($years)) return false;
  203. if ($years == 0) return true;
  204. $year = $this->year + $years;
  205. if ($year < 1 || $year > 9999) return false;
  206. $this->year = $year;
  207. return true;
  208. }
  209. public function timestamp() {
  210. if ($this->utc) {
  211. return gmmktime(0, 0, 0, $this->month, $this->day, $this->year);
  212. }
  213. else {
  214. return mktime(0, 0, 0, $this->month, $this->day, $this->year);
  215. }
  216. }
  217. public function toString($fullformat = true) {
  218. $format = ($fullformat ? '%04d-%02d-%02d': '%04d%02d%02d');
  219. $str = sprintf($format, $this->year, $this->month, $this->day);
  220. if ($this->utc) {
  221. $str .= 'Z';
  222. }
  223. return $str;
  224. }
  225. public function __toString() {
  226. return $this->toString();
  227. }
  228. public static function isLeapYear($year) {
  229. return (($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false;
  230. }
  231. public static function daysInMonth($year, $month) {
  232. if (($month < 1) || ($month > 12)) {
  233. return false;
  234. }
  235. return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  236. }
  237. public static function isValidDate($year, $month, $day) {
  238. if (($year >= 1) && ($year <= 9999)) {
  239. return checkdate($month, $day, $year);
  240. }
  241. return false;
  242. }
  243. public function dayOfWeek() {
  244. $num = func_num_args();
  245. if ($num == 3) {
  246. $args = func_get_args();
  247. $y = $args[0];
  248. $m = $args[1];
  249. $d = $args[2];
  250. }
  251. else {
  252. $y = $this->year;
  253. $m = $this->month;
  254. $d = $this->day;
  255. }
  256. $d += $m < 3 ? $y-- : $y - 2;
  257. return ((int)(23 * $m / 9) + $d + 4 + (int)($y / 4) - (int)($y / 100) + (int)($y / 400)) % 7;
  258. }
  259. public function dayOfYear() {
  260. static $daysToMonth365 = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
  261. static $daysToMonth366 = array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366);
  262. $num = func_num_args();
  263. if ($num == 3) {
  264. $args = func_get_args();
  265. $y = $args[0];
  266. $m = $args[1];
  267. $d = $args[2];
  268. }
  269. else {
  270. $y = $this->year;
  271. $m = $this->month;
  272. $d = $this->day;
  273. }
  274. $days = self::isLeapYear($y) ? $daysToMonth365 : $daysToMonth366;
  275. return $days[$m - 1] + $d;
  276. }
  277. }
  278. class HproseTime {
  279. public $hour;
  280. public $minute;
  281. public $second;
  282. public $microsecond = 0;
  283. public $utc = false;
  284. public function __construct() {
  285. $args_num = func_num_args();
  286. $args = func_get_args();
  287. switch ($args_num) {
  288. case 0:
  289. $time = getdate();
  290. $timeofday = gettimeofday();
  291. $this->hour = $time['hours'];
  292. $this->minute = $time['minutes'];
  293. $this->second = $time['seconds'];
  294. $this->microsecond = $timeofday['usec'];
  295. break;
  296. case 1:
  297. $time = false;
  298. if (is_int($args[0])) {
  299. $time = getdate($args[0]);
  300. }
  301. elseif (is_string($args[0])) {
  302. $time = getdate(strtotime($args[0]));
  303. }
  304. if (is_array($time)) {
  305. $this->hour = $time['hours'];
  306. $this->minute = $time['minutes'];
  307. $this->second = $time['seconds'];
  308. }
  309. elseif ($args[0] instanceof HproseTime) {
  310. $this->hour = $args[0]->hour;
  311. $this->minute = $args[0]->minute;
  312. $this->second = $args[0]->second;
  313. $this->microsecond = $args[0]->microsecond;
  314. }
  315. else {
  316. throw new HproseException('Unexpected arguments');
  317. }
  318. break;
  319. case 5:
  320. $this->utc = $args[4];
  321. case 4:
  322. if (($args[3] < 0) || ($args[3] > 999999)) {
  323. throw new HproseException('Unexpected arguments');
  324. }
  325. $this->microsecond = $args[3];
  326. case 3:
  327. if (!self::isValidTime($args[0], $args[1], $args[2])) {
  328. throw new HproseException('Unexpected arguments');
  329. }
  330. $this->hour = $args[0];
  331. $this->minute = $args[1];
  332. $this->second = $args[2];
  333. break;
  334. default:
  335. throw new HproseException('Unexpected arguments');
  336. }
  337. }
  338. public function timestamp() {
  339. if ($this->utc) {
  340. return gmmktime($this->hour, $this->minute, $this->second) +
  341. ($this->microsecond / 1000000);
  342. }
  343. else {
  344. return mktime($this->hour, $this->minute, $this->second) +
  345. ($this->microsecond / 1000000);
  346. }
  347. }
  348. public function toString($fullformat = true) {
  349. if ($this->microsecond == 0) {
  350. $format = ($fullformat ? '%02d:%02d:%02d': '%02d%02d%02d');
  351. $str = sprintf($format, $this->hour, $this->minute, $this->second);
  352. }
  353. if ($this->microsecond % 1000 == 0) {
  354. $format = ($fullformat ? '%02d:%02d:%02d.%03d': '%02d%02d%02d.%03d');
  355. $str = sprintf($format, $this->hour, $this->minute, $this->second, (int)($this->microsecond / 1000));
  356. }
  357. else {
  358. $format = ($fullformat ? '%02d:%02d:%02d.%06d': '%02d%02d%02d.%06d');
  359. $str = sprintf($format, $this->hour, $this->minute, $this->second, $this->microsecond);
  360. }
  361. if ($this->utc) {
  362. $str .= 'Z';
  363. }
  364. return $str;
  365. }
  366. public function __toString() {
  367. return $this->toString();
  368. }
  369. public static function isValidTime($hour, $minute, $second, $microsecond = 0) {
  370. return !(($hour < 0) || ($hour > 23) ||
  371. ($minute < 0) || ($minute > 59) ||
  372. ($second < 0) || ($second > 59) ||
  373. ($microsecond < 0) || ($microsecond > 999999));
  374. }
  375. }
  376. class HproseDateTime extends HproseDate {
  377. public $hour;
  378. public $minute;
  379. public $second;
  380. public $microsecond = 0;
  381. public function __construct() {
  382. $args_num = func_num_args();
  383. $args = func_get_args();
  384. switch ($args_num) {
  385. case 0:
  386. $time = getdate();
  387. $timeofday = gettimeofday();
  388. $this->year = $time['year'];
  389. $this->month = $time['mon'];
  390. $this->day = $time['mday'];
  391. $this->hour = $time['hours'];
  392. $this->minute = $time['minutes'];
  393. $this->second = $time['seconds'];
  394. $this->microsecond = $timeofday['usec'];
  395. break;
  396. case 1:
  397. $time = false;
  398. if (is_int($args[0])) {
  399. $time = getdate($args[0]);
  400. }
  401. elseif (is_string($args[0])) {
  402. $time = getdate(strtotime($args[0]));
  403. }
  404. if (is_array($time)) {
  405. $this->year = $time['year'];
  406. $this->month = $time['mon'];
  407. $this->day = $time['mday'];
  408. $this->hour = $time['hours'];
  409. $this->minute = $time['minutes'];
  410. $this->second = $time['seconds'];
  411. }
  412. elseif ($args[0] instanceof HproseDate) {
  413. $this->year = $args[0]->year;
  414. $this->month = $args[0]->month;
  415. $this->day = $args[0]->day;
  416. $this->hour = 0;
  417. $this->minute = 0;
  418. $this->second = 0;
  419. }
  420. elseif ($args[0] instanceof HproseTime) {
  421. $this->year = 1970;
  422. $this->month = 1;
  423. $this->day = 1;
  424. $this->hour = $args[0]->hour;
  425. $this->minute = $args[0]->minute;
  426. $this->second = $args[0]->second;
  427. $this->microsecond = $args[0]->microsecond;
  428. }
  429. elseif ($args[0] instanceof HproseDateTime) {
  430. $this->year = $args[0]->year;
  431. $this->month = $args[0]->month;
  432. $this->day = $args[0]->day;
  433. $this->hour = $args[0]->hour;
  434. $this->minute = $args[0]->minute;
  435. $this->second = $args[0]->second;
  436. $this->microsecond = $args[0]->microsecond;
  437. }
  438. else {
  439. throw new HproseException('Unexpected arguments');
  440. }
  441. break;
  442. case 2:
  443. if (($args[0] instanceof HproseDate) && ($args[1] instanceof HproseTime)) {
  444. $this->year = $args[0]->year;
  445. $this->month = $args[0]->month;
  446. $this->day = $args[0]->day;
  447. $this->hour = $args[1]->hour;
  448. $this->minute = $args[1]->minute;
  449. $this->second = $args[1]->second;
  450. $this->microsecond = $args[1]->microsecond;
  451. }
  452. else {
  453. throw new HproseException('Unexpected arguments');
  454. }
  455. break;
  456. case 3:
  457. if (!self::isValidDate($args[0], $args[1], $args[2])) {
  458. throw new HproseException('Unexpected arguments');
  459. }
  460. $this->year = $args[0];
  461. $this->month = $args[1];
  462. $this->day = $args[2];
  463. $this->hour = 0;
  464. $this->minute = 0;
  465. $this->second = 0;
  466. break;
  467. case 8:
  468. $this->utc = $args[7];
  469. case 7:
  470. if (($args[6] < 0) || ($args[6] > 999999)) {
  471. throw new HproseException('Unexpected arguments');
  472. }
  473. $this->microsecond = $args[6];
  474. case 6:
  475. if (!self::isValidDate($args[0], $args[1], $args[2])) {
  476. throw new HproseException('Unexpected arguments');
  477. }
  478. if (!self::isValidTime($args[3], $args[4], $args[5])) {
  479. throw new HproseException('Unexpected arguments');
  480. }
  481. $this->year = $args[0];
  482. $this->month = $args[1];
  483. $this->day = $args[2];
  484. $this->hour = $args[3];
  485. $this->minute = $args[4];
  486. $this->second = $args[5];
  487. break;
  488. default:
  489. throw new HproseException('Unexpected arguments');
  490. }
  491. }
  492. public function addMicroseconds($microseconds) {
  493. if (!is_int($microseconds)) return false;
  494. if ($microseconds == 0) return true;
  495. $microsecond = $this->microsecond + $microseconds;
  496. $microseconds = $microsecond % 1000000;
  497. if ($microseconds < 0) {
  498. $microseconds += 1000000;
  499. }
  500. $seconds = (int)(($microsecond - $microseconds) / 1000000);
  501. if ($this->addSeconds($seconds)) {
  502. $this->microsecond = (int)$microseconds;
  503. return true;
  504. }
  505. else {
  506. return false;
  507. }
  508. }
  509. public function addSeconds($seconds) {
  510. if (!is_int($seconds)) return false;
  511. if ($seconds == 0) return true;
  512. $second = $this->second + $seconds;
  513. $seconds = $second % 60;
  514. if ($seconds < 0) {
  515. $seconds += 60;
  516. }
  517. $minutes = (int)(($second - $seconds) / 60);
  518. if ($this->addMinutes($minutes)) {
  519. $this->second = (int)$seconds;
  520. return true;
  521. }
  522. else {
  523. return false;
  524. }
  525. }
  526. public function addMinutes($minutes) {
  527. if (!is_int($minutes)) return false;
  528. if ($minutes == 0) return true;
  529. $minute = $this->minute + $minutes;
  530. $minutes = $minute % 60;
  531. if ($minutes < 0) {
  532. $minutes += 60;
  533. }
  534. $hours = (int)(($minute - $minutes) / 60);
  535. if ($this->addHours($hours)) {
  536. $this->minute = (int)$minutes;
  537. return true;
  538. }
  539. else {
  540. return false;
  541. }
  542. }
  543. public function addHours($hours) {
  544. if (!is_int($hours)) return false;
  545. if ($hours == 0) return true;
  546. $hour = $this->hour + $hours;
  547. $hours = $hour % 24;
  548. if ($hours < 0) {
  549. $hours += 24;
  550. }
  551. $days = (int)(($hour - $hours) / 24);
  552. if ($this->addDays($days)) {
  553. $this->hour = (int)$hours;
  554. return true;
  555. }
  556. else {
  557. return false;
  558. }
  559. }
  560. public function after($when) {
  561. if (!($when instanceof HproseDateTime)) {
  562. $when = new HproseDateTime($when);
  563. }
  564. if ($this->utc != $when->utc) return ($this->timestamp() > $when->timestamp());
  565. if ($this->year < $when->year) return false;
  566. if ($this->year > $when->year) return true;
  567. if ($this->month < $when->month) return false;
  568. if ($this->month > $when->month) return true;
  569. if ($this->day < $when->day) return false;
  570. if ($this->day > $when->day) return true;
  571. if ($this->hour < $when->hour) return false;
  572. if ($this->hour > $when->hour) return true;
  573. if ($this->minute < $when->minute) return false;
  574. if ($this->minute > $when->minute) return true;
  575. if ($this->second < $when->second) return false;
  576. if ($this->second > $when->second) return true;
  577. if ($this->microsecond < $when->microsecond) return false;
  578. if ($this->microsecond > $when->microsecond) return true;
  579. return false;
  580. }
  581. public function before($when) {
  582. if (!($when instanceof HproseDateTime)) {
  583. $when = new HproseDateTime($when);
  584. }
  585. if ($this->utc != $when->utc) return ($this->timestamp() < $when->timestamp());
  586. if ($this->year < $when->year) return true;
  587. if ($this->year > $when->year) return false;
  588. if ($this->month < $when->month) return true;
  589. if ($this->month > $when->month) return false;
  590. if ($this->day < $when->day) return true;
  591. if ($this->day > $when->day) return false;
  592. if ($this->hour < $when->hour) return true;
  593. if ($this->hour > $when->hour) return false;
  594. if ($this->minute < $when->minute) return true;
  595. if ($this->minute > $when->minute) return false;
  596. if ($this->second < $when->second) return true;
  597. if ($this->second > $when->second) return false;
  598. if ($this->microsecond < $when->microsecond) return true;
  599. if ($this->microsecond > $when->microsecond) return false;
  600. return false;
  601. }
  602. public function equals($when) {
  603. if (!($when instanceof HproseDateTime)) {
  604. $when = new HproseDateTime($when);
  605. }
  606. if ($this->utc != $when->utc) return ($this->timestamp() == $when->timestamp());
  607. return (($this->year == $when->year) &&
  608. ($this->month == $when->month) &&
  609. ($this->day == $when->day) &&
  610. ($this->hour == $when->hour) &&
  611. ($this->minute == $when->minute) &&
  612. ($this->second == $when->second) &&
  613. ($this->microsecond == $when->microsecond));
  614. }
  615. public function timestamp() {
  616. if ($this->utc) {
  617. return gmmktime($this->hour,
  618. $this->minute,
  619. $this->second,
  620. $this->month,
  621. $this->day,
  622. $this->year) +
  623. ($this->microsecond / 1000000);
  624. }
  625. else {
  626. return mktime($this->hour,
  627. $this->minute,
  628. $this->second,
  629. $this->month,
  630. $this->day,
  631. $this->year) +
  632. ($this->microsecond / 1000000);
  633. }
  634. }
  635. public function toString($fullformat = true) {
  636. if ($this->microsecond == 0) {
  637. $format = ($fullformat ? '%04d-%02d-%02dT%02d:%02d:%02d'
  638. : '%04d%02d%02dT%02d%02d%02d');
  639. $str = sprintf($format,
  640. $this->year, $this->month, $this->day,
  641. $this->hour, $this->minute, $this->second);
  642. }
  643. if ($this->microsecond % 1000 == 0) {
  644. $format = ($fullformat ? '%04d-%02d-%02dT%02d:%02d:%02d.%03d'
  645. : '%04d%02d%02dT%02d%02d%02d.%03d');
  646. $str = sprintf($format,
  647. $this->year, $this->month, $this->day,
  648. $this->hour, $this->minute, $this->second,
  649. (int)($this->microsecond / 1000));
  650. }
  651. else {
  652. $format = ($fullformat ? '%04d-%02d-%02dT%02d:%02d:%02d.%06d'
  653. : '%04d%02d%02dT%02d%02d%02d.%06d');
  654. $str = sprintf($format,
  655. $this->year, $this->month, $this->day,
  656. $this->hour, $this->minute, $this->second,
  657. $this->microsecond);
  658. }
  659. if ($this->utc) {
  660. $str .= 'Z';
  661. }
  662. return $str;
  663. }
  664. public function __toString() {
  665. return $this->toString();
  666. }
  667. public static function isValidTime($hour, $minute, $second, $microsecond = 0) {
  668. return HproseTime::isValidTime($hour, $minute, $second, $microsecond);
  669. }
  670. }
  671. /*
  672. integer is_utf8(string $s)
  673. if $s is UTF-8 String, return 1 else 0
  674. */
  675. if (function_exists('mb_detect_encoding')) {
  676. function is_utf8($s) {
  677. return mb_detect_encoding($s, 'UTF-8', true) === 'UTF-8';
  678. }
  679. }
  680. elseif (function_exists('iconv')) {
  681. function is_utf8($s) {
  682. return iconv('UTF-8', 'UTF-8//IGNORE', $s) === $s;
  683. }
  684. }
  685. else {
  686. function is_utf8($s) {
  687. $len = strlen($s);
  688. for($i = 0; $i < $len; ++$i){
  689. $c = ord($s{$i});
  690. switch ($c >> 4) {
  691. case 0:
  692. case 1:
  693. case 2:
  694. case 3:
  695. case 4:
  696. case 5:
  697. case 6:
  698. case 7:
  699. break;
  700. case 12:
  701. case 13:
  702. if ((ord($s{++$i}) >> 6) != 0x2) return false;
  703. break;
  704. case 14:
  705. if ((ord($s{++$i}) >> 6) != 0x2) return false;
  706. if ((ord($s{++$i}) >> 6) != 0x2) return false;
  707. break;
  708. case 15:
  709. $b = $s{++$i};
  710. if ((ord($b) >> 6) != 0x2) return false;
  711. if ((ord($s{++$i}) >> 6) != 0x2) return false;
  712. if ((ord($s{++$i}) >> 6) != 0x2) return false;
  713. if (((($c & 0xf) << 2) | (($b >> 4) & 0x3)) > 0x10) return false;
  714. break;
  715. default:
  716. return false;
  717. }
  718. }
  719. return true;
  720. }
  721. }
  722. /*
  723. integer ustrlen(string $s)
  724. $s must be a UTF-8 String, return the Unicode code unit (not code point) length
  725. */
  726. if (function_exists('iconv')) {
  727. function ustrlen($s) {
  728. return strlen(iconv('UTF-8', 'UTF-16LE', $s)) >> 1;
  729. }
  730. }
  731. elseif (function_exists('mb_convert_encoding')) {
  732. function ustrlen($s) {
  733. return strlen(mb_convert_encoding($s, "UTF-16LE", "UTF-8")) >> 1;
  734. }
  735. }
  736. else {
  737. function ustrlen($s) {
  738. $pos = 0;
  739. $length = strlen($s);
  740. $len = $length;
  741. while ($pos < $length) {
  742. $a = ord($s{$pos++});
  743. if ($a < 0x80) {
  744. continue;
  745. }
  746. elseif (($a & 0xE0) == 0xC0) {
  747. ++$pos;
  748. --$len;
  749. }
  750. elseif (($a & 0xF0) == 0xE0) {
  751. $pos += 2;
  752. $len -= 2;
  753. }
  754. elseif (($a & 0xF8) == 0xF0) {
  755. $pos += 3;
  756. $len -= 2;
  757. }
  758. }
  759. return $len;
  760. }
  761. }
  762. /*
  763. bool is_list(array $a)
  764. if $a is list, return true else false
  765. */
  766. function is_list(array $a) {
  767. $count = count($a);
  768. if ($count === 0) return true;
  769. return !array_diff_key($a, array_fill(0, $count, NULL));
  770. }
  771. /*
  772. mixed array_ref_search(mixed &$value, array $array)
  773. if $value ref in $array, return the index else false
  774. */
  775. function array_ref_search(&$value, &$array) {
  776. if (!is_array($value)) return array_search($value, $array, true);
  777. $temp = $value;
  778. foreach ($array as $i => &$ref) {
  779. if (($ref === ($value = 1)) && ($ref === ($value = 0))) {
  780. $value = $temp;
  781. return $i;
  782. }
  783. }
  784. $value = $temp;
  785. return false;
  786. }
  787. /*
  788. string spl_object_hash(object $obj)
  789. This function returns a unique identifier for the object.
  790. This id can be used as a hash key for storing objects or for identifying an object.
  791. */
  792. if (!function_exists('spl_object_hash')) {
  793. function spl_object_hash($object) {
  794. ob_start();
  795. var_dump($object);
  796. preg_match('[#(\d+)]', ob_get_clean(), $match);
  797. return $match[1];
  798. }
  799. }
  800. ?>