Generators for datetime.
Project description
dt-range
Generators for datetime.
Usage
You can use pip
to install this package.
$ pip install dt-range
If you use it, import dt_range
.
Note the slight difference from this package name.
from dt_range import weekrange ,daterange ,hourrange ,minuterange ,secondrange ,millisecondrange ,microsecondrange ,timedeltarange
Examples
Here is an example of usage using START
and END
.
weekrange
weekrange
can be used to get the date for each week.
from datetime import datetime
from dt_range import weekrange
START = datetime(2021, 1, 1)
END = datetime(2021, 1, 31)
for dt in weekrange(START, END):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-08 00:00:00
# 2021-01-15 00:00:00
# 2021-01-22 00:00:00
# 2021-01-29 00:00:00
for dt in weekrange(START, END, weeks=2):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-15 00:00:00
# 2021-01-29 00:00:00
daterange
daterange
can be used to get the date for each day.
from datetime import datetime
from dt_range import daterange
START = datetime(2021, 1, 1)
END = datetime(2021, 1, 31)
for dt in daterange(START, END):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-02 00:00:00
# 2021-01-03 00:00:00
# ...
# 2021-01-29 00:00:00
# 2021-01-30 00:00:00
# 2021-01-31 00:00:00
for dt in daterange(START, END, days=4):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-05 00:00:00
# 2021-01-09 00:00:00
# 2021-01-13 00:00:00
# 2021-01-17 00:00:00
# 2021-01-21 00:00:00
# 2021-01-25 00:00:00
# 2021-01-29 00:00:00
hourrange
hourrange
can get the datetime
for each hour.
datetime
will be 0:00 if you don't specify the hour
argument, so you may need to specify 23:00 in some cases.
from datetime import datetime
from dt_range import hourrange
START = datetime(2021, 1, 1)
END = datetime(2021, 1, 1, 23)
for dt in hourrange(START, END):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-01 01:00:00
# 2021-01-01 02:00:00
# 2021-01-01 03:00:00
# ...
# 2021-01-01 21:00:00
# 2021-01-01 22:00:00
# 2021-01-01 23:00:00
for dt in hourrange(START, END, hours=4):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-01 04:00:00
# 2021-01-01 08:00:00
# 2021-01-01 12:00:00
# 2021-01-01 16:00:00
# 2021-01-01 20:00:00
Other
You can also use minuterange
, secondrange
, etc. in the same way.
If you want to break it into smaller time segments, use timedeltarange
.
from datetime import datetime, timedelta
from dt_range import timedeltarange
START = datetime(2021, 1, 1)
END = datetime(2021, 1, 31)
for dt in timedeltarange(
START,
END,
timedelta(days=5, hours=4, minutes=3, seconds=2, microseconds=1),
):
print(dt)
# 2021-01-01 00:00:00
# 2021-01-06 04:03:02.000001
# 2021-01-11 08:06:04.000002
# 2021-01-16 12:09:06.000003
# 2021-01-21 16:12:08.000004
# 2021-01-26 20:15:10.000005
GitHub Actions
-
pychecker
python code check (black, flake8, isort, mypy)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dt-range-1.0.tar.gz
.
File metadata
- Download URL: dt-range-1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98ec5a7e5f71591ec71f81f6be2f75a64ca3a37cf44ab0d78aca13013eb0d770 |
|
MD5 | 124e3a8a61e21442bd4b0df8dddb3b6f |
|
BLAKE2b-256 | 43c7a1d2bc4a9023a448328346212fa15189669a87f8a163e41557e1b39e3d3b |
File details
Details for the file dt_range-1.0-py3-none-any.whl
.
File metadata
- Download URL: dt_range-1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 921c66b93bb3b27c7eb460d6808659df63efe59c4ebb461661d60aa8a94f3227 |
|
MD5 | dc0b109f502cc71f0b81f12802d8ac5d |
|
BLAKE2b-256 | 51cea9e8a1f6494e1ee09af2274bd243fb303a3fff3a77ebc53b2f959852c17e |