Tuesday, August 31, 2021

Perl Date::Manip::Date business day

 #!/usr/bin/perl

# Creates /etc/cron.d file entries, one per month,
# for a program that should run on the previous
# day to last business day of the month.
# Uses Date::Manip::Date module, https://metacpan.org/dist/Date-Manip/view/lib/Date/Manip/Date.pod.
# 64board@gmail.com
# 2021-08-31
 
use strict;
use warnings;

use Date::Manip::Date;

sub cron_entry {

    my ($date) = @_;

    return $date->printf("30 19 %d %m %a\troot\t/opt/balmo_id/run.sh");
}

##MAIN##

my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

my $date = new Date::Manip::Date;

foreach my $month (@months) {

    print "Month: $month\n";

    # Get the last day of the month of current year.
    $date->parse("last day in $month");

    print 'Last day of the month: ', $date->printf('%Y-%m-%d, %a'), "\n";

    # Don't check time for business day.
    my $checktime = 0;

    my $offset = 1;
    # Case of last day of the month is not a business day.
    if (!$date->is_business_day($checktime)) {
    $offset = 2;
    }

    # The pevious day to last business day of the month.
    $date->prev_business_day($offset, $checktime);

    print 'Previous business day: ', $date->printf('%Y-%m-%d, %a'), "\n";

    # The CRON entry, use CRON: label to filter the cron entries
    # from the output.
    print "CRON: ", cron_entry($date), "\n";
}

__END__

Saturday, July 3, 2021

SSH Passwordless Login Using SSH Keygen in 5 Easy Steps.

 Step 1: Create Authentication SSH-Keygen Keys on the originating machine.

$ ssh-keygen -t rsa

On Windows you could use puttygen.exe

Use a passphrase to protect keys.

On Linux private key goes to .ssh/id_rsa
Public key goes to id_rsa.pub

Step 2: Create .ssh Directory on the destination machine.

Step 3: Upload Generated Public Keys to the destination machine.

$ cat .ssh/id_rsa.pub | ssh <username>@<destination_ip> 'cat >> .ssh/authorized_keys'

<username> should be the login on the destination machine.
<destination_ip> is the public IP of the destination machine.

Step 4: Set Permissions on the destination machine.

$ ssh <username>@<destination_ip> "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"

<username> should be the login on the destination machine.
<destination_ip> is the public IP of the destination machine.

Step 5: Login from originating machine to destination machine without password.

$ ssh <username>@<destination_ip>

<username> should be the login on the destination machine.
<destination_ip> is the public IP of the destination machine.

On Windows you could use putty.exe

Taken from https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/


Sunday, February 28, 2021

2021 Holiday Schedule

DateHoliday
Friday, January 1 New Year’s Day
Monday, January 18 Birthday of Martin Luther King, Jr.
Wednesday, January 20* Inauguration Day
Monday, February 15** Washington’s Birthday
Monday, May 31 Memorial Day
Monday, July 5*** Independence Day
Monday, September 6 Labor Day
Monday, October 11 Columbus Day
Thursday, November 11 Veterans Day
Thursday, November 25 Thanksgiving Day
Friday, December 24**** Christmas Day

 *This holiday is designated as "Inauguration Day" in section 6103(c) of title 5 of the United States Code, which is the law that specifies holidays for Federal employees. Federal employees in the Washington, DC area are entitled to a holiday on the day a President is inaugurated, January 20 for each fourth year after 1965. Though other institutions such as state and local governments and private businesses may use other names, it is our policy to always refer to holidays by the names designated in the law.

**This holiday is designated as "Washington’s Birthday" in section 6103(a) of title 5 of the United States Code, which is the law that specifies holidays for Federal employees. Though other institutions such as state and local governments and private businesses may use other names, it is our policy to always refer to holidays by the names designated in the law.

***July 4, 2021 (the legal public holiday for Independence Day), falls on a Sunday. For most Federal employees, Monday, July 5, will be treated as a holiday for pay and leave purposes. (See 5 U.S.C. 6103(b).)

****December 25, 2021 (the legal public holiday for Christmas Day), falls on a Saturday. For most Federal employees, Friday, December 24, will be treated as a holiday for pay and leave purposes. (See section 3(a) of Executive order 11582, February 11, 1971.) 

 Taken from:  https://www.opm.gov/policy-data-oversight/pay-leave/federal-holidays/#url=2021
 

Capturing with Perl and REGEX two numbers on a line, one of them is optional

 #!/usr/bin/perl

use strict;
use warnings;

sub get_long_short {
   my ($number, $spaces1, $spaces2) = @_;
   my ($long, $short) = ('', '');

   if (length($spaces1) > length($spaces2))
   {
      $short = $number;
   } else {
      $long = $number;
   }

   return ($long, $short);
}

while (<DATA>) {

   chomp();

   if (my ($b1, $n1, $n2, $b2, $s) = /
      ^(\s*)            # First spaces ($b1)
      ([\d,.]+)         # First number always exists ($n1)
      (?:               # Don't want to capture spaces
      \s+               # in front
      ([\d,.]+)         # Second number ($n2)
      )?                # Second number is optional
      (\s+)             # Space always exists ($b2)
      (\w{2,3})$        # Two or three letters symbol ($s)
      /x)
   {
      my ($long, $short);

      if (defined($n2)) {
         $long = $n1;
         $short = $n2;
      } else {
         ($long, $short) = get_long_short($n1, $b1, $b2);
      }

      $long =~ s/,//g;
      $short =~ s/,//g;

      print("$long|$short|$s\n");

   } else {
      print("NOT MATCHED: $_\n");
   }
}

__END__
long short symbol
12 3  NG
1,234 1,222 CL
1,333       PL
123.4 9,088     HNG
123.4 9,088     BBBB
       90.65 HO
   1   RB
    2  RB
     3 RB
4      RB
  100,000.00     CL
  CL

Monday, January 18, 2021

isLeapYear in PHP

// Returns TRUE if a year is a leap year
// The year must be evenly divisible by 4
// if the year can be evenly divisible by 100 is not a leap year
// unless it is also evenly divisible by 400 then it is a leap year
// https://en.wikipedia.org/wiki/Leap_year
public static function isLeapYear(int $year):bool {
    return (($year % 4 == 0) && (($year % 100 != 0) || ($year % 400 == 0)));
}