You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-1Lines changed: 50 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,6 +105,55 @@ for dt in Quarter(date(1958, 3, 25)):
105
105
106
106
we can also convert such collection to a list.
107
107
108
+
### Subscripting
109
+
110
+
The `Day`, `Week`, `Month, etc. classes have `.get_index_for_date(…)` and `.get_date_from_index(…)` methods, which allow to determine how many days, weeks, months, quarters and years are between `date.min` and the date given, and convert this back to a date. For example:
so 1958-03-25 is the 102'123 week since 0001-01-01, and that week starts the 24<sup>th</sup> of March, 1958.
118
+
119
+
We can also use the index to get a `TimUnit` with:
120
+
121
+
```python3
122
+
Week[102123] # Week(date(1958, 3, 24))
123
+
```
124
+
125
+
moreover a week itself can be subscripted, for example:
126
+
127
+
```python3
128
+
Week(date(1958, 3, 24))[2] # date(1958, 3, 26)
129
+
```
130
+
131
+
one can also slice to created an object that is a sliced "view" that generates `Week`s or `date`s in the week respectively. This view can then be sliced or indexed further. For example:
132
+
133
+
```python3
134
+
Week[102123:105341:2]
135
+
```
136
+
137
+
is a collection of `Week` objects between `1958-03-24` and `2019-11-25` each time with one week in between.
138
+
139
+
140
+
The `Week` class itself is also iterable, for example:
141
+
142
+
```python3
143
+
for week in Week:
144
+
print(week)
145
+
```
146
+
147
+
will start enumerating over all weeks since 0001-01-01.
148
+
149
+
A time unit also has a length: the number of time units that can be represented, so:
150
+
151
+
```python3
152
+
len(Week) # 521722
153
+
```
154
+
155
+
means the software can represent 521'722 weeks from 0001-01-01 to 9999-12-26.
156
+
108
157
### Shifting units of time
109
158
110
159
The units of time can also be shifted, for example:
@@ -181,7 +230,7 @@ class Decade(TimeunitKind):
181
230
182
231
this might be useful if the formatting is more advanced than what Python's date formatter can handle.
183
232
184
-
Furthermore, one implements the `.truncate(..)` class method to convert a date to the start of the date range, and the `_next(..)` which returns the first date for the next decade.
233
+
Furthermore, one implements the `.truncate(…)` class method to convert a date to the start of the date range, and the `_next(…)` which returns the first date for the next decade.
185
234
186
235
With these functions, we have registered a new time unit.
0 commit comments