#NEWLINE# def last_completed_audit_period( unit = None , before = None ) : #NEWLINE# #INDENT# "This method gives you the most recently *completed* audit period.\n\n    arguments:\n            units: string, one of 'hour', 'day', 'month', 'year'\n                    Periods normally begin at the beginning (UTC) of the\n                    period unit (So a 'day' period begins at midnight UTC,\n                    a 'month' unit on the 1st, a 'year' on Jan, 1)\n                    unit string may be appended with an optional offset\n                    like so:  'day@18'  This will begin the period at 18:00\n                    UTC.  'month@15' starts a monthly period on the 15th,\n                    and year@3 begins a yearly one on March 1st.\n            before: Give the audit period most recently completed before\n                    <timestamp>. Defaults to now.\n\n\n    returns:  2 tuple of datetimes (begin, end)\n              The begin timestamp of this audit period is the same as the\n              end of the previous.\n    " #NEWLINE# if ( not unit ) : #NEWLINE# #INDENT# unit = CONF . instance_usage_audit_period #NEWLINE# #UNINDENT# offset = 0 #NEWLINE# if ( '@' in unit ) : #NEWLINE# #INDENT# ( unit , offset ) = unit . split ( '@' , 1 ) #NEWLINE# offset = int ( offset ) #NEWLINE# #UNINDENT# if ( before is not None ) : #NEWLINE# #INDENT# rightnow = before #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rightnow = timeutils . utcnow ( ) #NEWLINE# #UNINDENT# if ( unit not in ( 'month' , 'day' , 'year' , 'hour' ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Time period must be hour, day, month or year' ) #NEWLINE# #UNINDENT# if ( unit == 'month' ) : #NEWLINE# #INDENT# if ( offset == 0 ) : #NEWLINE# #INDENT# offset = 1 #NEWLINE# #UNINDENT# end = datetime . datetime ( day = offset , month = rightnow . month , year = rightnow . year ) #NEWLINE# if ( end >= rightnow ) : #NEWLINE# #INDENT# year = rightnow . year #NEWLINE# if ( 1 >= rightnow . month ) : #NEWLINE# #INDENT# year -= 1 #NEWLINE# month = ( 12 + ( rightnow . month - 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# month = ( rightnow . month - 1 ) #NEWLINE# #UNINDENT# end = datetime . datetime ( day = offset , month = month , year = year ) #NEWLINE# #UNINDENT# year = end . year #NEWLINE# if ( 1 >= end . month ) : #NEWLINE# #INDENT# year -= 1 #NEWLINE# month = ( 12 + ( end . month - 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# month = ( end . month - 1 ) #NEWLINE# #UNINDENT# begin = datetime . datetime ( day = offset , month = month , year = year ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( unit == 'year' ) : #NEWLINE# #INDENT# if ( offset == 0 ) : #NEWLINE# #INDENT# offset = 1 #NEWLINE# #UNINDENT# end = datetime . datetime ( day = 1 , month = offset , year = rightnow . year ) #NEWLINE# if ( end >= rightnow ) : #NEWLINE# #INDENT# end = datetime . datetime ( day = 1 , month = offset , year = ( month . year - 1 ) ) #NEWLINE# begin = datetime . datetime ( day = 1 , month = offset , year = ( rightnow . year - 2 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# begin = datetime . datetime ( day = 1 , month = offset , year = ( rightnow . year - 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( unit == 'day' ) : #NEWLINE# #INDENT# end = datetime . datetime ( hour = offset , day = rightnow . day , month = rightnow . month , year = rightnow . year ) #NEWLINE# if ( end >= rightnow ) : #NEWLINE# #INDENT# end = ( end - datetime . timedelta ( days = 1 ) ) #NEWLINE# #UNINDENT# begin = ( end - datetime . timedelta ( days = 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( unit == 'hour' ) : #NEWLINE# #INDENT# end = rightnow . replace ( minute = offset , second = 0 , microsecond = 0 ) #NEWLINE# if ( end >= rightnow ) : #NEWLINE# #INDENT# end = ( end - datetime . timedelta ( hours = 1 ) ) #NEWLINE# #UNINDENT# begin = ( end - datetime . timedelta ( hours = 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return ( begin , end )