How can i put Day Event on particular Date using Tapku Library? - ios

I have used TKCalendarDayView for showing day events on Specific date but problem is when i put any events on it it always display in current date.
so can anybody help me to sort out this problem using Tapku Library?
Thank you in Advanced.

SOLUTION
There has another way to solve this problem
As you see, in the methods below:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"Day View", #"");
self.data = #[
#[#"Meeting with five random dudes", #"Five Guys", #48, #0, #50, #30],
#[#"Unlimited bread rolls got me sprung", #"Olive Garden", #-24, #0, #-22, #0],
#[#"Appointment", #"Dennys", #15, #0, #18, #0],
#[#"Turkey Time...... oh wait", #"Chick-fela", #14, #0, #19, #0]];
}
self.data is loaded.
Note that, there has no method to add an event to a specific date. But we can add an event like this: #[#"Meeting with five random dudes", #"Five Guys", #48, #0, #50, #30] in this #48 means the event at the day after tomorrow.
and
#[#"Unlimited bread rolls got me sprung", #"Olive Garden", #-24, #0, #-22, #0] in this #-24 means the event is before today(yesterday).
So, first you should calculate the interval between the specific date using following code.
- (NSArray *)getTimeFromNow:(NSDate *)startDate endDateTime:(NSDate *)endDate
{
NSDate *todayDate = [NSDate date];
NSTimeInterval startFromNowSeconds = [startDate timeIntervalSinceDate:todayDate];
NSTimeInterval endFromNowSeconds = [endDate timeIntervalSinceDate:todayDate];
NSNumber *startHour = [NSNumber numberWithInt:startFromNowSeconds / (60 * 60)];
NSNumber *startMinute = [NSNumber numberWithInt:startFromNowSeconds / 60 - startHour.intValue * 60];
NSNumber *endHour = [NSNumber numberWithInt:endFromNowSeconds / (60 * 60)];
NSNumber *endMinute = [NSNumber numberWithInt:endFromNowSeconds / 60 - endHour.intValue * 60];
return #[startHour, startMinute, endHour, endMinute];
}
Below code should be commented:
//if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]] == NSOrderedAscending) return #[];
//if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24*60*60]] == NSOrderedDescending) return #[];

Related

ios8 - seeing if a record is past or future

I'm parsing an array and want to weed out records from before now.
I've got this code:
int i = 0;
for (i=0; i < tempArray.count; i++) {
currentObjectArray = tempArray[i];
NSString *dateString = [currentObjectArray valueForKey:#"ScheduleTime" ];
NSDate *schedule = [dateFormatter dateFromString:dateString];
NSLog(#"schedule: %lu", (unsigned long) schedule );
NSLog(#"now: %lu", (unsigned long)[NSDate date] );
NSTimeInterval distanceBetweenDates = [schedule timeIntervalSinceDate: schedule];
NSLog(#"distanceBetweenDates: %lu", (unsigned long)distanceBetweenDates );
result:
schedule: 16436914033316069376
now: 6174145184
distanceBetweenDates: 0
but the two resulting numbers are incorrect, thus the result is incorrect. Could someone please tell me what I'm doing wrong? Thanks
UPDATE: Thanks to answers below, I've updated my code as follows:
NSString *dateString = [currentObjectArray valueForKey:#"ScheduleTime" ];
NSDate *schedule = [dateFormatter dateFromString:dateString];
float s = [schedule timeIntervalSince1970];
NSLog(#" %f", s );
NSTimeInterval timeInterval = [currentObjectArray timeIntervalSinceNow];
if (timeInterval > 0) {
NSLog(#"YES");
} else {
NSLog(#"NO");
The schedule date format is: "YYYY-MM-DD'T'HH:mm:ss"
Update2: I forgot to add in the local time zone. Thanks for all the help.
These two lines don't do what you think they do.
NSLog(#"schedule: %lu", (unsigned long) schedule );
NSLog(#"now: %lu", (unsigned long)[NSDate date] );
Performing this type cast is asking the system to return you an unsigned long representation of the pointer to the object, which is a memory address and not at all related to time. It is likely that you actually wanted to ask for the NSTimeInterval values.
NSLog(#"schedule: %f", [schedule timeIntervalSince1970] );
NSLog(#"now: %f", [[NSDate date] timeIntervalSince1970] );
Compounding your confusion, you have also misunderstood this line:
NSTimeInterval distanceBetweenDates = [schedule timeIntervalSinceDate: schedule];
You are asking the system to tell you how many seconds are between schedule and schedule; which is obviously always going to be 0 since they are identical. Instead, you probably meant one of:
NSTimeInterval distanceBetweenDates1 = [[NSDate date] timeIntervalSinceDate:schedule];
NSTimeInterval distanceBetweenDates2 = [schedule timeIntervalSinceDate:[NSDate date]];
You only need to check if the time interval is negative or positive to determine if a time comes before or after, respectively.
- (BOOL)isDateInPast:(NSDate *)date {
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
if (timeInterval < 0) {
return YES;
} else {
return NO;
}
}
Note that this doesn't check the condition where the time interval is 0 (the present).
EDIT: Adding to this for further clarification. Your loop code could look something like this...
NSMutableArray *datesOnlyInFuture = [NSMutableArray array];
for (NSDate *date in dateArray) {
if (![self isDateInPast:date]) {
[datesOnlyInFuture addObject:date];
}
}
NSLog(#"Future only dates: %#", datesOnlyInFuture);
This will actually create a new array for you. Clearly plenty of optimizations should be made. For example timeIntervalSinceNow is going to be different each time it is called, so you could pass in a constant date that is set before the loop starts so you're always checking against the same date/time.

Load an image on specific date and time in iOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My question is: how do I load a specific image on a specific date and time in iOS?
I have searched the net but did not find anything useful.
I have a list of images in an imageArray and want every image to be shown on a specific date, time and order.
Fx. say I want to load MyImage on MyDateAndTime. How can I do this?
Image 1 - DateAndTime 1
Image 2 - DateAndTime 2
Image 3 - DateAndTime 3
Any suggestions is appreciated, please provide some source code if possible.
I put simple logic, edit it as per your requirement otherwise if you have any query related to my answer then please tells to me.
Best way is store your image with Name of dateTime (dd_MM_yyyy_HH_mm_ss) and access image name such like,
NSString *imageName
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd_MM_yyyy_HH_mm_ss"];
imageName = [NSString stringWithFormat:#"%#.png", [formatter stringFromDate:[NSDate date]]]; // here you can set specific dateTime, i putted current dateTime
Here you get imageName such like 19_10_2013_6_13_21.png
And by this image name you can get image from bundle or document directory.
If all you want is to show a different image every minute, use this, otherwise skip below to see helpful date information.
NSTimeInterval secondsInMinute = 60;
[NSTimer timerWithTimeInterval:secondsInMinute target:self selector:#selector(minuteChanged:) userInfo:nil repeats:YES];
- (void)minuteChanged:(id)sender {
// change image here
}
You question could have many different answers, do you want to create this date dynamically? or is it a a predefined date? One solution is to get get the timeInterval of the date you are looking for.
NSDate* rightNow = [NSDate date];
NSTimeInterval timeInterval = [rightNow timeIntervalSince1970];
// since time intervals are in seconds we can just append the
// date as easily as adding time
NSInteger secondsInMinute = 60;
NSInteger minutesInHour = 60;
NSInteger hoursInDay = 24;
NSInteger daysInWeek = 7;
NSInteger secondsInWeek = secondsInMinute * minutesInHour * hoursInDay * daysInWeek;
timeInterval = timeInterval + secondsInWeek;
NSDate* aWeekInFuture = [NSDate dateWithTimeIntervalSince1970:timeInterval];
that i would say is the easiest to under stand to set a date, but you could also use components to set a future date dynamically. This leads into some problems but here is how it's done.
NSDate* rightNow = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* dateCompenents = [calendar components:(NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:rightNow];
[dateCompenents setDay:dateCompenents.day + 7];
NSDate* aWeekInFuture = [calendar dateFromComponents:dateCompenents];
to help explain this, here is some console logs
(lldb) po rightNow
$0 = 0x0b933440 2013-10-19 12:43:55 +0000
(lldb) po aWeekInFuture
$1 = 0x0ba32a60 2013-10-26 04:00:00 +0000
you see how the date is accurate for the day, year, month, but look at the exact time, the current time (right now) is 12:43:55 but the week in he future is 4:00:00 this is because i did not ask for the NSMinutesCalendarUnit, NSHoursCalendarUnit, NSSecondsCalendarUnit... so if i wanted a perfect date that would be inadequate unless i ask for every single thing, but you specifically may not need to be so accurate in fact you may even want to set your own time.
Now if you want a static date, a date the user enters, you will need to use NSDateFormatter example below
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:currentCalendar];
[dateFormatter setDateFormat:#"mm/dd/yyyy"];
NSDate* birthdayDate = [dateFormatter dateFromString:#"10/05/2013"];
Now you wanted to know how would you know if today is the specified date that is saved. Lets say you stored the date in NSUserDefaults or on a server or some place, the easiest way to compare the dates is with the compare function of an NSDate
NSDate* rightNow = [NSDate date];
NSDate* storedDate = [[NSUserDefaults standardUserDefaults] objectForKey#"storedDate"] // some date from server, or UserDefaults
NSComparisonResult = [rightNow compare:storedDate];
this is a bit inadequate since it test for perfection but it will return values of NSOrderedSame if they are equal, NSOrderedDescending if storedDate is behind rightNow, and NSOrderedAscending if storedDate is in front of rightNow. This is all specific down to the time interval. If you just want a generic day, you will have to test it via components
NSDate* rightNow = [NSDate date];
NSDate* birthdayDate = [[NSUserDefaults standardUserDefaults] objectForKey#"birthday"]
NSDateComponents* todayComponents = [currentCalendar components:(NSDayCalendarUnit | NSMonthCalendarUnit) fromDate:rightNow];
NSDateComponents* birthdayComponents = [currentCalendar components:(NSDayCalendarUnit | NSMonthCalendarUnit) fromDate:birthdayDate];
BOOL dayIsTheSame = ( todayComponents.day == birthdayComponents.day );
BOOL monthIsTheSame = ( todayComponents.month == birthdayComponents.month );
BOOL todayIsBirthday = ( dayIsTheSame && monthIsTheSame );
if (todayIsBirthday) {
[self.imgViewBirthday setImage[UIImage imageNamed:#"cake.png"]];
}
In your question you specified an array of images, lets say you have a different image depending on which hour it is, or which minute, you would use the component, todayComponent.minute after asking for the NSMinutesCalendarUnit as the index of this array;
UIImage* currentImageToDisplay = [self.arrayOfImage objectAtIndex:todayComponent.minute];
self.imageView.image = currentImageToDisplay;
References:
NSDate,
NSDateFormatter,
NSDateComponents,
NSCalendar,
NSTimer
If I understood your problem now, one fancy approach using a recursive block you might check out is this:
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSArray* dates = #[#1, #1, #1, #1, #1, #1, #1, #1, #1];
NSArray* urls = #[#"A", #"B", #"C", #"D", #"E", #"F", #"G", #"H", #"I"];
NSEnumerator* dateIter = [dates objectEnumerator];
NSEnumerator* urlIter = [urls objectEnumerator];
typedef void(^block_t)(NSEnumerator* dateIter, NSEnumerator* urlIter);
block_t asyncFunc;
__block __weak block_t _asyncFunc = asyncFunc = ^(NSEnumerator* dateIter, NSEnumerator* urlIter) {
NSNumber* date = [dateIter nextObject];
NSString* url = [urlIter nextObject];
if (date != nil && url != nil) {
double delayInSeconds = [date doubleValue];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_global_queue(0, 0), ^(void){
NSLog(#"%#", url);
_asyncFunc(dateIter, urlIter);
});
}
else {
printf("\n");
return;
}
};
// start:
asyncFunc(dateIter, urlIter);
sleep (10);
}
return 0;
}
Note:
The "dates" are actually "delays" and the URLs are actually just strings in this example. You should be able to adjust this as you like. Of course, NSLog(#"%#", url); would actually display your image.
Also, the block asyncFunc is asynchronous!

NSDateFormatter memory leak?

I have a problem with NSDateFormatter dateFromString: method. There is an array of dictionaries with string dates and I want to convert them to NSDates. But there is a huge heap growth every time I call this method (I need to call it more than once, cause it's an "update" method). Also I call it in background thread and ARC is on. What am I doing wrong? Any help, please.
UPDATE:
Full code of the function:
- (GraphController *) initGraphWithData: (NSArray *) points forInterval: (GraphInterval) interval andFrame: (CGRect) frame gestures: (BOOL) gestures secondGraph: (NSArray *) secondPoints graphNames: (NSArray *) graphNames
{
self = [super init];
_calendar = [NSCalendar currentCalendar];
_secondPoints = secondPoints;
_graphNames = graphNames;
[self.view setFrame:frame];
_points = [[NSMutableArray alloc] init];
double minValue = HUGE_VALF, maxValue = -HUGE_VALF;
NSDate *minDate = [NSDate date], *maxDate = [NSDate dateWithTimeIntervalSince1970:0];
NSMutableDictionary *datesTable = [[NSMutableDictionary alloc] init];
int index = 0;
for(NSArray *pointArray in points){
NSDictionary *point = pointArray[0];
NSMutableDictionary *newPoint = [[NSMutableDictionary alloc] initWithDictionary:point];
// convert date
NSString *dateString = (NSString *)[point objectForKey:#"date"];
NSLog(#"dateString to convert: %#", dateString);
[_dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [_dateFormatter dateFromString: dateString];
[newPoint setObject: date forKey:#"date"];
// convert numbers
[newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[point objectForKey:#"value"]) floatValue]] forKey:#"value"];
if(secondPoints)
[newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[secondPoints[index] objectForKey:#"value"]) floatValue]] forKey:#"secondValue"];
[newPoint setObject: [NSNumber numberWithInt:index] forKey:#"index"];
// min and max
if([[newPoint objectForKey:#"value"] floatValue] < minValue)
minValue = [[newPoint objectForKey:#"value"] floatValue];
if([[newPoint objectForKey:#"value"] floatValue] > maxValue)
maxValue = [[newPoint objectForKey:#"value"] floatValue];
// check second value
if(self.secondPoints){
if([[newPoint objectForKey:#"secondValue"] floatValue] < minValue)
minValue = [[newPoint objectForKey:#"secondValue"] floatValue];
if([[newPoint objectForKey:#"secondValue"] floatValue] > maxValue)
maxValue = [[newPoint objectForKey:#"secondValue"] floatValue];
}
if([[newPoint objectForKey:#"date"] timeIntervalSince1970] > [maxDate timeIntervalSince1970])
maxDate = [newPoint objectForKey:#"date"];
if([[newPoint objectForKey:#"date"] timeIntervalSince1970] < [minDate timeIntervalSince1970])
minDate = [newPoint objectForKey:#"date"];
[self.points addObject:newPoint];
[datesTable setObject:newPoint forKey: [[NSNumber numberWithDouble:[date timeIntervalSince1970]] stringValue] ];
index++;
}
// set draw parameters
_drawVars = [[NSMutableDictionary alloc] init];
NSDate *startSearchDate;
switch (interval) {
case GraphIntervalWeek:
startSearchDate = [maxDate dateByAddingTimeInterval:-7*24*3600];
break;
case GraphIntervalMonth:
startSearchDate = [maxDate dateByAddingTimeInterval: -31*24*3600];
break;
case GraphIntervalSixMonths:
startSearchDate = [maxDate dateByAddingTimeInterval:-6*31*24*3600];
break;
case GraphIntervalYear:
startSearchDate = [maxDate dateByAddingTimeInterval:-365*24*3600];
break;
case GraphIntervalAllTime:
break;
default:
break;
}
NSMutableDictionary *searchPoint;
while(searchPoint == nil && [startSearchDate timeIntervalSince1970] > [minDate timeIntervalSince1970]){
searchPoint = [datesTable objectForKey:[[NSNumber numberWithDouble:[startSearchDate timeIntervalSince1970]] stringValue]];
startSearchDate = [startSearchDate dateByAddingTimeInterval:-24*3600];
}
if(searchPoint != nil && interval != GraphIntervalAllTime)
[self.drawVars setObject:[searchPoint objectForKey:#"index"] forKey:#"startDrawIndex"];
else
[self.drawVars setObject:[NSNumber numberWithInt:0] forKey:#"startDrawIndex"];
[self.drawVars setObject:[[NSNumber numberWithFloat:minValue] copy] forKey:#"minValue"];
[self.drawVars setObject:[[NSNumber numberWithFloat:maxValue] copy] forKey:#"maxValue"];
[self.drawVars setObject:minDate forKey:#"minDate"];
[self.drawVars setObject:maxDate forKey:#"maxDate"];
[self.drawVars setObject:datesTable forKey:#"datesTable"];
// set drawImageView
_drawArea = [[UIImageView alloc] initWithFrame: frame];
[self.view addSubview:self.drawArea];
// set overlayImageView for fingerInpect
_inspectOverlay = [[UIImageView alloc] initWithFrame:frame];
[self.view addSubview:self.inspectOverlay];
// set hintUIView for fingerInspect
_hint = [[Hint alloc] initWithFrame:frame];
[self.hint initHint];
self.hint.hidden = YES;
[self.drawArea addSubview:self.hint];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panHandler:)];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
if(gestures)
[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:longPressRecognizer];
return self;
}
I see you are using Heapshot analysis. Good. Here are some more details:
http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/
Now, hopefully, each one of your Heapshot iterations represents doing something in your app that should return it back to the base state. Opening and closing a document, for example.
If that is the case, your data is showing a 500K-600K growth per iteration. That is quite a bit.
Note that trying to analyze the last iteration is generally useless; many of those objects will exist because of whatever the current state of the app is. You really want to focus on that 2nd or 4th iteration.
As for why that specific date or date formatter is leaking, turn on reference count event tracking and then review the retain/release events for one of the leaked objects. There will be some number of extra retains.
Something you are doing right is init the NSDateFormatter before entering the loop. If you are expecting the same date format (MM/dd/yyyy) you can put in the same place you init it as well. It can be that you are allocating too many objects, so I could advise putting everything inside an #autoreleasepool {} (aka: the content of the loop inside the autorelease).
I am seeing you are allocating the *newPoint inside the for and then you are setting the date to that object. What do you do after?

Events are not getting displayed after reloaddata in Timelineview of Tapku Calendar

Can any one help me with the issue that I am experiencing?
I am using the Tapku Calendar Library Day View in order to display events. Events are getting displayed properly when the calendar loads for the first time. If I change the date lets say either to yesterday or tomorrow, the calendar is not displaying the events.
I have implemented the following events.
- (void)calendarDayTimelineView:(TKCalendarDayView *)calendarDay didMoveToDate:(NSDate *)date
{
//Here is my logic to pull the data from db server.
//After this I am calling the method below.
[self.dayView reloadData];
}
- (NSArray *) calendarDayTimelineView:(TKCalendarDayView*)calendarDayTimeline eventsForDate:(NSDate *)eventDate{
self.myAppointments = nil;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Tasks" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.myAppointments = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]] == NSOrderedAscending) return #[];
if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24*60*60]] == NSOrderedDescending) return #[];
NSDateComponents *info = [[NSDate date] dateComponentsWithTimeZone:calendarDayTimeline.timeZone];
info.second = 0;
NSMutableArray *ret = [NSMutableArray array];
for(Tasks *apt in self.myAppointments){
TKCalendarDayEventView *event = [calendarDayTimeline dequeueReusableEventView];
if(event == nil) event = [TKCalendarDayEventView eventView];
event.identifier = nil;
event.titleLabel.text = apt.task_subject;
if ( [allTrim(apt.location) length] != 0 )
{
event.locationLabel.text = apt.location;
}
NSDate *startDate = apt.task_start;
NSDate *endDate = apt.task_end;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:startDate];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
info.hour = hour;
info.minute = minute;
event.startDate = [NSDate dateWithDateComponents:info];
components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:endDate];
hour = [components hour];
minute = [components minute];
info.hour = hour;
info.minute = minute;
event.endDate = [NSDate dateWithDateComponents:info];
[ret addObject:event];
}
return ret;
}
I have debugged the code, the data is getting assigned to the events, however I am not able to view any thing on the calendar.
Pls.. help in fixing up this issue.
Regards,
g.v.n.sandeep
There has another way to solve this problem.
As you see, in the methods below:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"Day View", #"");
self.data = #[
#[#"Meeting with five random dudes", #"Five Guys", #960, #0, #1000, #30],
#[#"Unlimited bread rolls got me sprung", #"Olive Garden", #7, #0, #12, #0],
#[#"Appointment", #"Dennys", #15, #0, #18, #0],
#[#"Hamburger Bliss", #"Wendys", #15, #0, #18, #0],
#[#"Fishy Fishy Fishfelayyyyyyyy", #"McDonalds", #5, #30, #6, #0],
#[#"Turkey Time...... oh wait", #"Chick-fela", #14, #0, #19, #0],
#[#"Greet the king at the castle", #"Burger King", #19, #30, #100, #0]];
}
self.data is loaded.
We can move the loading process to "viewWillAppear" methods to update data if you do some writing.
Note that, there has no method to add an event to a specific date. But we can add an event like this: #[#"Fishy Fishy Fishfelayyyyyyyy", #"McDonalds", #48, #00, #50, #0].
#48 means the event at the day after tomorrow.
So, first you should calculate the interval between the specific date and self.dayview.date(today).
Following codes may help:
- (NSArray *)getTimeFromNow:(NSDate *)startDate endDateTime:(NSDate *)endDate
{
NSDate *todayDate = [NSDate date];
NSTimeInterval startFromNowSeconds = [startDate timeIntervalSinceDate:todayDate];
NSTimeInterval endFromNowSeconds = [endDate timeIntervalSinceDate:todayDate];
NSNumber *startHour = [NSNumber numberWithInt:startFromNowSeconds / (60 * 60)];
NSNumber *startMinute = [NSNumber numberWithInt:startFromNowSeconds / 60 - startHour.intValue * 60];
NSNumber *endHour = [NSNumber numberWithInt:endFromNowSeconds / (60 * 60)];
NSNumber *endMinute = [NSNumber numberWithInt:endFromNowSeconds / 60 - endHour.intValue * 60];
return #[startHour, startMinute, endHour, endMinute];
}
Below should be commented:
if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]] == NSOrderedAscending) return #[];
if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24*60*60]] == NSOrderedDescending) return #[];
But pay attention to the timezone. I think it's not very easy to return a right time array calculated above.
More over, i think it is essential necessary to add the method that add event to a specific day in DayViewController.
Note: as i have do some coding, if your time is #900 hours from now, the event will not be showed in dayview... but #500 hours it works well.
What a terrible designs here!
Waiting a more complete dayviewcontroller ...

NSDate to Tick conversion

I wanted to convert a date (nsdate) to tick values. Tick values are (1 Tick = 0.1 microseconds or 0.0001 milliseconds) since 1 Jan 0001 00:00:00 GMT. NSDate has functions like timeIntervalSince1970. So, how do I convert it?
I would like to share my experience:
I tried to find the seconds from 01/01/0001 and then multiply by 10,000,000. However, it gave me wrong results. So, I found out that 01/01/1970 is 621355968000000000 ticks from 01/01/0001 and used the following formula along with timeIntervalSince1970 function of NSDate.
Ticks = (MilliSeconds * 10000) + 621355968000000000
MilliSeconds = (Ticks - 621355968000000000) / 10000
Here is the outcome:
+(NSString *) dateToTicks:(NSDate *) date
{
NSString *conversionDateStr = [self dateToYYYYMMDDString:date];
NSDate *conversionDate = [self stringYYYYMMDDToDate:conversionDateStr];
NSLog(#"%#",[date description]);
NSLog(#"%#",[conversionDate description]);
double tickFactor = 10000000;
double timeSince1970 = [conversionDate timeIntervalSince1970];
double doubleValue = (timeSince1970 * tickFactor ) + 621355968000000000;
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
NSNumber *nsNumber = [NSNumber numberWithDouble:doubleValue];
return [numberFormatter stringFromNumber:nsNumber];
}
Likewise, to convert from tick to date:
//MilliSeconds = (Ticks - 621355968000000000) / 10000
+(NSDate *) ticksToDate:(NSString *) ticks
{
double tickFactor = 10000000;
double ticksDoubleValue = [ticks doubleValue];
double seconds = ((ticksDoubleValue - 621355968000000000)/ tickFactor);
NSDate *returnDate = [NSDate dateWithTimeIntervalSince1970:seconds];
NSLog(#"%#",[returnDate description]);
return returnDate;
}

Resources