def __call__(self, parser, namespace, values, option_string=None): try: if type(values) == str: daytime = Daytime.fromtime(timeparser.parsetime(values)) else: daytime = [Daytime.fromtime(timeparser.parsetime(d)) for d in values] except ValueError: raise ArgumentError(self, self.ERR % (values, 'daytime')) else: setattr(namespace, self.dest, daytime)
def time_or_datetime(self, values): if len(values) == 1: return timeparser.parsetime(values[0]) elif len(values) == 2: return self.combine_datetime(*values) else: raise ValueError("'%s' couldn't be parsed as time or datetime" % values)
def __call__(self, parser, namespace, values, option_string=None): value = ' '.join(values) if isinstance(values, list) else values try: daytime = Daytime.fromtime(timeparser.parsetime(values)) except ValueError: raise ArgumentError(self, self.ERR % (values, 'daytime')) else: self.append(namespace, daytime)
def gettime(self, section, option): """ Get option as datetime.time-instance. Args: section and option Which formats are accepted depens on the configuration of the timeparser-module. Feel free to change it. """ return timeparser.parsetime(self.get(section, option))
def combine_datetime(self, datestring, timestring): date = timeparser.parsedate(datestring) time = timeparser.parsetime(timestring) return datetime.datetime.combine(date, time)
def test_type(self): self.assertIsInstance(timeparser.parsetime('23:44'), datetime.time) self.assertIsInstance(timeparser.parsedate('24.3.2013'), datetime.date) self.assertIsInstance(timeparser.parsedatetime('24.3.2013,23:44'), datetime.datetime) self.assertIsInstance(timeparser.parsetimedelta('24.3.2013,23:44'), datetime.timedelta)