我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用datetime.datetime.date()。
def convertToDate(fmt="%Y-%m-%d"): """ Helper to create a parse action for converting parsed date string to Python datetime.date Params - - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"}) Example:: date_expr = pyparsing_common.iso8601_date.copy() date_expr.setParseAction(pyparsing_common.convertToDate()) print(date_expr.parseString("1999-12-31")) prints:: [datetime.date(1999, 12, 31)] """ def cvt_fn(s,l,t): try: return datetime.strptime(t[0], fmt).date() except ValueError as ve: raise ParseException(s, l, str(ve)) return cvt_fn
def format_week(self, char, num): if char.islower(): # week of year day_of_year = self.get_day_of_year() week = self.get_week_number(day_of_year) if week == 0: date = self.value - timedelta(days=day_of_year) week = self.get_week_number(self.get_day_of_year(date), date.weekday()) return self.format(week, num) else: # week of month week = self.get_week_number(self.value.day) if week == 0: date = self.value - timedelta(days=self.value.day) week = self.get_week_number(date.day, date.weekday()) pass return '%d' % week
def get_quantity(self, name): pool = Pool() Date = pool.get('ir.date') Location = pool.get('stock.location') Product = pool.get('product.product') locations = Location.search([('type', '=', 'storage')]) Transaction().set_context({'locations': [l.id for l in locations]}) context = {} context['stock_date_end'] = Date.today() Transaction().set_context(context) pbl = Product.products_by_location( location_ids=Transaction().context['locations'], product_ids=[self.name.id], with_childs=True) quantity = 0.00 if pbl.values(): quantity = reduce(lambda x, y: x + y, pbl.values()) return quantity
def parse_backup_sizes(self, rootdir, customer, friendly_name, date_complete): cmd = ( 'get', '-o', 'value', '-Hp', 'used', self.get_dataset_name(rootdir, customer, friendly_name)) try: out = self._perform_binary_command(cmd) except CalledProcessError as e: msg = 'Error while calling: %r, %s' % (cmd, e.output.strip()) logger.warning(msg) size = '0' else: size = out.strip() return { 'size': size, 'date': date_complete, }
def __deserialize_date(self, string): """ Deserializes string to date. :param string: str. :return: date. """ try: from dateutil.parser import parse return parse(string).date() except ImportError: return string except ValueError: raise ApiException( status=0, reason="Failed to parse `{0}` into a date object".format(string) )
def __deserialize_date(self, string): """ Deserializes string to date. :param string: str. :return: date. """ try: from dateutil.parser import parse return parse(string).date() except ImportError: return string except ValueError: raise ApiException( status=0, reason="Failed to parse `{0}` into a date object" .format(string) )
def __init__(self, description, default=relativedelta()): """ default: may either be provided as a: * datetime.date object * string in ISO format (YYYY-mm-dd) * datetime.timedelta object. The date will be current - delta * dateutil.relativedelta object. The date will be current - delta If not specified, it will be the current date. Note that dateutil is not in the Python standard library. It provides a simpler API to specify a duration in days, weeks, months, etc. You can install it with pip. """ self.description = description self.default = to_date_model(default) # see Bootstrap date picker docs for options # https://bootstrap-datepicker.readthedocs.io/en/stable/# self.attributes = { 'data-date-format': 'yyyy-mm-dd', 'data-date-orientation': 'left bottom', 'data-date-autoclose': 'true', 'value': self.default.iso(), }
def parse_datetime(s): """Tries to parse a datetime object from a standard datetime format or date format :param str s: A string representing a date or datetime :return: A parsed date object :rtype: datetime.date """ try: dt = datetime.strptime(s, CREATION_DATE_FMT) return dt except: try: dt = datetime.strptime(s, PUBLISHED_DATE_FMT) return dt except: try: dt = datetime.strptime(s, PUBLISHED_DATE_FMT_2) return dt except: raise ValueError('Incorrect datetime format for {}'.format(s))
def gen_raw_pay(offer, start_date, end_date, paydays, vests): """Generate pre-tax income events. Inputs are as for the pay_info function. Each income event is a tuple of (DAY, CASH, and EQUITY). DAY is a datetime.date object giving the day of income dispersal; CASH and EQUITY (either or both of which can be zero) is the amount of money earned on that day.""" for day in iterdates(start_date, end_date): cash, equity = pay_info(offer, day, start_date, paydays, vests) yield (day, cash, equity)