Skip to content

Commit

Permalink
Add documentation for DateTime param type
Browse files Browse the repository at this point in the history
Add a docstring which defines the behaviors of the class, including
default formats supported, usage of ``datetime.strptime`` and "first
successful parse wins" behavior.

Also add '%Y-%m-%d %H:%M:%S' to the default formats, as it is at least
as commonly seen as '%Y-%m-%dT%H:%M:%S'.
Minor test fix to handle the new format.

Add ``click.DateTime`` to changelog.
  • Loading branch information
sirosen committed Aug 28, 2018
1 parent 26691e6 commit bf934ef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Version 7.0

(upcoming release with new features, release date to be decided)

- Added ``click.DateTime`` type for converting strings to ``datetime`` (`#423`_)
- Non-standalone calls to Context.exit return the exit code, rather than
calling ``sys.exit`` (`#667`_)(`#533`_)
- Updated test env matrix. (`#1027`_)
Expand Down
22 changes: 21 additions & 1 deletion click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,32 @@ def __repr__(self):


class DateTime(ParamType):
"""The DateTime type converts date strings into `datetime` objects.
The format strings which are checked are configurable, but default to some
common (non-timezone aware) ISO 8601 formats.
When specifying *DateTime* formats, you should only pass a list or a tuple.
Other iterables, like generators, may lead to surprising results.
The format strings are processed using ``datetime.strptime``, and this
consequently defines the format strings which are allowed.
Parsing is tried using each format, in order, and the first format which
parses successfully is used.
:param formats: A list or tuple of date format strings, in the order in
which they should be tried. Defaults to
``'%Y-%m-%d'``, ``'%Y-%m-%dT%H:%M:%S'``,
``'%Y-%m-%d %H:%M:%S'``.
"""
name = 'datetime'

def __init__(self, formats=None):
self.formats = formats or [
'%Y-%m-%d',
'%Y-%m-%dT%H:%M:%S'
'%Y-%m-%dT%H:%M:%S',
'%Y-%m-%d %H:%M:%S'
]

def get_metavar(self, param):
Expand Down
3 changes: 3 additions & 0 deletions docs/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ different behavior and some are supported out of the box:
.. autoclass:: FloatRange
:noindex:

.. autoclass:: DateTime
:noindex:

Custom parameter types can be implemented by subclassing
:class:`click.ParamType`. For simple cases, passing a Python function that
fails with a `ValueError` is also supported, though discouraged.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ def cli(start_date):

result = runner.invoke(cli, ['--start_date=2015-09'])
assert result.exit_code == 2
assert 'Invalid value for "--start_date": invalid datetime format: 2015-09. ' \
'(choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S)' in result.output
assert ('Invalid value for "--start_date": '
'invalid datetime format: 2015-09. '
'(choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S, %Y-%m-%d %H:%M:%S)'
) in result.output

result = runner.invoke(cli, ['--help'])
assert '--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S]' in result.output
Expand Down

0 comments on commit bf934ef

Please sign in to comment.