Help language development. Donate to The Perl Foundation

Date::Calendar::MayaAztec cpan:JFORGET last updated on 2020-07-30

examples/full-test-Aztec
#!/home/jf/rakudo/bin/perl6 -I../lib
# -*- encoding: utf-8; indent-tabs-mode: nil -*-

use v6;
use DBIish;
use Date::Calendar::Aztec;
use Date::Calendar::Aztec::Cortes;
use Date::Calendar::Gregorian;

my @index = ( (3,  8, 13, 18)
            , (4,  9, 14, 19)
            , (5, 10, 15, 20)
            , (1,  6, 11, 16)
            , (2,  7, 12, 17));
my $dbh = DBIish.connect('SQLite', :database<full-test-Aztec.db>);

my $sth = $dbh.do(q:to/EOF/);
   drop table if exists full_test_Aztec
EOF

$sth = $dbh.do(q:to/EOF/);
  create table full_test_Aztec (
      correlation
    , month1
    , day1
    , cle_num1
    , cle_idx1
    , mjd1
    , date1
    , mjd2
    , d2h
    , d2l
    , mjd3h
    , d3h
    , mjd3l
    , d3l
  )
EOF

$sth = $dbh.prepare(q:to/EOF/);
  insert into full_test_Aztec (correlation, month1, day1, cle_num1, cle_idx1, mjd1, date1, mjd2, d2h, d2l, mjd3h, d3h, mjd3l, d3l)
                values  (?,           ?,      ?,    ?,        ?,        ?,    ?,     ?,    ?,   ?,   ?,     ?,   ?,     ?)
EOF

my Date $ref .= new('2001-01-01');
# Starting with 1 Izcalli, 1 Cuetzpalin (index = 4)

for 1..19 -> $month1 {
  say $month1;
  my $last-day = 20;
  if $month1 == 19 {
    $last-day = 5;
  }
  for 1 .. $last-day -> $day1 {
    my $n = $day1 % 5;
    for 0..3 -> $p {
      my $cle_idx1 = @index[$n; $p];
      for 1..13 -> $cle_num1 {
         my Date::Calendar::Aztec $d1 .= new(month => $month1, day => $day1, clerical-number => $cle_num1, clerical-index => $cle_idx1, on-or-after => $ref);
         my $mjd1  = $d1.daycount;
         my $date1 = $d1.strftime("%e %B %V %A");

         my Date::Calendar::Gregorian $d2h = $d1.to-date('Date::Calendar::Gregorian');
         my Date::Calendar::Gregorian $d2l .= new-from-date($d1);
         my Int $mjd2 = $d2h.daycount;

         my Date::Calendar::Aztec $d3h = $d2h.to-date('Date::Calendar::Aztec');
         my Date::Calendar::Aztec $d3l .= new-from-date($d2h);
         my $mjd3h  = $d3h.daycount;
         my $date3h = $d3h.strftime("%e %B %V %A");
         my $mjd3l  = $d3l.daycount;
         my $date3l = $d3l.strftime("%e %B %V %A");

         $sth.execute('Caso', $month1, $day1, $cle_num1, $cle_idx1, $mjd1, $date1, $mjd2, $d2h.gist, $d2l.gist, $mjd3h, $date3h, $mjd3l, $date3l);

         my Date::Calendar::Aztec::Cortes $d1c .= new(month => $month1, day => $day1, clerical-number => $cle_num1, clerical-index => $cle_idx1, on-or-after => $ref);
         $mjd1  = $d1c.daycount;
         $date1 = $d1c.strftime("%e %B %V %A");

         $d2h = $d1c.to-date('Date::Calendar::Gregorian');
         $d2l .= new-from-date($d1c);
         $mjd2 = $d2h.daycount;

         my Date::Calendar::Aztec::Cortes $d3ch = $d2h.to-date('Date::Calendar::Aztec::Cortes');
         my Date::Calendar::Aztec::Cortes $d3cl .= new-from-date($d2h);
         $mjd3h  = $d3ch.daycount;
         $date3h = $d3ch.strftime("%e %B %V %A");
         $mjd3l  = $d3cl.daycount;
         $date3l = $d3cl.strftime("%e %B %V %A");

         $sth.execute('Cortes', $month1, $day1, $cle_num1, $cle_idx1, $mjd1, $date1, $mjd2, $d2h.gist, $d2l.gist, $mjd3h, $date3h, $mjd3l, $date3l);
      }
    }
  }

}
$sth.finish;
$dbh.dispose;

=begin pod

=head1 NAME

full-test-Aztec -- checking Aztec date creation over a full calendar round

=head1 DESCRIPTION

This program creates a SQLite  database containing Aztec dates created
over a  full calendar round,  that is, 18980  days or 52  vague years.
Each database row is populated  with intermediate and final values for
a round trip  Aztec → Gregorian →  Aztec. The push style  and the pull
style are  used at  both steps  (abbreviated "h"  for "pusH",  "l" for
"pulL"). There are  rows for the default Caso correlation  and for the
Cortes alternate correlation.

After the database  is populated, you can execute a  few checks on the
data wih the following SQL queries:

The push style and the pull style give the same result when converting
from Aztec into Gregorian, the following query should give zero rows:

  select * from full_test_Aztec
  where d2h != d2l

Same thing for the Gregorian to Aztec conversion:

  select * from full_test_Aztec
  where d3h != d3l

Two different Aztec  dates cannot convert to the  same Gregorian date,
the following query should give zero rows:

  select A.date1, B.date1
  from  full_test_Aztec A
      , full_test_Aztec B
  where A.correlation = B.correlation
  and   A.d2h         = B.d2h
  and  (A.month1   != B.month1
    or  A.day1     != B.day1
    or  A.cle_num1 != B.cle_num1
    or  A.cle_idx1 != B.cle_idx1)

No Gregorian dates are skipped.  The following query should still give
two rows:  the rows  for Gregorian  2001-01-01 for  both correlations.
Beware, this is a looooong query.

  select * from full_test_Aztec A
  where not exists (select 'x'
                    from   full_test_Aztec B
                    where  A.correlation = B.correlation
                    and    A.mjd2        = B.mjd2 + 1)

=head1 REMARKS

This script is  not part of the module's test  suite for the following
three reasons.

The  script needs  a few  prerequisites  outside of  core modules  and
outside   of   the  C<Date::Calendar::MayaAztec>   distribution:   the
C<DBIish> module and the C<Date::Calendar::Gregorian> module.

Its running time is very long, and some SQL queries are very long too.

I have not bothered to optimize the script: use of C<commit>, reducing
code duplication.

=head1 COPYRIGHT AND LICENSE

Copyright © 2020 Jean Forget, all rights reserved

This script is free software; you can redistribute it and/or modify it
under the Artistic License 2.0.

=end pod