Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Leap Year error in CAA DTUtils.GetDayOfWeek function

tvm
2020-02-29
2020-03-02
  • tvm - 2020-02-29

    I believe I have found an error in the CAA DTUtils GetDayOfWeek function. According to the documentation, it should return 0-6 for days of the week, Sunday-Saturday. Today, I'm getting a return value of 7, and an error of DTU_ERROR_UNKNOWN.
    I have library version 3.5.12.0. Anyone else have this problem?

     

    Last edit: tvm 2020-02-29
  • Thomas - 2020-03-02

    Hi,
    Yes, it is an known bug.
    The entry in our developer database is:
    CDS-47688: Library: CAA DTUtility GetDayOfWeek returns wrong weekday for leap day

    Regards Thomas

     
  • tvm - 2020-03-02

    @ThK you seem to have access to this developer database. Can you explain why this kind of information is not publicly available? I used the CAA library because I made the assumption that a date and time utility would have been tested against dates and times, leap years being one of the most obvious tests. Knowing that it doesn't work, I don't see the use of it--I might as well write my own function.

     
  • tvm - 2020-03-02

    For anyone who needs to replace it, here's a function that works

    FUNCTION DATE_TO_DAY : INT(0..6)
    VAR_INPUT
        DateIn:                 DATE;
    END_VAR
    VAR CONSTANT
        SECONDS_PER_DAY:        DINT:= 24*60*60;    //number of seconds in a day
        EPOCH_STARTDAY:         DINT:= 4;           //DATE variable is stored as seconds since Jan 1, 1970, which was a Thursday 
    END_VAR
    
    (*====================================================================================
    DATE_TO_DAY
    Calculate day of week from date 
    Sunday=0, Saturday=6
    ======================================================================================*)
    
    DATE_TO_DAY := DINT_TO_INT(((DATE_TO_DINT(DateIn) / SECONDS_PER_DAY) + EPOCH_STARTDAY) MOD 7);
    
     

Log in to post a comment.