Ich habe es gemacht, weil ich es von Zeit zu Zeit brauche.
Documentation http://datetimerange.readthedocs.org/en/latest/
Installation
pip install DateTimeRange
Usage Der Ort, an dem die Zeitzeichenfolge eingegeben wird, kann vom Typ datetime sein.
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'
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
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
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
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)
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
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))
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
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))
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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)