Skip to content

Commit 482bb80

Browse files
authored
Update the documentation to reflect the 3.0 changes (#777)
1 parent 78fdc33 commit 482bb80

File tree

8 files changed

+341
-297
lines changed

8 files changed

+341
-297
lines changed

docs/docs/difference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Difference
22

3-
The `diff()` method returns a [Period](#period) instance that represents the total duration
3+
The `diff()` method returns an [Interval](#interval) instance that represents the total duration
44
between two `DateTime` instances. This interval can be then expressed in various units.
55
These interval methods always return *the total difference expressed* in the specified time requested.
66
All values are truncated and not rounded.

docs/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
{!docs/modifiers.md!}
1313
{!docs/timezones.md!}
1414
{!docs/duration.md!}
15-
{!docs/period.md!}
15+
{!docs/interval.md!}
1616
{!docs/testing.md!}
1717
{!docs/limitations.md!}

docs/docs/installation.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,23 @@ or, if you are using [poetry](https://python-poetry.org):
1111
```bash
1212
$ poetry add pendulum
1313
```
14+
15+
## Optional features
16+
17+
Pendulum provides optional features that you must explicitly require in order to use them.
18+
19+
These optional features are:
20+
21+
- `test`: Provides a set of helpers to make testing easier by allowing you to control the flow of time.
22+
23+
You can install them by specifying them when installing Pendulum
24+
25+
```bash
26+
$ pip install pendulum[test]
27+
```
28+
29+
or, if you are using [poetry](https://python-poetry.org):
30+
31+
```bash
32+
$ poetry add pendulum[test]
33+
```
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Period
1+
# Interval
22

3-
When you subtract a `DateTime` instance from another, or use the `diff()` method, it will return a `Period` instance.
3+
When you subtract a `DateTime` instance from another, or use the `diff()` method, it will return an `Interval` instance.
44
It inherits from the [Duration](#duration) class with the added benefit that it is aware of the
55
instances that generated it, so that it can give access to more methods and properties:
66

@@ -10,29 +10,29 @@ instances that generated it, so that it can give access to more methods and prop
1010
>>> start = pendulum.datetime(2000, 11, 20)
1111
>>> end = pendulum.datetime(2016, 11, 5)
1212

13-
>>> period = end - start
13+
>>> interval = end - start
1414

15-
>>> period.years
15+
>>> interval.years
1616
15
17-
>>> period.months
17+
>>> interval.months
1818
11
19-
>>> period.in_years()
19+
>>> interval.in_years()
2020
15
21-
>>> period.in_months()
21+
>>> interval.in_months()
2222
191
2323

2424
# Note that the weeks property
2525
# will change compared to the Duration class
26-
>>> period.weeks
26+
>>> interval.weeks
2727
2 # 832 for the duration
2828

2929
# However the days property will still remain the same
3030
# to keep the compatibility with the timedelta class
31-
>>> period.days
31+
>>> interval.days
3232
5829
3333
```
3434

35-
Be aware that a period, just like an interval, is compatible with the `timedelta` class regarding
35+
Be aware that an interval, just like an duration, is compatible with the `timedelta` class regarding
3636
its attributes. However, its custom attributes (like `remaining_days`) will be aware of any DST
3737
transitions that might have occurred and adjust accordingly. Let's take an example:
3838

@@ -42,42 +42,42 @@ transitions that might have occurred and adjust accordingly. Let's take an examp
4242
>>> start = pendulum.datetime(2017, 3, 7, tz='America/Toronto')
4343
>>> end = start.add(days=6)
4444

45-
>>> period = end - start
45+
>>> interval = end - start
4646

4747
# timedelta properties
48-
>>> period.days
48+
>>> interval.days
4949
5
50-
>>> period.seconds
50+
>>> interval.seconds
5151
82800
5252

53-
# period properties
54-
>>> period.remaining_days
53+
# interval properties
54+
>>> interval.remaining_days
5555
6
56-
>>> period.hours
56+
>>> interval.hours
5757
0
58-
>>> period.remaining_seconds
58+
>>> interval.remaining_seconds
5959
0
6060
```
6161

6262
!!!warning
6363

6464
Due to their nature (fixed duration between two datetimes), most arithmetic operations will
65-
return a `Duration` instead of a `Period`.
65+
return a `Duration` instead of an `Interval`.
6666

6767
```python
6868
>>> import pendulum
6969

7070
>>> dt1 = pendulum.datetime(2016, 8, 7, 12, 34, 56)
7171
>>> dt2 = dt1.add(days=6, seconds=34)
72-
>>> period = pendulum.period(dt1, dt2)
73-
>>> period * 2
72+
>>> interval = pendulum.interval(dt1, dt2)
73+
>>> interval * 2
7474
Duration(weeks=1, days=5, minutes=1, seconds=8)
7575
```
7676

7777

7878
## Instantiation
7979

80-
You can create an instance by using the `period()` helper:
80+
You can create an instance by using the `interval()` helper:
8181

8282
```python
8383

@@ -86,39 +86,39 @@ You can create an instance by using the `period()` helper:
8686
>>> start = pendulum.datetime(2000, 1, 1)
8787
>>> end = pendulum.datetime(2000, 1, 31)
8888

89-
>>> period = pendulum.period(start, end)
89+
>>> interval = pendulum.interval(start, end)
9090
```
9191

92-
You can also make an inverted period:
92+
You can also make an inverted interval:
9393

9494
```python
95-
>>> period = pendulum.period(end, start)
96-
>>> period.remaining_days
95+
>>> interval = pendulum.interval(end, start)
96+
>>> interval.remaining_days
9797
-2
9898
```
9999

100-
If you have inverted dates but want to make sure that the period is positive,
100+
If you have inverted dates but want to make sure that the interval is positive,
101101
you should set the `absolute` keyword argument to `True`:
102102

103103
```python
104-
>>> period = pendulum.period(end, start, absolute=True)
105-
>>> period.remaining_days
104+
>>> interval = pendulum.interval(end, start, absolute=True)
105+
>>> interval.remaining_days
106106
2
107107
```
108108

109109
## Range
110110

111-
If you want to iterate over a period, you can use the `range()` method:
111+
If you want to iterate over a interval, you can use the `range()` method:
112112

113113
```python
114114
>>> import pendulum
115115

116116
>>> start = pendulum.datetime(2000, 1, 1)
117117
>>> end = pendulum.datetime(2000, 1, 10)
118118

119-
>>> period = pendulum.period(start, end)
119+
>>> interval = pendulum.interval(start, end)
120120

121-
>>> for dt in period.range('days'):
121+
>>> for dt in interval.range('days'):
122122
>>> print(dt)
123123

124124
'2000-01-01T00:00:00+00:00'
@@ -141,7 +141,7 @@ If you want to iterate over a period, you can use the `range()` method:
141141
You can pass an amount for the passed unit to control the length of the gap:
142142

143143
```python
144-
>>> for dt in period.range('days', 2):
144+
>>> for dt in interval.range('days', 2):
145145
>>> print(dt)
146146

147147
'2000-01-01T00:00:00+00:00'
@@ -151,18 +151,18 @@ You can pass an amount for the passed unit to control the length of the gap:
151151
'2000-01-09T00:00:00+00:00'
152152
```
153153

154-
You can also directly iterate over the `Period` instance,
154+
You can also directly iterate over the `Interval` instance,
155155
the unit will be `days` in this case:
156156

157157
```python
158-
>>> for dt in period:
158+
>>> for dt in interval:
159159
>>> print(dt)
160160
```
161161

162-
You can check if a `DateTime` instance is inside a period using the `in` keyword:
162+
You can check if a `DateTime` instance is inside a interval using the `in` keyword:
163163

164164
```python
165165
>>> dt = pendulum.datetime(2000, 1, 4)
166-
>>> dt in period
166+
>>> dt in interval
167167
True
168168
```

docs/docs/testing.md

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,87 @@
11
# Testing
22

3-
The testing methods allow you to set a `DateTime` instance (real or mock) to be returned
4-
when a "now" instance is created.
5-
The provided instance will be returned specifically under the following conditions:
3+
Pendulum provides a few helpers to help you control the flow of time in your tests. Note that
4+
these helpers are only available if you opted in the `test` extra during [installation](#installation).
65

7-
* A call to the `now()` method, ex. `pendulum.now()`.
8-
* When the string "now" is passed to the `parse()` method, ex. `pendulum.parse('now')`
6+
!!!warning
7+
If you are migrating from Pendulum 2, note that the `set_test_now()` and `test()`
8+
helpers have been removed.
9+
10+
11+
## Relative time travel
12+
13+
You can travel in time relatively to the current time
914

1015
```python
1116
>>> import pendulum
1217

13-
# Create testing datetime
14-
>>> known = pendulum.datetime(2001, 5, 21, 12)
18+
>>> now = pendulum.now()
19+
>>> pendulum.travel(minutes=5)
20+
>>> pendulum.now().diff_for_humans(now)
21+
"5 minutes after"
22+
```
1523

16-
# Set the mock
17-
>>> pendulum.set_test_now(known)
24+
Note that once you've travelled in time the clock **keeps ticking**. If you prefer to stop the time completely
25+
you can use the `freeze` parameter:
1826

19-
>>> print(pendulum.now())
20-
'2001-05-21T12:00:00+00:00'
27+
```python
28+
>>> import pendulum
2129

22-
>>> print(pendulum.parse('now'))
23-
'2001-05-21T12:00:00+00:00'
30+
>>> now = pendulum.now()
31+
>>> pendulum.travel(minutes=5, freeze=True)
32+
>>> pendulum.now().diff_for_humans(now)
33+
"5 minutes after" # This will stay like this indefinitely
34+
```
2435

25-
# Clear the mock
26-
>>> pendulum.set_test_now()
2736

28-
>>> print(pendulum.now())
29-
'2016-07-10T22:10:33.954851-05:00'
30-
```
37+
## Absolute time travel
3138

32-
Related methods will also return values mocked according to the **now** instance.
39+
Sometimes, you may want to place yourself at a specific point in time.
40+
This is possible by using the `travel_to()` helper. This helper accepts a `DateTime` instance
41+
that represents the point in time where you want to travel to.
3342

3443
```python
35-
>>> print(pendulum.today())
36-
'2001-05-21T00:00:00+00:00'
44+
>>> import pendulum
3745

38-
>>> print(pendulum.tomorrow())
39-
'2001-05-22T00:00:00+00:00'
46+
>>> pendulum.travel_to(pendulum.yesterday())
47+
```
4048

41-
>>> print(pendulum.yesterday())
42-
'2001-05-20T00:00:00+00:00'
49+
Similarly to `travel`, it's important to remember that, by default, the time keeps ticking so, if you prefer
50+
stopping the time, use the `freeze` parameter:
51+
52+
```python
53+
>>> import pendulum
54+
55+
>>> pendulum.travel_to(pendulum.yesterday(), freeze=True)
4356
```
4457

45-
If you don't want to manually clear the mock (or you are afraid of forgetting),
46-
you can use the provided `test()` contextmanager.
58+
## Travelling back to the present
59+
60+
Using any of the travel helpers will keep you in the past, or future, until you decide
61+
to travel back to the present time. To do so, you may use the `travel_back()` helper.
4762

4863
```python
4964
>>> import pendulum
5065

51-
>>> known = pendulum.datetime(2001, 5, 21, 12)
66+
>>> now = pendulum.now()
67+
>>> pendulum.travel(minutes=5, freeze=True)
68+
>>> pendulum.now().diff_for_humans(now)
69+
"5 minutes after"
70+
>>> pendulum.travel_back()
71+
>>> pendulum.now().diff_for_humans(now)
72+
"a few seconds after"
73+
```
74+
75+
However, it might be cumbersome to remember to travel back so, instead, you can use any of the helpers as a context
76+
manager:
5277

53-
>>> with pendulum.test(known):
54-
>>> print(pendulum.now())
55-
'2001-05-21T12:00:00+00:00'
78+
```python
79+
>>> import pendulum
5680

57-
>>> print(pendulum.now())
58-
'2016-07-10T22:10:33.954851-05:00'
81+
>>> now = pendulum.now()
82+
>>> with pendulum.travel(minutes=5, freeze=True):
83+
>>> pendulum.now().diff_for_humans(now)
84+
"5 minutes after"
85+
>>> pendulum.now().diff_for_humans(now)
86+
"a few seconds after"
5987
```

0 commit comments

Comments
 (0)