swift - NSFetchRequest by date -
i have method below pass nsdate() results today. i've trying results midnight midnight doesnt seem work. i've noticed predicate outputs this:
"date >= cast(460249200.000000, "nsdate") , date <= cast(460289100.000000, "nsdate")" presumed 460249200.000000 unix timestamp if @ day correct year 1984?
func getall(dayofrecords: nsdate) -> nsarray { var calendar = nscalendar.currentcalendar() var components = calendar.components(.calendarunitmonth | .calendarunitday | .calendarunityear, fromdate: dayofrecords); var midnightcomponents = nsdatecomponents(); midnightcomponents.day = components.day; midnightcomponents.month = components.month; midnightcomponents.year = 2015; midnightcomponents.hour = 0; var midnightdate = calendar.datefromcomponents(midnightcomponents); var twentyfourhourslater = nsdatecomponents(); twentyfourhourslater.day = components.day; twentyfourhourslater.month = components.month; twentyfourhourslater.year = 2015; twentyfourhourslater.hour = 11; twentyfourhourslater.minute = 59; var twentyfourhoursahead = calendar.datefromcomponents(twentyfourhourslater); var predicate = nspredicate(format: "(date >= %@) , (date <= %@)", midnightdate!, twentyfourhoursahead!); var fetchrequest = nsfetchrequest(entityname: "movement") //filter seems 1987!!!??? fetchrequest.predicate = predicate; fetchrequest.predicate = predicate; var results:nsarray = managedobjectcontext!.executefetchrequest(fetchrequest, error: nil)! return results; }
for date properties, core data stores number of seconds since 1 january 2001, gmt in sqlite database, i.e. nsdate
property timeintervalsincereferencedate
returns.
with
let date = nsdate(timeintervalsincereferencedate: 460249200.0)
you can verify first timestamp in sqlite query 2015-08-02 23:00:00 +0000
, 2015-08-03 00:00:00
in gmt+1 time zone.
so start date correctly set midnight on august 3. end date set same day @ 11:59, (approximately) noon , not 24 hours later. may want set 23:59, or better 00:00 @ following day.
this can simplified to
let calendar = nscalendar.currentcalendar() let startdate = calendar.startofdayfordate(dayofrecords) let enddate = calendar.datebyaddingunit(.calendarunitday, value: 1, todate: startdate, options: nil)! let predicate = nspredicate(format: "(date >= %@) , (date < %@)", startdate, enddate);
Comments
Post a Comment