time icon indicating copy to clipboard operation
time copied to clipboard

How to ask whether an event is more than n minutes ago?

Open trispo opened this issue 4 years ago • 2 comments

Hi,

I'm looking for a way to check, whether a previous call to my networking api was sent more than 5 minutes ago. I thought trying your project for this would be a nice start. On first launch, there was now call at all so I usually use Foundation.Date.distantPast. I ended with the following code ...

let now = Clock.system.thisMinute()
let lastUpdate = Absolute<Minute>(region: .current, instant: .init(date: .distantPast))

let differenceSinceLastUpdate = now - lastUpdate

guard differenceSinceLastUpdate.minutes > 5 else { return }

... but this seems to be wrong as differenceSinceLastUpdate.minutes is not the value I expected it to be.

What is the right way to get the number of minutes between two dates?

trispo avatar May 03 '20 14:05 trispo

@trispo This is now almost 18 months later... but the difference returns values of each varying unit, and not a total of that type of unit.

So a difference of 1 hour and 30 minutes will return

hours = 1
minutes = 30

So you have to tally it up yourself. I've requested some form of API in #57 to do this for library users, and we'll have to see if it's able to be expressed correctly.

Mordil avatar Oct 20 '21 06:10 Mordil

@Mordil thanks, I understand

trispo avatar Oct 22 '21 13:10 trispo

Hi @trispo! The method you're looking for in 1.0 is called Fixed.differenceInWholeMinutes(to:). That will give you a difference between two fixed values that is only in minutes.

davedelong avatar Mar 06 '24 02:03 davedelong