Skip to content

Instantly share code, notes, and snippets.

@ulope
Created August 19, 2014 18:02
Show Gist options
  • Save ulope/4b6fc547408f782c0dcd to your computer and use it in GitHub Desktop.
Save ulope/4b6fc547408f782c0dcd to your computer and use it in GitHub Desktop.
DateTime ParamType for the click CLI Parser library (uses arrow)
import arrow
from arrow.parser import ParserError
import click
class DateParamType(click.ParamType):
name = 'date'
def __init__(self, date_format="YYYY-MM-DD"):
super(DateParamType, self).__init__()
self.date_format = date_format
def convert(self, value, param, ctx):
try:
return arrow.get(value, self.date_format).date()
except ParserError:
self.fail("Could not parse '%s' as a date." % (value, ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment