adding days to a date

Corrected typo in y4 calculation: [SUB cury y4 y4] is correct; [SUB cury 3 y4] was incorrect.


...
Code:
########################################################################
### Exit this routine unless "finished cleaning" one-shot is 1
    
   fin_cln_ons
-------]/[---------<RET>---


########################################################################
### Assume count of days in current month, tag curdim, is 30
    _______________
---[MOV            ]----
   [Source       30]
   [Dest     curdim]
    ---------------


########################################################################
### Calculations to check for leap year
### - Int tag yN is a year evenly divisible by N and near cury
### - Strictly speaking, this calculation is not needed unless curm is 2
### - Assumes modulo instruction is not available
### - No more than y4 is really needed; this will be irrelevant ca. 2100
        _____________
---+---[AND          ]---+---
   |   [SourceA  cury]   |
   |   [SourceB     3]   |
   |   [Dest       y4]   |
   |    -------------    |
   |    _____________    |
   +---[SUB          ]---+
       [SourceA  cury]
       [SourceB    y4]
       [Dest       y4]
        -------------

        _____________
---+---[DIV          ]---+---
   |   [SourceA  cury]   |
   |   [SourceB   100]   |
   |   [Dest     y100]   |
   |    -------------    |
   |    _____________    |
   +---[MUL          ]---+
       [SourceA  y100]
       [SourceB   100]
       [Dest     y100]
        -------------

        _____________
---+---[DIV          ]---+---
   |   [SourceA  cury]   |
   |   [SourceB   400]   |
   |   [Dest     y400]   |
   |    -------------    |
   |    _____________    |
   +---[MUL          ]---+
       [SourceA  y400]
       [SourceB   400]
       [Dest     y400]
        -------------


########################################################################
### Adjust 30d assumption above for Jan, Mar, May, Jul, Aug, Oct, Dec

        _____________   curm/0        __________________
---+---[LES          ]----] [----+---[ADD               ]------
   |   [SourceA  curm]           |   [SourceA     curdim]
   |   [SourceB     8]           |   [SourceB          1]
   |    -------------            |   [Dest        curdim]
   |                             |    ------------------
   |    _____________   curm/0   |
   +---[GRT          ]----]/[----+
       [SourceA  curm]
       [SourceB     7]
        ------------- 


########################################################################
### Adjust 30d assumption above for Feb:
### 30-2 => 28, then +1 => 29 for leap years

    _____________                                                 _______________
---[EQU          ]---+-------------------------------------------[SUB            ]---+--
   [SourceA  curm]   |                                           [SourceA  curdim]   |
   [SourceB     2]   |                                           [SourceB      -2]   |
    -------------    |                                           [Dest     curdim]   |
                     |                                            ---------------    |
                     |    _____________     _____________         _______________    |
                     +---[EQU          ]---[NEQ          ]---+---[ADD            ]---+
                     |   [SourceA  cury]   [SourceA  cury]   |   [SourceA  curdim]
                     |   [SourceB    y4]   [SourceB  y100]   |   [SourceB       1]
                     |    -------------     -------------    |   [Dest     curdim]
                     |    _____________                      |    ---------------
                     +---[EQU          ]---------------------+
                         [SourceA  cury]
                         [SourceB  y400]
                          -------------


########################################################################
### Move cury & curm to nxty & nxtm as initial year and month estimates
### Calculate nxtd, day of month a fortnight hence
### Correct for rollover at end of month


        ____________
---+---[MOV         ]------------------------------+---
   |   [Source  cury]                              |
   |   [Dest    nxtd]                              |
   |    ------------                               |
   |    _____________                              |
   +---[MOV         ]------------------------------+
   |   [Source  curm]                              |
   |   [Dest    nxtm]                              |
   |    ------------                               |
   |    _____________                              |
   +---[ADD          ]-----------------------------+
   |   [SourceA  curd]                             |
   |   [SourceB    14]                             |
   |   [Dest     nxtd]                             |
   |    -------------                              |
   |    _______________         _______________    |
   +---[GRT            ]---+---[SUB            ]---+
       [SourceA    nxtd]   |   [SourceA    nxtd]   |
       [SourceB  curdim]   |   [SourceB  curdim]   |
        ---------------    |   [Dest       nxtd]   |
                           |    ---------------    |
                           |    _____________      |
                           +---[ADD          ]-----+
                               [SourceA  nxtm]
                               [SourceB     1]
                               [Dest     nxtm]
                                -------------


########################################################################
### Correct for rollover at end of year
    _______________         _______________
---[GRT            ]---+---[MOV            ]---+---
   [SourceA    nxtm]   |   [SourceA       1]   |
   [SourceB      12]   |   [SourceB    nxtm]   |
    ---------------    |    ---------------    |
                       |    _____________      |
                       +---[ADD          ]-----+
                           [SourceA  nxty]
                           [SourceB     1]
                           [Dest     nxty]
                            -------------
Kinda makes daba's relative approach more attractive, eh?
 
Where curm/0 bit is setted, as I don't understand how july and august are changed to 31 days?




months with 31 days before August(8) are Jan(1), Mar(3), May(5), Jul(7) i.e. odd months, so the /0 bit of curm is 1 for 31-day months if curm < 8.


months with 31 days after July(7) are Aug(8), Oct(10), Dec(12) i.e. even months, so the /0 bit of curm is 0 for 31-day months if curm > 7.


See [Knuckle Algorithm] below.


Fortunately Feb(2) is even, although there would be other ways around that.

This approach as written only works for delay periods of less than 29d; at 29d and above, the delay from Jan 31 increments nxtm into March or later i.e. two months at least


That said, it would be cleaner to have 13-element array DPM = [0,31,28,31,30,31,30,31,31,30,31,30,31] in it and get the days-per-month via DPM[curm], leaving only the leap-year check to be coded for Feb.



Knuckle Algorithm


months 1-7 (Jan-Jul) on left hand, four (odd) knuckles, three (even) valleys; months 8-12 (Aug-Dec) on right hand, three (even) knuckles, two (odd) valleys.


months-knuckles.svg
 
Last edited:
Maybe, but still what OP wants.
14days add or subtract is posted several times to this thread.

Is it? Anyone with a gram of sanity would just put in a 14 day timer. You know that and I know that. This thread may of lead him on the right path.
 

Similar Topics

Hi Everyone, Currently we have three plants running with Controllogix PLCs (L72, L73, L74). In each of these plants we have 2 FTView SE...
Replies
0
Views
53
Hello, Im building project with 1756-L82ES controller and 1756-IB16S card but i cant find it when trying to add the card to IO configuration...
Replies
3
Views
134
Hello, I have a pair of redundant 1756-L71 controllers rev 24. These controllers currently have produced and consumed tag interfaces to 3 other...
Replies
2
Views
157
I'm adding an IAI Gateway with 2 axes connected to it. To an ethernet network on my PLC. So, I think the math is correct on the Input and Output...
Replies
0
Views
141
Hi Folks. I am gearing up to add a 2nd identical HMI to a project. This 2nd HMI will basically be an exact clone of the 1st one. The equipment...
Replies
3
Views
256
Back
Top Bottom