diff --git a/.coveragerc b/.coveragerc index 4b76e9c..13d6586 100644 --- a/.coveragerc +++ b/.coveragerc @@ -7,6 +7,7 @@ source = datetime_helpers exclude_lines = pragma: no cover def __repr__ + def __str__ if self\.debug pragma: no cover raise NotImplementedError diff --git a/README.md b/README.md index 0a0432c..9575fd8 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ -# datetime-helpers +# Datetime Helpers A handy collection of datetime utils. -[![Build](https://github.com/DeveloperRSquared/datetime-helpers/actions/workflows/build.yml/badge.svg)](https://github.com/DeveloperRSquared/datetime-helpers/actions/workflows/build.yml) [![Publish](https://github.com/DeveloperRSquared/datetime-helpers/actions/workflows/publish.yml/badge.svg)](https://github.com/DeveloperRSquared/datetime-helpers/actions/workflows/publish.yml) [![Python 3.7+](https://img.shields.io/badge/python-3.7+-brightgreen.svg)](#datetime-helpers) @@ -15,6 +14,89 @@ A handy collection of datetime utils. [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) +## What's available? + +```py +import datetime_helpers + +# Given a datetime: +>>> dt = datetime.date(2017, 4, 17) + +# Check the day of week +>>> datetime_helpers.get_day_of_week(dt=dt) +'Monday' + +# Check if it is a weekend +>>> datetime_helpers.is_weekend(dt=dt) +False + +# Check if it is a weekday +>>> datetime_helpers.is_weekday(dt=dt) +True + +# Get the previous business day +>>> datetime_helpers.get_previous_business_day(dt=dt) +datetime.date(2017, 4, 14) + +# Get the next business day +>>> datetime_helpers.get_next_business_day(dt=dt) +datetime.date(2017, 4, 18) + +# Get the first business day of the month +>>> datetime_helpers.get_first_business_day_of_month(dt=dt) +datetime.date(2017, 4, 3) + +# Get the nth business day of the month +>>> n = 3 # e.g. third business day +>>> datetime_helpers.get_nth_business_day_of_month(dt=dt, n=n) +datetime.date(2017, 4, 5) + +# Convert to a datetime string with custom format (defaults to JSON date format) +>>> datetime_helpers.datetime_to_string(dt=dt) +'2017-04-17T00:00:00.000000Z' + +# Convert to a date string with custom format (defaults to YYYY-MM-DD) +>>> datetime_helpers.date_to_string(dt=dt) +'2017-04-17' + +# Convert a string with custom format to datetime (defaults to JSON date format) +>>> text = '2016-04-17T00:00:00.000000Z' +>>> datetime_helpers.datetime_from_string(text=text) +datetime.datetime(2016, 4, 17, 0, 0) + +# Convert a string with custom format to datetime (defaults to JSON date format) +>>> text = '2016-04-17T00:00:00.000000Z' +>>> datetime_helpers.datetime_from_string(text=text) +datetime.datetime(2016, 4, 17, 0, 0) + +# Convert a windows filetime to datetime +>>> windows_filetime = 116444736000000000 +>>> datetime_helpers.datetime_from_windows_filetime(windows_filetime=windows_filetime) +datetime.datetime(1970, 1, 1, 0, 0) + +# Convert to seconds +>>> datetime_helpers.datetime_to_seconds(dt=dt) +1492387200.0 + +# Convert seconds to datetime +>>> seconds = 1492387200 +>>> datetime_helpers.datetime_from_seconds(seconds=seconds) +datetime.datetime(2017, 4, 17, 0, 0) + +# Convert to millis +>>> datetime_helpers.datetime_to_millis(dt=dt) +1492387200000 + +# Convert millis to datetime +>>> millis = 1492387200000 +>>> datetime_helpers.datetime_from_millis(millis=millis) +datetime.datetime(2017, 4, 17, 0, 0) + +# Convert date to datetime +>>> datetime_helpers.datetime_from_date(dt=dt) +datetime.datetime(2017, 4, 17, 0, 0) +``` + ## Install Install and update using [pip](https://pypi.org/project/datetime-helpers/). diff --git a/poetry.lock b/poetry.lock index 62f0caa..c10b75d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -161,11 +161,11 @@ python-dateutil = ">=2.7" [[package]] name = "http-exceptions" -version = "0.2.3" +version = "0.2.6" description = "Raisable HTTP Exceptions" category = "main" optional = false -python-versions = ">=3.7" +python-versions = ">=3.7,<4.0" [[package]] name = "identify" @@ -519,7 +519,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "b96dc526ea8c6ad0de89dffafbdb3436d0642afea5408b8e14c90b542ea89520" +content-hash = "9c506a7736790e5ff7b81c159957ebd5b8598b895e0a73cd08bf285b6fe8997c" [metadata.files] astroid = [ @@ -616,7 +616,8 @@ freezegun = [ {file = "freezegun-1.1.0.tar.gz", hash = "sha256:177f9dd59861d871e27a484c3332f35a6e3f5d14626f2bf91be37891f18927f3"}, ] http-exceptions = [ - {file = "http_exceptions-0.2.3-py3-none-any.whl", hash = "sha256:826d17cc330318bb95fde14ce9031af4a0b4e305fa3070013785ce942550512a"}, + {file = "http-exceptions-0.2.6.tar.gz", hash = "sha256:545313a497dcdd16b8412ca6b2d30ab3a354ede321f80f5e17e7bb12a0828ff9"}, + {file = "http_exceptions-0.2.6-py3-none-any.whl", hash = "sha256:577ff5a947c33be8c6a9ea466b20dd13d9bbb8eff03d42716a9113d21a474fb9"}, ] identify = [ {file = "identify-2.4.6-py2.py3-none-any.whl", hash = "sha256:cf06b1639e0dca0c184b1504d8b73448c99a68e004a80524c7923b95f7b6837c"}, diff --git a/pyproject.toml b/pyproject.toml index 5ba6a91..20ff17c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "datetime-helpers" -version = "0.0.12" +version = "0.0.13" description = "Util for working with date and datetime objects" authors = ["rikhilrai"] license = "MIT" @@ -40,7 +40,7 @@ packages = [ python = "^3.7" importlib-metadata = "^4.2.0" -http-exceptions = "^0.2.3" +http-exceptions = "^0.2.6" [tool.poetry.dev-dependencies] mypy = "^0.931" diff --git a/src/datetime_helpers/__init__.py b/src/datetime_helpers/__init__.py index 04b3f66..747479c 100644 --- a/src/datetime_helpers/__init__.py +++ b/src/datetime_helpers/__init__.py @@ -1,10 +1,16 @@ # isort: skip_file +# pylint: disable=wrong-import-position try: from importlib.metadata import version + from importlib.metadata import PackageNotFoundError except ImportError: from importlib_metadata import version # type: ignore[no-redef] + from importlib_metadata import PackageNotFoundError # type: ignore[no-redef,misc] -__version__: str = version(__name__) +try: + __version__: str = version(__name__) +except PackageNotFoundError: + __version__ = "unknown" from .utils import DayOfWeek from .utils import get_day_of_week diff --git a/src/tests/test_utils.py b/src/tests/test_utils.py index 32db02d..7ee56a8 100644 --- a/src/tests/test_utils.py +++ b/src/tests/test_utils.py @@ -333,6 +333,7 @@ class TestDatetimeToMillis: (datetime.datetime(1970, 1, 1), 0), (datetime.datetime(1970, 1, 1, 0, 0, 1), 1000), (datetime.datetime(2016, 4, 17), 1460851200000), + (datetime.datetime(2017, 4, 17), 1492387200000), ], ) def test_datetime_to_millis(self, dt: datetime.datetime, millis: int) -> None: