Slide 1

Slide 1 text

Dodatko Alexander November 2012

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

How many months does the year consist of?

Slide 4

Slide 4 text

What is the first day of the year?

Slide 5

Slide 5 text

What day does a new week start on?

Slide 6

Slide 6 text

Right, but did you know that ...

Slide 7

Slide 7 text

Jewish calendar consists of 13 months. And month #6 is a leaping one.

Slide 8

Slide 8 text

In Japan the calendar may return to year #1 at the day of the emperor's death

Slide 9

Slide 9 text

In Russia week starts on Monday. In the USA the first day is Sunday

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Dates must look in the way the user expects

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

-(NSDate*)parseDate:( NSString* )date_ { NSDateFormatter* df_ = [ NSDateFormatter new ]; df_.dateFormat = @"yyyy-MM-dd"; return [ df_ dateFromString: date_ ]; } The Typical Solution

Slide 15

Slide 15 text

-(NSDate*)parseDate:( NSString* )date_ { NSDateFormatter* df_ = [ NSDateFormatter new ]; df_.dateFormat = @"yyyy-MM-dd"; return [ df_ dateFromString: date_ ]; } WRONG !

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Use en_US_POSIX locale for dates from the network Use Gregorian calendar too

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Do not forget to set the same Locale for both NSCalendar and NSDateFormatter

Slide 20

Slide 20 text

Or let my library do it for you dodikk / ESLocale

Slide 21

Slide 21 text

Ok. How about SQLite?

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

SELECT SUM( Visits) FROM VisitsLog WHERE Date BETWEEN x AND y GROUP BY week

Slide 24

Slide 24 text

What should I take as week ?

Slide 25

Slide 25 text

SELECT SUM( Visits) FROM VisitsLog WHERE Date BETWEEN x AND y GROUP BY Strftime('%Y-%W', Date )

Slide 26

Slide 26 text

SELECT SUM( Visits) FROM VisitsLog WHERE Date BETWEEN x AND y GROUP BY Strftime('%Y-%W', Date )

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

SELECT SUM( Visits) FROM VisitsLog WHERE Date BETWEEN x AND y GROUP BY ObjcFormatDate('YYYY-ww', Date, 'en_US_POSIX' )

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

int sqlite3_create_function( dbHandle, "ObjcFormatDate", 3, //int nArg, SQLITE_UTF8, NULL, // sqlite user data functionPointer, NULL, NULL // for aggregates );

Slide 31

Slide 31 text

Plan of attack Convert C strings to NSString Convert date string to NSDate Format NSDate using locale

Slide 32

Slide 32 text

SQLite uses ANSI format yyyy-MM-dd

Slide 33

Slide 33 text

Demo

Slide 34

Slide 34 text

One More Thing

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

1000 times slower than strftime 10K records Creating NSDateFormatter on-the-fly

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Same speed as strftime Same 10K records Singletone NSDateFormatter

Slide 39

Slide 39 text

Thread Safety

Slide 40

Slide 40 text

dodikk / ESLocale

Slide 41

Slide 41 text

Contacts Oleksandr Dodatko mail/jabber : [email protected] Skype : alexander.dodatko.work Twitter : @dodikk88 Github : https://github.com/dodikk https://github.com/EmbeddedSources

Slide 42

Slide 42 text

No content