summaryrefslogtreecommitdiff
path: root/t/03-date-handling.t
blob: c2e009ac25e97b81dcf94aa994c0f06e4262d879 (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Note: This file assumes good dates, SL behaviour with bad dates is undefined
  4. #
  5. use strict;
  6. use warnings;
  7. use Test::More 'no_plan';
  8. use Math::BigFloat;
  9. use LedgerSMB::Sysconfig;
  10. use LedgerSMB::Form;
  11. use LedgerSMB::Locale;
  12. my $form = new Form;
  13. my $locale_en = LedgerSMB::Locale->get_handle('en');
  14. my $locale_es = LedgerSMB::Locale->get_handle('es');
  15. my %myconfig;
  16. ok(defined $form);
  17. isa_ok($form, 'Form');
  18. $form->{dbh} = ${LedgerSMB::Sysconfig::GLOBALDBH};
  19. my @formats = ( ['mm-dd-yy', '-', 2, '02-29-00', '03-01-00'],
  20. ['mm/dd/yy', '/', 2, '02/29/00', '03/01/00'],
  21. ['dd-mm-yy', '-', 2, '29-02-00', '01-03-00'],
  22. ['dd/mm/yy', '/', 2, '29/02/00', '01/03/00'],
  23. ['dd.mm.yy', '.', 2, '29.02.00', '01.03.00'],
  24. # ['yyyymmdd', '', 4, '20000229', '20000301'],
  25. ['yyyy-mm-dd', '-', 4, '2000-02-29', '2000-03-01']);
  26. my @months = ('January', 'February', 'March', 'April', 'May ', 'June',
  27. 'July', 'August', 'September', 'October', 'November', 'December');
  28. my @mon = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  29. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  30. my %month_num = ('01' => '31', '02' => '28', '03' => '31', '04' => '30',
  31. '05' => '31', '06' => '30', '07' => '31', '08' => '31',
  32. '09' => '30', '10' => '31', '11' => '30', '12' => '31');
  33. my $today = `date +\%F`;
  34. chomp $today;
  35. my %today_parts;
  36. $today_parts{'yyyy'} = `date +\%Y`;
  37. $today_parts{'yy'} = $today_parts{'yyyy'};
  38. $today_parts{'yy'} =~ s/^..//;
  39. $today_parts{'mm'} = `date +\%m`;
  40. $today_parts{'dd'} = `date +\%d`;
  41. chomp $today_parts{'yyyy'};
  42. chomp $today_parts{'yy'};
  43. chomp $today_parts{'mm'};
  44. chomp $today_parts{'dd'};
  45. # $locale->date checks
  46. # Note that $locale->date assumes the year range 2000-2099
  47. # Note that $locale->date does not perform language-specific long forms
  48. # Note that $locale->date also takes in yyyymmdd
  49. foreach my $format (0 .. $#formats) {
  50. %myconfig = (dateformat => $formats[$format][0]);
  51. my $fmt = $formats[$format][0];
  52. my $sep = $formats[$format][1];
  53. my $yearcount = $formats[$format][2];
  54. my $result = $formats[$format][3];
  55. $result =~ s/^(.*)(20)?00(.*)$/${1}2000${3}/ if $yearcount == 2;
  56. cmp_ok($locale_en->date(\%myconfig), 'eq',
  57. '', "date, $fmt: empty string");
  58. cmp_ok($locale_en->date(\%myconfig, $formats[$format][3]), 'eq',
  59. $result, "date, $fmt: short");
  60. cmp_ok($locale_en->date(\%myconfig, '20000229'), 'eq',
  61. $result, "date, $fmt: chopped");
  62. for my $mm (1 .. 12) {
  63. my $start = $fmt;
  64. my $temp = sprintf('%02d', $mm);
  65. my $month_en = $locale_en->text($months[$mm - 1]);
  66. my $month_en_2 = $locale_en->text($mon[$mm - 1]);
  67. my $month_es = $locale_es->text($months[$mm - 1]);
  68. $start =~ s/dd/29/;
  69. $start =~ s/yyyy/2000/;
  70. $start =~ s/yy/00/;
  71. $start =~ s/mm/$temp/;
  72. cmp_ok($locale_es->date(\%myconfig, $start, 1), 'eq',
  73. "$month_es 29 2000", "date, $start, $fmt: long, es");
  74. cmp_ok($locale_en->date(\%myconfig, $start, 1), 'eq',
  75. "$month_en 29 2000", "date, $start, $fmt: long, en");
  76. cmp_ok($locale_en->date(\%myconfig, $start, ''), 'eq',
  77. "$month_en_2 29 2000", "date, $start, $fmt: '', en") if
  78. $start !~ /^\d{4}\D/; # Ack... special case
  79. }
  80. cmp_ok($locale_en->date(\%myconfig, '2007-05-18', ''), 'eq',
  81. "2007-05-18", "date, 2007-05-18, $fmt: '', en");
  82. }
  83. # $form->current_date checks
  84. # Note that $form->current_date always uses the database
  85. # Note that $form->current_date can take four digit years with all formats
  86. # Note that $form->current_date will always accept a dateformat of 'yyyymmdd'
  87. foreach my $format (0 .. $#formats) {
  88. %myconfig = (dateformat => $formats[$format][0]);
  89. my $fmt = $formats[$format][0];
  90. my $sep = $formats[$format][1];
  91. my $yearcount = $formats[$format][2];
  92. my $tv = $fmt;
  93. $tv =~ s/(yy)?yy/$today_parts{'yyyy'}/;
  94. $tv =~ s/mm/$today_parts{'mm'}/;
  95. $tv =~ s/dd/$today_parts{'dd'}/;
  96. is($form->current_date(\%myconfig),
  97. $today, "current_date, $fmt: $today");
  98. is($form->current_date(\%myconfig, $formats[$format][3]),
  99. '2000-02-29', "current_date, $fmt: 2000-02-29");
  100. is($form->current_date(\%myconfig, $formats[$format][3], 1),
  101. '2000-03-01', "current_date, $fmt: 2000-03-01");
  102. is($form->current_date(\%myconfig, $tv),
  103. $today, "current_date, $fmt: $tv");
  104. $tv = "$today_parts{'yyyy'}$today_parts{'mm'}$today_parts{'dd'}";
  105. is($form->current_date(\%myconfig, $tv),
  106. $today, "current_date, $fmt: $tv");
  107. }
  108. # $form->datetonum checks
  109. # Note that $form->datetonum assumes the year range 2000-2099
  110. # Note that $form->datetonum is identity if there is no date or non-digit
  111. foreach my $format (0 .. $#formats) {
  112. %myconfig = (dateformat => $formats[$format][0]);
  113. my $fmt = $formats[$format][0];
  114. my $sep = $formats[$format][1];
  115. my $yearcount = $formats[$format][2];
  116. cmp_ok($form->datetonum(\%myconfig, $formats[$format][3]), 'eq',
  117. '20000229', "datetonum, $fmt");
  118. }
  119. cmp_ok($form->datetonum(\%myconfig), 'eq', '', "datetonum, empty string");
  120. cmp_ok($form->datetonum(\%myconfig, '1234'), 'eq', '1234', "datetonum, 1234");
  121. # $form->split_date checks
  122. # Note that $form->split_date assumes the year range 2000-2099
  123. # Note that $form->split_date only outputs two digit years
  124. # Note that $form->split_date if a date provided without non-digit, identity
  125. foreach my $format (0 .. $#formats) {
  126. %myconfig = (dateformat => $formats[$format][0]);
  127. my $fmt = $formats[$format][0];
  128. my $sep = $formats[$format][1];
  129. my $yearcount = $formats[$format][2];
  130. my @output = $form->split_date($fmt, $formats[$format][3]);
  131. my $rv = $fmt;
  132. $rv =~ s/\Q$sep\E//g;
  133. $rv =~ s/(yy)?yy/$output[1]/;
  134. $rv =~ s/mm/$output[2]/;
  135. $rv =~ s/dd/$output[3]/;
  136. cmp_ok($output[1], 'eq', '00', "split_date specified, year");
  137. cmp_ok($output[2], 'eq', '02', "split_date specified, month");
  138. cmp_ok($output[3], 'eq', '29', "split_date specified, day");
  139. cmp_ok($output[0], 'eq', $rv, "split_date specified, unit");
  140. @output = $form->split_date($fmt);
  141. $rv = $fmt;
  142. $rv =~ s/\Q$sep\E//g;
  143. $rv =~ s/(yy)?yy/$output[1]/;
  144. $rv =~ s/mm/$output[2]/;
  145. $rv =~ s/dd/$output[3]/;
  146. my $tv = $fmt;
  147. $tv =~ s/\Q$sep\E//g;
  148. $tv =~ s/(yy)?yy/$today_parts{'yy'}/;
  149. $tv =~ s/mm/$today_parts{'mm'}/;
  150. $tv =~ s/dd/$today_parts{'dd'}/;
  151. cmp_ok($output[1], 'eq', $today_parts{'yy'},
  152. "split_date unspecified, year");
  153. cmp_ok($output[2], 'eq', $today_parts{'mm'},
  154. "split_date unspecified, month");
  155. cmp_ok($output[3], 'eq', $today_parts{'dd'},
  156. "split_date unspecified, day");
  157. @output = $form->split_date($fmt, '12345');
  158. cmp_ok($output[0], 'eq', '12345',
  159. 'split_date, 12345');
  160. }
  161. # $form->format_date checks
  162. # Note that $form->format_date always outputs four digit years
  163. foreach my $format (0 .. $#formats) {
  164. $form->{db_dateformat} = $formats[$format][0];
  165. my $fmt = $formats[$format][0];
  166. my $sep = $formats[$format][1];
  167. my $yearcount = $formats[$format][2];
  168. my $results = $fmt;
  169. $results =~ s/(yy)?yy/2000/;
  170. $results =~ s/mm/02/;
  171. $results =~ s/dd/29/;
  172. cmp_ok($form->format_date('2000-02-29'), 'eq',
  173. $results, "format_date, $fmt, ISO");
  174. cmp_ok($form->format_date($formats[$format][3]), 'eq',
  175. $formats[$format][3], "format_date, $fmt, non-ISO");
  176. }
  177. # $form->from_to checks
  178. # Note that $form->from_to requires $form->format_date
  179. # Note that $form->from_to outputs four digit years
  180. # Note that $form->from_to outputs 1999-12-31 (formatted) if no input given
  181. # Note that $form->from_to outputs the last day of the previous year if only year given
  182. # Note that $form->from_to outputs the last day of the chosen month if month given
  183. # Note that $form->from_to $interval of 0 is current day
  184. # Note that $form->from_to $interval is an integral quantity of months
  185. # Note that $form->from_to will fail if ($interval + $month) > 22
  186. # (2 + 23), 25 - 12, 13 - 1, 12
  187. foreach my $format (0 .. $#formats) {
  188. $form->{db_dateformat} = $formats[$format][0];
  189. my $fmt = $formats[$format][0];
  190. my $sep = $formats[$format][1];
  191. my $yearcount = $formats[$format][2];
  192. my $results = $fmt;
  193. $results =~ s/(yy)?yy/1999/;
  194. $results =~ s/mm/12/;
  195. $results =~ s/dd/31/;
  196. cmp_ok($form->from_to(), 'eq',
  197. $results, "from_to, $fmt, unspecified");
  198. $results =~ s/1999/2006/;
  199. cmp_ok($form->from_to('07'), 'eq',
  200. $results, "from_to, $fmt, 07");
  201. cmp_ok($form->from_to('2007'), 'eq',
  202. $results, "from_to, $fmt, 2007");
  203. $results =~ s/2006/2007/;
  204. $results =~ s/12/05/;
  205. cmp_ok($form->from_to('07', '05'), 'eq',
  206. $results, "from_to, $fmt, 07-05");
  207. cmp_ok($form->from_to('2007', '05'), 'eq',
  208. $results, "from_to, $fmt, 2007-05");
  209. $results =~ s/05/02/;
  210. $results =~ s/31/28/;
  211. cmp_ok($form->from_to('07', '02'), 'eq',
  212. $results, "from_to, $fmt, 07-02");
  213. cmp_ok($form->from_to('2007', '02'), 'eq',
  214. $results, "from_to, $fmt, 2007-02");
  215. $results =~ s/2007/2000/;
  216. $results =~ s/28/29/;
  217. cmp_ok($form->from_to('00', '02'), 'eq',
  218. $results, "from_to, $fmt, 00-02 leap day");
  219. cmp_ok($form->from_to('2000', '02'), 'eq',
  220. $results, "from_to, $fmt, 2000-02 leap day");
  221. $results =~ s/29/31/;
  222. $results =~ s/02/01/;
  223. cmp_ok($form->from_to('00', '01'), 'eq',
  224. $results, "from_to, $fmt, 00-01 year edge");
  225. cmp_ok($form->from_to('2000', '01'), 'eq',
  226. $results, "from_to, $fmt, 2000-01 year edge");
  227. $results =~ s/01/12/;
  228. cmp_ok($form->from_to('00', '12'), 'eq',
  229. $results, "from_to, $fmt, 00-12 year edge");
  230. cmp_ok($form->from_to('2000', '12'), 'eq',
  231. $results, "from_to, $fmt, 2000-12 year edge");
  232. $results =~ s/12/02/;
  233. $results =~ s/31/29/;
  234. cmp_ok($form->from_to('00', '02', '1'), 'eq',
  235. $results, "from_to, $fmt, 00-02, 1 interval");
  236. cmp_ok($form->from_to('2000', '02', '1'), 'eq',
  237. $results, "from_to, $fmt, 2000-02, 1 interval");
  238. $results =~ s/29/28/;
  239. my $month;
  240. my $lastmonth;
  241. for (2 .. 11) {
  242. $month = sprintf '%02d', $_ + 1;
  243. $lastmonth = sprintf '%02d', $_;
  244. $results =~ s/$lastmonth/$month/;
  245. $results =~ s/$month_num{$lastmonth}/$month_num{$month}/;
  246. cmp_ok($form->from_to('00', '02', $_), 'eq',
  247. $results, "from_to, $fmt, 00-02, $_ interval");
  248. cmp_ok($form->from_to('2000', '02', $_), 'eq',
  249. $results, "from_to, $fmt, 2000-02, $_ interval");
  250. }
  251. $results =~ s/2000/2001/;
  252. for (0 .. 10) {
  253. $month = sprintf '%02d', $_ + 1;
  254. $lastmonth = sprintf '%02d', $_;
  255. $lastmonth = '12' if $lastmonth eq '00';
  256. $results =~ s/([^0])$lastmonth/${1}$month/;
  257. $results =~ s/^$lastmonth/$month/;
  258. $results =~ s/$month_num{$lastmonth}/$month_num{$month}/;
  259. cmp_ok($form->from_to('00', '02', $_ + 12), 'eq',
  260. $results, "from_to, $fmt, 00-02, $_ + 12 interval");
  261. cmp_ok($form->from_to('2000', '02', $_ + 12), 'eq',
  262. $results, "from_to, $fmt, 2000-02, $_ + 12 interval");
  263. }
  264. $results =~ s/2001/$today_parts{'yyyy'}/;
  265. $results =~ s/11/$today_parts{'mm'}/;
  266. $results =~ s/30/$today_parts{'dd'}/;
  267. cmp_ok($form->from_to('00', '02', '0'), 'eq',
  268. $results, "from_to, $fmt, 00-02, 0 interval (today)");
  269. cmp_ok($form->from_to('2000', '02', '0'), 'eq',
  270. $results, "from_to, $fmt, 2000-02, 0 interval (today)");
  271. }
  272. # $form->add_date checks
  273. # returns undef if no date passed
  274. # valid units: days, weeks, months, years
  275. # all uses in LSMB use days unit
  276. # has no error handling capabilities
  277. foreach my $format (0 .. $#formats) {
  278. $form->{db_dateformat} = $formats[$format][0];
  279. %myconfig = (dateformat => $formats[$format][0]);
  280. my $fmt = $formats[$format][0];
  281. my $sep = $formats[$format][1];
  282. my $yearcount = $formats[$format][2];
  283. my $start = $fmt;
  284. $start =~ s/(yy)?yy/2000/;
  285. $start =~ s/mm/01/;
  286. $start =~ s/dd/29/;
  287. my $results = $start;
  288. $results =~ s/29/30/;
  289. cmp_ok($form->add_date(\%myconfig, $start, 1, 'days'), 'eq',
  290. $results, "add_date, $fmt, 1 days, 2000-01-29");
  291. $results =~ s/30/31/;
  292. cmp_ok($form->add_date(\%myconfig, $start, 2, 'days'), 'eq',
  293. $results, "add_date, $fmt, 2 days, 2000-01-29");
  294. $results =~ s/31/05/;
  295. $results =~ s/01/02/;
  296. cmp_ok($form->add_date(\%myconfig, $start, 1, 'weeks'), 'eq',
  297. $results, "add_date, $fmt, 1 weeks, 2000-01-29");
  298. $results =~ s/05/12/;
  299. cmp_ok($form->add_date(\%myconfig, $start, 2, 'weeks'), 'eq',
  300. $results, "add_date, $fmt, 2 weeks, 2000-01-29");
  301. $results =~ s/12/29/;
  302. cmp_ok($form->add_date(\%myconfig, $start, 1, 'months'), 'eq',
  303. $results, "add_date, $fmt, 1 months, 2000-01-29");
  304. $results =~ s/02/03/;
  305. cmp_ok($form->add_date(\%myconfig, $start, 2, 'months'), 'eq',
  306. $results, "add_date, $fmt, 2 months, 2000-01-29");
  307. $results = $start;
  308. $results =~ s/01/11/;
  309. cmp_ok($form->add_date(\%myconfig, $start, 10, 'months'), 'eq',
  310. $results, "add_date, $fmt, 10 months, 2000-01-29");
  311. $results = $start;
  312. $results =~ s/01/12/;
  313. cmp_ok($form->add_date(\%myconfig, $start, 11, 'months'), 'eq',
  314. $results, "add_date, $fmt, 11 months, 2000-01-29");
  315. $results = $start;
  316. $results =~ s/2000/2001/;
  317. cmp_ok($form->add_date(\%myconfig, $start, 12, 'months'), 'eq',
  318. $results, "add_date, $fmt, 12 months, 2000-01-29");
  319. cmp_ok($form->add_date(\%myconfig, $start, 1, 'years'), 'eq',
  320. $results, "add_date, $fmt, 1 years, 2000-01-29");
  321. $results =~ s/2001/2002/;
  322. cmp_ok($form->add_date(\%myconfig, $start, 2, 'years'), 'eq',
  323. $results, "add_date, $fmt, 2 years, 2000-01-29");
  324. }
  325. cmp_ok($form->add_date(\%myconfig, '20000129', 2, 'years'), 'eq',
  326. '20020129', 'add_date, yyyymmdd, 2 years, 20000129');
  327. ok(!defined $form->add_date(\%myconfig),
  328. 'add_date, undef if no date');