Skip to main content

datetime_formatter

datetime_formatter provides a DSL (domain-specific language) for formatting datetimes inline to strings. datetime_formatter is also capable of translating the datetime by most intervals, including some that are not supported by timedelta like “business_day” (skip weekends and holidays).

For example (see Available Output Formats for full details)

Use datetime_formatter to format dates effectively, including translations and holiday/weekend management using easy-to-remember shortcuts instead of esoteric strftime shortcuts, i.e. having to memorize that %m is month, while %M" is minute. For example, use YMD to refer to a YYYYMMDD formatting of a date.

datetime_formatter is especially useful for ingestion of configuration files where complicated date logic either has to be handled outside of the file itself, making understanding logic harder, or by making all configuration actual Python code – which again makes reading harder and interoperability with non-Python much harder or impossible. datetime_formatter allows all of this formatting to be done inline, making configuration files easier to comprehend.

https://github.com/nadime/datetime_formatter/workflows/Tests/badge.svg https://coveralls.io/repos/github/nadime/datetime_formatter/badge.svg?branch=main https://img.shields.io/badge/license-MIT-black Documentation Status

Install

The latest stable version can always be installed or updated via pip:

$ pip install --update datetime_formatter

Documentation

The documentation is hosted on Read the Docs.

Quick Start

datetime_formatter is intended to be used in one of two ways. You can either use the provided DateTimeFormatter class which inherits from string.Formatter, but overrides .format to provide unique functionality.

from datetime_formatter import DateTimeFormatter

dtf = DateTimeFormatter("2005-03-01 09:30:00")
dtf.format("Format this way %ISODATETIME% or that way %YYYY%-%MM%-%DD% %HH%:%MM:%SS%")
dtf.format("Or use translaters to change the datetime: %YYYYMMDD-P1Y%")

Or use the provided convenience function dtformat (aliased to dtfmt as well):

from datetime import datetime
from datetime_formatter import dtfmt

dtfmt(20050301, "YYYYMMDD") == "20050301"  # True
dtfmt("2005-03-01", "MMDDYY") == "030105"  # True
dtfmt("2005-03-01 08:30:00", "HHMMSS") == "083000" # True

You can use the dtfmt shortcut, intended for use within f-strings, or you can instantiate a DateTimeFormatter object and use its .format method (or take advantage of the fact that __call__ forwards to .format)

dtf = DateTimeFormatter(datetime(2005, 3, 1))
dtf.format("%YYYYMMDD%") == "20050301"

You can also translate dates and/or times using inline translation syntax, e.g.:

dtfmt(20050301, "YMD-M1D") == "20050228"
dtfmt(20050301, "YMD-M1Y") == "20040301"
dtfmt("20050301 08:30:00", "DATETIME-P1H") == "2005-03-01 09:30:00"

You can also convert to a new timezone on the fly, but only if you your datetime object is not timezone-naive.

from dateutil import tz

utc = tz.gettz("UTC")
est = tz.gettz("EST")
dt = datetime(2005, 3, 1, 8, 30, 0, 0, est)
dtfmt(dt, "ISODT", output_tz=utc) == "2005-03-01T13:30:00+00:00"

The full list of supported output shortcuts and translations are provided below. You can also use the holidays module with translations to skip well-known holidays, much like you can skip weekends using the business_day translation size.

import holidays
dtfmt(20061229, "DATE-P2B", holidays=holidays.US()) == "2007-01-03"

Please see the documentation for additional examples and detailed information.

Available Output Formats

These shortcuts are used either as the fmtstr argument to dtfmt or within a string passed to DateTimeFormatter.format - in the latter case, the fields to be replaced must be surrounded by %, e.g. %YYYYMMDD%.

FormatShortcut

datetime equivalent (strftime or function)

Output format example

DATE

%Y-%m-%d

2005-03-01

DATETIME

%Y-%m-%d %H:%M:%S

2005-03-01 13:30:00

USDATE

%x

03/01/05

USDATETIME

%x %X

03/01/05 13:30:00

TIME

%X

13:30:00

YEAR

%Y

2005

YMD

%Y%m%d

20050301

YYYYMM

%Y%m

200503

MMYYYY

%m%Y

032005

YYMM

%y%m

0503

MMYY

%m%y

0305

YYYYMMDD

%Y%m%d

20050301

MMDDYY

%m%d%y

030105

MMDDYYYY

%m%d%Y

03012005

ISODATE

%Y-%m-%d

2005-03-01

ISODATETIME

datetime.isoformat

2005-03-01T13:30:00.200Z-05:00

MONTH

%m

03

MON

%m

03

MONTHABV

%b

Mar

MONTHNAME

%B

March

DAYABV

%a

Tues

DAYNAME

%A

Tuesday

DAYNUM

%w

2

DAYYEAR

%j

060

TZOFF

%z

-0500

TZNAME

%Z

EST

WEEKNUM

%W

09

DAY

%d

01

DD

%d

01

MM

%m

03

YY

%y

05

YYYY

%Y

2005

LOCALE_DT

%c

Tue Mar 1 13:30:00 2005

HHMMSS

%H:%M:%S

13:30:00

HHMMSSZZ

%H:%M:%S.%f

13:30:00.200000

AMPM

%p

PM

HH

%H

13

HH12

%I

01

HOUR

%H

13

MIN

%M

30

SECOND

%S

00

SS

%S

00

MICROSECOND

%f

200000

ZZ

%f

200000

Available Translations

Translations are made up of three parts. The direction (M or P) determines whether to go forward/backward (plus/minus). The unit (see table below for unit-types) determines how far each step takes us foward or backward. Finally the size is a non-negative integer that tells us how far to move in the provided units.

Part name

Possible Values

Description

Direction

[ "M","P","m","p" ]

M = minus, P = plus

Number

Integer >= 0

The number of units to translate the date by

Size/Unit

[ "Y","m","D","W","H","M","S","Z","B" ]

Size

Meaning

Y

Year(s)

m

Month(s)

D

Day(s)

W

Week(s)

H

Hour(s)

M

Minute(s)

S

Second(s)

Z

Microsecond(s)

B

Business day(s)

F

Business week(s)

P

Business month(s)

K

Business year(s)

TS

Timestamp (UTC seconds since epoch)

You can string together any combination of these three translation parts, e.g.:

dtfmt(20050301, "YMD-M1B")      # 20050301 minus 2 business days (20050225)
dtfmt(20050301, "YMD-P1Y")      # 20050301 plus 1 year (20060301)
dtfmt(20050301, "DATETIME-P1H") # 20050301 00:00:00 plus 1 hour: (2005-03-01 01:00:00)

Beta Version

The latest development (beta) version can be installed directly from GitHub:

$ pip install --upgrade https://github.com/nadime/datetime_formatter/tarball/beta

All new features are always first pushed to beta branch, then released on master branch upon official version upgrades.

Contributions

Issues and pull requests are always welcome. Please see here for more information.

License

Code and documentation are available according to the MIT License (see LICENSE).

Metadata

Release files for datetime-formatter 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datetime-formatter 0.0.2
File Size Uploaded Provenance
datetime_formatter-0.0.2.tar.gz 16.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for datetime-formatter 0.0.2
File Interpreter ABI Platform
datetime_formatter-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 29.6 kB

Release files / datetime_formatter-0.0.2.tar.gz

Download URL datetime_formatter-0.0.2.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b634d3ac7dc639680f281c756d01df45ab2765b5b8a41b28e42ccca0930888aa
BLAKE2b-256 checksum
How to use checksums
13b3bc86a07fd6e538743b6da9b2712129aa73101d52620b425e0af1eaeb15fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / datetime_formatter-0.0.2-py3-none-any.whl

Download URL datetime_formatter-0.0.2-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b592650d1355e23d6a6721c92b846ebb7e4aa9f41172ebdc294f3da12af58a65
BLAKE2b-256 checksum
How to use checksums
7f1912019fe8cbb39c155e29f17529bc67bfd69fecde5d10c238661e5b92a8ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for datetime-formatter 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page