tlslite.utils.datefuncs

Package tlslite :: Package utils :: Module datefuncs |
[hide private]
[frames] | no frames]

# Source Code for Module tlslite.utils.datefuncs

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