Skip to content

Commit

Permalink
Respond to comments from @dopplershift
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-rose committed Jun 14, 2021
1 parent df315d4 commit 6e89dd5
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions core/datetime/datetime.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"import time as tm\n",
"```\n",
"\n",
"We can now reference the `datetime` module (alaised to `dt`) and `datetime` object unambiguously"
"We can now reference the `datetime` module (aliased to `dt`) and `datetime` object unambiguously."
]
},
{
Expand Down Expand Up @@ -464,8 +464,8 @@
"print(f\"The time between high and low tide is {tide_duration}.\")\n",
"print(f\"The current high tide is {high_tide}.\")\n",
"print(f\"The next low tide is {next_low_tide}.\")\n",
"print(f\"The next high tide {next_high_tide}.\")\n",
"print(f\"The tide length is {tide_length}.\")\n",
"print(f\"The next high tide {next_high_tide}.\")\n",
"print(f\"The tide length is {tide_length}.\")\n",
"print(\"The type of the 'tide_length' variable is {}.\".format(type(tide_length)))"
]
},
Expand Down Expand Up @@ -493,9 +493,39 @@
"When you create `datetime` objects in Python, they are so-called \"naive\" which means they are time zone unaware. In many situations, you can happily go forward without this detail getting in the way of your work. As the [Python documentation states](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects):\n",
">Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality. \n",
"\n",
"However, if you wish to convey time zone information, you will have to make your `datetime` objects time zone aware. In order to handle time zones in Python, you will need the third-party [pytz](https://pypi.org/project/pytz/) module whose classes build upon, or \"inherit\" in OO terminology, from the `tzinfo` class. You cannot solely rely on the Python Standard Library unfortunately. \n",
"However, if you wish to convey time zone information, you will have to make your `datetime` objects time zone aware. The `datetime` library is able to handle conversions to UTC:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"naive = dt.datetime.now()\n",
"aware = dt.datetime.now(dt.timezone.utc)\n",
"print(f\"I am time zone naive {naive}.\")\n",
"print(f\"I am time zone aware {aware}.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The time difference between `naive` and `aware` will depend on what time you're in when you run this code!\n",
"\n",
"In the code above, we used `dt.timezone.utc` to initialize the UTC timezone for our `aware` object. Unfortunately at this time the Python Standard Library does not fully support initializing `datetime` objects with arbitrary time zones, or conversions between different time zones. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Full time zone support with the `pytz` module\n",
"\n",
"For improved handling of time zones in Python, you will need the third-party [pytz](https://pypi.org/project/pytz/) module whose classes build upon, or \"inherit\" in OO terminology, from `datetime` classes. \n",
"\n",
"Here, we create time zone naive and time zone aware `datetime` objects:"
"Here, we repeat the above exercise but initialize our `aware` object in a different time zone:"
]
},
{
Expand Down

0 comments on commit 6e89dd5

Please sign in to comment.