Scripts:Day of the week

From OHRRPGCE-Wiki
Jump to navigation Jump to search

The scripts below calculate the day of the week given a date no earlier than 1900. The calc weekday number script can then be used like this:

 variable(n)
 n := calc weekday number(system year, system month, system day)
 switch(n) do(
   case(0) $0="Sunday"
   case(1) $0="Monday"
   case(2) $0="Tuesday"
   case(3) $0="Wednesday"
   case(4) $0="Thursday"
   case(5) $0="Friday"
   case(6) $0="Saturday"
 )
 show string(0)

An .rpg file demonstrating it is here: Weekday Tool

Scripts[edit]

script, calc weekday number, y, m, d, begin
  # Return value is:
  #
  # 0 = Sunday
  # 1 = Monday
  # 2 = Tuesday
  # 3 = Wednesday
  # 4 = Thursday
  # 5 = Friday
  # 6 = Saturday
  #
  # This function is only smart enough to handle days starting with 1900AD
  variable(dnum)
  dnum := 1 # January 1900 began on a Monday
  variable(i)
  for(i, 1900, y -- 1) do(
    dnum += days in year(i)
  )
  for(i, 1, m -- 1) do(
    dnum += days in month(y, i)
  )
  dnum += d -- 1
  exit returning(dnum ,mod, 7)
end

script, days in month, y, m, begin
  variable(d)
  switch(m) do(
    case(1) d := 31
    case(2) d := days in february(y)
    case(3) d := 31
    case(4) d := 30
    case(5) d := 31
    case(6) d := 30
    case(7) d := 31
    case(8) d := 31
    case(9) d := 30
    case(10) d := 31
    case(11) d := 30
    case(12) d := 31
  )
  exit returning(d)
end

script, days in february, y, begin
  if(is leap year(y)) then(
    exit returning(29)
  )
  exit returning(28)
end

script, days in year, y, begin
  if(is leap year(y)) then(
    exit returning(366)
  )
  exit returning(365)
end

script, is leap year, y, begin
  variable(leap year)
  leap year := false # Common year
  if((y ,mod, 4) == 0) then(
    leap year := true # Leap year
    if(y ,mod, 100 == 0) then(
      leap year := false # Exceptional Common Year
      if(y ,mod, 400 == 0) then(
        leap year := true # Century leap year
      )
    )
  )
  exit returning(leap year)
end