summaryrefslogtreecommitdiff
path: root/t/03-date-handling.t
blob: 68efe7dc6513fa4c707d6e32f2462b9068661b69 (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. foreach my $format (0 .. $#formats) {
  46. %myconfig = (dateformat => $formats[$format][0]);
  47. my $fmt = $formats[$format][0];
  48. my $sep = $formats[$format][1];
  49. my $yearcount = $formats[$format][2];
  50. my $result = $formats[$format][3];
  51. $result =~ s/^(.*)(20)?00(.*)$/${1}2000${3}/ if $yearcount == 2;
  52. cmp_ok($locale_en->date(\%myconfig), 'eq',
  53. '', "date, $fmt: empty string");
  54. cmp_ok($locale_en->date(\%myconfig, $formats[$format][3]), 'eq',
  55. $result, "date, $fmt: short");
  56. for my $mm (1 .. 12) {
  57. my $start = $fmt;
  58. my $temp = sprintf('%02d', $mm);
  59. my $month_en = $locale_en->text($months[$mm - 1]);
  60. my $month_es = $locale_es->text($months[$mm - 1]);
  61. $start =~ s/dd/29/;
  62. $start =~ s/yyyy/2000/;
  63. $start =~ s/yy/00/;
  64. $start =~ s/mm/$temp/;
  65. cmp_ok($locale_es->date(\%myconfig, $start, 1), 'eq',
  66. "$month_es 29 2000", "date, $start, $fmt: long, es");
  67. cmp_ok($locale_en->date(\%myconfig, $start, 1), 'eq',
  68. "$month_en 29 2000", "date, $start, $fmt: long, en");
  69. }
  70. }
  71. # $form->current_date checks
  72. foreach my $format (0 .. $#formats) {
  73. %myconfig = (dateformat => $formats[$format][0]);
  74. my $fmt = $formats[$format][0];
  75. my $sep = $formats[$format][1];
  76. my $yearcount = $formats[$format][2];
  77. is($form->current_date(\%myconfig), $today,
  78. "current_date, $fmt: $today");
  79. is($form->current_date(\%myconfig, $formats[$format][3]),
  80. '2000-02-29', "current_date, $fmt: 2000-02-29");
  81. is($form->current_date(\%myconfig, $formats[$format][3], 1),
  82. '2000-03-01', "current_date, $fmt: 2000-03-01");
  83. }
  84. # $form->datetonum checks
  85. # Note that $form->datetonum assumes the year range 2000-2099
  86. foreach my $format (0 .. $#formats) {
  87. %myconfig = (dateformat => $formats[$format][0]);
  88. my $fmt = $formats[$format][0];
  89. my $sep = $formats[$format][1];
  90. my $yearcount = $formats[$format][2];
  91. cmp_ok($form->datetonum(\%myconfig, $formats[$format][3]), 'eq',
  92. '20000229', "datetonum, $fmt");
  93. }
  94. # $form->split_date checks
  95. # Note that $form->split_date assumes the year range 2000-2099
  96. # Note that $form->split_date only outputs two digit years
  97. foreach my $format (0 .. $#formats) {
  98. %myconfig = (dateformat => $formats[$format][0]);
  99. my $fmt = $formats[$format][0];
  100. my $sep = $formats[$format][1];
  101. my $yearcount = $formats[$format][2];
  102. my @output = $form->split_date($fmt, $formats[$format][3]);
  103. my $rv = $fmt;
  104. $rv =~ s/\Q$sep\E//g;
  105. $rv =~ s/(yy)?yy/$output[1]/;
  106. $rv =~ s/mm/$output[2]/;
  107. $rv =~ s/dd/$output[3]/;
  108. cmp_ok($output[1], 'eq', '00', "split_date specified, year");
  109. cmp_ok($output[2], 'eq', '02', "split_date specified, month");
  110. cmp_ok($output[3], 'eq', '29', "split_date specified, day");
  111. cmp_ok($output[0], 'eq', $rv, "split_date specified, unit");
  112. @output = $form->split_date($fmt);
  113. my $rv = $fmt;
  114. $rv =~ s/\Q$sep\E//g;
  115. $rv =~ s/(yy)?yy/$output[1]/;
  116. $rv =~ s/mm/$output[2]/;
  117. $rv =~ s/dd/$output[3]/;
  118. my $tv = $fmt;
  119. $tv =~ s/\Q$sep\E//g;
  120. $tv =~ s/(yy)?yy/$today_parts{'yy'}/;
  121. $tv =~ s/mm/$today_parts{'mm'}/;
  122. $tv =~ s/dd/$today_parts{'dd'}/;
  123. cmp_ok($output[1], 'eq', $today_parts{'yy'},
  124. "split_date unspecified, year");
  125. cmp_ok($output[2], 'eq', $today_parts{'mm'},
  126. "split_date unspecified, month");
  127. cmp_ok($output[3], 'eq', $today_parts{'dd'},
  128. "split_date unspecified, day");
  129. }
  130. # $form->format_date checks
  131. # Note that $form->format_date always outputs four digit years
  132. foreach my $format (0 .. $#formats) {
  133. $form->{db_dateformat} = $formats[$format][0];
  134. my $fmt = $formats[$format][0];
  135. my $sep = $formats[$format][1];
  136. my $yearcount = $formats[$format][2];
  137. my $results = $fmt;
  138. $results =~ s/(yy)?yy/2000/;
  139. $results =~ s/mm/02/;
  140. $results =~ s/dd/29/;
  141. cmp_ok($form->format_date('2000-02-29'), 'eq',
  142. $results, "format_date, $fmt, ISO");
  143. cmp_ok($form->format_date($formats[$format][3]), 'eq',
  144. $formats[$format][3], "format_date, $fmt, non-ISO");
  145. }