Skip to main content

Date/time types for Argparse

Project description

https://travis-ci.org/petedmarsh/dtargs.png?branch=master

Introduction

dtargs is a small module that makes accepting date/time arguments with Argparse easier.

Examples

A date argument

import argparse
import dtargs

parser = argparse.ArgumentParser()
parser.add_argument('start', type=dtargs.DateType()) # defaults to %Y-%m-%d
parser.add_argument('end', type=dtargs.DateType('%d/%m/%Y'))

A date/time argument

import argparse
import dtargs

parser = argparse.ArgumentParser()
parser.add_argument('start', type=dtargs.DateTimeType()) # defaults to %Y-%m-%dT%H:%M:%SZ
parser.add_argument('end', type=dtargs.DateTimeType('%H:%M:%S_%Y-%m-%d', tz=None)

Timezones

The DateTimeType accepts a tz parameter, which is handled thusly

import dtargs

dtargs.DateTimeType()('2015-01-02T12:34:56Z')
>>> datetime.datetime(2015, 1, 2, 12, 34, 56, tzinfo=<UTC>)

dtargs.DateTimeType(tz=None)('2015-01-02T12:34:56Z')
>> datetime.datetime(2015, 1, 2, 12, 34, 56, tzinfo=None)

dtargs.DateTimeType('%Y-%m-%dT%H:%M:%S%z', tz=None)('2015-01-02T12:34:56+0100')
>> datetime.datetime(2015, 1, 2, 12, 34, 56, tzinfo=datetime.timezone(datetime.timedelta(0, 3600))

dtargs.DateTimeType('%Y-%m-%dT%H:%M:%S%z', tz=pytz.utc)('2015-01-02T12:34:56+0500')
>> datetime.datetime(2015, 1, 2, 7, 34, 56, tzinfo=<UTC>)

Testing

Install dependencies

$ pip install -r dev-requirements.txt

To run tests

$ py.test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dtargs-1.0.0-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dtargs-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dtargs-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0a66565f48e6ee35cd6032b1dc1132d773e0b36c8ac92d3bb83eb912edbec5ce
MD5 ec8b23b77ddf36d43bbf55a96fca1926
BLAKE2b-256 f4852802d28f0efd8c7f9ec04fd82c1d2e9fcb9e02429e03fe86e263b507002e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page