-
Notifications
You must be signed in to change notification settings - Fork 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
How do I get an Instant from a SystemDateTime #146
Comments
Hi @spacemanspiff2007, thanks for reaching out. Let me see if I understand your use case correctly: if:
then: starting with # example: local system set to Europe/Amsterdam
>>> my_dt = SystemDateTime(2023, 4, 1, 12)
SystemDateTime(2023-04-01T12:00:00+02:00)
>>> my_dt.instant()
Instant(2023-04-01T10:00:00Z)
In general, yes—if your doing calculations in "exact" units like hours, minutes, seconds etc. If you'd like to do calendar arithmetic (like adding months, calendar days) you need to consider more carefully: you may be better off doing arithmetic on the The edit: clarifications |
Thank you for your quick and thorough reply. Edit: now = Instant.now()
target = (now + TimeDelta(seconds=1)).subtract(nanoseconds=now.to_system_tz().nanosecond) |
@spacemanspiff2007 rounding an About your suggestion: I had named BTW: A less hacky workaround for you until then. Still ugly, but handles nanoseconds properly. now = Instant.now()
target = Instant.from_timestamp(math.ceil(now.timestamp_nanos() / 1_000_000_000)) |
@spacemanspiff2007 follow-up question that would help me out: What is the reason in particular you need to round an |
I am reworking EAScheduler - an easy to use asyncio scheduler - which lets you run coros/functions at specific points in time. Another thing I realized is that Instant.add does not take days as an argument any more. I understand that months is ambiguous so removing that makes sense. Have you removed days because of the leap second? |
Thanks for explaining, that helps. Rounding methods should be added to the other datetimes as well, I think. In fact, I probably think it'd be best to do this rounding before converting to Relating to your suggestion about exception names: While I'm personally not a fan of redundant suffixes, I have to admit I'm in the minority here when it comes to exceptions. I'll probably bundle this with a bunch of other renames in the next release. edit: I've created an issue to track naming discussions here: #151 |
I'm closing this issue since the original question has been answered. Relevant follow-ups:
|
I agree! That's how I'm doing it now and it seems to be the most straightforward and logic implementation. |
Before 0.6 I called
LocalSystemDateTime(...).as_utc()
to get anUTCDateTime
.Now
UTCDateTime
Is calledInstant
andLocalSystemDateTime
is calledSystemDateTime
,however it's unclear how I can get it to normalize.
I thought the Idea is to normalize timestamps to
Instant
and do all calculations there.Is this a misunderstanding from my side?
The text was updated successfully, but these errors were encountered: