Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature (v0.0.13): update to use http-exceptions v0.2.6 #14

Merged
merged 4 commits into from Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -7,6 +7,7 @@ source = datetime_helpers
exclude_lines =
pragma: no cover
def __repr__
def __str__
if self\.debug
pragma: no cover
raise NotImplementedError
Expand Down
86 changes: 84 additions & 2 deletions 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)
Expand All @@ -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/).
Expand Down
9 changes: 5 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions 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"
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion 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
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_utils.py
Expand Up @@ -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:
Expand Down