-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52fe95d
commit 69777ad
Showing
1 changed file
with
8 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69777ad
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.
This is completely wrong and error prone!
You should definitely consider using the standard Foundation date arithmetic capabilities.
let calendar = NSCalendar.currentCalendar() let comps = NSDateComponents() comps.year = 2016 comps.month = 2 calendar.rangeOfUnit(.Day, inUnit: .Month, forDate: calendar.dateFromComponents(comps)!).length
You can copy and paste it in a playground to test it a little bit.
69777ad
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.
It is actually 100% accurate.
It could be even shorter- but then with more division operations which would slow things down.
The point is not doing too much when drawing a frame. Using the built-in date arithmetic will definitely be slower. It requires more allocation, and many computations which we do not really need here.
If you want- you can set up a loop in the playground, between year -10000 and year 10000, or 10000000, and test my formula against the system-provided one. Remember to skip year 0 :)
69777ad
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.
Remember: The fact that you don't know how to do something simple because of "mistery" math, does not mean it's error prone. Somebody else did this for you in NSDate, and NSDate is great- but not for this purpose.
69777ad
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.
Sorry m8, i was too hasty and harsh :(
Out of curiosity, i'll try to profile performances for both implementations as soon as have some spare time: date methods should be quite optimized in Foundation.
Keep up the good work, can't wait for version 3!
69777ad
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.
For the other months it could be further optimized by having an indexed array by month.
But it's a thing of calculations only - it's about memory allocations which generally take a lot more time than a few math calculations. So the rule of thumb is "do not allocate in drawing code". This rule is cross-platform.
You can already try out version 3 - as it's in its final stages :-)
Just polishing stuff, mostly the demos.
@PhilJay and I have decided not to introduce any more breaking changes, leave those for the next version...