Skip to content

Commit f8501ce

Browse files
authored
Create intoduction for dig deeper in gigasecond exercise
1 parent b061dd0 commit f8501ce

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Dig deeper
2+
There is only one correct and safe way to deal with date and datetime in python. We are going to use the built-in `datetime` module. A `datetime` object contains multiple attributes, like `year`, `month`, `day`, `hour`, `minute` and `second`.
3+
But you can't update a `datetime` object directly like:
4+
```py
5+
from datetime import datetime
6+
datetime_2000_01_25 = datetime(year = 2000, month = 1, day = 25)
7+
wrong_date = datetime_2000_01_25 + "2 weeks" # This won't work at all
8+
```
9+
Instead, we have to use another one object, `timedelta`. It will be used to accomplish the same thing as shown in the previous example. This object is a time interval, which can be used to modify a `datetime` object. We can add or subtract the `timedelta` to a `datetime` object to create a new `datetime` object with the updated values.
10+
```py
11+
from datetime import timedelta, datetime
12+
datetime_2000_01_01 = datetime(year = 2000, month = 1, day = 1)
13+
delta = timedelta(weeks=2)
14+
datetime_2000_01_15 = datetime_2000_01_01 + delta
15+
```
16+
In the exercise, we have one `datetime` parameter, so one of the correct answer is:
17+
```py
18+
from datetime import timedelta, datetime
19+
def add(moment: datetime) -> datetime:
20+
return moment + timedelta(seconds=10**9)
21+
```
22+
For more information, check the official [datetime documentation](https://docs.python.org/3/library/datetime.html#datetime.datetime.year)

0 commit comments

Comments
 (0)