ios - Why compareDate from NSCalendar seems to need to set an UTC timeZone to work properly? -


i create 2 dates following:

let date1 = stringtodate("2015-02-12 12:29:29")! let date2 = stringtodate("2015-02-11 19:18:49")!  func stringtodate(var datestring: string) -> nsdate? {     let dateformatter = nsdateformatter()     dateformatter.dateformat = "yyyy-mm-dd hh:mm:ss"     dateformatter.timezone = nstimezone(name: "utc")      return dateformatter.datefromstring(datestring) } 

as can see, 2 date different , not on same day.

to test if 2 dates on same day, use following method:

func issamedaythan(date1: nsdate, date2: nsdate) -> bool {     let calendar = nscalendar.currentcalendar()     calendar.timezone = nstimezone(abbreviation: "gmt+10")!     return calendar.comparedate(date1, todate: date2, tounitgranularity: .daycalendarunit) == .orderedsame } 

there don't precise timezone in calendar. local timezone of device set gmt+10.

in case, issamedaythan(date1, date2)-> true

if change timezone inferior or equal gmt+04, issamedaythan(date1, date2)-> false.

what don't understand result different depending on timezone, comparing 2 nsdate() , nsdate() has nothing time zone if i'm not wrong.

the timezone comes play because compare dates granularity timezone dependent. comparing against local representation of date. point in time model used describe nsdate doesn't know days , weeks. abstract standpoint (i.e. point in time same everywhere in universe) doesn't know seconds.

anyway, if compare == not need timezone. that's comparison independent local representation. if 2 points in time same equal. easy.

everything beyond straight == comparison has converted local units. not have use correct calendar, have use correct timezone well.

luckily there no calendars have days shorter or longer 24 hours. , there no timezones differ in seconds either. because know that, can see if dates within same minute easy calculation. e.g.:

int(date1.timeintervalsince1970 / 60) == int(date2.timeintervalsince1970 / 60) 

no calendar needed because (currently) don't have calendars have minutes not 60 seconds long. no timezone needed, because don't have timezones offsets differ in number of seconds.

but have few timezones have offsets fractions of hour. example india time zone has offset of +05:30. starting hours boundaries of granularity units timezone dependent.

if have 2 nsdates set 9:25 , 9:35 utc, in same hour if compare in timezone has offset not differ in number of minutes (e.g. 00 in +x:00). in same hour in utc, in same hour in utc+5:00 , utc-5:00.

but if compare in india time zone these 2 dates in different hours. because 9:25 utc in ist 2:55, , 9:35 utc 3:05 in ist.

in example comparing granularity of day. needs take timezones account. can still ignore calendar, because calendars use days 24 hours long.

but if compare granularity of week, month or year have take calendar account well. there calendars have totally different months. because 2 dates in same month in gregorian calendars doesn't mean in same month in hebrew calendars.


yes, it's complicated. , that's reason date calculation appear verbose. people try hide complexity behind fancy helper function. leads problems. aware of creation functions issameday().
each time compare date have make decision timezone , calendar use. if rely on helper functions miss 1 instance should compare against utc instead of local timezone.

tl;dr: if compare granularity should set correct calendar , correct timezone.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -