This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: New DateType for argparse (like FileType but for dates)
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: bethard, paul.j3, petedmarsh, r.david.murray
Priority: normal Keywords: patch

Created on 2015-04-07 17:29 by petedmarsh, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
argparse_datetype.patch petedmarsh, 2015-04-07 17:29 Patch for new argparse.DateType feature review
Messages (5)
msg240220 - (view) Author: Peter Marsh (petedmarsh) * Date: 2015-04-07 17:29

Reasonably frequently I find myself needing to pass a date as a command line argument to a Python script I've written. Currently, argparse does not have a built support for dates - this adds a new class to argparse (much like the existing FileType) that parses arguments to instances.


      >>> parser = argparse.ArgumentParser()
      >>> parser.add_argument('--start', type=argparse.DateType('%d/%m/%Y))
      >>> parser.add_argument('end', type=argparse.DateType())
      >>> parser.parse_args(['--start', '01/02/2015', '2015-01-03'])
      Namespace(, 1, 3),, 1, 2))

I think this would be a useful addition to the standard library, a quick Google shows that many people roll their own version of this anyway.

Support for datetime.datetime and perhaps even datetime.timedeltas might be good too, but date/times get a bit more complicated (timezones in general and varying support for the '%z' format string which is required to default to an ISO8601 date time).


msg240233 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-04-07 22:19
It's a possible addition, but I don't have sense of the demand for it.

I wonder, what does the class offer that a function like this doesn't?

    def adate(date_string):
        return datetime.datetime.strptime(date_string,'%Y-%m-%d').date()

I'd be hesitant to add FileType if wasn't already present.  It hasn't aged very well.  We've had bug issues related to v3 binary files, and contexts.   The main feature that FileType (beyond verifying that the file actually does exist) adds is the recognition of '-' as stdin/out.  

By analogy I'm lukewarm about adding a DateType class.
msg240235 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-04-07 22:57
Examples of datetime types from Stackoverflow:
The suggested answer (but not accepted) is 'type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d')'


which references
' An argparse type factory that produces datetime.timedelta objects'

with a type function
msg240241 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-07 23:49
Yes, I think this is a case where it is better for the application to define exactly what it needs rather than try to define a general solution that won't satisfy everyone :)

FileType's purpose is actually, I think, to give you an *open* file.  Which is also where a number of the problems came from :)
msg240259 - (view) Author: Peter Marsh (petedmarsh) * Date: 2015-04-08 10:52
The consensus seems to be that this is simple enough for people to implement themselves (if they need it) and it's probably not worth adding to argparse, so I've closed this :)
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68072
2015-04-08 10:52:05petedmarshsetstatus: open -> closed
resolution: rejected
messages: + msg240259
2015-04-07 23:49:27r.david.murraysetnosy: + r.david.murray
messages: + msg240241
2015-04-07 22:57:34paul.j3setmessages: + msg240235
2015-04-07 22:19:30paul.j3setmessages: + msg240233
2015-04-07 20:16:47BreamoreBoysetnosy: + paul.j3
2015-04-07 17:53:23SilentGhostsetnosy: + bethard
2015-04-07 17:29:13petedmarshcreate