u_date_code.php 3.4 KB

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