u_date_code.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * $Id: u_date_code.php,v 1.3 2007-04-29 15:40:29 marand Exp $
  8. */
  9. require_once('misc.php'); // needed for "iComparable" interface
  10. require_once('boxed_scalars.php'); // needed to sign-compare ints
  11. class osinet_date_code implements iComparable
  12. {
  13. private $f_value;
  14. /**
  15. * Implements iComparable restrictively, only comparing to other osinet_date_code
  16. * objects, not to any other class implementing iComparable
  17. *
  18. */
  19. public function cmp(iComparable $other)
  20. {
  21. if (!($other instanceof osinet_date_code))
  22. throw new Exception('osinet_date_code only compares to other osinet_date_code objects');
  23. $ar_d1 = $this->to_mkdate();
  24. $d1 = new boxed_int($ar_d1['mday']);
  25. $m1 = new boxed_int($ar_d1['mon']) ;
  26. $y1 = new boxed_int($ar_d1['year']);
  27. $ar_d2 = $other->to_mkdate();
  28. $d2 = new boxed_int($ar_d2['mday']);
  29. $m2 = new boxed_int($ar_d2['mon']) ;
  30. $y2 = new boxed_int($ar_d2['year']);
  31. $ret = $y1->cmp($y2);
  32. if ($ret)
  33. return $ret;
  34. else // same year
  35. {
  36. $ret = $m1->cmp($m2);
  37. if ($ret)
  38. return $ret;
  39. else // same year and month
  40. {
  41. $ret = $d1->cmp($d2);
  42. return $ret;
  43. }
  44. }
  45. }
  46. /**
  47. * Generate an mkdate()-like array for a date code
  48. *
  49. * @param string $date_code
  50. * @return array ['mday', 'mon', 'year']
  51. */
  52. public function to_mkdate()
  53. {
  54. $date_code = strtoupper($this->f_value);
  55. $matches = array();
  56. preg_match('/^([0-9]*)([A-Z])([0-9]?.*$)/', $date_code, $matches);
  57. if (count($matches) <> 4)
  58. {
  59. throw new Exception("Incorrect date code $date_code");
  60. }
  61. $ret = array(
  62. 'mday' => $matches[3],
  63. 'mon' => ord($matches[2]) - ord('@'),
  64. 'year' => 2000 + $matches[1],
  65. );
  66. return $ret;
  67. }
  68. public function __construct($time = null)
  69. {
  70. date_default_timezone_set('Europe/Paris');
  71. if (!$time)
  72. $time = time();
  73. $date = getdate();
  74. $day = $date['mday'];
  75. $month = $date['mon'];
  76. $year = $date['year'] % 100;
  77. $month = chr(ord('@') + $month);
  78. $this->f_value = sprintf('%d%s%02d', $year, $month, $day);
  79. }
  80. private function set_value($date_code)
  81. {
  82. $date_code = strtoupper($date_code);
  83. $matches = array();
  84. preg_match('/^([0-9]*)([A-Z])([0-9]?.*$)/', $date_code, $matches);
  85. if (count($matches) <> 4)
  86. {
  87. throw new Exception("Incorrect date code $date_code");
  88. }
  89. else
  90. $this->f_value = $date_code;
  91. }
  92. protected function __get($nm)
  93. {
  94. // echo "osinet_date_code::__get($nm)\n";
  95. if ($nm <> 'value')
  96. throw new Exception("$nm: undefined property for " . get_class($this));
  97. return $this->f_value;
  98. }
  99. private function __set($nm, $val)
  100. {
  101. if ($nm <> 'value')
  102. throw new Exception("$nm: undefined property for " . get_class($this));
  103. $this->set_value($val);
  104. }
  105. }