zoukankan      html  css  js  c++  java
  • 计算多月/多日前后 日期

    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.')
  • 相关阅读:
    一步一步搭建客服系统 (4) 客户列表
    一步一步搭建客服系统 (3) js 实现“截图粘贴”及“生成网页缩略图”
    【Servlet】深入浅出JavaServlet重定向和请求转发
    【java】深入了解JAVA可变长度的参数
    【HTML】网页中如何让DIV在网页滚动到特定位置时出现
    【HTML】 向网页<Title></Title>中插入图片以及跑马灯
    【JQuery】jQuery中的常用方法小结
    【JQuery】jQuery(document).ready(function($) { });的几种表示方法及load和ready的区别
    【JQuery】jquery对象和javascript对象即DOM对象相互转换
    【javascript】javascript中function(){},function(){}(),new function(){},new Function()
  • 原文地址:https://www.cnblogs.com/azure-west/p/7483578.html
Copyright © 2011-2022 走看看