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