How to resolve the algorithm Day of the week step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Day of the week step by step in the jq programming language

Table of Contents

Problem Statement

A company decides that whenever Xmas falls on a Sunday they will give their workers all extra paid holidays so that, together with any public holidays, workers will not have to work the following week (between the 25th of December and the first of January).

In what years between 2008 and 2121 will the 25th of December be a Sunday? Using any standard date handling libraries of your programming language; compare the dates calculated with the output of other languages to discover any anomalies in the handling of dates which may be due to, for example, overflow in types used to represent dates/times similar to   y2k   type problems.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Day of the week step by step in the jq programming language

Source code in the jq programming language

# Use Zeller's Congruence to determine the day of the week, given
# year, month and day as integers in the conventional way.
# If iso == "iso" or "ISO", then emit an integer in 1 -- 7 where 
# 1 represents Monday, 2 Tuesday, etc;
# otherwise emit 0 for Saturday, 1 for Sunday, etc.
#
def day_of_week(year; month; day; iso):
  if month == 1 or month == 2 then
    [month + 12, year - 1]
  else
    [month, year]
  end 
  | day + (13*(.[0] + 1)/5|floor)
    +  (.[1]%100)       + ((.[1]%100)/4|floor)
    +  (.[1]/400|floor) - 2*(.[1]/100|floor) 
  | if iso == "iso" or iso == "ISO" then 1 + ((. + 5) % 7)
    else . % 7
    end;

# Give the results as an array so they can
# readily be presented on a single line:
[range(2008; 2122) | select( day_of_week(.;12;25;0) == 1 )]

  

You may also check:How to resolve the algorithm Read a configuration file step by step in the Lasso programming language
You may also check:How to resolve the algorithm Inverted index step by step in the Scala programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Octave programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the Lasso programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the PowerShell programming language