Changeset 175

Show
Ignore:
Timestamp:
Fri Dec 2 17:56:35 2005
Author:
drew
Message:

- completed unit testing for timetool
- incrementMonth function for timetool

Files:

Legend:

Unmodified
Added
Removed
Modified
  • branches/utils/xix/utils/timetool.py

    r159 r175  
    139 139     return dtime + (delta * ONE_DAY)  
    140 140  
    141    
      141 _MONTH_PAIRS = zip(range(1,13) +  [12] + range(1,12), [12] + range(1,12) + range(1,13))  
      142      
    142 143 def incrementMonth(dtime, iterations=1):  
    143       """Increment month by iterations.  If iterations is less than 0, the  
    144       datetime is decrement by month.  
      144     """Increment month for x iterations.  If iterations is less than 0, the  
      145     datetime is decremented by one month. If the day the month of the datetime  
      146     instance provided is the max day of the month, the resulting datetime instance  
      147     will also end on the last day of the month.  Otherwise, the day of the month  
      148     for the next in the iteration will be the same when possible.  
      149  
      150     >>> from time import strptime, strftime, mktime, tzset  
      151     >>> from datetime import datetime  
      152     >>> t = strptime('2005-01-01', '%Y-%m-%d')  
      153     >>> dt = datetime.fromtimestamp(mktime(t))  
      154     >>> dt2 = incrementMonth(dt)  
      155     >>> print strftime('%Y-%m-%d', dt2.timetuple())  
      156     2005-02-01  
      157  
    145 158     """  
      159     mseqpairs = zip(range(1,13))  
      160     step = (27, -27)[iterations < 0]  
      161     adjstep = (1, -1)[iterations < 0]  
      162     eom = monthmax(dtime) == dayOfMonth(dtime)  
      163     newtime = dtime  
      164     if eom:  
      165         step = (1, -27)[iterations < 0]  
      166         for i in range(abs(iterations)):  
      167             mo = newtime.month  
      168             newtime = newtime + step * ONE_DAY  
      169             while (newtime.month, mo) not in _MONTH_PAIRS:  
      170                 newtime = newtime + adjstep * ONE_DAY  
      171             mmax = monthmax(newtime)  
      172             newtime = newtime + (mmax - dayOfMonth(newtime)) * ONE_DAY  
      173     else:  
      174         dom = dayOfMonth(dtime)  
      175         for i in range(abs(iterations)):  
      176             mo = newtime.month  
      177             newtime = newtime + step * ONE_DAY  
      178             while (newtime.month, mo) not in _MONTH_PAIRS:  
      179                 newtime = newtime + adjstep * ONE_DAY  
      180             mmax = monthmax(dtime)  
      181             tgt = (dom, mmax)[dom > mmax]  
      182             newtime = newtime + (tgt - dayOfMonth(newtime)) * ONE_DAY  
      183     return newtime  
      184      
    146 185  
    147 186 def monthmax(dtime):  
  • branches/utils/xix/utils/config.py

    r159 r175  
    55 55 from xix.utils.python import ModuleWrapper  
    56 56 from xix.utils.python import getCallersGlobals  
      57 import warnings  
    57 58 import re  
    58 59 import os.path  
     
    240 241  
    241 242     def getConfig(self, name=None):  
    242           if name is None: name = getCallersGlobals([__name__])['__name__']  
    243           if name in self.loaded: return self.loaded[name]  
      243         if name is None:  
      244             warnings.warn('name argument will be required in future release')  
      245             name = getCallersGlobals([__name__])['__name__']  
      246         if name in self.loaded:  
      247             return self.loaded[name]  
    244 248         self.loaded[name] = self.loader.load(open(self.resources[name]))  
    245 249         return self.loaded[name]  
  • branches/utils/xix/__init__.py

    r172 r175  
    2 2 from xix.utils.config import configFactory  
    3 3 import os  
      4 from pkg_resources import resource_filename  
    4 5  
    5 6 pj = os.path.join  
    6 7 dir = os.path.dirname  
    7 8  
    8   configFactory.addResource('app.cfg', pj(dir(__file__), '__app__.cfg'))  
      9 #configFactory.addResource('app.cfg', pj(dir(__file__), '__app__.cfg'))  
      10 configFactory.addResource('app.cfg', resource_filename(__name__, '__app__.cfg'))  
    9 11  
    10 12 _cfg = configFactory.getConfig('app.cfg')