1
- # Period
1
+ # Interval
2
2
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.
4
4
It inherits from the [ Duration] ( #duration ) class with the added benefit that it is aware of the
5
5
instances that generated it, so that it can give access to more methods and properties:
6
6
@@ -10,29 +10,29 @@ instances that generated it, so that it can give access to more methods and prop
10
10
>> > start = pendulum.datetime(2000 , 11 , 20 )
11
11
>> > end = pendulum.datetime(2016 , 11 , 5 )
12
12
13
- >> > period = end - start
13
+ >> > interval = end - start
14
14
15
- >> > period .years
15
+ >> > interval .years
16
16
15
17
- >> > period .months
17
+ >> > interval .months
18
18
11
19
- >> > period .in_years()
19
+ >> > interval .in_years()
20
20
15
21
- >> > period .in_months()
21
+ >> > interval .in_months()
22
22
191
23
23
24
24
# Note that the weeks property
25
25
# will change compared to the Duration class
26
- >> > period .weeks
26
+ >> > interval .weeks
27
27
2 # 832 for the duration
28
28
29
29
# However the days property will still remain the same
30
30
# to keep the compatibility with the timedelta class
31
- >> > period .days
31
+ >> > interval .days
32
32
5829
33
33
```
34
34
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
36
36
its attributes. However, its custom attributes (like ` remaining_days ` ) will be aware of any DST
37
37
transitions that might have occurred and adjust accordingly. Let's take an example:
38
38
@@ -42,42 +42,42 @@ transitions that might have occurred and adjust accordingly. Let's take an examp
42
42
>> > start = pendulum.datetime(2017 , 3 , 7 , tz = ' America/Toronto' )
43
43
>> > end = start.add(days = 6 )
44
44
45
- >> > period = end - start
45
+ >> > interval = end - start
46
46
47
47
# timedelta properties
48
- >> > period .days
48
+ >> > interval .days
49
49
5
50
- >> > period .seconds
50
+ >> > interval .seconds
51
51
82800
52
52
53
- # period properties
54
- >> > period .remaining_days
53
+ # interval properties
54
+ >> > interval .remaining_days
55
55
6
56
- >> > period .hours
56
+ >> > interval .hours
57
57
0
58
- >> > period .remaining_seconds
58
+ >> > interval .remaining_seconds
59
59
0
60
60
```
61
61
62
62
!!!warning
63
63
64
64
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 `.
66
66
67
67
```python
68
68
>>> import pendulum
69
69
70
70
>>> dt1 = pendulum.datetime(2016, 8, 7, 12, 34, 56)
71
71
>>> 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
74
74
Duration(weeks=1, days=5, minutes=1, seconds=8)
75
75
```
76
76
77
77
78
78
## Instantiation
79
79
80
- You can create an instance by using the ` period ()` helper:
80
+ You can create an instance by using the ` interval ()` helper:
81
81
82
82
``` python
83
83
@@ -86,39 +86,39 @@ You can create an instance by using the `period()` helper:
86
86
>> > start = pendulum.datetime(2000 , 1 , 1 )
87
87
>> > end = pendulum.datetime(2000 , 1 , 31 )
88
88
89
- >> > period = pendulum.period (start, end)
89
+ >> > interval = pendulum.interval (start, end)
90
90
```
91
91
92
- You can also make an inverted period :
92
+ You can also make an inverted interval :
93
93
94
94
``` python
95
- >> > period = pendulum.period (end, start)
96
- >> > period .remaining_days
95
+ >> > interval = pendulum.interval (end, start)
96
+ >> > interval .remaining_days
97
97
- 2
98
98
```
99
99
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,
101
101
you should set the ` absolute ` keyword argument to ` True ` :
102
102
103
103
``` 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
106
106
2
107
107
```
108
108
109
109
## Range
110
110
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:
112
112
113
113
``` python
114
114
>> > import pendulum
115
115
116
116
>> > start = pendulum.datetime(2000 , 1 , 1 )
117
117
>> > end = pendulum.datetime(2000 , 1 , 10 )
118
118
119
- >> > period = pendulum.period (start, end)
119
+ >> > interval = pendulum.interval (start, end)
120
120
121
- >> > for dt in period .range(' days' ):
121
+ >> > for dt in interval .range(' days' ):
122
122
>> > print (dt)
123
123
124
124
' 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:
141
141
You can pass an amount for the passed unit to control the length of the gap:
142
142
143
143
``` python
144
- >> > for dt in period .range(' days' , 2 ):
144
+ >> > for dt in interval .range(' days' , 2 ):
145
145
>> > print (dt)
146
146
147
147
' 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:
151
151
' 2000-01-09T00:00:00+00:00'
152
152
```
153
153
154
- You can also directly iterate over the ` Period ` instance,
154
+ You can also directly iterate over the ` Interval ` instance,
155
155
the unit will be ` days ` in this case:
156
156
157
157
``` python
158
- >> > for dt in period :
158
+ >> > for dt in interval :
159
159
>> > print (dt)
160
160
```
161
161
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:
163
163
164
164
``` python
165
165
>> > dt = pendulum.datetime(2000 , 1 , 4 )
166
- >> > dt in period
166
+ >> > dt in interval
167
167
True
168
168
```
0 commit comments