-
-
Notifications
You must be signed in to change notification settings - Fork 0
get dt index for time unit #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 30 commits
0e3e16b
3d68d1e
8a6b784
6c1fa4c
7ccdf38
07b0a64
b128213
9ea0b3b
26320bb
e391018
d77e04b
f292d4c
cbfe24d
e206aaf
6dc9467
ab3c583
360fd15
6407573
5e97d1b
6f8224e
0c31704
0174990
b11c55a
fed70bc
c842d6c
2a29deb
80d8f9c
744f35f
04ba81b
e1d30f3
dc5caa2
196cd37
7b8c486
3d94a19
47a1ea8
9aae40a
b36e7e0
9592bb7
6df07aa
627e11c
146359f
c24b639
e8c7efe
63d66b4
cce7bff
e348f65
d2c10e0
4e097a0
e969811
0dc930f
37dfafb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,6 +105,55 @@ for dt in Quarter(date(1958, 3, 25)): | |
|
|
||
| we can also convert such collection to a list. | ||
|
|
||
| ### Subscripting | ||
|
|
||
| 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: | ||
|
|
||
| ``` | ||
| Week.get_index_for_date(date(1958, 3, 25)) # 102123 | ||
| Week.get_date_from_index(102123) # date(1958, 3, 24) | ||
| ``` | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
|
|
||
|
||
| We can also use the index to get a `TimUnit` with: | ||
|
|
||
|
Comment on lines
+119
to
+120
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct the “TimUnit” typo. 🤖 Prompt for AI Agents |
||
| ``` | ||
| Week[102123] # Week(date(1958, 3, 24)) | ||
| ``` | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| moreover a week itself can be subscripted, for example: | ||
|
|
||
| ``` | ||
| Week(date(1958, 3, 24))[2] # date(1958, 3, 26) | ||
| ``` | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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: | ||
|
|
||
| ``` | ||
| Week[102123:105341:2] | ||
| ``` | ||
|
|
||
| is a collection of `Week` objects between `1958-03-24` and `2019-11-25` each time with one week in between. | ||
|
|
||
|
|
||
| The `Week` class itself is also iterable, for example: | ||
|
|
||
| ``` | ||
| for week in Week: | ||
| print(week) | ||
| ``` | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| will start enumerating over all weeks since 0001-01-01. | ||
|
|
||
| A time unit also has a length: the number of time units that can be represented, so: | ||
|
|
||
| ``` | ||
| len(Week) # 521722 | ||
| `` | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| means the software can represent 521'722 weeks from 0001-01-01 to 9999-12-26. | ||
|
|
||
| ### Shifting units of time | ||
|
|
||
| The units of time can also be shifted, for example: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,24 +21,50 @@ def truncate(cls, dt): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return date(10 * (dt.year // 10), 1, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_index_for_date(cls, dt): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return the zero-based decade index for the given date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dt (date or datetime): The date for which to compute the decade index. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int: The decade index equal to the calendar year divided by 10 using integer division (year // 10). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dt.year // 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_date_from_index(cls, idx): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return the start date (January 1) of the decade represented by the given index. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| idx (int): Decade index; the corresponding year is 10 * idx. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| datetime.date: January 1 of the year 10 * idx. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return date(10 * idx, 1, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | |
| def get_index_for_date(cls, dt): | |
| """ | |
| Return the zero-based decade index for the given date. | |
| Parameters: | |
| dt (date or datetime): The date for which to compute the decade index. | |
| Returns: | |
| int: The decade index equal to the calendar year divided by 10 using integer division (year // 10). | |
| """ | |
| return dt.year // 10 | |
| @classmethod | |
| def get_date_from_index(cls, idx): | |
| """ | |
| Return the start date (January 1) of the decade represented by the given index. | |
| Parameters: | |
| idx (int): Decade index; the corresponding year is 10 * idx. | |
| Returns: | |
| datetime.date: January 1 of the year 10 * idx. | |
| """ | |
| return date(10 * idx, 1, 1) | |
| @classmethod | |
| def get_index_for_date(cls, dt): | |
| """ | |
| Return the zero-based decade index for the given date. | |
| Parameters: | |
| dt (date or datetime): The date for which to compute the decade index. | |
| Returns: | |
| int: The decade index relative to date.min, computed as (year - date.min.year) // 10. | |
| """ | |
| return (dt.year - date.min.year) // 10 | |
| @classmethod | |
| def get_date_from_index(cls, idx): | |
| """ | |
| Return the start date (January 1) of the decade represented by the given index. | |
| Parameters: | |
| idx (int): Decade index relative to date.min; the corresponding year is (10 * idx) + date.min.year. | |
| Returns: | |
| datetime.date: January 1 of the year (10 * idx) + date.min.year. | |
| """ | |
| return date((10 * idx) + date.min.year, 1, 1) |
🤖 Prompt for AI Agents
In timetest.py around lines 24 to 48, Decade.get_index_for_date and
get_date_from_index are inconsistent with other units and produce invalid year
0; change get_index_for_date to compute the offset from date.min (i.e. (dt.year
- date.min.year) // 10) so date.min maps to index 0, and change
get_date_from_index to construct the decade start using that offset added back
to date.min.year (i.e. year = 10 * idx + date.min.year) so
get_date_from_index(0) returns a valid date(1,1,1).
Uh oh!
There was an error while loading. Please reload this page.