date_api.test 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * @file
  4. * Test Date API functions
  5. */
  6. class DateAPITestCase extends DrupalWebTestCase {
  7. /**
  8. * @todo.
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => t('Date API'),
  13. 'description' => t('Test Date API functions.') ,
  14. 'group' => t('Date'),
  15. );
  16. }
  17. /**
  18. * @todo.
  19. */
  20. public function setUp() {
  21. // Load the date_api module.
  22. parent::setUp('date_api');
  23. variable_set('date_api_use_iso8601', FALSE);
  24. variable_set('date_first_day', 1);
  25. }
  26. /**
  27. * @todo.
  28. */
  29. public function testDateAPI() {
  30. // Test date_format_date().
  31. $formatters = array(
  32. 'a',
  33. 'A',
  34. 'B',
  35. 'c',
  36. 'd',
  37. 'D',
  38. 'e',
  39. 'F',
  40. 'g',
  41. 'G',
  42. 'h',
  43. 'H',
  44. 'i',
  45. 'I',
  46. 'j',
  47. 'l',
  48. 'L',
  49. 'm',
  50. 'M',
  51. 'n',
  52. 'N',
  53. 'o',
  54. 'O',
  55. 'P',
  56. 'r',
  57. 'R',
  58. 's',
  59. 'S',
  60. 't',
  61. 'T',
  62. 'u',
  63. 'U',
  64. 'w',
  65. 'W',
  66. 'y',
  67. 'Y',
  68. 'z',
  69. 'Z',
  70. );
  71. foreach ($formatters as $formatter) {
  72. $date_api_format = date_format_date(date_now(), 'custom', $formatter);
  73. $php_format = date_format(date_now(), $formatter);
  74. $this->assertEqual($date_api_format, $php_format, 'Test that the "' . $formatter . '" formatter is formatted correctly by date_format_date()');
  75. }
  76. // Test the order of the weeks days for a calendar that starts on Monday and
  77. // one that starts on Sunday.
  78. variable_set('date_first_day', 1);
  79. $expected = array(0 => t('Mon'), 1 => t('Tue'), 2 => t('Wed'), 3 => t('Thu'), 4 => t('Fri'), 5 => t('Sat'), 6 => t('Sun'));
  80. $days = date_week_days_ordered(date_week_days_abbr(1));
  81. $this->assertEqual($expected, $days, 'Test that date_week_days_ordered() array starts on Monday when the site first day is on Monday.');
  82. variable_set('date_first_day', 0);
  83. $expected = array(0 => t('Sun'), 1 => t('Mon'), 2 => t('Tue'), 3 => t('Wed'), 4 => t('Thu'), 5 => t('Fri'), 6 => t('Sat'));
  84. $days = date_week_days_ordered(date_week_days_abbr(1));
  85. $this->assertEqual($expected, $days, 'Test that date_week_days_ordered() array starts on Sunday when the site first day is on Sunday.');
  86. // Test days in February for a leap year and a non-leap year.
  87. $expected = 28;
  88. $value = date_days_in_month(2005, 2);
  89. $this->assertEqual($expected, $value, "Test date_days_in_month(2, 2005): should be $expected, found $value.");
  90. $expected = 29;
  91. $value = date_days_in_month(2004, 2);
  92. $this->assertEqual($expected, $value, "Test date_days_in_month(2, 2004): should be $expected, found $value.");
  93. // Test days in year for a leap year and a non-leap year.
  94. $expected = 365;
  95. $value = date_days_in_year('2005-06-01 00:00:00');
  96. $this->assertEqual($expected, $value, "Test date_days_in_year(2005-06-01): should be $expected, found $value.");
  97. $expected = 366;
  98. $value = date_days_in_year('2004-06-01 00:00:00');
  99. $this->assertEqual($expected, $value, "Test date_days_in_year(2004-06-01): should be $expected, found $value.");
  100. // Test ISO weeks for a leap year and a non-leap year.
  101. $expected = 52;
  102. $value = date_iso_weeks_in_year('2008-06-01 00:00:00');
  103. $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2008-06-01): should be $expected, found $value.");
  104. $expected = 53;
  105. $value = date_iso_weeks_in_year('2009-06-01 00:00:00');
  106. $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2009-06-01): should be $expected, found $value.");
  107. // Test day of week for March 1, the day after leap day.
  108. $expected = 6;
  109. $value = date_day_of_week('2008-03-01 00:00:00');
  110. $this->assertEqual($expected, $value, "Test date_day_of_week(2008-03-01): should be $expected, found $value.");
  111. $expected = 0;
  112. $value = date_day_of_week('2009-03-01 00:00:00');
  113. $this->assertEqual($expected, $value, "Test date_day_of_week(2009-03-01): should be $expected, found $value.");
  114. // Test day of week name for March 1, the day after leap day.
  115. $expected = 'Sat';
  116. $value = date_day_of_week_name('2008-03-01 00:00:00');
  117. $this->assertEqual($expected, $value, "Test date_day_of_week_name(2008-03-01): should be $expected, found $value.");
  118. $expected = 'Sun';
  119. $value = date_day_of_week_name('2009-03-01 00:00:00');
  120. $this->assertEqual($expected, $value, "Test date_day_of_week_name(2009-03-01): should be $expected, found $value.");
  121. // Test week range with calendar weeks.
  122. variable_set('date_first_day', 0);
  123. variable_set('date_api_use_iso8601', FALSE);
  124. $expected = '2008-01-27 to 2008-02-03';
  125. $result = date_week_range(5, 2008);
  126. $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
  127. $this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2008): should be $expected, found $value.");
  128. $expected = '2009-01-25 to 2009-02-01';
  129. $result = date_week_range(5, 2009);
  130. $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
  131. $this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2009): should be $expected, found $value.");
  132. // And now with ISO weeks.
  133. variable_set('date_first_day', 1);
  134. variable_set('date_api_use_iso8601', TRUE);
  135. $expected = '2008-01-28 to 2008-02-04';
  136. $result = date_week_range(5, 2008);
  137. $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
  138. $this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2008): should be $expected, found $value.");
  139. $expected = '2009-01-26 to 2009-02-02';
  140. $result = date_week_range(5, 2009);
  141. $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
  142. $this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2009): should be $expected, found $value.");
  143. variable_set('date_api_use_iso8601', FALSE);
  144. // Find calendar week for a date.
  145. variable_set('date_first_day', 0);
  146. $expected = '09';
  147. $value = date_week('2008-03-01');
  148. $this->assertEqual($expected, $value, "Test date_week(2008-03-01): should be $expected, found $value.");
  149. $expected = '10';
  150. $value = date_week('2009-03-01');
  151. $this->assertEqual($expected, $value, "Test date_week(2009-03-01): should be $expected, found $value.");
  152. // Create date object from datetime string.
  153. $input = '2009-03-07 10:30';
  154. $timezone = 'America/Chicago';
  155. $date = new dateObject($input, $timezone);
  156. $value = $date->format('c');
  157. $expected = '2009-03-07T10:30:00-06:00';
  158. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone): should be $expected, found $value.");
  159. // Same during daylight savings time.
  160. $input = '2009-06-07 10:30';
  161. $timezone = 'America/Chicago';
  162. $date = new dateObject($input, $timezone);
  163. $value = $date->format('c');
  164. $expected = '2009-06-07T10:30:00-05:00';
  165. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone): should be $expected, found $value.");
  166. // Create date object from date string.
  167. $input = '2009-03-07';
  168. $timezone = 'America/Chicago';
  169. $date = new dateObject($input, $timezone);
  170. $value = $date->format('c');
  171. $expected = '2009-03-07T00:00:00-06:00';
  172. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone): should be $expected, found $value.");
  173. // Same during daylight savings time.
  174. $input = '2009-06-07';
  175. $timezone = 'America/Chicago';
  176. $date = new dateObject($input, $timezone);
  177. $value = $date->format('c');
  178. $expected = '2009-06-07T00:00:00-05:00';
  179. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone): should be $expected, found $value.");
  180. // Create date object from date array, date only.
  181. $input = array('year' => 2010, 'month' => 2, 'day' => 28);
  182. $timezone = 'America/Chicago';
  183. $date = new dateObject($input, $timezone);
  184. $value = $date->format('c');
  185. $expected = '2010-02-28T00:00:00-06:00';
  186. $this->assertEqual($expected, $value, "Test new dateObject(array('year' => 2010, 'month' => 2, 'day' => 28), $timezone): should be $expected, found $value.");
  187. // Create date object from date array with hour.
  188. $input = array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10);
  189. $timezone = 'America/Chicago';
  190. $date = new dateObject($input, $timezone);
  191. $value = $date->format('c');
  192. $expected = '2010-02-28T10:00:00-06:00';
  193. $this->assertEqual($expected, $value, "Test new dateObject(array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10), $timezone): should be $expected, found $value.");
  194. // 0 = January 1, 1970 00:00:00 (UTC);
  195. // 1000000000 = September 9, 2001 01:46:40 (UTC);
  196. // Create date object from unix timestamp and convert it to a local date.
  197. $input = 0;
  198. $timezone = 'UTC';
  199. $date = new dateObject($input, $timezone);
  200. $value = $date->format('c');
  201. $expected = '1970-01-01T00:00:00+00:00';
  202. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone): should be $expected, found $value.");
  203. $expected = 'UTC';
  204. $value = $date->getTimeZone()->getName();
  205. $this->assertEqual($expected, $value, "The current timezone is $value: should be $expected.");
  206. $expected = 0;
  207. $value = $date->getOffset();
  208. $this->assertEqual($expected, $value, "The current offset is $value: should be $expected.");
  209. $timezone = 'America/Los_Angeles';
  210. $date->setTimezone(new DateTimeZone($timezone));
  211. $value = $date->format('c');
  212. $expected = '1969-12-31T16:00:00-08:00';
  213. $this->assertEqual($expected, $value, "Test \$date->setTimezone(new DateTimeZone($timezone)): should be $expected, found $value.");
  214. $expected = 'America/Los_Angeles';
  215. $value = $date->getTimeZone()->getName();
  216. $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
  217. $expected = '-28800';
  218. $value = $date->getOffset();
  219. $this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
  220. // Convert the local version of a timestamp to UTC.
  221. $input = 0;
  222. $timezone = 'America/Los_Angeles';
  223. $date = new dateObject($input, $timezone);
  224. $offset = $date->getOffset();
  225. $value = $date->format('c');
  226. $expected = '1969-12-31T16:00:00-08:00';
  227. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone): should be $expected, found $value.");
  228. $expected = 'America/Los_Angeles';
  229. $value = $date->getTimeZone()->getName();
  230. $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
  231. $expected = '-28800';
  232. $value = $date->getOffset();
  233. $this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
  234. $timezone = 'UTC';
  235. $date->setTimezone(new DateTimeZone($timezone));
  236. $value = $date->format('c');
  237. $expected = '1970-01-01T00:00:00+00:00';
  238. $this->assertEqual($expected, $value, "Test \$date->setTimezone(new DateTimeZone($timezone)): should be $expected, found $value.");
  239. $expected = 'UTC';
  240. $value = $date->getTimeZone()->getName();
  241. $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
  242. $expected = '0';
  243. $value = $date->getOffset();
  244. $this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
  245. // Create date object from datetime string and convert it to a local date.
  246. $input = '1970-01-01 00:00:00';
  247. $timezone = 'UTC';
  248. $date = new dateObject($input, $timezone);
  249. $value = $date->format('c');
  250. $expected = '1970-01-01T00:00:00+00:00';
  251. $this->assertEqual($expected, $value, "Test new dateObject('$input', '$timezone'): should be $expected, found $value.");
  252. $expected = 'UTC';
  253. $value = $date->getTimeZone()->getName();
  254. $this->assertEqual($expected, $value, "The current timezone is $value: should be $expected.");
  255. $expected = 0;
  256. $value = $date->getOffset();
  257. $this->assertEqual($expected, $value, "The current offset is $value: should be $expected.");
  258. $timezone = 'America/Los_Angeles';
  259. $date->setTimezone(new DateTimeZone($timezone));
  260. $value = $date->format('c');
  261. $expected = '1969-12-31T16:00:00-08:00';
  262. $this->assertEqual($expected, $value, "Test \$date->setTimezone(new DateTimeZone($timezone)): should be $expected, found $value.");
  263. $expected = 'America/Los_Angeles';
  264. $value = $date->getTimeZone()->getName();
  265. $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
  266. $expected = '-28800';
  267. $value = $date->getOffset();
  268. $this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
  269. // Convert the local version of a datetime string to UTC.
  270. $input = '1969-12-31 16:00:00';
  271. $timezone = 'America/Los_Angeles';
  272. $date = new dateObject($input, $timezone);
  273. $offset = $date->getOffset();
  274. $value = $date->format('c');
  275. $expected = '1969-12-31T16:00:00-08:00';
  276. $this->assertEqual($expected, $value, "Test new dateObject('$input', '$timezone'): should be $expected, found $value.");
  277. $expected = 'America/Los_Angeles';
  278. $value = $date->getTimeZone()->getName();
  279. $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
  280. $expected = '-28800';
  281. $value = $date->getOffset();
  282. $this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
  283. $timezone = 'UTC';
  284. $date->setTimezone(new DateTimeZone($timezone));
  285. $value = $date->format('c');
  286. $expected = '1970-01-01T00:00:00+00:00';
  287. $this->assertEqual($expected, $value, "Test \$date->setTimezone(new DateTimeZone($timezone)): should be $expected, found $value.");
  288. $expected = 'UTC';
  289. $value = $date->getTimeZone()->getName();
  290. $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
  291. $expected = '0';
  292. $value = $date->getOffset();
  293. $this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
  294. // Create year-only date.
  295. $input = '2009';
  296. $timezone = NULL;
  297. $format = 'Y';
  298. $date = new dateObject($input, $timezone, $format);
  299. $value = $date->format('Y');
  300. $expected = '2009';
  301. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone, $format): should be $expected, found $value.");
  302. // Create month and year-only date.
  303. $input = '2009-10';
  304. $timezone = NULL;
  305. $format = 'Y-m';
  306. $date = new dateObject($input, $timezone, $format);
  307. $value = $date->format('Y-m');
  308. $expected = '2009-10';
  309. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone, $format): should be $expected, found $value.");
  310. // Create time-only date.
  311. $input = '0000-00-00T10:30:00';
  312. $timezone = NULL;
  313. $format = 'Y-m-d\TH:i:s';
  314. $date = new dateObject($input, $timezone, $format);
  315. $value = $date->format('H:i:s');
  316. $expected = '10:30:00';
  317. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone, $format): should be $expected, found $value.");
  318. // Create time-only date.
  319. $input = '10:30:00';
  320. $timezone = NULL;
  321. $format = 'H:i:s';
  322. $date = new dateObject($input, $timezone, $format);
  323. $value = $date->format('H:i:s');
  324. $expected = '10:30:00';
  325. $this->assertEqual($expected, $value, "Test new dateObject($input, $timezone, $format): should be $expected, found $value.");
  326. // Test date ranges.
  327. $valid = array(
  328. '-20:+20',
  329. '-1:+0',
  330. '-10:-5',
  331. '2000:2020',
  332. '-10:2010',
  333. '1980:-10',
  334. '1920:+20',
  335. );
  336. $invalid = array(
  337. 'abc',
  338. 'abc:+20',
  339. '1920:+20a',
  340. '+-20:+-30',
  341. '12:12',
  342. '0:+20',
  343. '-20:0',
  344. );
  345. foreach ($valid as $range) {
  346. $this->assertTrue(date_range_valid($range), "$range recognized as a valid date range.");
  347. }
  348. foreach ($invalid as $range) {
  349. $this->assertFalse(date_range_valid($range), "$range recognized as an invalid date range.");
  350. }
  351. // Test for invalid month names when we are using a short version of the month
  352. $input = '23 abc 2012';
  353. $timezone = NULL;
  354. $format = 'd M Y';
  355. $date = new dateObject($input, $timezone, $format);
  356. $this->assertNotEqual(count($date->errors), 0, '23 abc 2012 should be an invalid date');
  357. }
  358. /**
  359. * @todo.
  360. */
  361. public function tearDown() {
  362. variable_del('date_first_day');
  363. variable_del('date_api_use_iso8601');
  364. parent::tearDown();
  365. }
  366. }