summaryrefslogtreecommitdiff
path: root/t/03-date-handling.t
blob: 55fb424900323c23e7ddbc0eb68f11dc3fdb60c8 (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Note: This file assumes good dates, SL behaviour with bad dates is undefined
  4. #
  5. #LedgerSMB/Form.pm:3153:sub from_to
  6. #LedgerSMB/Form.pm:1361:sub add_date {
  7. use strict;
  8. use warnings;
  9. use Test::More 'no_plan';
  10. use Math::BigFloat;
  11. use LedgerSMB::Sysconfig;
  12. use LedgerSMB::Form;
  13. use LedgerSMB::Locale;
  14. my $form = new Form;
  15. my $locale_en = LedgerSMB::Locale->get_handle('en');
  16. my $locale_es = LedgerSMB::Locale->get_handle('es');
  17. my %myconfig;
  18. ok(defined $form);
  19. isa_ok($form, 'Form');
  20. $form->{dbh} = ${LedgerSMB::Sysconfig::GLOBALDBH};
  21. my @formats = ( ['mm-dd-yy', '-', 2, '02-29-00', '03-01-00'],
  22. ['mm/dd/yy', '/', 2, '02/29/00', '03/01/00'],
  23. ['dd-mm-yy', '-', 2, '29-02-00', '01-03-00'],
  24. ['dd/mm/yy', '/', 2, '29/02/00', '01/03/00'],
  25. ['dd.mm.yy', '.', 2, '29.02.00', '01.03.00'],
  26. # ['yyyymmdd', '', 4, '20000229', '20000301'],
  27. ['yyyy-mm-dd', '-', 4, '2000-02-29', '2000-03-01']);
  28. my @months = ('January', 'February', 'March', 'April', 'May ', 'June',
  29. 'July', 'August', 'September', 'October', 'November', 'December');
  30. my $today = `date +\%F`;
  31. chomp $today;
  32. my %today_parts;
  33. $today_parts{'yyyy'} = `date +\%Y`;
  34. $today_parts{'yy'} = $today_parts{'yyyy'};
  35. $today_parts{'yy'} =~ s/^..//;
  36. $today_parts{'mm'} = `date +\%m`;
  37. $today_parts{'dd'} = `date +\%d`;
  38. chomp $today_parts{'yyyy'};
  39. chomp $today_parts{'yy'};
  40. chomp $today_parts{'mm'};
  41. chomp $today_parts{'dd'};
  42. # $locale->date checks
  43. # Note that $locale->date assumes the year range 2000-2099
  44. # Note that $locale->date does not perform language-specific long forms
  45. # Note that $locale->date also takes in yyyymmdd
  46. foreach my $format (0 .. $#formats) {
  47. %myconfig = (dateformat => $formats[$format][0]);
  48. my $fmt = $formats[$format][0];
  49. my $sep = $formats[$format][1];
  50. my $yearcount = $formats[$format][2];
  51. my $result = $formats[$format][3];
  52. $result =~ s/^(.*)(20)?00(.*)$/${1}2000${3}/ if $yearcount == 2;
  53. cmp_ok($locale_en->date(\%myconfig), 'eq',
  54. '', "date, $fmt: empty string");
  55. cmp_ok($locale_en->date(\%myconfig, $formats[$format][3]), 'eq',
  56. $result, "date, $fmt: short");
  57. cmp_ok($locale_en->date(\%myconfig, '20000229'), 'eq',
  58. $result, "date, $fmt: chopped");
  59. for my $mm (1 .. 12) {
  60. my $start = $fmt;
  61. my $temp = sprintf('%02d', $mm);
  62. my $month_en = $locale_en->text($months[$mm - 1]);
  63. my $month_es = $locale_es->text($months[$mm - 1]);
  64. $start =~ s/dd/29/;
  65. $start =~ s/yyyy/2000/;
  66. $start =~ s/yy/00/;
  67. $start =~ s/mm/$temp/;
  68. cmp_ok($locale_es->date(\%myconfig, $start, 1), 'eq',
  69. "$month_es 29 2000", "date, $start, $fmt: long, es");
  70. cmp_ok($locale_en->date(\%myconfig, $start, 1), 'eq',
  71. "$month_en 29 2000", "date, $start, $fmt: long, en");
  72. }
  73. }
  74. # $form->current_date checks
  75. # Note that $form->current_date always uses the database
  76. foreach my $format (0 .. $#formats) {
  77. %myconfig = (dateformat => $formats[$format][0]);
  78. my $fmt = $formats[$format][0];
  79. my $sep = $formats[$format][1];
  80. my $yearcount = $formats[$format][2];
  81. is($form->current_date(\%myconfig), $today,
  82. "current_date, $fmt: $today");
  83. is($form->current_date(\%myconfig, $formats[$format][3]),
  84. '2000-02-29', "current_date, $fmt: 2000-02-29");
  85. is($form->current_date(\%myconfig, $formats[$format][3], 1),
  86. '2000-03-01', "current_date, $fmt: 2000-03-01");
  87. }
  88. # $form->datetonum checks
  89. # Note that $form->datetonum assumes the year range 2000-2099
  90. foreach my $format (0 .. $#formats) {
  91. %myconfig = (dateformat => $formats[$format][0]);
  92. my $fmt = $formats[$format][0];
  93. my $sep = $formats[$format][1];
  94. my $yearcount = $formats[$format][2];
  95. cmp_ok($form->datetonum(\%myconfig, $formats[$format][3]), 'eq',
  96. '20000229', "datetonum, $fmt");
  97. }
  98. # $form->split_date checks
  99. # Note that $form->split_date assumes the year range 2000-2099
  100. # Note that $form->split_date only outputs two digit years
  101. foreach my $format (0 .. $#formats) {
  102. %myconfig = (dateformat => $formats[$format][0]);
  103. my $fmt = $formats[$format][0];
  104. my $sep = $formats[$format][1];
  105. my $yearcount = $formats[$format][2];
  106. my @output = $form->split_date($fmt, $formats[$format][3]);
  107. my $rv = $fmt;
  108. $rv =~ s/\Q$sep\E//g;
  109. $rv =~ s/(yy)?yy/$output[1]/;
  110. $rv =~ s/mm/$output[2]/;
  111. $rv =~ s/dd/$output[3]/;
  112. cmp_ok($output[1], 'eq', '00', "split_date specified, year");
  113. cmp_ok($output[2], 'eq', '02', "split_date specified, month");
  114. cmp_ok($output[3], 'eq', '29', "split_date specified, day");
  115. cmp_ok($output[0], 'eq', $rv, "split_date specified, unit");
  116. @output = $form->split_date($fmt);
  117. my $rv = $fmt;
  118. $rv =~ s/\Q$sep\E//g;
  119. $rv =~ s/(yy)?yy/$output[1]/;
  120. $rv =~ s/mm/$output[2]/;
  121. $rv =~ s/dd/$output[3]/;
  122. my $tv = $fmt;
  123. $tv =~ s/\Q$sep\E//g;
  124. $tv =~ s/(yy)?yy/$today_parts{'yy'}/;
  125. $tv =~ s/mm/$today_parts{'mm'}/;
  126. $tv =~ s/dd/$today_parts{'dd'}/;
  127. cmp_ok($output[1], 'eq', $today_parts{'yy'},
  128. "split_date unspecified, year");
  129. cmp_ok($output[2], 'eq', $today_parts{'mm'},
  130. "split_date unspecified, month");
  131. cmp_ok($output[3], 'eq', $today_parts{'dd'},
  132. "split_date unspecified, day");
  133. }
  134. # $form->format_date checks
  135. # Note that $form->format_date always outputs four digit years
  136. foreach my $format (0 .. $#formats) {
  137. $form->{db_dateformat} = $formats[$format][0];
  138. my $fmt = $formats[$format][0];
  139. my $sep = $formats[$format][1];
  140. my $yearcount = $formats[$format][2];
  141. my $results = $fmt;
  142. $results =~ s/(yy)?yy/2000/;
  143. $results =~ s/mm/02/;
  144. $results =~ s/dd/29/;
  145. cmp_ok($form->format_date('2000-02-29'), 'eq',
  146. $results, "format_date, $fmt, ISO");
  147. cmp_ok($form->format_date($formats[$format][3]), 'eq',
  148. $formats[$format][3], "format_date, $fmt, non-ISO");
  149. }