Day of Week extraction for a day that is not "Today"

Discussion in 'C-Touch/HomeGate/SchedulePlus/PICED Software' started by ChrisJC, Jan 14, 2013.

  1. ChrisJC

    ChrisJC

    Joined:
    Jan 21, 2012
    Messages:
    53
    Likes Received:
    0
    Location:
    Edmonton Qld
    How do I do this? Day of Week only seems to get the day of week for the current date but I want to display what day a future date is. I'm doing a project that requires me to set a disable on schedules and that it can be for a certain number of days. Using an Integer variable for date calculation from a System IO Integer Variable that the client uses to select the number of days delay I calculate the restart date, but I seem to be at a loss on how to display the day of the week that that is.

    There must be a way to do this because I used a clock field (to display the System IO Variable) setting it to show the full day then when I changed it to a SystemIO it still displayed the day which changed with each delay change. This morning when I re-opened the Schedule Plus software the day was gone and I couldn't get it back.

    Any ideas would be gratefully listened too. Cheers
     
    Last edited by a moderator: Jan 14, 2013
    ChrisJC, Jan 14, 2013
    #1
    1. Advertisements

  2. ChrisJC

    Goran C Forum Member

    Joined:
    Jun 7, 2012
    Messages:
    25
    Likes Received:
    0
    Location:
    Adelaide
    Hi Chris

    If you are using a system io to allow the user to define the number of days to delay before restarting the schedule and you are calculating the restart date.
    You can create another system io variable restartdate which defines the restart date, the type of this system io should be date and you can set the value of this system io to your calculated restart date.

    Then when you create a clock component you can define clock components date to be set from this user defined system io (restartdate) that you created via the System IO tab. Then under the visual properties for the clock you can show the day of the week or full name and change the format ie dd/mm/yy, month name etc.

    Remember to save your project with these new components when exiting Schedule Plus.
     
    Goran C, Jan 14, 2013
    #2
    1. Advertisements

  3. ChrisJC

    ChrisJC

    Joined:
    Jan 21, 2012
    Messages:
    53
    Likes Received:
    0
    Location:
    Edmonton Qld
    As Done

    Goran, code as follows......

    {Set the Days Delay for disabling the Irrigation Schedules}
    once GetCBusState("20 - Molecular Science Annexe", "Lighting", "SetDaysDelayVar") = ON
    then begin SetDaysDelayInt:= GetIntSystemIO("SetDaysDelay"); (Integer Variable)
    SetDaysDelayInt:= Date + SetDaysDelayInt;
    SetIntSystemIO("ReStartDate", SetDaysDelayInt); (Date Variable)
    end;

    I place a clock on the screen & select Day, Full name, no time and then change it to a SystemIO Status and it works. Change the properties in any way shape or form & it drops the day and no longer works; when I say it no longer works I mean the code no longer works - shut down & restart Schedule Plus delete the field, replace it and DONT select day.

    Schedule Plus 4.11.1.0. and I think it has some "issues".

    I'd drop in a picture but this board only asks for a http:// link; time to update the software no doubt - and while we're on the subject the link to the forum is broken on the CIS Technical site; its still pointing to www2.clipsal.com/cis/technical/cbus_forum
     
    Last edited by a moderator: Jan 15, 2013
    ChrisJC, Jan 15, 2013
    #3
  4. ChrisJC

    ChrisJC

    Joined:
    Jan 21, 2012
    Messages:
    53
    Likes Received:
    0
    Location:
    Edmonton Qld
    Sorted

    As always there is a way.... When I first started programming I couldn't believe just how much has to be repetitively done and that was way before I started on C-Bus. To write a simple calendar was a bucket load of code and that was with writing libraries to reuse the stuff.

    The trick is not just to add the delayed days to the date to get the Restart Date but to display the Day of the Week as well you have to add the delay to the DayOfWeek integer as well and then use "Case of" to find the day (1 & 8 being Sunday and 7 & 14 being Saturday [1 week delay])

    here is the code....

    {Set the Days Delay for disabling the Irrigation Schedules}
    once GetCBusState("20 - Molecular Science Annexe", "Lighting", "SetDaysDelayVar") = ON
    then begin DayInt:= DayOfWeek; //get the DayOfWeek
    SetDaysDelayInt:= GetIntSystemIO("SetDaysDelay"); //get the number of days
    DayInt:= DayInt + SetDaysDelayInt; //add the number of days to the DayOfWeek
    SetDaysDelayInt:= Date + SetDaysDelayInt; //add the number of days to the date
    SetIntSystemIO("ReStartDate", SetDaysDelayInt); //set the restart date
    case DayInt of //set the day of week the restart day is on
    1 : SetStringSystemIO("RestartDay", 'Sunday');​
    2 : SetStringSystemIO("RestartDay", 'Monday');​
    3 : SetStringSystemIO("RestartDay", 'Tuesday');​
    4 : SetStringSystemIO("RestartDay", 'Wednesday');​
    5 : SetStringSystemIO("RestartDay", 'Thursday');​
    6 : SetStringSystemIO("RestartDay", 'Friday');​
    7 : SetStringSystemIO("RestartDay", 'Saturday');​
    8 : SetStringSystemIO("RestartDay", 'Sunday');​
    9 : SetStringSystemIO("RestartDay", 'Monday');​
    10 : SetStringSystemIO("RestartDay", 'Tuesday');​
    11 : SetStringSystemIO("RestartDay", 'Wednesday');​
    12 : SetStringSystemIO("RestartDay", 'Thursday');​
    13 : SetStringSystemIO("RestartDay", 'Friday');​
    14 : SetStringSystemIO("RestartDay", 'Saturday');​
    end; ​
    end;
     
    Last edited by a moderator: Jan 15, 2013
    ChrisJC, Jan 15, 2013
    #4
  5. ChrisJC

    Goran C Forum Member

    Joined:
    Jun 7, 2012
    Messages:
    25
    Likes Received:
    0
    Location:
    Adelaide
    I can see that you are using the logic engine, have you referred to the Logic Engine Help - specifically the Date Functions. From the Logic Engine Help File the example for setting the time is as follows (i assume that the date would be likewise). I believe that in the case of a Date it requires the second parameter to be a date formatted string.

    To set the value of System IO variable called "Start Time" to 46800 (1PM) :

    SetIntSystemIO("Start Time", "1:00 PM");


    Have you tried implementing an addition variable of type string ie DateAfterDelay and used the DateToString procedure as below

    SetDaysDelayInt:= Date + SetDaysDelayInt;
    DateToString(SetDaysDelayInt, DateAfterDelay);
    SetIntSystemIO("ReStartDate", DateAfterDelay); (Date Variable)

    This way you are setting the ReStartDate user defined System IO (Date Type) to a date format string.

    For example if the date was 1/1/2010, this would hence make the code be in a format similar to
    SetIntSystemIO("ReStartDate", "1/1/2010);
    rather than
    SetIntSystemIO("ReStartDate", 35065) which i believe the SetIntSystemIO is not expecting if the SystemIO is of Date type.
     
    Last edited by a moderator: Jan 15, 2013
    Goran C, Jan 15, 2013
    #5
  6. ChrisJC

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,185
    Likes Received:
    123
    Location:
    Adelaide, Australia
    You only really need 2 lines of active code:


     
    Ashley, Jan 15, 2013
    #6
  7. ChrisJC

    ChrisJC

    Joined:
    Jan 21, 2012
    Messages:
    53
    Likes Received:
    0
    Location:
    Edmonton Qld
    WOW, Arrays! Long time no see.....

    Thanks Ashley; I'm a very visual person and arrays have never been my strong point as it's something I cannot visualise but I have used them in the past but never with C-Bus logic. As the delay is only required for a week I figured that "case x of" was a perfectly adequate way of going about it. If it were a large number of days delay then your array would be the best way to go but the job requires a few days delay or an indefinite disable; irrigation enable/disable dependant on the weather, rainy season or floods.

    Thanks for reminding me that arrays are there and that I haven't used them since 2000 and I actually don't miss them but then again, as an electrician I don't miss the parallelogram of power which I could do but never got my head around either (power factor correction?).

    Thanks again for your time in this; your are, as always, a great source of code.

    Cheers
     
    ChrisJC, Jan 15, 2013
    #7
  8. ChrisJC

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,185
    Likes Received:
    123
    Location:
    Adelaide, Australia
    Just remember that the more conditional code you have, the more conditions you have to test. If you make a typo or logic error in one line of a case statement you won't know until that line is executed, which could be a very long time and result in an expensive call-out. It's very time consuming to test every possible path through a piece of code.

    Also, in your code you need to ensure the forward interval is always in range or the case statement will just fall though and the day of week will not be updated.
     
    Ashley, Jan 15, 2013
    #8
  9. ChrisJC

    ChrisJC

    Joined:
    Jan 21, 2012
    Messages:
    53
    Likes Received:
    0
    Location:
    Edmonton Qld
    Already taken care of

    once GetCBusState("20 - Molecular Science Annexe", "Lighting", "StartScheduleDelay") = ON
    then begin RetrievedDateInt:= GetIntSystemIO("ReStartDate"); // get the reset date
    if (Date >= RetrievedDateInt) // Make sure it's in the future
    then begin SetCBusState("20 - Molecular Science Annexe", "Lighting", "StartScheduleDelay", OFF); // if it isn't in the future, abort
    etc......

    Always, but always, test for conditions; at least we dont have to test for the mouse which adds 500+% to the coding (Mouse Enter, Mouse Exit, Mouse Left Down, Mouse Left Up, Mouse Right Down, Mouse Right Up, Mouse Left Double, Mouse Right Double, Mouse Centre Down, Mouse Centre Up, Mouse Centre Double, Mouse Scroll Down, Mouse Scroll Up, Mouse Scroll Left, Mouse Scroll Right and so forth - a right pain when you want to disable the mouse from selecting a field in a database but only want to use the tab key :D
     
    Last edited by a moderator: Jan 15, 2013
    ChrisJC, Jan 15, 2013
    #9
  10. ChrisJC

    ssaunders

    Joined:
    Dec 17, 2008
    Messages:
    74
    Likes Received:
    0
    Location:
    Melbourne
    Modified version of Ashley's code to get a day name for any date, future, past or present. Implemented as a procedure with local variables to allow easy re-use and minimise memory footprint (at the expense of execution speed).

    Remove the case conditionals to dispense with 'yeasterday', 'today' and 'tomorrow'.

    Code:
    procedure GetDayOfWeek(var dayname:string; fordate:integer);
    var
      dayno: integer;
      DayNames: array[0..63] of char;
      DayNameL: array[0..7] of char;
    begin
      dayNames := 'Sunday   Monday   Tuesday  WednesdayThursday Friday   Saturday ';
      dayNameL := #6#6#7#9#8#6#8;
    
      case fordate-date of
        -1: dayname := 'Yesterday';
        0 : dayname := 'Today';
        +1: dayname := 'Tomorrow';
      else
        begin
          // Day number 1-7
          if fordate < date then
            dayno := (6 + DayOfWeek + ((fordate-date) mod 7)) mod 7 + 1
          else
            dayno := (DayOfWeek - 1 + (fordate-date)) mod 7 + 1;
          // Copy the day name
          copy(dayname, dayNames, dayno * 9 - 8, ord(dayNameL[dayno]));
        end;
      end;
    end;
    Cheers,
    Steve.
     
    ssaunders, Jun 2, 2013
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.