1 2 import os 3 4 #Functions for manipulating datetime objects 5 #CCYY-MM-DDThh:mm:ssZ 6 def parseDateClass(s): 7 year, month, day = s.split("-") 8 day, tail = day[:2], day[2:] 9 hour, minute, second = tail[1:].split(":") 10 second = second[:2] 11 year, month, day = int(year), int(month), int(day) 12 hour, minute, second = int(hour), int(minute), int(second) 13 return createDateClass(year, month, day, hour, minute, second) 14 15 16 if os.name != "java": 17 from datetime import datetime, timedelta 18 19 #Helper functions for working with a date/time class 20 def createDateClass(year, month, day, hour, minute, second): 21 return datetime(year, month, day, hour, minute, second) 22 23 def printDateClass(d): 24 #Split off fractional seconds, append 'Z' 25 return d.isoformat().split(".")[0]+"Z" 26 27 def getNow(): 28 return datetime.utcnow() 29 30 def getHoursFromNow(hours): 31 return datetime.utcnow() + timedelta(hours=hours) 32 33 def getMinutesFromNow(minutes): 34 return datetime.utcnow() + timedelta(minutes=minutes) 35 36 def isDateClassExpired(d): 37 return d < datetime.utcnow() 38 39 def isDateClassBefore(d1, d2): 40 return d1 < d2 41 42 else: 43 #Jython 2.1 is missing lots of python 2.3 stuff, 44 #which we have to emulate here: 45 import java 46 import jarray 47 48 def createDateClass(year, month, day, hour, minute, second): 49 c = java.util.Calendar.getInstance() 50 c.setTimeZone(java.util.TimeZone.getTimeZone("UTC")) 51 c.set(year, month-1, day, hour, minute, second) 52 return c 53 54 def printDateClass(d): 55 return "%04d-%02d-%02dT%02d:%02d:%02dZ" % \ 56 (d.get(d.YEAR), d.get(d.MONTH)+1, d.get(d.DATE), \ 57 d.get(d.HOUR_OF_DAY), d.get(d.MINUTE), d.get(d.SECOND)) 58 59 def getNow(): 60 c = java.util.Calendar.getInstance() 61 c.setTimeZone(java.util.TimeZone.getTimeZone("UTC")) 62 c.get(c.HOUR) #force refresh? 63 return c 64 65 def getHoursFromNow(hours): 66 d = getNow() 67 d.add(d.HOUR, hours) 68 return d 69 70 def isDateClassExpired(d): 71 n = getNow() 72 return d.before(n) 73 74 def isDateClassBefore(d1, d2): 75 return d1.before(d2) 76