u_date_code.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Handle standard OSInet date code strings, uppercase format: [y]ymdd
  4. *
  5. * 4-characters until 2009-12-31, 5 afterwards
  6. *
  7. * @copyright (c) 2007 OSI
  8. * @author Frédéric G. MARAND
  9. * @license Licensed under the CeCILL 2.0
  10. */
  11. /**
  12. * Needed to sign-compare ints
  13. */
  14. require_once('boxed_scalars.php');
  15. /**
  16. * OSInet-format date codes like 07F09 to 2007-06-09
  17. */
  18. class osinet_date_code implements Comparable_Interface {
  19. private $f_value;
  20. /**
  21. * Implements Comparable_Interface restrictively, only comparing to other osinet_date_code objects, not to any other class implementing Comparable_Interface
  22. * @return int
  23. */
  24. public function cmp(Comparable_Interface $other) {
  25. if (!($other instanceof osinet_date_code))
  26. throw new Exception('osinet_date_code only compares to other osinet_date_code objects');
  27. $ar_d1 = $this->to_mkdate();
  28. $d1 = new boxed_int($ar_d1['mday']);
  29. $m1 = new boxed_int($ar_d1['mon']) ;
  30. $y1 = new boxed_int($ar_d1['year']);
  31. $ar_d2 = $other->to_mkdate();
  32. $d2 = new boxed_int($ar_d2['mday']);
  33. $m2 = new boxed_int($ar_d2['mon']) ;
  34. $y2 = new boxed_int($ar_d2['year']);
  35. $ret = $y1->cmp($y2);
  36. if ($ret)
  37. return $ret;
  38. else // same year
  39. {
  40. $ret = $m1->cmp($m2);
  41. if ($ret)
  42. return $ret;
  43. else // same year and month
  44. {
  45. $ret = $d1->cmp($d2);
  46. return $ret;
  47. }
  48. }
  49. }
  50. /**
  51. * Generate an mkdate()-like array for a date code
  52. *
  53. * @param string $date_code
  54. * @return array ['mday', 'mon', 'year']
  55. */
  56. public function to_mkdate() {
  57. $date_code = strtoupper($this->f_value);
  58. $matches = array();
  59. preg_match('/^([0-9]*)([A-Z])([0-9]?.*$)/', $date_code, $matches);
  60. if (count($matches) <> 4) {
  61. throw new Exception("Incorrect date code $date_code");
  62. }
  63. $ret = array(
  64. 'mday' => $matches[3],
  65. 'mon' => ord($matches[2]) - ord('@'),
  66. 'year' => 2000 + $matches[1],
  67. );
  68. return $ret;
  69. }
  70. /**
  71. * @param int $time
  72. */
  73. public function __construct($time = null) {
  74. date_default_timezone_set('Europe/Paris');
  75. if (!$time) {
  76. $time = time();
  77. }
  78. $date = getdate();
  79. $day = $date['mday'];
  80. $month = $date['mon'];
  81. $year = $date['year'] % 100;
  82. $month = chr(ord('@') + $month);
  83. $this->f_value = sprintf('%d%s%02d', $year, $month, $day);
  84. }
  85. /**
  86. * @param string $date_code
  87. */
  88. private function set_value($date_code) {
  89. $date_code = strtoupper($date_code);
  90. $matches = array();
  91. preg_match('/^([0-9]*)([A-Z])([0-9]?.*$)/', $date_code, $matches);
  92. if (count($matches) <> 4) {
  93. throw new Exception("Incorrect date code $date_code");
  94. }
  95. else {
  96. $this->f_value = $date_code;
  97. }
  98. }
  99. /**
  100. * @param string $nm
  101. *
  102. * @return mixed
  103. */
  104. protected function __get($nm) {
  105. // echo "osinet_date_code::__get($nm)\n";
  106. if ($nm <> 'value') {
  107. throw new Exception("$nm: undefined property for " . get_class($this));
  108. }
  109. return $this->f_value;
  110. }
  111. /**
  112. * @param string $nm
  113. * @param mixed $val
  114. *
  115. * @return void
  116. */
  117. protected function __set($nm, $val) {
  118. if ($nm <> 'value') {
  119. throw new Exception("$nm: undefined property for " . get_class($this));
  120. }
  121. $this->set_value($val);
  122. }
  123. }