Friday, February 13, 2015

Ignition's Calendar Components

I have created a few python scripts for use with the Calendar Components in Ignition. First, I would like to start with how to default to today's date when the window (or template) with the Calendar is opened. 

In the property editor under 'Data', bind the code   now(0)   to the Date field. This will push today's date into the calendar only when the window is opened as the poll rate is 0. 

Next, I added a the following custom properties to the calendar.

  • day - The day of the week we wanted.
  • DOW - Day of Week we want.
  • previous - This allows the previous week's day to be returned.

With these variables, I run my script 'getday' to get the correct day I want. This code should be called anytime the date changes.

Here is that code.

def getday(date,dow,previous = 0):
     from java.util import Calendar

c = Calendar.getInstance()
dayofweek = c.get(Calendar.DAY_OF_WEEK)

#Determine what day to return...
if dow == 'Monday':
newday = dayofweek - 2
elif dow == 'Tuesday':
newday = dayofweek - 3
elif dow == 'Wednesday':
newday = dayofweek - 4
elif dow == 'Thursday':
newday = dayofweek - 5
elif dow == 'Friday':
newday = dayofweek - 6
elif dow == 'Saturday':
newday = dayofweek - 7
else: #Default day is Sunday....
newday = dayofweek - 1

if (previous == 1) and (newday < 0):
newday += 7

c.add(Calendar.DATE, -newday)
day = c.getTime()

return day

The other common calendar date wanted is the last day of the month. To do this is simple.

def lastday(date):
from java.util import Calendar

c = Calendar.getInstance()

lastday = c.getActualMaximum(Calendar.DAY_OF_MONTH);

return lastday

Here is a window that has two calendar's on it. It has the example calendar and another calendar to get the same result. You need to have Ignition v7.7.2 or newer.

No comments:

Post a Comment