u_date_code.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.4 2007-06-03 21:25:47 marand Exp $
  11. * @link
  12. * @since Not applicable yet
  13. * @package default
  14. */
  15. require_once('boxed_scalars.php'); // needed to sign-compare ints
  16. class osinet_date_code implements Comparable_Interface
  17. {
  18. private $f_value;
  19. /**
  20. * Implements Comparable_Interface restrictively, only comparing to other osinet_date_code objects, not to any other class implementing Comparable_Interface
  21. * @return int
  22. */
  23. public function cmp(Comparable_Interface $other)
  24. {
  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. {
  58. $date_code = strtoupper($this->f_value);
  59. $matches = array();
  60. preg_match('/^([0-9]*)([A-Z])([0-9]?.*$)/', $date_code, $matches);
  61. if (count($matches) <> 4)
  62. {
  63. throw new Exception("Incorrect date code $date_code");
  64. }
  65. $ret = array(
  66. 'mday' => $matches[3],
  67. 'mon' => ord($matches[2]) - ord('@'),
  68. 'year' => 2000 + $matches[1],
  69. );
  70. return $ret;
  71. }
  72. /**
  73. * @param int $time
  74. */
  75. public function __construct($time = null)
  76. {
  77. date_default_timezone_set('Europe/Paris');
  78. if (!$time)
  79. $time = time();
  80. $date = getdate();
  81. $day = $date['mday'];
  82. $month = $date['mon'];
  83. $year = $date['year'] % 100;
  84. $month = chr(ord('@') + $month);
  85. $this->f_value = sprintf('%d%s%02d', $year, $month, $day);
  86. }
  87. /**
  88. * @param string $date_code
  89. */
  90. private function set_value($date_code)
  91. {
  92. $date_code = strtoupper($date_code);
  93. $matches = array();
  94. preg_match('/^([0-9]*)([A-Z])([0-9]?.*$)/', $date_code, $matches);
  95. if (count($matches) <> 4)
  96. {
  97. throw new Exception("Incorrect date code $date_code");
  98. }
  99. else
  100. $this->f_value = $date_code;
  101. }
  102. /**
  103. * @param string $nm
  104. * @return mixed
  105. */
  106. protected function __get($nm)
  107. {
  108. // echo "osinet_date_code::__get($nm)\n";
  109. if ($nm <> 'value')
  110. throw new Exception("$nm: undefined property for " . get_class($this));
  111. return $this->f_value;
  112. }
  113. /**
  114. * @param string $nm
  115. * @param mixed $val
  116. * @return void
  117. */
  118. protected function __set($nm, $val)
  119. {
  120. if ($nm <> 'value')
  121. throw new Exception("$nm: undefined property for " . get_class($this));
  122. $this->set_value($val);
  123. }
  124. }