# Local base Lib ...

sub set_datetime_str
{
    local ($wkstr) = $_[0];
    local ($outStr) = "";
    local ($fday, $fmon, $fyea, $fhou, $fmin, $fsec, $fwday);

    # format should be: `date "+%d-%m-%Y %H:%M:%S %w"`;

    $fday=substr($wkstr,0,2); 
    $fmon=substr($wkstr,3,2); 
    $fyea=substr($wkstr,6,4); 
    $fhou=substr($wkstr,11,2); 
    $fmin=substr($wkstr,14,2); 
    $fsec=substr($wkstr,17,2); 
    $fwday=substr($wkstr,20,1); 

    # format for order criteria

    $outStr=$fyea.$fmon.$fday.$fhou.$fmin.$fsec.$wday ;
  return $outStr;
}

sub get_datetime_str
{
    local ($wkstr) = $_[0];
    local ($outStr) = "";
    local ($fday, $fmon, $fyea, $fhou, $fmin, $fsec, $fwday);

    # format should be: `date "+%d-%m-%Y %H:%M:%S $w"`;

    $fday=substr($wkstr,6,2); 
    $fmon=substr($wkstr,4,2); 
    $fyea=substr($wkstr,0,4); 
    $fhou=substr($wkstr,8,2); 
    $fmin=substr($wkstr,10,2); 
    $fsec=substr($wkstr,12,2); 
    $fsec=substr($wkstr,14,1); 

    # format for order criteria

    $outStr=$fday."-".$fmon."-".$fyea." ".$fhou.":".$fmin.":".$fsec." ".$fwday ;

  return $outStr;
}
sub set_now
{
local ($now);
if ( !$_[0] ) { 
     $now = time();
} else {
     $now=$_[0];
}     
local @tm = localtime($now);
local ($t_day, $t_mon, $t_yea, $t_hou, $t_min, $t_sec, $t_wday);

 # format should be: `date "+%d-%m-%Y %H:%M:%S $w"`;
 $t_sec=$tm[0];
 if ($t_sec < 10 ) { $t_sec="0".$t_sec;}
 $t_min=$tm[1];
 if ($t_min < 10 ) { $t_min="0".$t_min;}
 $t_hou=$tm[2];
 if ($t_hou < 10 ) { $t_hou="0".$t_hou;}
 $t_day=$tm[3];
 if ($t_day < 10 ) { $t_day="0".$t_day;}
 $t_mon=($tm[4]+1);
 if ($t_mon < 10 ) { $t_mon="0".$t_mon;}
 $t_yea=($tm[5]+1900);
 $t_wday=($tm[6]);
 $t_yead=$tm[7];
 if ($t_yead < 100 ) { $t_yead="0".$t_yead;}
 if ($t_yead < 10 ) { $t_yead="0".$t_yead;}
 $t_isdst=$tm[8];


return "$t_day-$t_mon-$t_yea $t_hou:$t_min:$t_sec $t_wday $t_yead";

}

sub show_hash
{
 local ($k,$v,$r);
  if ( $_[1] ) { select($_[1]);}
#  return if (!%{$_[0]});
  local %h_lst=%{$_[0]};
  local %h_cp;

  foreach $item (keys (%h_lst)) {
         if ( ! $h_lst{$item} ) { next ;}
         local $v=$h_lst{$item};
         $r=ref($v);
         print "---$r\n";
         if ( $r eq 'HASH') { 
               &show_array(\%{$h_lst{$item}},$_[1],$_[2]);  
#                foreach $ky ( keys (%{$h_lst{$item}})) {
#                          local $vy=${$h_lst{$item}}{$k};
#                          print "$ky\t=\t$vy \n";
#                }
#              %h_cp=%{$h_lst{$item}}; 
#              &show_hash(\%h_cp,$_[1],$_[2]); }
        } elsif ( $r eq 'ARRAY') { &show_array(\%v,$_[1],$_[2]);  }
         else {
              if ( $_[1] ) { 
                     print $h_lst{$item}."$_[1]=$_[1]".$h_lst{$item}."\n" ;
               } else  { 
                     print $h_lst{$item}."=".$h_lst{$item}."\n" ;
               }
         }
  }
  if ( $_[1] ) { $| =1; select(STDOUT);}
}

sub show_array
{
 local ($k,$v,$r);
  if ( $_[1] ) { select($_[1]);}
  return if (!@_[0]);
  foreach $k (@_[0]) { 
         $r=ref($k->{$k});
         $v=$k->{$k};
         if ( $r eq 'HASH') { &show_hash($k->{$k},$_[1],$_[2]); next; };
         if ( $r eq 'ARRAY') { &show_array($k->{$k},$_[1],$_[2]); next; };
         if ( $_[1] ) { 
                print "$k$_[1]=$_[1]$v\n" ;
          } else  { print "$k=$v\n"; }
  }
  if ( $_[1] ) { $| =1; select(STDOUT);}
}

# To show each item from Rec in a list (hash-list: key + hash ) 
sub showrec_list
{
  &show_hash(\%{$_[0]},"\t");
  return;
  return if (!%{$_[0]});
  local %h_lst=%{$_[0]};
  local $lnum=1;
  local %rh,@ra;
  local ($item,$k,$v,$r);

      if ( !$_[1]  && $no_out eq 0 ) { print  "<pre>\n";};
      if ( $_[1] ) { select($_[1]);}
      foreach $item (keys (%h_lst)) {
            if ( ! $h_lst{$item} ) { next ;}
            $r=ref($h_lst{$item});
            if ( $r eq 'HASH') {
                print "_________________________________________________$lnum\n";
                foreach $k ( keys (%{$h_lst{$item}})) {
                          local $v=${$h_lst{$item}}{$k};
                          print "$k\t= \n";
                     $r=ref($v);
print "$r ----\n";
                     if ( $r eq 'HASH') { 
                          %rh=${$h_lst{$item}}{$k};
                          &showrec_list(\%rh);
                          next;
                     } elsif ( $r eq 'ARRAY') {
                          &showrec_list(\%rh);
                          next;
                     } else { 
                          print "$k\t=\t$v \n";
                     };
                }
            } elsif ( $r  eq 'ARRAY') {
                foreach $k ( @{$h_lst{$item}}) {
                     local $v=$k->{$k};
                     print "$k\t=\t$v \n";
                }
            } else {
                     local $v=$h_lst{$item};
                     print "$item\t=\t$v \n";
            }
            $lnum++;
      }
      print  "__________________________________________________\n";
      print  "TOTAL: $lnum \n";
      print  "__________________________________________________\n";
      if ( !$_[1]  && $no_out eq 0 ) { print  "</pre>\n";};
      if ( $_[1] ) { $| =1; select(STDOUT);}
}
1;  # Return true  
