I’m sure that this is easy for python and java gurus in this community, but I’m struggling with it…
I need to get WEEK OF YEAR from date/time which I get from the Popup Calendar component in Vision.
On the screen there are two Popup Calendars so the user can choose the start and end date for showing data from SQL database in the Power table.
No problem with that.
But then I have some tables in the db, where the data are stored with the week of year number and the user should be able to choose to show data according to week numbers.
So I thought I would use the same Popup calendars date property to extract week of year number from that date. But it turns out that (at least for me) that’s not as simple as it seems…
Today is 26. August 2021 and the week number is 34, not 35.
That’s one problem.
Another is that I don’t know how to put the date from my Popup calendar (which is java.util.date) )to the Calendar (which is java.util.GregorianCalendar)…?
The problem is that I’m reading data from PostgresSQL where week number is 34, and in Ignition I get 35. So when the user chooses todays date it’ll look for week number 35 in the PostgreSQL, which doesn’t exist (yet… until next week according to PostgreSQL)…?
Lots of programming languages treat things like this differently. It just means you need to be aware of the differences and convert between them if using them together. Not much you can do, unless there’s an option to change the default in the config
you’ll have to also reduce the week number by when when you filter i guess
i would create a project script with a similar syntax like ‘getWeekOfYear(date)’ that does it for you, then you only need to call that and never need to do all the calender stuff and you wont have to remeber to do -1 every time
OK. Apparently, there are two systems for WEEK numbering: non-ISO and ISO SYSTEM 1 (non-ISO): The week containing January 1 is the first week of the year, and is numbered week 1. SYSTEM 2 (ISO): The week containing the first Thursday of the year is the first week of the year, and is numbered as week 1. This system is the methodology specified in ISO 8601, which is commonly known as the European week numbering system.
So, because I’m in Europe, I want ISO week numbering.
In Ignition you have to do:
from java.util import Calendar
cal = Calendar.getInstance()
cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.MONDAY);
start_date= event.source.parent.getComponent('calZacetniDatum').date
end_date = event.source.parent.getComponent('calKoncniDatum').date
cal.setTime(start_date)
start_week_number = cal.get(Calendar.WEEK_OF_YEAR)
print "start_week_number= ", start_week_number
cal.setTime(end_date )
end_week_number = cal.get(Calendar.WEEK_OF_YEAR)
print "end_week_number= ", end_week_number
#Use start_week_number and end_week_number for filtering the results in query...
PostgreSQL apparently uses ISO week numbering by default:
select extract(week from '2021-08-26'::date); -->34
select date_part('week'::text, '2021-08-26'::date); -->34
SELECT to_char(DATE '2021-08-26', 'IW')::int; --> 34
-->If you want non-ISO results, use
SELECT to_char(DATE '2021-08-26', 'WW')::int; --> 35
java.time was added to overcome the inconsistency of Calendar.
from java.time import ZoneId
from java.time.temporal import IsoFields
now = system.date.now()
date = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
week = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)
print now
print date
print week