From f6e374db79acd484a6b4b930b06ce4a2b0a8a183 Mon Sep 17 00:00:00 2001 From: Ron Anderson Date: Tue, 2 Dec 2014 11:45:05 -0600 Subject: [PATCH] Omaha #3875 Fixed SmartScript.createTimeRange(). Added API to better deal with local time. Change-Id: I8e6f79bf6b7754924480a39f1631ba10a12bdcae Former-commit-id: ebeb4440ef28da9148177af21ef53cf06e6972ee [formerly e656ec3ccf20ae4a9eae23040e1a08d7032ee42a] Former-commit-id: 4efd17db742d7ace2eb6d92cd50cae370d196f64 --- .../gfe/userPython/utilities/SmartScript.py | 63 ++++++++++++------- .../common_static/base/python/gfe/AbsTime.py | 8 ++- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/cave/com.raytheon.viz.gfe/localization/gfe/userPython/utilities/SmartScript.py b/cave/com.raytheon.viz.gfe/localization/gfe/userPython/utilities/SmartScript.py index aedac1e2ef..4ede62829c 100644 --- a/cave/com.raytheon.viz.gfe/localization/gfe/userPython/utilities/SmartScript.py +++ b/cave/com.raytheon.viz.gfe/localization/gfe/userPython/utilities/SmartScript.py @@ -56,6 +56,12 @@ # Wx/Discrete in First mode. # Dec 23, 2013 16893 ryu Added unloadWEs() method (created by njensen) # Apr 29, 2014 3097 randerso Fixed getGrids() to return non-scalar grids as tuples in all cases +# Dec 01, 2014 3875 randerso Added gmTime() and localTime() functions which are exact equivalents +# to those in the python time module. +# Added getTimeZoneStr and getTzInfo which return the site's local time +# zone as a string or as an object respectively +# Fixed createTimeRange to correctly return time ranges relative to local +# time regardless of setting of os.environ['TZ'] # ######################################################################## import types, string, time, sys @@ -100,7 +106,7 @@ class SmartScript(BaseTool.BaseTool): # A cache of grids accessed by the derived class #self.__pythonGrids = [] self.__accessTime = 0 - self.__gridLoc = None + self.__gridLoc = self.__parmMgr.compositeGridLocation() self.__topoGrid = None self.__toolType = "numeric" self._empty = zeros(self.getGridShape(), float32) @@ -125,12 +131,6 @@ class SmartScript(BaseTool.BaseTool): return self.__mutableID def getGridLoc(self): - if self.__gridLoc is None: - parmIDs = self.__parmMgr.getAvailableParms(self.__mutableID.toJavaObj()) - if len(parmIDs) > 0: - tempParmID = parmIDs[0] - info = self.__dataMgr.getClient().getGridParmInfo(tempParmID) - self.__gridLoc = info.getGridLoc() return self.__gridLoc def setToolType(self, toolType): @@ -1401,8 +1401,8 @@ class SmartScript(BaseTool.BaseTool): "Must specify a database ID for mode=Database") if mode == "LT": - localTime = time.mktime(time.localtime()) - gmTime = time.mktime(time.gmtime()) + localTime = time.mktime(self.localtime()) + gmTime = time.mktime(self.gmtime()) localAbsTime = AbsTime.AbsTime(localTime) delta = localTime - gmTime @@ -1416,7 +1416,7 @@ class SmartScript(BaseTool.BaseTool): end = dbID.modelTime() + (endHour * 3600) return TimeRange.TimeRange(start, end) else: - currentTime = time.gmtime() + currentTime = self.gmtime() today = AbsTime.absTimeYMD(currentTime.tm_year, currentTime.tm_mon, currentTime.tm_mday) start = today + (startHour * 3600) @@ -1473,7 +1473,7 @@ class SmartScript(BaseTool.BaseTool): display = display + timeRange.endTime().stringFmt(endFmt) if LTorZulu == "LT": # Adjust time zone to local time - localTime = time.localtime(time.time()) + localTime = self.localtime() zoneName = time.strftime("%Z", localTime) display = string.replace(display, "GMT", zoneName) return display @@ -1497,28 +1497,49 @@ class SmartScript(BaseTool.BaseTool): ''' Assumes date (default is current Simulate Time) is a UTC time to convert to the time zone tz (default is Site Time Zone). returns datetime - - This should be used instead of time.localtime() ''' - import dateutil.tz - if tz is None: - tzname = self.__dataMgr.getClient().getSiteTimeZone() - tz = dateutil.tz.gettz(tzname) + tz = self.getTzInfo() - utczone = dateutil.tz.gettz('UTC') - gmdt = self._gmtime(date).replace(tzinfo=utczone) + gmdt = self._gmtime(date) tzdt = gmdt.astimezone(tz) return tzdt def _gmtime(self, date=None): ''' This takes date (default current Simulated Time) and converts it to AbsTime - - This should be used instead of time.gmtime() ''' if date is None: date = SimulatedTime.getSystemTime().getTime() return AbsTime.AbsTime(date) + + def gmtime(self, date=None): + ''' This takes date (default current Simulated Time) and converts it to AbsTime + + This should be used instead of time.gmtime() + ''' + return self._gmtime(date).utctimetuple() + + def localtime(self, date=None): + ''' Assumes date (default is current Simulated Time) is a UTC time to convert + to the time zone of the local site. + + This should be used instead of time.localtime() + ''' + return self._localtime(date).timetuple() + + def getTimeZoneStr(self): + ''' Returns local time zone of the current site as a string + ''' + return self.__gridLoc.getTimeZone() + + def getTzInfo(self, tzname=None): + ''' Returns time zone object compatible with datetime for the desired time zone. + Defaults to local site's time zone if tzname not specified. + ''' + if tzname is None: + tzname = self.getTimeZoneStr() + import dateutil.tz + return dateutil.tz.gettz(tzname) def dayTime(self, timeRange, startHour=6, endHour=18): # Return 1 if start of timeRange is between the diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/utility/common_static/base/python/gfe/AbsTime.py b/edexOsgi/com.raytheon.edex.plugin.gfe/utility/common_static/base/python/gfe/AbsTime.py index a7c702e1a4..dc7d70a43f 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/utility/common_static/base/python/gfe/AbsTime.py +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/utility/common_static/base/python/gfe/AbsTime.py @@ -22,6 +22,9 @@ import calendar from java.util import Date import JUtil +import dateutil.tz +GMT_ZONE = dateutil.tz.gettz('GMT') + # # Provides a AWIPS I GFE partially-compatible wrapper to AbsTime # @@ -34,7 +37,7 @@ import JUtil # Date Ticket# Engineer Description # ------------ ---------- ----------- -------------------------- # 04/10/08 chammack Initial Creation. -# +# 12/01/2014 #3875 randerso Set time zone on underlying datetime object to GMT # # class AbsTime(datetime, JUtil.JavaWrapperClass): @@ -47,8 +50,9 @@ class AbsTime(datetime, JUtil.JavaWrapperClass): tmp = datetime.utcfromtimestamp(arg) else: tmp = datetime.utcfromtimestamp(arg.getTime() / 1000) + return datetime.__new__(self, tmp.year,tmp.month, - tmp.day, tmp.hour, tmp.minute, tmp.second) + tmp.day, tmp.hour, tmp.minute, tmp.second, tzinfo=GMT_ZONE) def unixTime(self): tuple = self.utctimetuple()