Source for file Date.class.php

Documentation is available at Date.class.php

  1. <?php
  2. /**
  3. * @since 5/4/05
  4. * @package harmoni.primitives.chronology
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: Date.class.php,v 1.4 2007/09/04 20:25:25 adamfranco Exp $
  10. *
  11. * @link http://harmoni.sourceforge.net/
  12. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  13. */
  14. require_once(dirname(__FILE__)."/Timespan.class.php");
  15. require_once(dirname(__FILE__)."/DateAndTime.class.php");
  16.  
  17. /**
  18. * Instances of Date are Timespans with duration of 1 day.
  19. * Their default creation assumes a start of midnight in the local time zone.
  20. *
  21. * To create new Date instances, <b>use one of the static instance-creation
  22. * methods</b>, NOT 'new Date':
  23. * - {@link current Date::current()}
  24. * - {@link current Date::current()}
  25. * - {@link epoch Date::epoch()}
  26. * - {@link fromString Date::fromString($aString)}
  27. * - {@link starting Date::starting($aDateAndTime)}
  28. * - {@link startingDuration Date::startingDuration($aDateAndTime, $aDuration)}
  29. * - {@link startingEnding Date::startingEnding($startDateAndTime, $endDateAndTime)}
  30. * - {@link today Date::today()}
  31. * - {@link tomorrow Date::tomorrow()}
  32. * - {@link withJulianDayNumber Date::withJulianDayNumber($aJulianDayNumber)}
  33. * - {@link withYearDay Date::withYearDay($anIntYear, $anIntDayOfYear)}
  34. * - {@link yesterday Date::yesterday()}
  35. *
  36. * @since 5/4/05
  37. * @package harmoni.primitives.chronology
  38. *
  39. * @copyright Copyright &copy; 2005, Middlebury College
  40. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  41. *
  42. * @version $Id: Date.class.php,v 1.4 2007/09/04 20:25:25 adamfranco Exp $
  43. *
  44. * @link http://harmoni.sourceforge.net/
  45. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  46. */
  47. class Date
  48. extends Timespan
  49. {
  50.  
  51. /*******************************************************
  52. * Class Methods
  53. *********************************************************/
  54.  
  55. /*******************************************************
  56. * Class Methods - Instance Creation
  57. *
  58. * All static instance creation methods have an optional
  59. * $class parameter which is used to get around the limitations
  60. * of not being able to find the class of the object that
  61. * recieved the initial method call rather than the one in
  62. * which it is implemented. These parameters SHOULD NOT BE
  63. * USED OUTSIDE OF THIS PACKAGE.
  64. *********************************************************/
  65.  
  66. /**
  67. * Answer a new object that represents now.
  68. *
  69. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  70. * This parameter is used to get around the limitations of not being
  71. * able to find the class of the object that recieved the initial
  72. * method call.
  73. * @return object Date
  74. * @access public
  75. * @since 5/5/05
  76. * @static
  77. */
  78. function current ( $class = 'Date' ) {
  79. $obj = parent::current($class);
  80. return $obj;
  81. }
  82. /**
  83. * Answer a Date starting on the Squeak epoch: 1 January 1901
  84. *
  85. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  86. * This parameter is used to get around the limitations of not being
  87. * able to find the class of the object that recieved the initial
  88. * method call.
  89. * @return object Date
  90. * @access public
  91. * @since 5/5/05
  92. * @static
  93. */
  94. function epoch ( $class = 'Date' ) {
  95. $obj = parent::epoch($class);
  96. return $obj;
  97. }
  98. /**
  99. * Read a Date from the stream in any of the forms:
  100. *
  101. * - <day> <monthName> <year> (5 April 1982; 5-APR-82)
  102. *
  103. * - <monthName> <day> <year> (April 5, 1982)
  104. *
  105. * - <monthNumber> <day> <year> (4/5/82)
  106. * - <day><monthName><year> (5APR82)
  107. * - <four-digit year><two-digit monthNumber><two-digit day>
  108. * (19820405; 1982-04-05)
  109. *
  110. * @param string $aString
  111. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  112. * This parameter is used to get around the limitations of not being
  113. * able to find the class of the object that recieved the initial
  114. * method call.
  115. * @return object Date
  116. * @access public
  117. * @since 5/10/05
  118. */
  119. function fromString ( $aString, $class = 'Date' ) {
  120. $parser = StringParser::getParserFor($aString);
  121. if (!is_string($aString) || !preg_match('/[^\W]/', $aString) || !$parser) {
  122. $null = null;
  123. return $null;
  124. // die("'".$aString."' is not in a valid format.");
  125. }
  126. eval('$result = '.$class.'::withYearMonthDay($parser->year(),
  127. $parser->month(), $parser->day(), $class);');
  128. return $result;
  129. }
  130. /**
  131. * Create a new object starting now, with our default one day duration
  132. *
  133. * @param object DateAndTime $aDateAndTime
  134. * @return object Date
  135. * @access public
  136. * @since 5/5/05
  137. * @static
  138. */
  139. function starting ( $aDateAndTime, $class = 'Date' ) {
  140. $obj = parent::startingDuration($aDateAndTime->atMidnight(),
  141. Duration::withDays(1), $class);
  142.  
  143. return $obj;
  144. }
  145. /**
  146. * Create a new object starting from midnight
  147. *
  148. * @param object DateAndTime $aDateAndTime
  149. * @param object Duration $aDuration
  150. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  151. * This parameter is used to get around the limitations of not being
  152. * able to find the class of the object that recieved the initial
  153. * method call.
  154. * @return object Year
  155. * @access public
  156. * @since 5/5/05
  157. * @static
  158. */
  159. function startingDuration ( $aDateAndTime, $aDuration, $class = 'Date' ) {
  160. $obj = parent::startingDuration ( $aDateAndTime, $aDuration, $class );
  161. return $obj;
  162. }
  163. /**
  164. * Answer today's date
  165. *
  166. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  167. * This parameter is used to get around the limitations of not being
  168. * able to find the class of the object that recieved the initial
  169. * method call.
  170. * @return object Date
  171. * @access public
  172. * @since 5/10/05
  173. */
  174. function today ( $class = 'Date' ) {
  175. eval('$today = '.$class.'::current($class);');
  176. return $today;
  177. }
  178. /**
  179. * Answer tommorow's date
  180. *
  181. * @return object Date
  182. * @access public
  183. * @since 5/10/05
  184. */
  185. function tomorrow ( $class = 'Date' ) {
  186. eval('$today = '.$class.'::today($class);');
  187. $obj =$today->next();
  188. return $obj;
  189. }
  190. /**
  191. * Create a new object starting on the julian day number specified.
  192. *
  193. * @param integer $anInteger
  194. * @return object Date
  195. * @access public
  196. * @since 5/10/05
  197. */
  198. function withJulianDayNumber ( $anInteger, $class = 'Date' ) {
  199. eval('$result = '.$class.'::starting(DateAndTime::withJulianDayNumber($anInteger));');
  200. return $result;
  201. }
  202. /**
  203. * Create a new object starting on the year, month, and day of month specified.
  204. *
  205. * @param integer $anIntYear
  206. * @param integer $anIntOrStringMonth
  207. * @param integer $anIntDay
  208. * @return object Date
  209. * @access public
  210. * @since 5/10/05
  211. */
  212. function withYearMonthDay ( $anIntYear, $anIntOrStringMonth, $anIntDay, $class = 'Date' ) {
  213. eval('$result = '.$class.'::starting(DateAndTime::withYearMonthDay($anIntYear,
  214. $anIntOrStringMonth, $anIntDay));');
  215. return $result;
  216. }
  217. /**
  218. * Create a new object starting on the year and day of year specified.
  219. *
  220. * @param integer $anIntYear
  221. * @param integer $anIntDay
  222. * @return object Date
  223. * @access public
  224. * @since 5/10/05
  225. */
  226. function withYearDay ( $anIntYear, $anIntDay, $class = 'Date' ) {
  227. eval('$result = '.$class.'::starting(DateAndTime::withYearDay($anIntYear, $anIntDay));');
  228. return $result;
  229. }
  230. /**
  231. * Answer yesterday's date
  232. *
  233. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  234. * This parameter is used to get around the limitations of not being
  235. * able to find the class of the object that recieved the initial
  236. * method call.
  237. * @return object Date
  238. * @access public
  239. * @since 5/10/05
  240. */
  241. function yesterday ( $class = 'Date' ) {
  242. eval('$today = '.$class.'::today($class);');
  243. $obj =$today->previous();
  244. return $obj;
  245. }
  246. /*******************************************************
  247. * Instance methods - Accessing/Printing
  248. *********************************************************/
  249.  
  250. /**
  251. * Answer the receiver rendered in standard U.S.A format mm/dd/yyyy.
  252. *
  253. * @return string
  254. * @access public
  255. * @since 5/23/05
  256. */
  257. function mmddyyyyString () {
  258. return $this->printableStringWithFormat(array(2, 1, 3, '/', 1, 1, 2));
  259. }
  260. /**
  261. * Format is '4 June 2005'
  262. *
  263. * @return string
  264. * @access public
  265. * @since 5/20/05
  266. */
  267. function printableString () {
  268. return $this->printableStringWithFormat(array(1, 2, 3, ' ', 3, 1));
  269. }
  270. /**
  271. * Print a description of the receiver on aStream using the format
  272. * denoted the argument, formatArray:
  273. *
  274. * array(item, item, item, sep, monthfmt, yearfmt, twoDigits)
  275. *
  276. * items: 1=day 2=month 3=year will appear in the order given,
  277. *
  278. * separated by sep which is eaither an ascii code or character.
  279. *
  280. * monthFmt: 1=09 2=Sep 3=September
  281. *
  282. * yearFmt: 1=1996 2=96
  283. *
  284. * digits: (missing or)1=9 2=09.
  285. *
  286. * See the examples in printOn: and mmddyy
  287. *
  288. * @param array $formatArray
  289. * @return string
  290. * @access public
  291. * @since 5/20/05
  292. */
  293. function printableStringWithFormat ( $formatArray ) {
  294. $result = '';
  295. $twoDigits = (count($formatArray) > 6 && $formatArray[6] > 1);
  296. $monthFormat = $formatArray[4];
  297. $yearFormat = $formatArray[5];
  298. $separator = $formatArray[3];
  299. for ($i = 0; $i < 3; $i++) {
  300. $element = $formatArray[$i];
  301. switch ($element) {
  302. case 1:
  303. if ($twoDigits)
  304. $result .= str_pad($this->dayOfMonth(), 2, '0', STR_PAD_LEFT);
  305. else
  306. $result .= $this->dayOfMonth();
  307. break;
  308. case 2:
  309. if ($monthFormat == 1) {
  310. if ($twoDigits)
  311. $result .= str_pad($this->startMonth(), 2, '0', STR_PAD_LEFT);
  312. else
  313. $result .= $this->startMonth();
  314. } else if ($monthFormat == 2) {
  315. $result .= substr(Month::nameOfMonth($this->startMonth()), 0, 3);
  316. } else if ($monthFormat == 3) {
  317. $result .= Month::nameOfMonth($this->startMonth());
  318. }
  319. break;
  320. case 3:
  321. if ($yearFormat == 2) {
  322. $result .= str_pad(($this->startYear() % 100), 2, '0', STR_PAD_LEFT);
  323. } else
  324. $result .= $this->startYear();
  325. }
  326. if ($i < 2 && $separator)
  327. $result .= $separator;
  328. }
  329. return $result;
  330. }
  331. /**
  332. * Format the date in ISO 8601 standard like '2002-10-22'.
  333. *
  334. * @return string
  335. * @access public
  336. * @since 5/23/05
  337. */
  338. function yyyymmddString () {
  339. return $this->printableStringWithFormat(array(3, 2, 1, '-', 1, 1, 2));
  340. }
  341. /*******************************************************
  342. * Instance Methods - Operations
  343. *********************************************************/
  344.  
  345.  
  346. /**
  347. * Answer the date that occurs $anInteger days from this date
  348. *
  349. * @param integer $anInteger
  350. * @return object Date
  351. * @access public
  352. * @since 5/20/05
  353. */
  354. function addDays ( $anInteger ) {
  355. $asDateAndTime =$this->asDateAndTime();
  356. $newDateAndTime =$asDateAndTime->plus(Duration::withDays($anInteger));
  357. $obj =$newDateAndTime->asDate();
  358. return $obj;
  359. }
  360. /**
  361. * Answer the date that occurs $anInteger days before this date
  362. *
  363. * @param integer $anInteger
  364. * @return object Date
  365. * @access public
  366. * @since 5/23/05
  367. */
  368. function subtractDays ( $anInteger ) {
  369. $obj =$this->addDays(0 - $anInteger);
  370. return $obj;
  371. }
  372. /**
  373. * Answer the previous date whose weekday name is dayName.
  374. *
  375. * @param string $dayNameString
  376. * @return object Date
  377. * @access public
  378. * @since 5/23/05
  379. */
  380. function previousDayNamed ( $dayNameString ) {
  381. $days = abs($this->dayOfWeek() - (Week::indexOfDay($dayNameString) % 7));
  382. if ($days == 0)
  383. $days = 7;
  384. $obj =$this->subtractDays($days);
  385. return $obj;
  386. }
  387. /*******************************************************
  388. * Instance Methods - Converting
  389. *********************************************************/
  390.  
  391. /**
  392. * Answer the reciever as a Date
  393. *
  394. * @return object Date
  395. * @access public
  396. * @since 5/23/05
  397. */
  398. function asDate () {
  399. return $this;
  400. }
  401. /**
  402. * Answer the seconds since the Squeak epoch: 1 January 1901
  403. *
  404. * @return integer
  405. * @access public
  406. * @since 5/23/05
  407. */
  408. function asSeconds () {
  409. return $this->start->asSeconds();
  410. }
  411. }
  412.  
  413. ?>

Documentation generated on Wed, 19 Sep 2007 10:22:32 -0400 by phpDocumentor 1.3.0RC3