-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add tz argument to some localtime functions #88
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -60,12 +60,15 @@ def datetime( | |
minute: int = 0, | ||
second: int = 0, | ||
microsecond: int = 0, | ||
tz: timezone.zoneinfo.ZoneInfo | None = None, | ||
) -> datetime_.datetime: | ||
""" | ||
Return a datetime in the local timezone. | ||
Return a datetime in the local timezone, or in the desired timezone if tz is provided. | ||
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. ❓ This module is just helpers around putting things in a specific (local) timezone -- if we want to use arbitrary timezones, why don't we just use In particular, 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. It is as strange as the 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. I think you've identified a problem, but this isn't the right fix for it. I think we're using this module to do two things: provide date times in the "local" timezone and a set of datetime utilities. We should probably make those two jobs two different modules: something that contains a tz-agnostic set of helpers (midnight, next-midnight, days_in_past, etc) and then a module that wraps those and injects a specific timezone like industrytime does in kraken-core. I think the clearest indication of that is that one of these functions will have 'as localtime' in the name and will allow you to set any timezone and does nothing that just using the Django timezone package doesn't do. If there's absolutely no way we can make that change and this needs to go in urgently, then I could accept the compromise of following the pattern of the rest of the module, as long as someone commits to fixing it later. Apart from the function with localtime in its name -- that one seems too wrong to me. 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. I do agree that this module is providing both, a series of functions that depends on timezones and a series of helpers for handling datetime operations. This is not urgent, it was part of my last Spa Day because I don't usually have enough time to work on this. Feel free to approve this PR or close it if you think you can provide a better alternative to this problem 👍 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.
I'm not aware of this being the case. There are times when using the Kraken instance's "home" timezone is not always appropriate, but there are a lot of systems (notably all of the financials systems) which rely on there being a single timezone to use as a reference. Can you point to the discussion.decision where this concept was deprecated? 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. Let me explain better: I'm not saying that accessing the time configured behind the One place with discussion around how to create logic that can be used to supersede the "one localtime" concept was the channel #temp-localtime-as-industrytime where there was some discussion on whether we should still have only one localtime (but set its timezone to the industry timezone). Another place with discussion is a thread where you are involved and where there is an intention differenciating between "localtime" and "kraken localtime": I think the naming is the key and we should talk about "kraken time" as a concretion of 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.
I think this is where we disagree: as I understand it, Remember this repo isn't Kraken, this is a set of generic tools we've open sourced, so it needs to make sense outside the context of kraken-core. Also worth being aware that having a single "industry" timezone is something we know doesn't necessarily make sense: we have clients in places where there will be multipel industry time zones in different industries/markets, so we already know we need to be able to expand on a generic base of functions. Given that sounds like a job you aren't prepared to take on right now (and fair enough), I'm not blocking this change. |
||
""" | ||
if tz is None: | ||
tz = timezone.get_current_timezone() | ||
dt = datetime_.datetime(year, month, day, hour, minute, second, microsecond) | ||
return timezone.make_aware(dt) | ||
return timezone.make_aware(dt, timezone=tz) | ||
|
||
|
||
# Returning dates | ||
|
@@ -349,13 +352,17 @@ def as_range( | |
return midnight(_date, tz), latest(_date, tz) | ||
|
||
|
||
def make_aware_assuming_local(dt: datetime_.datetime) -> datetime_.datetime: | ||
def make_aware_assuming_local( | ||
dt: datetime_.datetime, tz: Optional[datetime_.tzinfo] = None | ||
) -> datetime_.datetime: | ||
""" | ||
Just a wrapper for Django's method, which will takes a naive datetime, and makes it timezone | ||
aware, assuming the current timezone if none is passed (which it isn't from this wrapper | ||
function). It will also raise an exception if the passed datetime is already timezone-aware. | ||
aware, assuming the current timezone if none is passed. | ||
It will also raise an exception if the passed datetime is already timezone-aware. | ||
""" | ||
return timezone.make_aware(dt) | ||
if tz is None: | ||
tz = timezone.get_current_timezone() | ||
return timezone.make_aware(dt, timezone=tz) | ||
|
||
|
||
def make_aware_assuming_utc(dt: datetime_.datetime) -> datetime_.datetime: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to some refactoring, the leading
timezone
is causing mypy indigestion. We get the type directly from thezoneinfo
stdlib now.