PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/services/authcharts/lib/thx/culture/FormatDate.class.php

https://github.com/precog/visualizations
PHP | 658 lines | 657 code | 1 blank | 0 comment | 157 complexity | 54b18e3ef842b29abe46a9759e64ab51 MD5 | raw file
  1. <?php
  2. class thx_culture_FormatDate {
  3. public function __construct(){}
  4. static function format($pattern, $date, $culture, $leadingspace) {
  5. if($leadingspace === null) {
  6. $leadingspace = true;
  7. }
  8. if(null === $culture) {
  9. $culture = thx_culture_Culture::getDefaultCulture();
  10. }
  11. $pos = 0;
  12. $len = strlen($pattern);
  13. $buf = new StringBuf();
  14. $info = $culture->date;
  15. while($pos < $len) {
  16. $c = _hx_char_at($pattern, $pos);
  17. if($c !== "%") {
  18. {
  19. $x = $c;
  20. if(is_null($x)) {
  21. $x = "null";
  22. } else {
  23. if(is_bool($x)) {
  24. $x = (($x) ? "true" : "false");
  25. }
  26. }
  27. $buf->b .= $x;
  28. unset($x);
  29. }
  30. $pos++;
  31. continue;
  32. }
  33. $pos++;
  34. $c = _hx_char_at($pattern, $pos);
  35. switch($c) {
  36. case "a":{
  37. $x = $info->abbrDays[$date->getDay()];
  38. if(is_null($x)) {
  39. $x = "null";
  40. } else {
  41. if(is_bool($x)) {
  42. $x = (($x) ? "true" : "false");
  43. }
  44. }
  45. $buf->b .= $x;
  46. }break;
  47. case "A":{
  48. $x = $info->days[$date->getDay()];
  49. if(is_null($x)) {
  50. $x = "null";
  51. } else {
  52. if(is_bool($x)) {
  53. $x = (($x) ? "true" : "false");
  54. }
  55. }
  56. $buf->b .= $x;
  57. }break;
  58. case "b":case "h":{
  59. $x = $info->abbrMonths[$date->getMonth()];
  60. if(is_null($x)) {
  61. $x = "null";
  62. } else {
  63. if(is_bool($x)) {
  64. $x = (($x) ? "true" : "false");
  65. }
  66. }
  67. $buf->b .= $x;
  68. }break;
  69. case "B":{
  70. $x = $info->months[$date->getMonth()];
  71. if(is_null($x)) {
  72. $x = "null";
  73. } else {
  74. if(is_bool($x)) {
  75. $x = (($x) ? "true" : "false");
  76. }
  77. }
  78. $buf->b .= $x;
  79. }break;
  80. case "c":{
  81. $x = thx_culture_FormatDate::dateTime($date, $culture);
  82. if(is_null($x)) {
  83. $x = "null";
  84. } else {
  85. if(is_bool($x)) {
  86. $x = (($x) ? "true" : "false");
  87. }
  88. }
  89. $buf->b .= $x;
  90. }break;
  91. case "C":{
  92. $x = thx_culture_FormatNumber::digits("" . Math::floor($date->getFullYear() / 100), $culture);
  93. if(is_null($x)) {
  94. $x = "null";
  95. } else {
  96. if(is_bool($x)) {
  97. $x = (($x) ? "true" : "false");
  98. }
  99. }
  100. $buf->b .= $x;
  101. }break;
  102. case "d":{
  103. $x = thx_culture_FormatNumber::digits(str_pad("" . $date->getDate(), 2, "0", STR_PAD_LEFT), $culture);
  104. if(is_null($x)) {
  105. $x = "null";
  106. } else {
  107. if(is_bool($x)) {
  108. $x = (($x) ? "true" : "false");
  109. }
  110. }
  111. $buf->b .= $x;
  112. }break;
  113. case "D":{
  114. $x = thx_culture_FormatDate::format("%m/%d/%y", $date, $culture, null);
  115. if(is_null($x)) {
  116. $x = "null";
  117. } else {
  118. if(is_bool($x)) {
  119. $x = (($x) ? "true" : "false");
  120. }
  121. }
  122. $buf->b .= $x;
  123. }break;
  124. case "e":{
  125. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_0($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  126. if(is_null($x)) {
  127. $x = "null";
  128. } else {
  129. if(is_bool($x)) {
  130. $x = (($x) ? "true" : "false");
  131. }
  132. }
  133. $buf->b .= $x;
  134. }break;
  135. case "f":{
  136. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_1($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  137. if(is_null($x)) {
  138. $x = "null";
  139. } else {
  140. if(is_bool($x)) {
  141. $x = (($x) ? "true" : "false");
  142. }
  143. }
  144. $buf->b .= $x;
  145. }break;
  146. case "G":{
  147. throw new HException("Not Implemented Yet");
  148. }break;
  149. case "g":{
  150. throw new HException("Not Implemented Yet");
  151. }break;
  152. case "H":{
  153. $x = thx_culture_FormatNumber::digits(str_pad("" . $date->getHours(), 2, "0", STR_PAD_LEFT), $culture);
  154. if(is_null($x)) {
  155. $x = "null";
  156. } else {
  157. if(is_bool($x)) {
  158. $x = (($x) ? "true" : "false");
  159. }
  160. }
  161. $buf->b .= $x;
  162. }break;
  163. case "i":{
  164. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_2($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  165. if(is_null($x)) {
  166. $x = "null";
  167. } else {
  168. if(is_bool($x)) {
  169. $x = (($x) ? "true" : "false");
  170. }
  171. }
  172. $buf->b .= $x;
  173. }break;
  174. case "I":{
  175. $x = thx_culture_FormatNumber::digits(str_pad("" . thx_culture_FormatDate::getMHours($date), 2, "0", STR_PAD_LEFT), $culture);
  176. if(is_null($x)) {
  177. $x = "null";
  178. } else {
  179. if(is_bool($x)) {
  180. $x = (($x) ? "true" : "false");
  181. }
  182. }
  183. $buf->b .= $x;
  184. }break;
  185. case "j":{
  186. throw new HException("Not Implemented Yet");
  187. }break;
  188. case "k":{
  189. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_3($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  190. if(is_null($x)) {
  191. $x = "null";
  192. } else {
  193. if(is_bool($x)) {
  194. $x = (($x) ? "true" : "false");
  195. }
  196. }
  197. $buf->b .= $x;
  198. }break;
  199. case "l":{
  200. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_4($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  201. if(is_null($x)) {
  202. $x = "null";
  203. } else {
  204. if(is_bool($x)) {
  205. $x = (($x) ? "true" : "false");
  206. }
  207. }
  208. $buf->b .= $x;
  209. }break;
  210. case "m":{
  211. $x = thx_culture_FormatNumber::digits(str_pad("" . ($date->getMonth() + 1), 2, "0", STR_PAD_LEFT), $culture);
  212. if(is_null($x)) {
  213. $x = "null";
  214. } else {
  215. if(is_bool($x)) {
  216. $x = (($x) ? "true" : "false");
  217. }
  218. }
  219. $buf->b .= $x;
  220. }break;
  221. case "M":{
  222. $x = thx_culture_FormatNumber::digits(str_pad("" . $date->getMinutes(), 2, "0", STR_PAD_LEFT), $culture);
  223. if(is_null($x)) {
  224. $x = "null";
  225. } else {
  226. if(is_bool($x)) {
  227. $x = (($x) ? "true" : "false");
  228. }
  229. }
  230. $buf->b .= $x;
  231. }break;
  232. case "n":{
  233. $x = "\x0A";
  234. if(is_null($x)) {
  235. $x = "null";
  236. } else {
  237. if(is_bool($x)) {
  238. $x = (($x) ? "true" : "false");
  239. }
  240. }
  241. $buf->b .= $x;
  242. }break;
  243. case "p":{
  244. $x = thx_culture_FormatDate_5($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos);
  245. if(is_null($x)) {
  246. $x = "null";
  247. } else {
  248. if(is_bool($x)) {
  249. $x = (($x) ? "true" : "false");
  250. }
  251. }
  252. $buf->b .= $x;
  253. }break;
  254. case "P":{
  255. $x = strtolower((thx_culture_FormatDate_6($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos)));
  256. if(is_null($x)) {
  257. $x = "null";
  258. } else {
  259. if(is_bool($x)) {
  260. $x = (($x) ? "true" : "false");
  261. }
  262. }
  263. $buf->b .= $x;
  264. }break;
  265. case "q":{
  266. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_7($buf, $c, $culture, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  267. if(is_null($x)) {
  268. $x = "null";
  269. } else {
  270. if(is_bool($x)) {
  271. $x = (($x) ? "true" : "false");
  272. }
  273. }
  274. $buf->b .= $x;
  275. }break;
  276. case "r":{
  277. $x = thx_culture_FormatDate::format("%I:%M:%S %p", $date, $culture, null);
  278. if(is_null($x)) {
  279. $x = "null";
  280. } else {
  281. if(is_bool($x)) {
  282. $x = (($x) ? "true" : "false");
  283. }
  284. }
  285. $buf->b .= $x;
  286. }break;
  287. case "R":{
  288. $x = thx_culture_FormatDate::format("%H:%M", $date, $culture, null);
  289. if(is_null($x)) {
  290. $x = "null";
  291. } else {
  292. if(is_bool($x)) {
  293. $x = (($x) ? "true" : "false");
  294. }
  295. }
  296. $buf->b .= $x;
  297. }break;
  298. case "s":{
  299. $x = "" . intval($date->getTime() / 1000);
  300. if(is_null($x)) {
  301. $x = "null";
  302. } else {
  303. if(is_bool($x)) {
  304. $x = (($x) ? "true" : "false");
  305. }
  306. }
  307. $buf->b .= $x;
  308. }break;
  309. case "S":{
  310. $x = thx_culture_FormatNumber::digits(str_pad("" . $date->getSeconds(), 2, "0", STR_PAD_LEFT), $culture);
  311. if(is_null($x)) {
  312. $x = "null";
  313. } else {
  314. if(is_bool($x)) {
  315. $x = (($x) ? "true" : "false");
  316. }
  317. }
  318. $buf->b .= $x;
  319. }break;
  320. case "t":{
  321. $x = "\x09";
  322. if(is_null($x)) {
  323. $x = "null";
  324. } else {
  325. if(is_bool($x)) {
  326. $x = (($x) ? "true" : "false");
  327. }
  328. }
  329. $buf->b .= $x;
  330. }break;
  331. case "T":{
  332. $x = thx_culture_FormatDate::format("%H:%M:%S", $date, $culture, null);
  333. if(is_null($x)) {
  334. $x = "null";
  335. } else {
  336. if(is_bool($x)) {
  337. $x = (($x) ? "true" : "false");
  338. }
  339. }
  340. $buf->b .= $x;
  341. }break;
  342. case "u":{
  343. $d = $date->getDay();
  344. {
  345. $x = thx_culture_FormatNumber::digits(thx_culture_FormatDate_8($buf, $c, $culture, $d, $date, $info, $leadingspace, $len, $pattern, $pos), $culture);
  346. if(is_null($x)) {
  347. $x = "null";
  348. } else {
  349. if(is_bool($x)) {
  350. $x = (($x) ? "true" : "false");
  351. }
  352. }
  353. $buf->b .= $x;
  354. }
  355. }break;
  356. case "U":{
  357. throw new HException("Not Implemented Yet");
  358. }break;
  359. case "V":{
  360. throw new HException("Not Implemented Yet");
  361. }break;
  362. case "w":{
  363. $x = thx_culture_FormatNumber::digits("" . $date->getDay(), $culture);
  364. if(is_null($x)) {
  365. $x = "null";
  366. } else {
  367. if(is_bool($x)) {
  368. $x = (($x) ? "true" : "false");
  369. }
  370. }
  371. $buf->b .= $x;
  372. }break;
  373. case "W":{
  374. throw new HException("Not Implemented Yet");
  375. }break;
  376. case "x":{
  377. $x = thx_culture_FormatDate::date($date, $culture);
  378. if(is_null($x)) {
  379. $x = "null";
  380. } else {
  381. if(is_bool($x)) {
  382. $x = (($x) ? "true" : "false");
  383. }
  384. }
  385. $buf->b .= $x;
  386. }break;
  387. case "X":{
  388. $x = thx_culture_FormatDate::time($date, $culture);
  389. if(is_null($x)) {
  390. $x = "null";
  391. } else {
  392. if(is_bool($x)) {
  393. $x = (($x) ? "true" : "false");
  394. }
  395. }
  396. $buf->b .= $x;
  397. }break;
  398. case "y":{
  399. $x = thx_culture_FormatNumber::digits(_hx_substr(("" . $date->getFullYear()), -2, null), $culture);
  400. if(is_null($x)) {
  401. $x = "null";
  402. } else {
  403. if(is_bool($x)) {
  404. $x = (($x) ? "true" : "false");
  405. }
  406. }
  407. $buf->b .= $x;
  408. }break;
  409. case "Y":{
  410. $x = thx_culture_FormatNumber::digits("" . $date->getFullYear(), $culture);
  411. if(is_null($x)) {
  412. $x = "null";
  413. } else {
  414. if(is_bool($x)) {
  415. $x = (($x) ? "true" : "false");
  416. }
  417. }
  418. $buf->b .= $x;
  419. }break;
  420. case "z":{
  421. $x = "+0000";
  422. if(is_null($x)) {
  423. $x = "null";
  424. } else {
  425. if(is_bool($x)) {
  426. $x = (($x) ? "true" : "false");
  427. }
  428. }
  429. $buf->b .= $x;
  430. }break;
  431. case "Z":{
  432. $x = "GMT";
  433. if(is_null($x)) {
  434. $x = "null";
  435. } else {
  436. if(is_bool($x)) {
  437. $x = (($x) ? "true" : "false");
  438. }
  439. }
  440. $buf->b .= $x;
  441. }break;
  442. case "%":{
  443. $x = "%";
  444. if(is_null($x)) {
  445. $x = "null";
  446. } else {
  447. if(is_bool($x)) {
  448. $x = (($x) ? "true" : "false");
  449. }
  450. }
  451. $buf->b .= $x;
  452. }break;
  453. default:{
  454. $x = "%" . $c;
  455. if(is_null($x)) {
  456. $x = "null";
  457. } else {
  458. if(is_bool($x)) {
  459. $x = (($x) ? "true" : "false");
  460. }
  461. }
  462. $buf->b .= $x;
  463. }break;
  464. }
  465. $pos++;
  466. unset($c);
  467. }
  468. return $buf->b;
  469. }
  470. static function getMHours($date) {
  471. $v = $date->getHours();
  472. return thx_culture_FormatDate_9($date, $v);
  473. }
  474. static function yearMonth($date, $culture) {
  475. if(null === $culture) {
  476. $culture = thx_culture_Culture::getDefaultCulture();
  477. }
  478. return thx_culture_FormatDate::format($culture->date->patternYearMonth, $date, $culture, false);
  479. }
  480. static function monthDay($date, $culture) {
  481. if(null === $culture) {
  482. $culture = thx_culture_Culture::getDefaultCulture();
  483. }
  484. return thx_culture_FormatDate::format($culture->date->patternMonthDay, $date, $culture, false);
  485. }
  486. static function date($date, $culture) {
  487. if(null === $culture) {
  488. $culture = thx_culture_Culture::getDefaultCulture();
  489. }
  490. return thx_culture_FormatDate::format($culture->date->patternDate, $date, $culture, false);
  491. }
  492. static function dateShort($date, $culture) {
  493. if(null === $culture) {
  494. $culture = thx_culture_Culture::getDefaultCulture();
  495. }
  496. return thx_culture_FormatDate::format($culture->date->patternDateShort, $date, $culture, false);
  497. }
  498. static function dateRfc($date, $culture) {
  499. if(null === $culture) {
  500. $culture = thx_culture_Culture::getDefaultCulture();
  501. }
  502. return thx_culture_FormatDate::format($culture->date->patternDateRfc, $date, $culture, false);
  503. }
  504. static function dateTime($date, $culture) {
  505. if(null === $culture) {
  506. $culture = thx_culture_Culture::getDefaultCulture();
  507. }
  508. return thx_culture_FormatDate::format($culture->date->patternDateTime, $date, $culture, false);
  509. }
  510. static function universal($date, $culture) {
  511. if(null === $culture) {
  512. $culture = thx_culture_Culture::getDefaultCulture();
  513. }
  514. return thx_culture_FormatDate::format($culture->date->patternUniversal, $date, $culture, false);
  515. }
  516. static function sortable($date, $culture) {
  517. if(null === $culture) {
  518. $culture = thx_culture_Culture::getDefaultCulture();
  519. }
  520. return thx_culture_FormatDate::format($culture->date->patternSortable, $date, $culture, false);
  521. }
  522. static function time($date, $culture) {
  523. if(null === $culture) {
  524. $culture = thx_culture_Culture::getDefaultCulture();
  525. }
  526. return thx_culture_FormatDate::format($culture->date->patternTime, $date, $culture, false);
  527. }
  528. static function timeShort($date, $culture) {
  529. if(null === $culture) {
  530. $culture = thx_culture_Culture::getDefaultCulture();
  531. }
  532. return thx_culture_FormatDate::format($culture->date->patternTimeShort, $date, $culture, false);
  533. }
  534. static function hourShort($date, $culture) {
  535. if(null === $culture) {
  536. $culture = thx_culture_Culture::getDefaultCulture();
  537. }
  538. if(null === $culture->date->am) {
  539. return thx_culture_FormatDate::format("%H", $date, $culture, false);
  540. } else {
  541. return thx_culture_FormatDate::format("%l %p", $date, $culture, false);
  542. }
  543. }
  544. static function year($date, $culture) {
  545. if(null === $culture) {
  546. $culture = thx_culture_Culture::getDefaultCulture();
  547. }
  548. return thx_culture_FormatNumber::digits("" . $date->getFullYear(), $culture);
  549. }
  550. static function month($date, $culture) {
  551. if(null === $culture) {
  552. $culture = thx_culture_Culture::getDefaultCulture();
  553. }
  554. return thx_culture_FormatNumber::digits("" . ($date->getMonth() + 1), $culture);
  555. }
  556. static function monthName($date, $culture) {
  557. if(null === $culture) {
  558. $culture = thx_culture_Culture::getDefaultCulture();
  559. }
  560. return $culture->date->abbrMonths[$date->getMonth()];
  561. }
  562. static function monthNameShort($date, $culture) {
  563. if(null === $culture) {
  564. $culture = thx_culture_Culture::getDefaultCulture();
  565. }
  566. return $culture->date->months[$date->getMonth()];
  567. }
  568. static function weekDay($date, $culture) {
  569. if(null === $culture) {
  570. $culture = thx_culture_Culture::getDefaultCulture();
  571. }
  572. return thx_culture_FormatNumber::digits("" . ($date->getDay() + $culture->date->firstWeekDay), $culture);
  573. }
  574. static function weekDayName($date, $culture) {
  575. if(null === $culture) {
  576. $culture = thx_culture_Culture::getDefaultCulture();
  577. }
  578. return $culture->date->abbrDays[$date->getDay()];
  579. }
  580. static function weekDayNameShort($date, $culture) {
  581. if(null === $culture) {
  582. $culture = thx_culture_Culture::getDefaultCulture();
  583. }
  584. return $culture->date->days[$date->getDay()];
  585. }
  586. function __toString() { return 'thx.culture.FormatDate'; }
  587. }
  588. function thx_culture_FormatDate_0(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  589. if($leadingspace) {
  590. return str_pad("" . $date->getDate(), 2, " ", STR_PAD_LEFT);
  591. } else {
  592. return "" . $date->getDate();
  593. }
  594. }
  595. function thx_culture_FormatDate_1(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  596. if($leadingspace) {
  597. return str_pad("" . ($date->getMonth() + 1), 2, " ", STR_PAD_LEFT);
  598. } else {
  599. return "" . ($date->getMonth() + 1);
  600. }
  601. }
  602. function thx_culture_FormatDate_2(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  603. if($leadingspace) {
  604. return str_pad("" . $date->getMinutes(), 2, " ", STR_PAD_LEFT);
  605. } else {
  606. return "" . $date->getMinutes();
  607. }
  608. }
  609. function thx_culture_FormatDate_3(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  610. if($leadingspace) {
  611. return str_pad("" . $date->getHours(), 2, " ", STR_PAD_LEFT);
  612. } else {
  613. return "" . $date->getHours();
  614. }
  615. }
  616. function thx_culture_FormatDate_4(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  617. if($leadingspace) {
  618. return str_pad("" . thx_culture_FormatDate::getMHours($date), 2, " ", STR_PAD_LEFT);
  619. } else {
  620. return "" . thx_culture_FormatDate::getMHours($date);
  621. }
  622. }
  623. function thx_culture_FormatDate_5(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  624. if($date->getHours() > 11) {
  625. return $info->pm;
  626. } else {
  627. return $info->am;
  628. }
  629. }
  630. function thx_culture_FormatDate_6(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  631. if($date->getHours() > 11) {
  632. return $info->pm;
  633. } else {
  634. return $info->am;
  635. }
  636. }
  637. function thx_culture_FormatDate_7(&$buf, &$c, &$culture, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  638. if($leadingspace) {
  639. return str_pad("" . $date->getSeconds(), 2, " ", STR_PAD_LEFT);
  640. } else {
  641. return "" . $date->getSeconds();
  642. }
  643. }
  644. function thx_culture_FormatDate_8(&$buf, &$c, &$culture, &$d, &$date, &$info, &$leadingspace, &$len, &$pattern, &$pos) {
  645. if($d === 0) {
  646. return "7";
  647. } else {
  648. return "" . $d;
  649. }
  650. }
  651. function thx_culture_FormatDate_9(&$date, &$v) {
  652. if($v > 12) {
  653. return $v - 12;
  654. } else {
  655. return $v;
  656. }
  657. }