Created a Python library DateTimeRange that handles time ranges

I made it because I need it from time to time.

--GitHub repository

Documentation http://datetimerange.readthedocs.org/en/latest/

Installation

pip install DateTimeRange

Usage The place where the time character string is input can be datetime type.

Creation and conversion to strings

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
str(time_range)
'2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900'

Comparison

from datetimerange import DateTimeRange
lhs = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
rhs = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print "lhs == rhs: ", lhs == rhs
print "lhs != rhs: ", lhs != rhs
lhs == rhs:  True
lhs != rhs:  False

Move time range

import datetime
from datetimerange import DateTimeRange
value = DateTimeRange("2015-03-22T10:10:00+0900", "2015-03-22T10:20:00+0900")
print value + datetime.timedelta(seconds=10 * 60)
print value - datetime.timedelta(seconds=10 * 60)
2015-03-22T10:20:00+0900 - 2015-03-22T10:30:00+0900
2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900

Specifying the string conversion format

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.start_time_format = "%Y/%m/%d"
time_range.end_time_format = "%Y/%m/%dT%H:%M:%S%z"
time_range
2015/03/22 - 2015/03/22T10:10:00+0900

Added elapsed time when returning strings

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.is_output_elapse = True
time_range
2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)

Separator setting

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.separator = " to "
time_range
2015-03-22T10:00:00+0900 to 2015-03-22T10:10:00+0900

Get start time (datetime.datetime type)

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.start_datetime
datetime.datetime(2015, 3, 22, 10, 0, tzinfo=tzoffset(None, 32400))

Get the start time as a string

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print time_range.get_start_time_str()
time_range.start_time_format = "%Y/%m/%d %H:%M:%S"
print time_range.get_start_time_str()
2015-03-22T10:00:00+0900
2015/03/22 10:00:00

Get end time (datetime.datetime type)

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.end_datetime
datetime.datetime(2015, 3, 22, 10, 10, tzinfo=tzoffset(None, 32400))

Get the end time as a string

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print time_range.get_end_time_str()
time_range.end_time_format = "%Y/%m/%d %H:%M:%S"
print time_range.get_end_time_str()
2015-03-22T10:10:00+0900
2015/03/22 10:10:00

Get timedelta

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.timedelta
datetime.timedelta(0, 600)

Get timedelta in seconds

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.get_timedelta_second()
600.0

Set start time

from datetimerange import DateTimeRange
time_range = DateTimeRange()
print time_range
time_range.set_start_datetime("2015-03-22T10:00:00+0900")
print time_range
NaT - NaT
2015-03-22T10:00:00+0900 - NaT

Set end time

from datetimerange import DateTimeRange
time_range = DateTimeRange()
print time_range
time_range.set_end_datetime("2015-03-22T10:10:00+0900")
print time_range
NaT - NaT
NaT - 2015-03-22T10:10:00+0900

Set time range

from datetimerange import DateTimeRange
time_range = DateTimeRange()
print time_range
time_range.set_time_range("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print time_range
NaT - NaT
2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900

Is the value set

from datetimerange import DateTimeRange
time_range = DateTimeRange()
print time_range.is_set()
time_range.set_time_range("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print time_range.is_set()
False
True

Is the time reversed?

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:10:00+0900", "2015-03-22T10:00:00+0900")
try:
    time_range.validate_time_inversion()
except ValueError:
    print "time inversion"
time inversion

Time range value check

from datetimerange import DateTimeRange
time_range = DateTimeRange()
print time_range.is_valid_timerange()
time_range.set_time_range("2015-03-22T10:20:00+0900", "2015-03-22T10:10:00+0900")
print time_range.is_valid_timerange()
time_range.set_time_range("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print time_range.is_valid_timerange()
False
False
True

Is the time within the time range?

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print "2015-03-22T10:05:00+0900" in time_range
print "2015-03-22T10:15:00+0900" in time_range
True
False

Get iterator (range)

import datetime
from datetimerange import DateTimeRange

time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2015-01-04T00:00:00+0900")
for value in time_range.range(datetime.timedelta(days=1)):
    print value
2015-01-01 00:00:00+09:00
2015-01-02 00:00:00+09:00
2015-01-03 00:00:00+09:00
2015-01-04 00:00:00+09:00
from datetimerange import DateTimeRange
from dateutil.relativedelta import relativedelta

time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2016-01-01T00:00:00+0900")
for value in time_range.range(relativedelta(months=+4)):
    print value
2015-01-01 00:00:00+09:00
2015-05-01 00:00:00+09:00
2015-09-01 00:00:00+09:00
2016-01-01 00:00:00+09:00

Does the time range overlap?

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.is_intersection(x)
True

Set the time range where the time ranges overlap

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.intersection(x)
time_range
2015-03-22T10:05:00+0900 - 2015-03-22T10:10:00+0900

Set to a time range that includes the time range

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.encompass(x)
time_range
2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900

Truncate the time range

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
time_range.is_output_elapse = True
print "before truncate: ", time_range
time_range.truncate(10)
print "after truncate:  ", time_range
before truncate:  2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)
after truncate:   2015-03-22T10:00:30+0900 - 2015-03-22T10:09:30+0900 (0:09:00)

Recommended Posts

Created a Python library DateTimeRange that handles time ranges
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Created a library for python that can easily handle morpheme division
Created Simple SQLite, a Python library that simplifies SQLite table creation / data insertion
A note about the functions of the Linux standard library that handles time
[Python] A convenient library that converts kanji to hiragana
Publish / upload a library created in Python to PyPI
Published a library that hides character data in Python images
A Python program that aggregates time usage from icalendar data
Use pymol as a python library
A memo that handles double-byte double quotes in Python regular expressions
I created a Python library to call the LINE WORKS API
About psd-tools, a library that can process psd files in Python
A function that measures the processing time of a method in python
[For beginners] How to register a library created in Python in PyPI
I made a library that adds docstring to a Python stub file.
Generate a Python library download badge
A python script that deletes ._DS_Store and ._ * files created on Mac
A note about mock (Python mock library)
I created a template for a Python project that can be used universally
Introducing a library that was not included in pip on Python / Windows
I created a Go library nzargv that arranges command line arguments nicely.
[Python] Created a Twitter bot that generates friend-like tweets using Markov chains
Try using APSW, a Python library that SQLite can get serious about
How to use hmmlearn, a Python library that realizes hidden Markov models
Code reading of faker, a library that generates test data in Python
Python Date / Time Library Reverse Lookup Reference
[Python] A program that creates stairs with #
Created a darts trip with python (news)
Try HTML scraping with a Python library
[Python] Create a LineBot that runs regularly
A typed world that begins with Python
A program that plays rock-paper-scissors using Python
I created a password tool in Python.
python: Creating a ramen timer (pyttsx3, time)
[Python] A program that rounds the score
I took a quick look at the fractions package that handles Python built-in fractions.
How to install a Python library that can be used by pharmaceutical companies
A program that sends a fixed amount of mail at a specified time by Python
Python: Time Series Analysis: Building a SARIMA Model
Created a Python wrapper for the Qiita API
A memo that I wrote a quicksort in Python
[Python] A tool that allows intuitive relative import
[Python3] A story stuck with time zone conversion
[Python / Tkinter] A class that creates a scrollable Frame
A nice nimporter that connects nim and python
Create a page that loads infinitely with python
A program that removes duplicate statements in Python
Publish a Python module that calculates meteorological factors
A clever way to time processing in Python
"Python Kit" that calls a Python script from Swift
Python: Create a class that supports unpacked assignment
A memorandum about the Python tesseract wrapper library
A Vim plugin that automatically formats Python styles
A simple to-do list created with Python + Django
Created a Python library to write complex comprehensions and reduce in an easy-to-read manner
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
A python script that wants to use Mac startup / end time for attendance management