lastday_of_month

Date:

2017-11-08

Intro

While writing this documentation I started to see that another code block could move to a separate file: lastday_of_month. This function is not tied to the remaining code, so I decided to separate it.

Details

In the Gregorian Calendar used in the western world, months have distinct lengths. The pattern for a common year is well known

create gregorian_length_of_month
  #31 , #28 , #31 , #30 , #31 , #30 ,
  #31 , #31 , #30 , #31 , #30 , #31 ,

This list of numbers is stored in flash using the , (comma) word. As a well known matter of fact, however, the length of February is increased by one during leap years. So I wanted a function to produce the correct result when needed.

#include leap_year_q.fs

: gregorian_lastday_of_month ( year month -- last_day )
  dup 1-                                \ array starts at 0
  gregorian_length_of_month + @i
  swap #2 = if                          \ if February
    swap leap_year? if                  \   if leap_year
      1+                                \     month += 1
    then                                \
  else                                  \ else
    swap drop                           \   remove year
  then                                  \
;

lastday_of_month will consult the list defined above, and in case its Februar and in case its a leap year, will increment the result by one.

I decided to use the prefix gregorian_ just in case.

The Code

 1\ 2017-11-08 gregorian_lastday_of_month.fs
 2\
 3\ Written in 2017 by Erich Wälde <erich.waelde@forth-ev.de>
 4\
 5\ To the extent possible under law, the author(s) have dedicated
 6\ all copyright and related and neighboring rights to this software
 7\ to the public domain worldwide. This software is distributed
 8\ without any warranty.
 9\
10\ You should have received a copy of the CC0 Public Domain
11\ Dedication along with this software. If not, see
12\ <http://creativecommons.org/publicdomain/zero/1.0/>.
13\
14\
15\ words:
16\     gregorian_lastday_of_month ( year month -- last_day )
17
18#include leap_year_q.fs
19
20create gregorian_length_of_month
21  #31 , #28 , #31 , #30 , #31 , #30 ,
22  #31 , #31 , #30 , #31 , #30 , #31 ,
23
24: gregorian_lastday_of_month ( year month -- last_day )
25  dup 1-                                \ array starts at 0
26  gregorian_length_of_month + @i
27  swap #2 = if                          \ if February
28    swap leap_year? if                  \   if leap_year
29      1+                                \     month += 1
30    then                                \
31  else                                  \ else
32    swap drop                           \   remove year
33  then                                  \
34;