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

Date - Date order issue #306

Closed
fever324 opened this issue Oct 17, 2016 · 7 comments
Closed

Date - Date order issue #306

fever324 opened this issue Oct 17, 2016 · 7 comments
Labels
Milestone

Comments

@fever324
Copy link

If we have (not sure if it will compile, just want to show that D2 is later than D1)
D1 = "1/2/2015"
D2 = "1/2/2016"

I was expecting D2 - D1 would give me 365 days. But in fact in code we have to use D1 - D2 to get the actual time interval.

Is this intended behavior?

Thank you!

@malcommac
Copy link
Owner

Hi, you can use .in() method an specify the .day as parameter.

let D1 = try! "1/2/2015".date(format: .custom("dd/MM/yyyy"))
let D2 = try! "1/2/2016".date(format: .custom("dd/MM/yyyy"))
let v = (D2 - D1).in(.day) // 365

@fever324
Copy link
Author

I believe this will crash because in here we specifies the lhs is the start of the interval instead of the end of the interval.

Please try run the code above and let me know!

@malcommac
Copy link
Owner

I've tried it right now; do you encounter a crash?

@fever324
Copy link
Author

Hi @malcommac I tested your code and it works fine.
But I don't understand why using Date instead of SwiftDate.DateInRegion in the following code crashes

let today = Date()
let tomorrow = Date() + 1.days
print(tomorrow - today) // This crashes here

@malcommac
Copy link
Owner

malcommac commented Oct 17, 2016

I've tried using DateInRegion instead of Date and it still works fine, in the comments what I get

let today = DateInRegion()
let tomorrow = DateInRegion() + 1.days
let diffInDays = (tomorrow - today).in(.day) // -> 1
print(tomorrow - today) // -> in seconds = 86400.0012260079

Can you post your crash?

@fever324
Copy link
Author

The problem is that I have to use Date().
The crash message is

fatal error: Reverse intervals are not allowed: file /Users/hongfeili/Documents/GitHub/Just Did It/Carthage/Checkouts/SwiftDate/Sources/SwiftDate/DateTimeInterval.swift, line 69

I still think the Date+Math order should be reversed or consider absolute like how it is done in DateInRegion+Math

Is the order of minus between Dates intended?

malcommac added a commit that referenced this issue Oct 18, 2016
malcommac added a commit that referenced this issue Oct 18, 2016
This reverts commit 332902e.
@malcommac
Copy link
Owner

Ok should be fixed 👍

@malcommac malcommac added bug and removed invalid labels Oct 18, 2016
@malcommac malcommac modified the milestones: 3.0.7, 4.0.7 Oct 18, 2016
@malcommac malcommac mentioned this issue Oct 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants