|
1 | 1 | {
|
2 | 2 | "cells": [
|
3 | 3 | {
|
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b53cacffa29d30a6", |
| 6 | + "metadata": {}, |
| 7 | + "source": "# Working with Dates and Times" |
| 8 | + }, |
| 9 | + { |
| 10 | + "cell_type": "markdown", |
| 11 | + "id": "783a656d272204c2", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Overview\n", |
| 15 | + "Working with dates and times in Python is a common task in geoscience workflows. This notebook will cover:\n", |
| 16 | + "- Working with the [`datetime`](docs.python.org/3/library/datetime.html) module from the Python Standard Library\n", |
| 17 | + "- [`cftime`](unidata.github.io/cftime/index.html), [CF Conventions](cfconventions.org), and how they are related to working with geoscience data\n", |
| 18 | + "- A resource guide to point you to more detailed information depending on your use case" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "id": "13edacb9510e559e", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "## The `datetime` module\n", |
| 27 | + "From the [module's documentation](https://docs.python.org/3/library/datetime.html):\n", |
| 28 | + "\n", |
| 29 | + "> The `datetime` module supplies classes for manipulating dates and times.\n", |
| 30 | + ">\n", |
| 31 | + "> While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "id": "2092ca5d86a3a24b", |
| 37 | + "metadata": { |
| 38 | + "ExecuteTime": { |
| 39 | + "end_time": "2024-06-03T04:44:37.784336Z", |
| 40 | + "start_time": "2024-06-03T04:44:37.781266Z" |
| 41 | + } |
| 42 | + }, |
| 43 | + "source": [ |
| 44 | + "import datetime\n", |
| 45 | + "\n", |
| 46 | + "today = datetime.date.today()\n", |
| 47 | + "print(f\"datetime.date.today() -> \\t {today}\")\n", |
| 48 | + "now = datetime.datetime.now()\n", |
| 49 | + "print(f\"datetime.datetime.now() -> \\t {now}\")" |
| 50 | + ], |
| 51 | + "outputs": [ |
| 52 | + { |
| 53 | + "name": "stdout", |
| 54 | + "output_type": "stream", |
| 55 | + "text": [ |
| 56 | + "datetime.date.today() -> \t 2024-06-02\n", |
| 57 | + "datetime.datetime.now() -> \t 2024-06-02 22:44:37.783255\n" |
| 58 | + ] |
| 59 | + } |
| 60 | + ], |
| 61 | + "execution_count": 1 |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "markdown", |
| 65 | + "id": "6368d2ab290b3d1", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "### `strftime()`\n", |
| 69 | + "\n", |
| 70 | + "You can use [`strftime()`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime) to parse and format these date objects.To see all formatting options, see the [full list of format codes](https://docs.python.org/3/library/datetime.html#format-codes)." |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "id": "1d18147498a434eb", |
| 76 | + "metadata": { |
| 77 | + "ExecuteTime": { |
| 78 | + "end_time": "2024-06-03T04:44:37.787843Z", |
| 79 | + "start_time": "2024-06-03T04:44:37.785520Z" |
| 80 | + } |
| 81 | + }, |
| 82 | + "source": [ |
| 83 | + "# print just the year\n", |
| 84 | + "print(\"now.strftime('%Y')\")\n", |
| 85 | + "print(now.strftime(\"%Y\"))\n", |
| 86 | + "print()\n", |
| 87 | + "\n", |
| 88 | + "# print weekday, month, day, year, 12h time, and AM/PM\n", |
| 89 | + "print(\"now.strftime('%A, %B %d %Y %I:%M:%S %p')\")\n", |
| 90 | + "print(now.strftime(\"%A, %B %d %Y %I:%M:%S %p\"))\n", |
| 91 | + "print()\n", |
| 92 | + "\n", |
| 93 | + "# use a shorter version to produce \"locale appropriate\" date and time\n", |
| 94 | + "print(\"now.strftime('%c')\")\n", |
| 95 | + "print(now.strftime(\"%c\"))" |
| 96 | + ], |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "now.strftime('%Y')\n", |
| 103 | + "2024\n", |
| 104 | + "\n", |
| 105 | + "now.strftime('%A, %B %d %Y %I:%M:%S %p')\n", |
| 106 | + "Sunday, June 02 2024 10:44:37 PM\n", |
| 107 | + "\n", |
| 108 | + "now.strftime('%c')\n", |
| 109 | + "Sun Jun 2 22:44:37 2024\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "execution_count": 2 |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "id": "d176a3090c4730eb", |
4 | 118 | "metadata": {},
|
| 119 | + "source": "These functions use a [propeleptic Gregorian](https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar) calendar, though, which is not always sufficient for working with geoscience data." |
| 120 | + }, |
| 121 | + { |
5 | 122 | "cell_type": "markdown",
|
6 |
| - "source": "# Working with Date and Time", |
7 |
| - "id": "b53cacffa29d30a6" |
| 123 | + "id": "c8a19ab9f50212c5", |
| 124 | + "metadata": {}, |
| 125 | + "source": [ |
| 126 | + "## cftime\n", |
| 127 | + "\n", |
| 128 | + "[`cftime`](https://unidata.github.io/cftime/) is another library for working with dates and times in Python that conforms to the [Climate and Forecasting (CF) metadata conventions](https://cfconventions.org/cf-conventions/cf-conventions#time-coordinate).\n", |
| 129 | + "\n", |
| 130 | + "Importantly, `cftime` supports all the calendars defined in the CF conventions, including `gregorian` or `standard`, which is the mixed Gregorian/Julian calendar defined by [Udunits](https://www.unidata.ucar.edu/software/udunits/), `proleptic_gregorian`, `noleap` or `365_day`, `all_leap` or `366_day`, `360_day`, and `julian`.\n", |
| 131 | + "\n", |
| 132 | + "`cftime` is often useful for working with geoscience data and can be used with [`xarray`](https://docs.xarray.dev/en/stable/generated/xarray.CFTimeIndex.html)." |
| 133 | + ] |
8 | 134 | },
|
9 | 135 | {
|
10 | 136 | "metadata": {},
|
11 | 137 | "cell_type": "markdown",
|
12 |
| - "source": "## The datetime module", |
13 |
| - "id": "13edacb9510e559e" |
| 138 | + "source": "## Specific Use Cases", |
| 139 | + "id": "a710537046d5a159" |
14 | 140 | },
|
15 | 141 | {
|
16 | 142 | "metadata": {},
|
| 143 | + "cell_type": "markdown", |
| 144 | + "source": [ |
| 145 | + "### Calculating the number of days in a month\n", |
| 146 | + "To calculate the number of days in a month, use [`cftime.datetime().daysinmonth`](https://unidata.github.io/cftime/api.html#cftime.datetime)." |
| 147 | + ], |
| 148 | + "id": "de2a66ba6d34497d" |
| 149 | + }, |
| 150 | + { |
| 151 | + "metadata": { |
| 152 | + "ExecuteTime": { |
| 153 | + "end_time": "2024-06-03T04:44:37.861353Z", |
| 154 | + "start_time": "2024-06-03T04:44:37.788458Z" |
| 155 | + } |
| 156 | + }, |
17 | 157 | "cell_type": "code",
|
18 |
| - "outputs": [], |
19 |
| - "execution_count": null, |
20 |
| - "source": "", |
21 |
| - "id": "bc54610955d8ef71" |
| 158 | + "source": [ |
| 159 | + "import cftime\n", |
| 160 | + "\n", |
| 161 | + "day = 1\n", |
| 162 | + "month = 6\n", |
| 163 | + "year = 2024\n", |
| 164 | + "\n", |
| 165 | + "date = cftime.datetime(year, month, day, calendar=\"standard\").daysinmonth\n", |
| 166 | + "date" |
| 167 | + ], |
| 168 | + "id": "f0390a82422e4913", |
| 169 | + "outputs": [ |
| 170 | + { |
| 171 | + "data": { |
| 172 | + "text/plain": [ |
| 173 | + "30" |
| 174 | + ] |
| 175 | + }, |
| 176 | + "execution_count": 3, |
| 177 | + "metadata": {}, |
| 178 | + "output_type": "execute_result" |
| 179 | + } |
| 180 | + ], |
| 181 | + "execution_count": 3 |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "markdown", |
| 185 | + "id": "1d434857c1cd81d1", |
| 186 | + "metadata": {}, |
| 187 | + "source": [ |
| 188 | + "## Curated Resources\n", |
| 189 | + "\n", |
| 190 | + "To learn more about working with dates and times in Python, we suggest:\n", |
| 191 | + "- the Project Pythia Foundations chapter titled [Times and Dates in Python](https://foundations.projectpythia.org/core/datetime/datetime.html)\n", |
| 192 | + "- this Xarray [documentation page on timeseries data](https://docs.xarray.dev/en/latest/user-guide/time-series.html)\n", |
| 193 | + "- the [\"How to handle time series data with ease\"](https://pandas.pydata.org/docs/getting_started/intro_tutorials/09_timeseries.html) tutorial if you're working with `pandas`" |
| 194 | + ] |
22 | 195 | }
|
23 | 196 | ],
|
24 | 197 | "metadata": {
|
|
0 commit comments