Александр Додатко - Работа с датами в ObjectiveC и SQLite

848 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
848
On SlideShare
0
From Embeds
0
Number of Embeds
145
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Александр Додатко - Работа с датами в ObjectiveC и SQLite

  1. 1. Dodatko Alexander November 2012
  2. 2. How many months does the year consist of?
  3. 3. What is the first day of the year?
  4. 4. What day does a new week start on?
  5. 5. Right, but did you know that ...
  6. 6. Jewish calendar consists of 13 months. And month #6 is a leaping one.
  7. 7. In Japan the calendar mayreturn to year #1 at the day of the emperors death
  8. 8. In Russia week starts on Monday.In the USA the first day is Sunday
  9. 9. Dates must look in the way the user expects
  10. 10. The Typical Solution-(NSDate*)parseDate:( NSString* )date_{ NSDateFormatter* df_ = [ NSDateFormatter new ]; df_.dateFormat = @"yyyy-MM-dd"; return [ df_ dateFromString: date_ ];}
  11. 11. WRONG !-(NSDate*)parseDate:( NSString* )date_{ NSDateFormatter* df_ = [ NSDateFormatter new ]; df_.dateFormat = @"yyyy-MM-dd"; return [ df_ dateFromString: date_ ];}
  12. 12. Use en_US_POSIX locale for dates from the network Use Gregorian calendar too
  13. 13. Do not forget to setthe same Locale for both NSCalendar and NSDateFormatter
  14. 14. Or let my library do it for you dodikk / ESLocale
  15. 15. Ok. How about SQLite?
  16. 16. SELECT SUM( Visits)FROM VisitsLogWHERE Date BETWEENx AND yGROUP BY week
  17. 17. What should I take as week ?
  18. 18. SELECT SUM( Visits)FROM VisitsLogWHERE Date BETWEEN x AND yGROUP BYStrftime(%Y-%W, Date )
  19. 19. SELECT SUM( Visits)FROM VisitsLogWHERE Date BETWEEN x AND yGROUP BYStrftime(%Y-%W, Date )
  20. 20. SELECT SUM( Visits)FROM VisitsLogWHERE Date BETWEEN x AND yGROUP BYObjcFormatDate(YYYY-ww,Date, en_US_POSIX )
  21. 21. int sqlite3_create_function( dbHandle, "ObjcFormatDate", 3, //int nArg, SQLITE_UTF8, NULL, // sqlite user data functionPointer, NULL, NULL // for aggregates);
  22. 22. Plan of attackConvert C strings to NSStringConvert date string to NSDateFormat NSDate using locale
  23. 23. SQLite uses ANSI format yyyy-MM-dd
  24. 24. Demo
  25. 25. One More Thing
  26. 26. 1000 times slower than strftime 10K records Creating NSDateFormatter on-the-fly
  27. 27. Same speed as strftime Singletone NSDateFormatter Same 10K records
  28. 28. Thread Safety
  29. 29. dodikk / ESLocale
  30. 30. ContactsOleksandr Dodatkomail/jabber : dodikk88.tutor@gmail.comSkype : alexander.dodatko.workTwitter : @dodikk88Github : https://github.com/dodikk https://github.com/EmbeddedSources

×