blob: 36dca2b6f17b192ed0ec8a9aae569c61041029d5 [file] [log] [blame]
Paul Ganssle4c0ba122018-06-07 20:07:21 +01001Exercises
2=========
3
4It is often useful to work through some examples in order to understand how a module works; on this page, there are several exercises of varying difficulty that you can use to learn how to use ``dateutil``.
5
6If you are interested in helping improve the documentation of ``dateutil``, it is recommended that you attempt to complete these exercises with no resources *other than dateutil's documentation*. If you find that the documentation is not clear enough to allow you to complete these exercises, open an issue on the `dateutil issue tracker <https://github.com/dateutil/dateutil/issues>`_ to let the developers know what part of the documentation needs improvement.
7
8
9.. contents:: Table of Contents
10 :backlinks: top
11 :local:
12
Paul Ganssle404af912018-06-07 20:09:47 +010013
14Martin Luther King Day
15--------------------------------
16
17
18 `Martin Luther King, Jr Day <https://en.wikipedia.org/wiki/Martin_Luther_King_Jr._Day>`_ is a US holiday that occurs every year on the third Monday in January?
19
20 How would you generate a `recurrence rule <../rrule.html>`_ that generates Martin Luther King Day, starting from its first observance in 1986?
21
22
23**Test Script**
24
25To solve this exercise, copy-paste this script into a document, change anything between the ``--- YOUR CODE ---`` comment blocks.
26
27.. raw:: html
28
29 <details>
30
31.. code-block:: python3
32
33 # ------- YOUR CODE -------------#
34 from dateutil import rrule
35
36 MLK_DAY = <<YOUR CODE HERE>>
37
38 # -------------------------------#
39
40 from datetime import datetime
41 MLK_TEST_CASES = [
42 ((datetime(1970, 1, 1), datetime(1980, 1, 1)),
43 []),
44 ((datetime(1980, 1, 1), datetime(1989, 1, 1)),
45 [datetime(1986, 1, 20),
46 datetime(1987, 1, 19),
47 datetime(1988, 1, 18)]),
48 ((datetime(2017, 2, 1), datetime(2022, 2, 1)),
49 [datetime(2018, 1, 15, 0, 0),
50 datetime(2019, 1, 21, 0, 0),
51 datetime(2020, 1, 20, 0, 0),
52 datetime(2021, 1, 18, 0, 0),
53 datetime(2022, 1, 17, 0, 0)]
54 ),
55 ]
56
57 def test_mlk_day():
58 for (between_args, expected) in MLK_TEST_CASES:
59 assert MLK_DAY.between(*between_args) == expected
60
61 if __name__ == "__main__":
62 test_mlk_day()
63 print('Success!')
64
Paul Gansslea5c248b2018-06-08 07:26:25 +010065.. raw:: html
66
67 </details>
68
69
70
71Next Monday meeting
72-------------------
73
74 A team has a meeting at 10 AM every Monday and wants a function that tells them, given a ``datetime.datetime`` object, what is the date and time of the *next* Monday meeting? This is probably best accomplished using a `relativedelta <../relativedelta.html>`_.
75
76**Test Script**
77
78To solve this exercise, copy-paste this script into a document, change anything between the ``--- YOUR CODE ---`` comment blocks.
79
80.. raw:: html
81
82 <details>
83
84
85.. code-block:: python3
86
87 # --------- YOUR CODE -------------- #
88 from dateutil import relativedelta
89
90 def next_monday(dt):
91 <<YOUR CODE HERE>>
92
93 # ---------------------------------- #
94
95 from datetime import datetime
96 from dateutil import tz
97
98 NEXT_MONDAY_CASES = [
99 (datetime(2018, 4, 11, 14, 30, 15, 123456),
100 datetime(2018, 4, 16, 10, 0)),
101 (datetime(2018, 4, 16, 10, 0),
102 datetime(2018, 4, 16, 10, 0)),
103 (datetime(2018, 4, 16, 10, 30),
104 datetime(2018, 4, 23, 10, 0)),
105 (datetime(2018, 4, 14, 9, 30, tzinfo=tz.gettz('America/New_York')),
106 datetime(2018, 4, 16, 10, 0, tzinfo=tz.gettz('America/New_York'))),
107 ]
108
109 def test_next_monday_1():
110 for dt_in, dt_out in NEXT_MONDAY_CASES:
111 assert next_monday(dt_in) == dt_out
112
113 if __name__ == "__main__":
114 test_next_monday_1()
Paul Ganssle404af912018-06-07 20:09:47 +0100115
116.. raw:: html
117
118 </details>
119
120