Community discussions

MikroTik App
 
randomwalk
just joined
Topic Author
Posts: 10
Joined: Sun Apr 21, 2013 3:40 am
Location: Canada

Fast and Accurate Leap Year calculation

Wed Oct 06, 2021 10:43 am

Hi,
The forum has helped me a lot over the past several years, and I thought I would give something back. Hope you find this useful.
{
  # Fast and accurate Leap Year calculation

  :local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"];
  :local CurrentDate [system clock get date];   # current date (mar/01/1981)
  :local yr [:tonum [:pick $CurrentDate 7 11]]; # current year
  :local mo [:pick $CurrentDate 0 3];           # current month (jan...dec)
  :local mm ([find key=$mo in=$months]+1);      # month (1...12)

  :local leap; # current leap year (true|false)
  :local days; # No_of days in the current month
  {
    :if (($yr&3!=0)||(($yr&15!=0)&&((($yr/25)*25)=$yr))) do={
      :set $leap false} else={:set $leap true}; :set $days 30
    :if ($mm!=2) do={:set $days ($days+(($mm+($mm>>3))&1))
      } else={:if (!$leap) do={:set $days ($days-2)
        } else={:set $days ($days-1)}}
  }
# testing
:put ( " Year: $yr  Leap: $leap " )
:put ( " Month: $mo  Days: $days " )
}

Who is online

Users browsing this forum: No registered users and 5 guests