def _nearby_season_month(self, datestr, offset): date = dt.strptime(datestr, '%Y%m%d') date = date.replace(day=1) season_month = date.replace( month=3 * (date.month // 3 + int(date.month % 3 > 0))) + td( months=3 * offset) return season_month.replace(day=monthrange( season_month.year, season_month.month)[1]).strftime('%Y%m%d')
def __call__(self, parser, namespace, values, option_string=None): # Convert '3 months' to ('3', ' ', 'months') split = values.partition(' ') # Normalize inclusion of 's'. i.e. "month" -> "months" unit_as_written = split[2] if unit_as_written[-1] != 's': unit_as_written = unit_as_written + 's' # Compute accurate time delta from human expression. # If argument is "3 months", dict looks like: # {hours: 0, days: 0, weeks: 0, months: 3, years: 0} units = {k + 's': int(split[0]) * int(k + 's' == unit_as_written) for k in timeunits} # The beginning of Git commit history that the user deems important. user_epoch = datetime.utcfromtimestamp(git.headtime()) - td(**units) # We cannot go before the beginning of the commit history. timestamp = max(int(time.mktime(user_epoch.timetuple())), git.inittime()) setattr(namespace, self.dest, timestamp)