Skip to content
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

Getting a Foundation Date from Value #36

Closed
jdmcd opened this issue Mar 22, 2020 · 3 comments
Closed

Getting a Foundation Date from Value #36

jdmcd opened this issue Mar 22, 2020 · 3 comments

Comments

@jdmcd
Copy link

jdmcd commented Mar 22, 2020

Hey there,

Thank you for this fantastic library!! Quick question that I couldn't find in the docs (hopefully I didn't just miss it).

I need to calculate a date 5 minutes from now in UTC. Here's what I'm doing:

let utc = Clock.system.converting(to: TimeZone(identifier: "UTC")!).thisMinute()
let plusFiveMinutes = (utc + .minutes(5)).firstInstant.date

I'm clear on everything up until .firstInstant.date - that was the only way I could find to pull a Foundation Date out of my addition operation. Does that seem right?

Thank you again!

@davedelong
Copy link
Owner

Yep, this is probably correct for what you're trying to do.

All calendar values represents ranges of possible instants. When you're asking for "5 minutes from now", you're getting back a value that contains all possible Instants in that 5-minute range.

Technically, any Instant value within that range meets the criteria of "occurs five minutes from now", because your accuracy is only down to the minute (because you used .thisMinute()).

The firstInstant of an absolute Value is therefore the first point in time that meets the criteria of being "five minutes from now".

And then once you've got an Instant, you can easily convert it back to a Foundation.Date by using its .date value.


All this to say that yes, this is correct. 😁

@jdmcd
Copy link
Author

jdmcd commented Mar 22, 2020

@davedelong thanks so much for the quick response! This actually helps tighten my understanding quite a bit, I think I was missing the key point about values being ranges, not singular points. Thank you!!

@ricobeck
Copy link

Thanks! That helped me a lot.

Did I miss this in the documentation? If not I'd suggest that it should be mentioned somewhere explicitly because after reading in the docs I was under the impression that converting back to Foundation.Date is impossible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants