Load year data in a reference table.

1 Creation Order

The order that must be followed to get the calendar structure is the following.

  1. Tables
    1. Year
    2. Day
  2. Procedures
    1. Week of year
    2. Calendar generation
  3. Trigger

2 Tables

There are two tables that need to be created to generate the calendar.

2.1 Year table

Example
Copy
-- **************************************************************************
                        -- DEISTER WebStudio XSQL-SCHEMA Fri May 24 15:26:30 CEST 2019 Engine: informix
-- calendar_year
-- **************************************************************************
CREATE TABLE calendar_year (
    numyear smallint not null,
    nameyear varchar(20)
);
ALTER TABLE calendar_year LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_calendar_year ON calendar_year(numyear);
ALTER TABLE calendar_year
 ADD CONSTRAINT 
 PRIMARY KEY (numyear)
 CONSTRAINT p_calendar_year;
Copy
<table name='calendar_year'>
    <column name='numyear' type='smallint' required='y'/> <!-- Year number -->
    <column name='nameyear' type='varchar' size='20' /> <!-- Year name -->
    
    <!-- INDEX -->
    <primary name='p_calendar_year' columns='numyear' />
</table>

2.2 Day table

Example
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri May 24 15:06:16 CEST 2019 Engine: informix
-- calendar_day
-- **************************************************************************
CREATE TABLE calendar_day (
    day date not null,
    daynum smallint not null,
    dayname varchar(15),
    yearnum smallint not null,
    yearname varchar(10),
    semesternum smallint not null,
    semestername varchar(10),
    quarternum smallint not null,
    quartername varchar(10),
    monthnum smallint not null,
    fullmonthname varchar(16),
    monthname varchar(10),
    sevennum smallint not null,
    sevenname varchar(10),
    weeknum smallint not null,
    weekofyear integer not null,
    weekname varchar(10),
    weekdayname varchar(10)
);
ALTER TABLE calendar_day LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_calendar_day ON calendar_day(day);
ALTER TABLE calendar_day
 ADD CONSTRAINT 
 PRIMARY KEY (day)
 CONSTRAINT p_calendar_day;

CREATE  INDEX f_calendar_day1 ON calendar_day(yearnum);
ALTER TABLE calendar_day
 ADD CONSTRAINT  FOREIGN KEY (yearnum)
 REFERENCES calendar_year(numyear)
 ON DELETE CASCADE 
 CONSTRAINT f_calendar_day1;
Copy
<table name='calendar_day'>
    <column name='day'              type='date'                 required='y'/> <!-- Day -->
    <column name='daynum'           type='smallint'             required='y'/> <!-- Day in month number -->
    <column name='dayname'          type='varchar'  size='15'               /> <!-- Day name -->
    <column name='yearnum'          type='smallint'             required='y'/> <!-- Year number -->
    <column name='yearname'         type='varchar'  size='10'               /> <!-- Year name -->
    <column name='semesternum'      type='smallint'             required='y'/> <!-- Semester -->
    <column name='semestername'     type='varchar'  size='10'               /> <!-- Semester -->
    <column name='quarternum'       type='smallint'             required='y'/> <!-- Quarter Code -->
    <column name='quartername'      type='varchar'  size='10'               /> <!-- Quarter name -->
    <column name='monthnum'         type='smallint'             required='y'/> <!-- Month number -->
    <column name='fullmonthname'    type='varchar'  size='16'               /> <!-- Period name = month name in Catalán -->
    <column name='monthname'        type='varchar'  size='10'               /> <!-- Month name -->
    <column name='sevennum'         type='smallint'             required='y'/> <!-- Natural 7 days 1 to 7 = 1, 8 to 14=2, ... -->
    <column name='sevenname'        type='varchar'  size='10'               /> <!-- Week name -->
    <column name='weeknum'          type='smallint'             required='y'/> <!-- Week code -->
    <column name='weekofyear'       type='integer'              required='y'/> <!-- Week name -->
    <column name='weekname'         type='varchar'  size='10'               /> <!-- Week name -->
    <column name='weekdayname'      type='varchar'  size='10'               /> <!-- WeekDay Name: Mon,Tue,..Sun -->

    <!-- INDEX -->
    <primary name='p_calendar_day' columns='day' />
    
    <!-- FOREIGN KEYS -->
    <foreign name='f_calendar_day1' columns='yearnum' references='calendar_year' refcols='numyear' ondeletecascade='y' />
</table>

3 Procedures

There are two procedures that need to be created to generate the calendar.

3.1 sdm_get_weeknum

Example
Copy
-- ==========================================================================
-- Calculate ISO 8601 Week Number for given date
-- 
-- According to a summary of the ISO 8601:1988 standard "Data Elements and
-- Interchange Formats - Information Interchange - Representation of
-- dates and times":
-- 
--     In commercial and industrial applications (delivery times,
--     production plans, etc.), especially in Europe, it is often required
--     to refer to a week of a year.  Week 01 of a year is per definition
--     the first week which has the Thursday in this year, which is
--     equivalent to the week which contains the fourth day of January.  In
--     other words, the first week of a new year is the week which has the
--     majority of its days in the new year.  Week 01 might also contain
--     days from the previous year and the week before week 01 of a year is
--     the last week (52 or 53) of the previous year even if it contains
--     days from the new year.  A week starts with Monday (day 1) and ends
--     with Sunday (day 7).  For example, the first week of the year 1997
--     lasts from 1996-12-30 to 1997-01-05 and can be written in standard
--     notation as
-- 
--         1997-W01 or 1997W01
-- 
--     The week notation can also be extended by a number indicating the
--     day of the week.  For example the day 1996-12-31 which is the
--     Tuesday (day 2) of the first week of 1997 can also be written as
-- 
--         1997-W01-2 or 1997W012
-- 
--     for applications like industrial planning where many things like
--     shift rotations are organized per week and knowing the week number
--     and the day of the week is more handy than knowing the day of the
--     month.
-- 
-- Referring to the standard itself, section 3.17 defines a calendar week:
-- 
--     week, calendar: A seven day period within a calendar year, starting
--     on a Monday and identified by its ordinal number within the year;
--     the first calendar week of the year is the one that includes the
--     first Thursday of that year.  In the Gregorian calendar, this is
--     equivalent to the week which includes 4 January.
-- 
-- Section 5.2.3 "Date identified by Calendar week and day numbers" states:
-- 
--     Calendar week is represented by two numeric digits.  The first
--     calendar week of a year shall be identified as 01 [...]
-- 
--     Day of the week is represented by one decimal digit.  Monday
--     shall be identified as day 1 of any calendar week [...]
-- 
-- Section 5.2.3.1 "Complete representation" states:
-- 
--     When the application clearly identifies the need for a complete
--     representation of a date identified by calendar week and day
--     numbers, it shall be one of the alphanumeric representations as
--     follows, where CCYY represents a calendar year, W is the week
--     designator, ww represents the ordinal number of a calendar week
--     within the year, and D represents the ordinal number within the
--     calendar week.
-- 
--     Basic format:    CCYYWwwD      Example: 1985W155
--     Extended format: CCYY-Www-D    Example: 1985-W15-5
-- 
-- Both the summary and the formal definition are intuitively clear, but it
-- is not obvious how to translate it into an algorithm.  However, we can
-- deal with the problem by exhaustively enumerating the seven options for
-- the day of the week on which 1st January falls (with actual year values
-- for concreteness):
-- 
--     1st January 2001 is Monday    => Week 1 starts on 2001-01-01
--     1st January 2002 is Tuesday   => Week 1 starts on 2001-12-31
--     1st January 2003 is Wednesday => Week 1 starts on 2002-12-30
--     1st January 2004 is Thursday  => Week 1 starts on 2003-12-29
--     1st January 2010 is Friday    => Week 1 starts on 2010-01-04
--     1st January 2005 is Saturday  => Week 1 starts on 2005-01-03
--     1st January 2006 is Sunday    => Week 1 starts on 2006-01-02
-- 
--     1st January 2020 is Wednesday => Week 1 starts on 2019-12-30
--     1st January 2021 is Friday    => Week 1 starts on 2020-01-04
--     1st January 2022 is Saturday  => Week 1 starts on 2021-01-03
--     1st January 2023 is Sunday    => Week 1 starts on 2023-01-02
--     1st January 2024 is Monday    => Week 1 starts on 2024-01-01
-- 
--     1st January 2030 is Tuesday   => Week 1 starts on 2029-12-31
--     1st January 2031 is Wednesday => Week 1 starts on 2030-12-30
-- 
-- (Cross-check: 1st January 1997 was a Wednesday; the summary notes state
-- that week 1 of 1997 started on 1996-12-30, which is consistent with the
-- table derived for dates in the first decade of the third millennium
-- above).
-- 
-- When working with the Informix DATE types, bear in mind that Informix
-- uses WEEKDAY values 0 = Sunday, 1 = Monday, 6 = Saturday.  When the
-- weekday of the first of January has the value in the LH column, you need
-- to add the value in the RH column to the 1st of January to obtain the
-- date of the first day of the first week of the year.
-- 
--     Weekday         Offset to
--     1st January     1st day of week 1
-- 
--     0               +1
--     1                0
--     2               -1
--     3               -2
--     4               -3
--     5               +3
--     6               +2
-- 
-- This can be written as MOD(11-w,7)-3 where w is the (Informix  encoding
-- of the) weekday of 1st January and the value 11 is used to ensure that
-- no negative values are presented to the MOD operator.  Hence, the
-- expression for the date corresponding to the 1st day (Monday) of the 1st
-- week of a given year, yyyy, is:
-- 
--     d1w1 = MDY(1, 1, yyyy) + MOD(11 - WEEKDAY(MDY(1,1,yyyy)), 7) - 3
-- 
-- ==========================================================================
-- ==========================================================================
-- Given this date d1w1, we can calculate the week number of any other date
-- in the same year as:
-- 
--     TRUNC((dateval - d1w1) / 7) + 1
-- 
-- The residual issues are ensuring that the wraparounds are correct.  If
-- the given date is earlier than the start of the first week of the year
-- that contains it, then the date belongs to the last week of the previous
-- year.  If the given date is on or after the start of the first week of
-- the next year, then the date belongs to the first week of the next year.
-- 
-- Given these observations, we can write iso8601_weeknum as shown below.
-- (Beware: iso8601_week_number() is too long for servers with the
-- 18-character limit; so is day_one_of_week_one()).
-- 
-- Then comes the interesting testing phase when do you get week 53?
-- One answer is on Friday 1st January 2010, which is in 2009-W53 (as,
-- indeed, is Sunday 3rd January 2010).  Similarly, Saturday 1st January
-- 2005 is in 2004-W53, but Sunday 1st January 2006 is in 2005-W52.
-- 
--  ==========================================================================
CREATE PROCEDURE sdm_get_weeknum(dateval DATE DEFAULT TODAY) RETURNING INTEGER;
    DEFINE rv CHAR(8);
    DEFINE yyyy CHAR(4);
    DEFINE ww CHAR(2);
    DEFINE d1w1 DATE;
    DEFINE tv DATE;
    DEFINE wn INTEGER;
    DEFINE yn INTEGER;

    -- Calculate year and week number.
    LET yn = YEAR(dateval);
    LET d1w1 = sdm_day_one_week_one(yn);
    IF dateval < d1w1 THEN
        -- Date is in early January and is in last week of prior year
        LET yn = yn - 1;
        LET d1w1 = sdm_day_one_week_one(yn);
    ELSE
        LET tv = sdm_day_one_week_one(yn + 1);
        IF dateval >= tv THEN
            -- Date is in late December and is in the first week of ne-- t year
            LET yn = yn + 1;
            LET d1w1 = tv;
        END IF;
    END IF;
    LET wn = TRUNC((dateval - d1w1) / 7) + 1;

    RETURN wn;
END PROCEDURE;

3.2 sdm_day_one_week_one

Example
Copy
-- ==========================================================================
-- Calculate ISO 8601 Week Number for given date
-- 
-- According to a summary of the ISO 8601:1988 standard "Data Elements and
-- Interchange Formats - Information Interchange - Representation of
-- dates and times":
-- 
--     In commercial and industrial applications (delivery times,
--     production plans, etc.), especially in Europe, it is often required
--     to refer to a week of a year.  Week 01 of a year is per definition
--     the first week which has the Thursday in this year, which is
--     equivalent to the week which contains the fourth day of January.  In
--     other words, the first week of a new year is the week which has the
--     majority of its days in the new year.  Week 01 might also contain
--     days from the previous year and the week before week 01 of a year is
--     the last week (52 or 53) of the previous year even if it contains
--     days from the new year.  A week starts with Monday (day 1) and ends
--     with Sunday (day 7).  For example, the first week of the year 1997
--     lasts from 1996-12-30 to 1997-01-05 and can be written in standard
--     notation as
-- 
--         1997-W01 or 1997W01
-- 
--     The week notation can also be extended by a number indicating the
--     day of the week.  For example the day 1996-12-31 which is the
--     Tuesday (day 2) of the first week of 1997 can also be written as
-- 
--         1997-W01-2 or 1997W012
-- 
--     for applications like industrial planning where many things like
--     shift rotations are organized per week and knowing the week number
--     and the day of the week is more handy than knowing the day of the
--     month.
-- 
-- Referring to the standard itself, section 3.17 defines a calendar week:
-- 
--     week, calendar: A seven day period within a calendar year, starting
--     on a Monday and identified by its ordinal number within the year;
--     the first calendar week of the year is the one that includes the
--     first Thursday of that year.  In the Gregorian calendar, this is
--     equivalent to the week which includes 4 January.
-- 
-- Section 5.2.3 "Date identified by Calendar week and day numbers" states:
-- 
--     Calendar week is represented by two numeric digits.  The first
--     calendar week of a year shall be identified as 01 [...]
-- 
--     Day of the week is represented by one decimal digit.  Monday
--     shall be identified as day 1 of any calendar week [...]
-- 
-- Section 5.2.3.1 "Complete representation" states:
-- 
--     When the application clearly identifies the need for a complete
--     representation of a date identified by calendar week and day
--     numbers, it shall be one of the alphanumeric representations as
--     follows, where CCYY represents a calendar year, W is the week
--     designator, ww represents the ordinal number of a calendar week
--     within the year, and D represents the ordinal number within the
--     calendar week.
-- 
--     Basic format:    CCYYWwwD      Example: 1985W155
--     Extended format: CCYY-Www-D    Example: 1985-W15-5
-- 
-- Both the summary and the formal definition are intuitively clear, but it
-- is not obvious how to translate it into an algorithm.  However, we can
-- deal with the problem by exhaustively enumerating the seven options for
-- the day of the week on which 1st January falls (with actual year values
-- for concreteness):
-- 
--     1st January 2001 is Monday    => Week 1 starts on 2001-01-01
--     1st January 2002 is Tuesday   => Week 1 starts on 2001-12-31
--     1st January 2003 is Wednesday => Week 1 starts on 2002-12-30
--     1st January 2004 is Thursday  => Week 1 starts on 2003-12-29
--     1st January 2010 is Friday    => Week 1 starts on 2010-01-04
--     1st January 2005 is Saturday  => Week 1 starts on 2005-01-03
--     1st January 2006 is Sunday    => Week 1 starts on 2006-01-02
-- 
--     1st January 2020 is Wednesday => Week 1 starts on 2019-12-30
--     1st January 2021 is Friday    => Week 1 starts on 2020-01-04
--     1st January 2022 is Saturday  => Week 1 starts on 2021-01-03
--     1st January 2023 is Sunday    => Week 1 starts on 2023-01-02
--     1st January 2024 is Monday    => Week 1 starts on 2024-01-01
-- 
--     1st January 2030 is Tuesday   => Week 1 starts on 2029-12-31
--     1st January 2031 is Wednesday => Week 1 starts on 2030-12-30
-- 
-- (Cross-check: 1st January 1997 was a Wednesday; the summary notes state
-- that week 1 of 1997 started on 1996-12-30, which is consistent with the
-- table derived for dates in the first decade of the third millennium
-- above).
-- 
-- When working with the Informix DATE types, bear in mind that Informix
-- uses WEEKDAY values 0 = Sunday, 1 = Monday, 6 = Saturday.  When the
-- weekday of the first of January has the value in the LH column, you need
-- to add the value in the RH column to the 1st of January to obtain the
-- date of the first day of the first week of the year.
-- 
--     Weekday         Offset to
--     1st January     1st day of week 1
-- 
--     0               +1
--     1                0
--     2               -1
--     3               -2
--     4               -3
--     5               +3
--     6               +2
-- 
-- This can be written as MOD(11-w,7)-3 where w is the (Informix  encoding
-- of the) weekday of 1st January and the value 11 is used to ensure that
-- no negative values are presented to the MOD operator.  Hence, the
-- expression for the date corresponding to the 1st day (Monday) of the 1st
-- week of a given year, yyyy, is:
-- 
--     d1w1 = MDY(1, 1, yyyy) + MOD(11 - WEEKDAY(MDY(1,1,yyyy)), 7) - 3
-- 
-- ==========================================================================
CREATE PROCEDURE sdm_day_one_week_one(yyyy INTEGER) RETURNING DATE;
    DEFINE jan1 DATE;
    LET    jan1 = MDY(1, 1, yyyy);
    RETURN jan1 + MOD(11 - WEEKDAY(jan1), 7) - 3;
END PROCEDURE;

3.3 Calendar generation

Example
Copy
CREATE PROCEDURE calendar_gen(p_anyo SMALLINT)

  -- Llamado desde trigger INSERT de labco_bical_year

    DEFINE errno INT;
    DEFINE isamno INT;
    DEFINE errmsg CHAR(80);
    DEFINE GLOBAL gl_debug SMALLINT DEFAULT 0; -- DEBUG FLAG
    
    DEFINE m_days_day LIKE calendar_day.day;
    DEFINE m_days_daynum LIKE calendar_day.daynum;
    DEFINE m_days_dayname LIKE calendar_day.dayname;
    DEFINE m_days_yearnum LIKE calendar_day.yearnum;
    DEFINE m_days_yearname LIKE calendar_day.yearname;
    DEFINE m_days_semesternum LIKE calendar_day.semesternum;
    DEFINE m_days_semestername LIKE calendar_day.semestername;
    DEFINE m_days_quarternum LIKE calendar_day.quarternum;
    DEFINE m_days_quartername LIKE calendar_day.quartername;
    DEFINE m_days_monthnum LIKE calendar_day.monthnum;
    DEFINE m_days_monthname LIKE calendar_day.monthname;
    DEFINE m_days_fullmonthname LIKE calendar_day.fullmonthname;
    DEFINE m_days_sevennum LIKE calendar_day.sevennum;
    DEFINE m_days_sevenname LIKE calendar_day.sevenname;
    DEFINE m_days_weeknum LIKE calendar_day.weeknum;
    DEFINE m_days_weekofyear LIKE calendar_day.weekofyear;
    DEFINE m_days_weekname LIKE calendar_day.weekname;
    DEFINE m_days_weekdayname LIKE calendar_day.weekdayname;
    
    
    
    DEFINE m_monthsname CHAR(3);
    DEFINE m_week_numweek SMALLINT;
    DEFINE m_curr_date DATE;
    
    -- --------------------------------------------------------------------
    -- Determina si se ha activado el flag de DEBUG global
    -- --------------------------------------------------------------------
    IF gl_debug > 0 THEN
        SET DEBUG FILE TO '/tmp/debug.log';
        TRACE ON;
    END IF
    
    -- ==============================================================
    -- Generacion de Dias (p_bptfa_cal_day)
    -- ==============================================================
    
    
    
    -- --------------------------------------------------------------------
    
    LET m_curr_date = MDY(1,1,p_anyo);
    WHILE m_curr_date <= MDY(12,31,p_anyo)
    
    LET m_monthsname = CASE WHEN MONTH(m_curr_date) = 1 THEN 'JAN'
        WHEN MONTH(m_curr_date) = 2 THEN 'FEB'
        WHEN MONTH(m_curr_date) = 3 THEN 'MAR'
        WHEN MONTH(m_curr_date) = 4 THEN 'APR'
        WHEN MONTH(m_curr_date) = 5 THEN 'MAY'
        WHEN MONTH(m_curr_date) = 6 THEN 'JUN'
        WHEN MONTH(m_curr_date) = 7 THEN 'JUL'
        WHEN MONTH(m_curr_date) = 8 THEN 'AUG'
        WHEN MONTH(m_curr_date) = 9 THEN 'SEP'
        WHEN MONTH(m_curr_date) = 10 THEN 'OCT'
        WHEN MONTH(m_curr_date) = 11 THEN 'NOV'
        WHEN MONTH(m_curr_date) = 12 THEN 'DEC'
    END;
    
    LET m_days_fullmonthname = CASE WHEN MONTH(m_curr_date) = 1 THEN   'Gener'
        WHEN MONTH(m_curr_date) = 2 THEN  'Febrer'
        WHEN MONTH(m_curr_date) = 3 THEN  'Març'
        WHEN MONTH(m_curr_date) = 4 THEN  'Abril'
        WHEN MONTH(m_curr_date) = 5 THEN  'Maig'
        WHEN MONTH(m_curr_date) = 6 THEN  'Juny'
        WHEN MONTH(m_curr_date) = 7 THEN  'Juliol'
        WHEN MONTH(m_curr_date) = 8 THEN  'Agost'
        WHEN MONTH(m_curr_date) = 9 THEN  'Setembre'
        WHEN MONTH(m_curr_date) = 10 THEN 'Octubre'
        WHEN MONTH(m_curr_date) = 11 THEN 'Novembre'
        WHEN MONTH(m_curr_date) = 12 THEN 'Desembre'
    END;
    
    EXECUTE PROCEDURE sdm_get_weeknum(m_curr_date) INTO m_week_numweek;
    
    LET m_days_dayname = LPAD(DAY(m_curr_date),2,'0') || '-' || m_monthsname || '-' || YEAR(m_curr_date);
    
    LET m_days_daynum = DAY(m_curr_date);
    LET m_days_yearnum = YEAR(m_curr_date);
    LET m_days_yearname = 'Year ' || YEAR(m_curr_date);
    LET m_days_semesternum = CASE WHEN m_curr_date <= MDY(6,30,p_anyo) THEN 1 ELSE 2 END;
    LET m_days_semestername = 'S' || m_days_semesternum;
    -- LET m_days_semestername = m_days_yearnum || '-S' || m_days_semesternum;
    LET m_days_quarternum = CASE WHEN m_curr_date <= MDY(3,31,p_anyo) THEN 1
        WHEN m_curr_date <= MDY(6,30,p_anyo) THEN 2
        WHEN m_curr_date <= MDY(9,30,p_anyo) THEN 3
    ELSE 4 END;
    LET m_days_quartername = 'Q' || m_days_quarternum;
    -- LET m_days_monthnum = (YEAR(m_curr_date) * 100) + MONTH(m_curr_date);
    LET m_days_monthnum = MONTH(m_curr_date);
    LET m_days_monthname = 'M' || LPAD(MONTH(m_curr_date), 2, '0');
    LET m_days_sevennum = CASE WHEN DAY(m_curr_date) BETWEEN 1 AND 7 THEN 1
        WHEN DAY(m_curr_date) BETWEEN 8 AND 14 THEN 2
        WHEN DAY(m_curr_date) BETWEEN 15 AND 21 THEN 3
        WHEN DAY(m_curr_date) BETWEEN 22 AND 28 THEN 4
        WHEN DAY(m_curr_date) BETWEEN 29 AND 31 THEN 5
    END;
    LET m_days_sevenname = CASE WHEN DAY(m_curr_date) BETWEEN 1 AND 7 THEN '01 - 07'
        WHEN DAY(m_curr_date) BETWEEN 8 AND 14 THEN '08 - 14'
        WHEN DAY(m_curr_date) BETWEEN 15 AND 21 THEN '15 - 21'
        WHEN DAY(m_curr_date) BETWEEN 22 AND 28 THEN '22 - 28'
        WHEN DAY(m_curr_date) BETWEEN 29 AND 31 THEN '29, 30, 31'
    END;
    LET m_days_weeknum = m_week_numweek;
    LET m_days_weekofyear = (m_days_yearnum * 100) + m_days_weeknum;
    LET m_days_weekname = 'W' || LPAD(m_week_numweek,2,'0');
    LET m_days_weekdayname = CASE WHEN WEEKDAY(m_curr_date) = 0 THEN '7.Sunday'
        WHEN WEEKDAY(m_curr_date) = 1 THEN '1.Monday'
        WHEN WEEKDAY(m_curr_date) = 2 THEN '2.Tuesday'
        WHEN WEEKDAY(m_curr_date) = 3 THEN '3.Wednesday'
        WHEN WEEKDAY(m_curr_date) = 4 THEN '4.Thursday'
        WHEN WEEKDAY(m_curr_date) = 5 THEN '5.Friday'
        WHEN WEEKDAY(m_curr_date) = 6 THEN '6.Saturday'
    END;
    
    
    
    INSERT INTO calendar_day(day                  , daynum             , dayname            , yearnum           , yearname, 
                              semesternum         , semestername       , quarternum         , quartername       , monthnum,
                              fullmonthname       , monthname          , sevennum           , sevenname         , weeknum ,
                              weekofyear          , weekname           , weekdayname        )
                                       
                      VALUES (m_curr_date         , m_days_daynum      , m_days_dayname     , m_days_yearnum    , m_days_yearname,
                              m_days_semesternum  , m_days_semestername, m_days_quarternum  , m_days_quartername, m_days_monthnum,
                              m_days_fullmonthname, m_days_monthname   , m_days_sevennum    , m_days_sevenname  , m_days_weeknum, 
                              m_days_weekofyear   , m_days_weekname    , m_days_weekdayname );
    
    LET m_curr_date = m_curr_date + 1 UNITS DAY;
    END WHILE;

END PROCEDURE;

4 Trigger

There is one trigger needed to fill the table when generating the calendar.

Example
Copy
-- **************************************************************************
-- calendar_year_ins
-- DEISTER WebStudio XSQL-TRIGGER Fri May 24 15:21:47 CEST 2019 Engine: informix
-- **************************************************************************
CREATE TRIGGER calendar_year_ins
INSERT  ON calendar_year
REFERENCING NEW AS nxt
  FOR EACH ROW
   (
   EXECUTE PROCEDURE calendar_gen(nxt.numyear)
   )
;
Copy
<xsql-trigger
    name='calendar_year_ins'
    table='calendar_year'
    event='insert'
>
    <foreach-row>
        <execute-procedure name='calendar_gen'>
            <in>
                <param><nxt>numyear</nxt></param>
            </in>
        </execute-procedure>
    </foreach-row>
</xsql-trigger>

5 Example of year generation

5.1 Insert of the year

The next code shows how to insert a new year in the calendar table system.

Example
Copy
INSERT INTO calendar_year (numyear, nameyear) VALUES (2019, "2019");

5.2 Select of the data generated

The select after the insert of the desired year will show:

Example
Copy
SELECT * FROM calendar year;

        
        Result:
            +=======+========+
            |numyear|nameyear|
            +=======+========+
            |2.019  |2019    | 
            +=======+========+
        

SELECT * FROM calendar_day;

        
        Result:
|==========+======+===============+=======+==========+===========+============+==========+===========+========+================+==========+========+==========+=======+==========+==========+===========+
|day       |daynum|dayname        |yearnum|yearname  |semesternum|semestername|quarternum|quartername|monthnum|fullmonthname   |monthname |sevennum|sevenname |weeknum|weekofyear|weekname  |weekdayname|
|==========+======+===============+=======+==========+===========+============+==========+===========+========+================+==========+========+==========+=======+==========+==========+===========+
|2019-01-01|     1|01-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      1|    201901|W01       |2.Tuesday  |
|2019-01-02|     2|02-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      1|    201901|W01       |3.Wednesda |
|2019-01-03|     3|03-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      1|    201901|W01       |4.Thursday |
|2019-01-04|     4|04-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      1|    201901|W01       |5.Friday   |
|2019-01-05|     5|05-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      1|    201901|W01       |6.Saturday |
|2019-01-06|     6|06-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      1|    201901|W01       |7.Sunday   |
|2019-01-07|     7|07-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       1|01 - 07   |      2|    201902|W02       |1.Monday   |
|2019-01-08|     8|08-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      2|    201902|W02       |2.Tuesday  |
|2019-01-09|     9|09-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      2|    201902|W02       |3.Wednesda |
|2019-01-10|    10|10-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      2|    201902|W02       |4.Thursday |
|2019-01-11|    11|11-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      2|    201902|W02       |5.Friday   |
|2019-01-12|    12|12-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      2|    201902|W02       |6.Saturday |
|2019-01-13|    13|13-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      2|    201902|W02       |7.Sunday   |
|2019-01-14|    14|14-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       2|08 - 14   |      3|    201903|W03       |1.Monday   |
|2019-01-15|    15|15-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      3|    201903|W03       |2.Tuesday  |
|2019-01-16|    16|16-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      3|    201903|W03       |3.Wednesda |
|2019-01-17|    17|17-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      3|    201903|W03       |4.Thursday |
|2019-01-18|    18|18-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      3|    201903|W03       |5.Friday   |
|2019-01-19|    19|19-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      3|    201903|W03       |6.Saturday |
|2019-01-20|    20|20-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      3|    201903|W03       |7.Sunday   |
|2019-01-21|    21|21-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       3|15 - 21   |      4|    201904|W04       |1.Monday   |
|2019-01-22|    22|22-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      4|    201904|W04       |2.Tuesday  |
|2019-01-23|    23|23-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      4|    201904|W04       |3.Wednesda |
|2019-01-24|    24|24-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      4|    201904|W04       |4.Thursday |
|2019-01-25|    25|25-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      4|    201904|W04       |5.Friday   |
|2019-01-26|    26|26-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      4|    201904|W04       |6.Saturday |
|2019-01-27|    27|27-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      4|    201904|W04       |7.Sunday   |
|2019-01-28|    28|28-JAN-2019    |   2019|Year 2019 |          1|S1          |         1|Q1         |       1|Gener           |M01       |       4|22 - 28   |      5|    201905|W05       |1.Monday   |
|==========+======+===============+=======+==========+===========+============+==========+===========+========+================+==========+========+==========+=======+==========+==========+===========+