计算多月/多日前后 日期

from time import strftime, localtime
from datetime import timedelta, date
import calendar

def _get_days_of_month(self, year, mon): """ get days of month """ # print calendar.monthrange(year, mon)[1] return calendar.monthrange(year, mon)[1] def _addzero(self, n): """ add 0 before 0-9 return 01-09 """ nabs = abs(int(n)) if nabs < 10: return "0" + str(nabs) else: return nabs def _getyearandmonth(self, n=0): """ get the month,days,year from today before or after n months """ thisyear = int(self.year) thismon = int(self.mon) totalmon = thismon + n if n >= 0: if totalmon <= 12: days = str(self._get_days_of_month(thisyear, totalmon)) totalmon = self._addzero(totalmon) return self.year, totalmon, days else: i = totalmon / 12 j = totalmon % 12 if j == 0: i -= 1 j = 12 thisyear += i days = str(self._get_days_of_month(thisyear, j)) j = self._addzero(j) return str(thisyear), str(j), days else: if (totalmon > 0) and (totalmon < 12): days = str(self._get_days_of_month(thisyear, totalmon)) totalmon = self._addzero(totalmon) return self.year, totalmon, days else: i = totalmon / 12 j = totalmon % 12 if j == 0: i -= 1 j = 12 thisyear += i days = str(self._get_days_of_month(thisyear, j)) j = self._addzero(j) return str(thisyear), str(j), days def _get_today_month(self, n=0): (y, m, d) = self._getyearandmonth(n) arr = (y, m, d) if int(self.day) < int(d): arr = (y, m, self.day) return "/".join("%s" % i for i in arr) def _get_day_of_day(self, n=0): """ if n>=0,date is larger than today if n<0,date is less than today date format = "YYYY-MM-DD" """ return date.today() + timedelta(days=n) def _get_access_info(self): username = self.session.selenium2.find_element_by_id('username_set').text passphrase = self.session.selenium2.find_element_by_id('passphrase_set').text expiration = self.session.selenium2.find_element_by_id('expiration_set').text self.session.sleep(1) self.session.selenium2.click('css', 'button.wgrd-modal-save:nth-child(1)') return username, passphrase, expiration def _get_exp_time(self): if self.expiration.strip() == 'None': exp_time = 'Never' elif self.expiration.strip() == '3 months': exp_time = self._get_today_month(3) elif self.expiration.strip() == '1 month': exp_time = self._get_today_month(1) elif self.expiration.strip() == '1 week': exp_time = str(self._get_day_of_day(7)).replace('-', '/') else: exp_time = str(self._get_day_of_day(1)).replace('-', '/') return exp_time def _check_access_info(self, usr, pas, exp): exp_time = self._get_exp_time() print exp_time if exp_time != 'Never': day = exp.strip().split(' ')[0].split('/')[1] month = exp.strip().split(' ')[0].split('/')[0] year = exp.strip().split(' ')[0].split('/')[2] day = self._addzero(day) month = self._addzero(month) exp = str(year) + '/' + str(month) + '/' + str(day) if self.automatic_credentials == 'no': print 'exp:', exp if usr == self.user_name and pas == self.passphrase and exp == exp_time: self.session.log.info('Enable Support Access successfully, ' 'username: %s passphrase: %s expiration: %s' % (usr, pas, exp)) else: self.session.log.warning('Some information are wrong, please check.') else: if exp == exp_time: self.session.log.info('Enable Support Access successfully, expiration: %s' % exp) else: self.session.log.warning('Some information are wrong, please check.') else: if self.automatic_credentials == 'no': if usr == self.user_name and pas == self.passphrase and exp == 'Never': self.session.log.info('Enable Support Access successfully, ' 'username: %s passphrase: %s expiration: %s' % (usr, pas, exp)) else: self.session.log.warning('Some information are wrong, please check.') else: if exp == 'Never': self.session.log.info('Enable Support Access successfully, expiration: %s' % exp) else: self.session.log.warning('Some information are wrong, please check.')
原文地址:https://www.cnblogs.com/azure-west/p/7483578.html