Skip to content

Commit fdf2733

Browse files
authored
Add day of week (#51)
* Add day of week * take two add day of week * add d-o-w section to ncl index table * trying some small changes * Fix index.rst issue * Updates to year 0 documentation * Grammer & updates based on PR #44 comments * strftime implementation * add speciic use case to datetime * Many small edits * moved return value content into using cftime section * consistancy things
1 parent cb7719a commit fdf2733

File tree

4 files changed

+321
-48
lines changed

4 files changed

+321
-48
lines changed

applications/date_time/datetime.ipynb

+90-48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"cell_type": "markdown",
55
"id": "b53cacffa29d30a6",
66
"metadata": {},
7-
"source": "# Working with Dates and Times"
7+
"source": [
8+
"# Working with Dates and Times"
9+
]
810
},
911
{
1012
"cell_type": "markdown",
@@ -33,21 +35,14 @@
3335
},
3436
{
3537
"cell_type": "code",
38+
"execution_count": 1,
3639
"id": "2092ca5d86a3a24b",
3740
"metadata": {
3841
"ExecuteTime": {
3942
"end_time": "2024-06-03T04:44:37.784336Z",
4043
"start_time": "2024-06-03T04:44:37.781266Z"
4144
}
4245
},
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-
],
5146
"outputs": [
5247
{
5348
"name": "stdout",
@@ -58,7 +53,14 @@
5853
]
5954
}
6055
],
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+
]
6264
},
6365
{
6466
"cell_type": "markdown",
@@ -72,28 +74,14 @@
7274
},
7375
{
7476
"cell_type": "code",
77+
"execution_count": 2,
7578
"id": "1d18147498a434eb",
7679
"metadata": {
7780
"ExecuteTime": {
7881
"end_time": "2024-06-03T04:44:37.787843Z",
7982
"start_time": "2024-06-03T04:44:37.785520Z"
8083
}
8184
},
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-
],
9785
"outputs": [
9886
{
9987
"name": "stdout",
@@ -110,13 +98,29 @@
11098
]
11199
}
112100
],
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+
]
114116
},
115117
{
116118
"cell_type": "markdown",
117119
"id": "d176a3090c4730eb",
118120
"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+
]
120124
},
121125
{
122126
"cell_type": "markdown",
@@ -133,28 +137,44 @@
133137
]
134138
},
135139
{
136-
"metadata": {},
137140
"cell_type": "markdown",
138-
"source": "## Specific Use Cases",
139-
"id": "a710537046d5a159"
141+
"id": "a710537046d5a159",
142+
"metadata": {},
143+
"source": [
144+
"## Specific Use Cases"
145+
]
140146
},
141147
{
142-
"metadata": {},
143148
"cell_type": "markdown",
149+
"id": "de2a66ba6d34497d",
150+
"metadata": {},
144151
"source": [
145152
"### Calculating the number of days in a month\n",
146153
"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+
]
149155
},
150156
{
157+
"cell_type": "code",
158+
"execution_count": 3,
159+
"id": "f0390a82422e4913",
151160
"metadata": {
152161
"ExecuteTime": {
153162
"end_time": "2024-06-03T04:44:37.861353Z",
154163
"start_time": "2024-06-03T04:44:37.788458Z"
155164
}
156165
},
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+
],
158178
"source": [
159179
"import cftime\n",
160180
"\n",
@@ -164,21 +184,43 @@
164184
"\n",
165185
"date = cftime.datetime(year, month, day, calendar=\"standard\").daysinmonth\n",
166186
"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": {},
169203
"outputs": [
170204
{
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+
]
179210
}
180211
],
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+
]
182224
},
183225
{
184226
"cell_type": "markdown",
@@ -203,14 +245,14 @@
203245
"language_info": {
204246
"codemirror_mode": {
205247
"name": "ipython",
206-
"version": 2
248+
"version": 3
207249
},
208250
"file_extension": ".py",
209251
"mimetype": "text/x-python",
210252
"name": "python",
211253
"nbconvert_exporter": "python",
212-
"pygments_lexer": "ipython2",
213-
"version": "2.7.6"
254+
"pygments_lexer": "ipython3",
255+
"version": "3.12.3"
214256
}
215257
},
216258
"nbformat": 4,

0 commit comments

Comments
 (0)