|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "id": "b53cacffa29d30a6",
|
6 | 6 | "metadata": {},
|
7 |
| - "source": "# Working with Dates and Times" |
| 7 | + "source": [ |
| 8 | + "# Working with Dates and Times" |
| 9 | + ] |
8 | 10 | },
|
9 | 11 | {
|
10 | 12 | "cell_type": "markdown",
|
|
33 | 35 | },
|
34 | 36 | {
|
35 | 37 | "cell_type": "code",
|
| 38 | + "execution_count": 1, |
36 | 39 | "id": "2092ca5d86a3a24b",
|
37 | 40 | "metadata": {
|
38 | 41 | "ExecuteTime": {
|
39 | 42 | "end_time": "2024-06-03T04:44:37.784336Z",
|
40 | 43 | "start_time": "2024-06-03T04:44:37.781266Z"
|
41 | 44 | }
|
42 | 45 | },
|
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 | 46 | "outputs": [
|
52 | 47 | {
|
53 | 48 | "name": "stdout",
|
|
58 | 53 | ]
|
59 | 54 | }
|
60 | 55 | ],
|
61 |
| - "execution_count": 1 |
| 56 | + "source": [ |
| 57 | + "import datetime\n", |
| 58 | + "\n", |
| 59 | + "today = datetime.date.today()\n", |
| 60 | + "print(f\"datetime.date.today() -> \\t {today}\")\n", |
| 61 | + "now = datetime.datetime.now()\n", |
| 62 | + "print(f\"datetime.datetime.now() -> \\t {now}\")" |
| 63 | + ] |
62 | 64 | },
|
63 | 65 | {
|
64 | 66 | "cell_type": "markdown",
|
|
72 | 74 | },
|
73 | 75 | {
|
74 | 76 | "cell_type": "code",
|
| 77 | + "execution_count": 2, |
75 | 78 | "id": "1d18147498a434eb",
|
76 | 79 | "metadata": {
|
77 | 80 | "ExecuteTime": {
|
78 | 81 | "end_time": "2024-06-03T04:44:37.787843Z",
|
79 | 82 | "start_time": "2024-06-03T04:44:37.785520Z"
|
80 | 83 | }
|
81 | 84 | },
|
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 | 85 | "outputs": [
|
98 | 86 | {
|
99 | 87 | "name": "stdout",
|
|
110 | 98 | ]
|
111 | 99 | }
|
112 | 100 | ],
|
113 |
| - "execution_count": 2 |
| 101 | + "source": [ |
| 102 | + "# print just the year\n", |
| 103 | + "print(\"now.strftime('%Y')\")\n", |
| 104 | + "print(now.strftime(\"%Y\"))\n", |
| 105 | + "print()\n", |
| 106 | + "\n", |
| 107 | + "# print weekday, month, day, year, 12h time, and AM/PM\n", |
| 108 | + "print(\"now.strftime('%A, %B %d %Y %I:%M:%S %p')\")\n", |
| 109 | + "print(now.strftime(\"%A, %B %d %Y %I:%M:%S %p\"))\n", |
| 110 | + "print()\n", |
| 111 | + "\n", |
| 112 | + "# use a shorter version to produce \"locale appropriate\" date and time\n", |
| 113 | + "print(\"now.strftime('%c')\")\n", |
| 114 | + "print(now.strftime(\"%c\"))" |
| 115 | + ] |
114 | 116 | },
|
115 | 117 | {
|
116 | 118 | "cell_type": "markdown",
|
117 | 119 | "id": "d176a3090c4730eb",
|
118 | 120 | "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." |
| 121 | + "source": [ |
| 122 | + "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." |
| 123 | + ] |
120 | 124 | },
|
121 | 125 | {
|
122 | 126 | "cell_type": "markdown",
|
|
133 | 137 | ]
|
134 | 138 | },
|
135 | 139 | {
|
136 |
| - "metadata": {}, |
137 | 140 | "cell_type": "markdown",
|
138 |
| - "source": "## Specific Use Cases", |
139 |
| - "id": "a710537046d5a159" |
| 141 | + "id": "a710537046d5a159", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "## Specific Use Cases" |
| 145 | + ] |
140 | 146 | },
|
141 | 147 | {
|
142 |
| - "metadata": {}, |
143 | 148 | "cell_type": "markdown",
|
| 149 | + "id": "de2a66ba6d34497d", |
| 150 | + "metadata": {}, |
144 | 151 | "source": [
|
145 | 152 | "### Calculating the number of days in a month\n",
|
146 | 153 | "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" |
| 154 | + ] |
149 | 155 | },
|
150 | 156 | {
|
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": 3, |
| 159 | + "id": "f0390a82422e4913", |
151 | 160 | "metadata": {
|
152 | 161 | "ExecuteTime": {
|
153 | 162 | "end_time": "2024-06-03T04:44:37.861353Z",
|
154 | 163 | "start_time": "2024-06-03T04:44:37.788458Z"
|
155 | 164 | }
|
156 | 165 | },
|
157 |
| - "cell_type": "code", |
| 166 | + "outputs": [ |
| 167 | + { |
| 168 | + "data": { |
| 169 | + "text/plain": [ |
| 170 | + "30" |
| 171 | + ] |
| 172 | + }, |
| 173 | + "execution_count": 3, |
| 174 | + "metadata": {}, |
| 175 | + "output_type": "execute_result" |
| 176 | + } |
| 177 | + ], |
158 | 178 | "source": [
|
159 | 179 | "import cftime\n",
|
160 | 180 | "\n",
|
|
164 | 184 | "\n",
|
165 | 185 | "date = cftime.datetime(year, month, day, calendar=\"standard\").daysinmonth\n",
|
166 | 186 | "date"
|
167 |
| - ], |
168 |
| - "id": "f0390a82422e4913", |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "id": "e47737aa", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "### Calculating the day of the week\n", |
| 195 | + "To calculate the day of the week, use `cftime.datetime().strftime(\"%A\")`." |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "code", |
| 200 | + "execution_count": 2, |
| 201 | + "id": "a7e20432", |
| 202 | + "metadata": {}, |
169 | 203 | "outputs": [
|
170 | 204 | {
|
171 |
| - "data": { |
172 |
| - "text/plain": [ |
173 |
| - "30" |
174 |
| - ] |
175 |
| - }, |
176 |
| - "execution_count": 3, |
177 |
| - "metadata": {}, |
178 |
| - "output_type": "execute_result" |
| 205 | + "name": "stdout", |
| 206 | + "output_type": "stream", |
| 207 | + "text": [ |
| 208 | + "2024-6-4 is a Tuesday\n" |
| 209 | + ] |
179 | 210 | }
|
180 | 211 | ],
|
181 |
| - "execution_count": 3 |
| 212 | + "source": [ |
| 213 | + "import cftime\n", |
| 214 | + "\n", |
| 215 | + "day = 4\n", |
| 216 | + "month = 6\n", |
| 217 | + "year = 2024\n", |
| 218 | + "\n", |
| 219 | + "dow = cftime.datetime(\n", |
| 220 | + " year, month, day, calendar='proleptic_gregorian', has_year_zero=True\n", |
| 221 | + ").strftime(\"%A\")\n", |
| 222 | + "print(f\"{year}-{month}-{day} is a {dow}\")" |
| 223 | + ] |
182 | 224 | },
|
183 | 225 | {
|
184 | 226 | "cell_type": "markdown",
|
|
203 | 245 | "language_info": {
|
204 | 246 | "codemirror_mode": {
|
205 | 247 | "name": "ipython",
|
206 |
| - "version": 2 |
| 248 | + "version": 3 |
207 | 249 | },
|
208 | 250 | "file_extension": ".py",
|
209 | 251 | "mimetype": "text/x-python",
|
210 | 252 | "name": "python",
|
211 | 253 | "nbconvert_exporter": "python",
|
212 |
| - "pygments_lexer": "ipython2", |
213 |
| - "version": "2.7.6" |
| 254 | + "pygments_lexer": "ipython3", |
| 255 | + "version": "3.12.3" |
214 | 256 | }
|
215 | 257 | },
|
216 | 258 | "nbformat": 4,
|
|
0 commit comments