Script Coding Question (Import Datetime)

I’m trying to create a script that runs every second to populate variables with various time information. Here is a snippet that I cannot get to work. I’m trying to use the Python Datetime module or if this module isn’t supported than similiar functionality.

Here is the code sample:

import datetime
tYday = datetime.datetime.now().timetuple().tm_yday

I believe the problem is with the import item.

The complete section of code is listed below. In the IDLE python editor it funcitons as normal.

[i]import datetime
tYear = datetime.datetime.now().timetuple().tm_year
tMon = datetime.datetime.now().timetuple().tm_mon
tDay = datetime.datetime.now().timetuple().tm_mday
tHour = datetime.datetime.now().timetuple().tm_hour
tMin = datetime.datetime.now().timetuple().tm_min
tSec = datetime.datetime.now().timetuple().tm_sec
tYday = datetime.datetime.now().timetuple().tm_yday

if tMin >= 0 and tMin < 15:
if tHour%2 == 0:
period = 1
else:
period = 5

if tMin >= 15 and tMin < 30:
if tHour%2 == 0:
period = 2
else:
period = 6

if tMin >= 30 and tMin < 45:
if tHour%2 == 0:
period = 3
else:
period = 7

if tMin >= 45 and tMin < 60:
if tHour%2 == 0:
period = 4
else:
period = 8

block = abs(round(((tHour/2)-.1),0))+1
dayyear = tYear*1000 + tYday

print period
print dayyear

print tYear
print tMon
print tDay
print tHour
print tMin
print tSec
print tYday[/i]

Have you tried using tags and dynamic properties? Bind a property to the following expression and you’ll get a property that updates automatically (from events firing) without the worry of getting a script to run at the right times.

dateExtract({[System]Client/System/CurrentDateTime}, "minute")

You can then create other properties that are bound to such expressions to get all your desired values.

Nathan - Thanks for your help. I really need alot of the datetime, period, block, dayyear information updated continously. These values will be used to fire events for other activities.

I tried to use the [System]Client/System/CurrentDateTime in a 1 sec timer script and I cannot get it to work. I also notice that dateextract function doesn’t support day of year. When ever there is change in tags period or block I need to perform tasks.

Maybe I’m still approaching this problem incorrectly. I’m definitly open to suggestions. I’m familiar with the Python datetime library.

datetime doesn’t appear to be a supported module. This post lists supported python modules. It may be dated.

My first suggestion used Dynamic Properties. It’s a powerful tool, but probably not relevant since it would require that some window be open (you want this to run continually).

You could take a similar approach with “DB SQLTags”. You could derive your period and block values from SQL Queries, then run your scripts as “Gateway Tag Script Changes” on value changes.

Could you provide a higher level description of the things you want the script to do and when?

The list of supported modules is in the user manual. Datetime is not among them. Conveniently, Java has capable date handling. Here is a re-written version of your script that should work:

from java.util import Calendar now = Calendar.getInstance() tYear = now.get(Calendar.YEAR) tMon = now.get(Calendar.MONTH) tDay = now.get(Calendar.DAY_OF_MONTH) tHour = now.get(Calendar.HOUR) tMin = now.get(Calendar.MINUTE) tSec = now.get(Calendar.SECOND) tYday = now.get(Calendar.DAY_OF_YEAR)

Hope this helps,

Thanks for your help guys!!! I appreciate the outstanding support that is provided in this forum!