computing.inc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Any extension to compute things like a stop watch deadline or working hours
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * Metric computing for stop watches
  26. */
  27. interface iMetricComputer
  28. {
  29. public static function GetDescription();
  30. public function ComputeMetric($oObject);
  31. }
  32. /**
  33. * Working time computing for stop watches
  34. */
  35. interface iWorkingTimeComputer
  36. {
  37. public static function GetDescription();
  38. /**
  39. * Get the date/time corresponding to a given delay in the future from the present
  40. * considering only the valid (open) hours for a specified object
  41. * @param $oObject DBObject The object for which to compute the deadline
  42. * @param $iDuration integer The duration (in seconds) in the future
  43. * @param $oStartDate DateTime The starting point for the computation
  44. * @return DateTime The date/time for the deadline
  45. */
  46. public function GetDeadline($oObject, $iDuration, DateTime $oStartDate);
  47. /**
  48. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  49. * @param $oObject DBObject The object for which to compute the duration
  50. * @param $oStartDate DateTime The starting point for the computation (default = now)
  51. * @param $oEndDate DateTime The ending point for the computation (default = now)
  52. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  53. */
  54. public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate);
  55. }
  56. /**
  57. * Default implementation oof deadline computing: NO deadline
  58. */
  59. class DefaultMetricComputer implements iMetricComputer
  60. {
  61. public static function GetDescription()
  62. {
  63. return "Null";
  64. }
  65. public function ComputeMetric($oObject)
  66. {
  67. return null;
  68. }
  69. }
  70. /**
  71. * Default implementation of working time computing
  72. */
  73. class DefaultWorkingTimeComputer implements iWorkingTimeComputer
  74. {
  75. public static function GetDescription()
  76. {
  77. return "24x7, no holidays";
  78. }
  79. /**
  80. * Get the date/time corresponding to a given delay in the future from the present
  81. * considering only the valid (open) hours for a specified object
  82. * @param $oObject DBObject The object for which to compute the deadline
  83. * @param $iDuration integer The duration (in seconds) in the future
  84. * @param $oStartDate DateTime The starting point for the computation
  85. * @return DateTime The date/time for the deadline
  86. */
  87. public function GetDeadline($oObject, $iDuration, DateTime $oStartDate)
  88. {
  89. //echo "GetDeadline - default: ".$oStartDate->format('Y-m-d H:i:s')." + $iDuration<br/>\n";
  90. // Default implementation: 24x7, no holidays: to compute the deadline, just add
  91. // the specified duration to the given date/time
  92. $oResult = clone $oStartDate;
  93. $oResult->modify('+'.$iDuration.' seconds');
  94. return $oResult;
  95. }
  96. /**
  97. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  98. * @param $oObject DBObject The object for which to compute the duration
  99. * @param $oStartDate DateTime The starting point for the computation (default = now)
  100. * @param $oEndDate DateTime The ending point for the computation (default = now)
  101. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  102. */
  103. public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate)
  104. {
  105. //echo "GetOpenDuration - default: ".$oStartDate->format('Y-m-d H:i:s')." to ".$oEndDate->format('Y-m-d H:i:s')."<br/>\n";
  106. return abs($oEndDate->format('U') - $oStartDate->format('U'));
  107. }
  108. }
  109. ?>