{ "cells": [ { "cell_type": "code", "execution_count": 1, "source": [ "# Create and convert to string\n", "from datetimerange import DateTimeRange\n", "\n", "DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900" ] }, "metadata": {}, "execution_count": 1 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 2, "source": [ "# Compare time range\n", "from datetimerange import DateTimeRange\n", "\n", "lhs = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "rhs = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(\"lhs == rhs: \", lhs == rhs)\n", "print(\"lhs != rhs: \", lhs != rhs)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "lhs == rhs: True\n", "lhs != rhs: False\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 3, "source": [ "# Move the time range\n", "import datetime\n", "from datetimerange import DateTimeRange\n", "\n", "value = DateTimeRange(\"2015-03-22T10:10:00+0900\", \"2015-03-22T10:20:00+0900\")\n", "print(value + datetime.timedelta(seconds=10 * 60))\n", "print(value - datetime.timedelta(seconds=10 * 60))" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2015-03-22T10:20:00+0900 - 2015-03-22T10:30:00+0900\n", "2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 4, "source": [ "# Change string conversion format\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.start_time_format = \"%Y/%m/%d\"\n", "time_range.end_time_format = \"%Y/%m/%dT%H:%M:%S%z\"\n", "time_range" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2015/03/22 - 2015/03/22T10:10:00+0900" ] }, "metadata": {}, "execution_count": 4 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 5, "source": [ "# Add elapsed time when conversion to string\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.is_output_elapse = True\n", "time_range" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)" ] }, "metadata": {}, "execution_count": 5 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 6, "source": [ "# Change separator of the converted string\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.separator = \" to \"\n", "time_range" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2015-03-22T10:00:00+0900 to 2015-03-22T10:10:00+0900" ] }, "metadata": {}, "execution_count": 6 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 7, "source": [ "# Get start time as datetime.datetime\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.start_datetime" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "datetime.datetime(2015, 3, 22, 10, 0, tzinfo=tzoffset(None, 32400))" ] }, "metadata": {}, "execution_count": 7 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 8, "source": [ "# Get start time as string (formatted with `start_time_format`)\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(time_range.get_start_time_str())\n", "time_range.start_time_format = \"%Y/%m/%d %H:%M:%S\"\n", "print(time_range.get_start_time_str())" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2015-03-22T10:00:00+0900\n", "2015/03/22 10:00:00\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 9, "source": [ "# Get end time as datetime.datetime\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.end_datetime" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "datetime.datetime(2015, 3, 22, 10, 10, tzinfo=tzoffset(None, 32400))" ] }, "metadata": {}, "execution_count": 9 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 10, "source": [ "# Get end time as string (formatted with `end_time_format`)\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(time_range.get_end_time_str())\n", "time_range.end_time_format = \"%Y/%m/%d %H:%M:%S\"\n", "print(time_range.get_end_time_str())" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2015-03-22T10:10:00+0900\n", "2015/03/22 10:10:00\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 11, "source": [ "# Get datetime.timedelta (from start_datetime to the end_datetime)\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.timedelta" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "datetime.timedelta(seconds=600)" ] }, "metadata": {}, "execution_count": 11 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 12, "source": [ "# Get timedelta as seconds (from start_datetime to the end_datetime)\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.get_timedelta_second()" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "600.0" ] }, "metadata": {}, "execution_count": 12 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 13, "source": [ "# Get iterator\n", "import datetime\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-01-01T00:00:00+0900\", \"2015-01-04T00:00:00+0900\")\n", "for value in time_range.range(datetime.timedelta(days=1)):\n", " print(value)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2015-01-01 00:00:00+09:00\n", "2015-01-02 00:00:00+09:00\n", "2015-01-03 00:00:00+09:00\n", "2015-01-04 00:00:00+09:00\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 14, "source": [ "# Get iterator\n", "from datetimerange import DateTimeRange\n", "from dateutil.relativedelta import relativedelta\n", "\n", "time_range = DateTimeRange(\"2015-01-01T00:00:00+0900\", \"2016-01-01T00:00:00+0900\")\n", "for value in time_range.range(relativedelta(months=+4)):\n", " print(value)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2015-01-01 00:00:00+09:00\n", "2015-05-01 00:00:00+09:00\n", "2015-09-01 00:00:00+09:00\n", "2016-01-01 00:00:00+09:00\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 15, "source": [ "# Set start time\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange()\n", "print(time_range)\n", "time_range.set_start_datetime(\"2015-03-22T10:00:00+0900\")\n", "print(time_range)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NaT - NaT\n", "2015-03-22T10:00:00+0900 - NaT\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 16, "source": [ "# Set end time\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange()\n", "print(time_range)\n", "time_range.set_end_datetime(\"2015-03-22T10:10:00+0900\")\n", "print(time_range)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NaT - NaT\n", "NaT - 2015-03-22T10:10:00+0900\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 17, "source": [ "# Set time range (set both start and end time)\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange()\n", "print(time_range)\n", "time_range.set_time_range(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(time_range)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NaT - NaT\n", "2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 18, "source": [ "# Test whether the time range is set\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange()\n", "print(time_range.is_set())\n", "time_range.set_time_range(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(time_range.is_set())" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "False\n", "True\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 19, "source": [ "# Validate time inversion\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:10:00+0900\", \"2015-03-22T10:00:00+0900\")\n", "try:\n", " time_range.validate_time_inversion()\n", "except ValueError:\n", " print(\"time inversion\")" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "time inversion\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 20, "source": [ "# Test whether the time range is valid\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange()\n", "print(time_range.is_valid_timerange())\n", "time_range.set_time_range(\"2015-03-22T10:20:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(time_range.is_valid_timerange())\n", "time_range.set_time_range(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(time_range.is_valid_timerange())" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "False\n", "False\n", "True\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 21, "source": [ "# Test whether a value within the time range\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "print(\"2015-03-22T10:05:00+0900\" in time_range)\n", "print(\"2015-03-22T10:15:00+0900\" in time_range)\n", "\n", "time_range_smaller = DateTimeRange(\"2015-03-22T10:03:00+0900\", \"2015-03-22T10:07:00+0900\")\n", "print(time_range_smaller in time_range)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "True\n", "False\n", "True\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 22, "source": [ "# Test whether a value intersect the time range\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "x = DateTimeRange(\"2015-03-22T10:05:00+0900\", \"2015-03-22T10:15:00+0900\")\n", "time_range.is_intersection(x)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 22 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 23, "source": [ "# Make an intersected time range\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "x = DateTimeRange(\"2015-03-22T10:05:00+0900\", \"2015-03-22T10:15:00+0900\")\n", "time_range.intersection(x)\n", "time_range" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900" ] }, "metadata": {}, "execution_count": 23 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 24, "source": [ "# Make an encompassed time range\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "x = DateTimeRange(\"2015-03-22T10:05:00+0900\", \"2015-03-22T10:15:00+0900\")\n", "time_range.encompass(x)\n", "time_range" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900" ] }, "metadata": {}, "execution_count": 24 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 25, "source": [ "# Truncate time range\n", "from datetimerange import DateTimeRange\n", "\n", "time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n", "time_range.is_output_elapse = True\n", "print(\"before truncate: \", time_range)\n", "time_range.truncate(10)\n", "print(\"after truncate: \", time_range)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "before truncate: 2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)\n", "after truncate: 2015-03-22T10:00:30+0900 - 2015-03-22T10:09:30+0900 (0:09:00)\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [], "outputs": [], "metadata": {} } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3.8.11 64-bit ('3.8.11')" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.11" }, "metadata": { "interpreter": { "hash": "a89ed08815272de879559165cd445f83ec1092d708f739d2a7c0690790beb904" } }, "interpreter": { "hash": "e1c230d0d7838db35080269a6918dd30585979c6f8544df43693174aeb261eb3" } }, "nbformat": 4, "nbformat_minor": 1 }