Help language development. Donate to The Perl Foundation

Date::Calendar::Persian cpan:JFORGET last updated on 2021-02-22

t/06-conversion-astro.rakutest
#
# Checking the conversions when using the astronomical calendar
#
use v6.c;
use Test;
use Date::Calendar::Persian::Astronomical;

my @test-data = load-data();

plan 2 × @test-data.elems;

for @test-data -> $datum {
  my ($y-pe, $m-pe, $d-pe, $y-gr, $m-gr, $d-gr) = $datum;

  my Date::Calendar::Persian::Astronomical
          $date-pe;
  my Date $date-gr;

  $date-gr .= new($y-gr, $m-gr, $d-gr);
  $date-pe .= new-from-date($date-gr);

  my Str $expected = sprintf("%04d-%02d-%02d", $y-pe, $m-pe, $d-pe);
  is($date-pe.gist, $expected);
}

for @test-data -> $datum {
  my ($y-pe, $m-pe, $d-pe, $y-gr, $m-gr, $d-gr) = $datum;

  my Date::Calendar::Persian::Astronomical
          $date-pe;
  my Date $date-gr;

  $date-pe .= new(year => +$y-pe, month => +$m-pe, day => +$d-pe);
  $date-gr  = $date-pe.to-date;

  my Str $expected = sprintf("%04d-%02d-%02d", $y-gr, $m-gr, $d-gr);
  is($date-gr.gist, $expected);
}

done-testing;

# Test data were copied from table 14.1 on page 225 of Calendrical Computations 3rd edition.
# 29 Esfand and 30 Esfand were determined with this reasoning:
# (1) If astro year N begins before arith year N, that means that astro year N-1 is shorter than arith year N-1
# which in turn means that astro year N-1 is normal and arith year N-1 is leap.
# (2) If astro year N begins after arith year N, that means that astro year N-1 is longer than arith year N-1
# which in turn means that astro year N-1 is leap and arith year N-1 is normal.
sub load-data {
  return (
         < 1015 12 29      1637  3 19 >
       , < 1016  1  1      1637  3 20 >
       , < 1048 12 29      1670  3 19 >
       , < 1049  1  1      1670  3 20 >
       , < 1077 12 29      1699  3 19 >
       , < 1078  1  1      1699  3 20 >
       , < 1209 12 29      1831  3 20 >
       , < 1210  1  1      1831  3 21 >
       , < 1242 12 29      1864  3 19 >
       , < 1243  1  1      1864  3 20 >
       , < 1403 12 30      2025  3 20 >
       , < 1404  1  1      2025  3 21 >
       , < 1597 12 30      2219  3 21 >
       , < 1598  1  1      2219  3 22 >
  );
}