This question is basically about the custom date formatting. I have managed to develop a countdown application that manages to create an event which later can be viewed, customised or tracked by the customer. The important part is the count down timer that grabs seconds from db and displays the time remaining according to the date formatter. Until now I was using the default setting in app that the counter format was dd:hh:mm, apparently my customer wants to allow the app users to customise the way how the time is being displayed. So now the user can choose four options like yy:weeks:hh:mm or months:weeks:days:hours. I thought the best way would be to save the table four indexes to db as a string. My problem came out with the calendar.
conversionInfo = [sysCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSSecondCalendarUnit fromDate:today toDate:future options:0];
one = [NSString stringWithFormat:@"%d Days %d Hours %d Minutes %d Seconds %@", [conversionInfo day], [conversionInfo hour],[conversionInfo minute],[conversionInfo second], untilOrSince]; self.eventTimer.text = one;
conversionInfo = [sysCalendar components: NSHourCalendarUnit | NSSecondCalendarUnit fromDate:today toDate:future options:0];
one = [NSString stringWithFormat:@"%ld Seconds %@", (long)[conversionInfo second], untilOrSince];
Also if i do build the calendar with all units but only display seconds for example, then the countdown will only show the 59 seconds and not 343746545 seconds until
These calendar units can't be placed in an array for example, so how would i choose the calendar formatting if user chooses 2,4,5,6 which will be months:days:hours:minutes? I can't say components = [NSArray arrayWithObjects: [arrComponents objectAtIndex:2], [arrComponents objectAtIndex:2],nil];
Any ideas?
Thank you for all your answers, I appreciate them.
Before you reply I think of my own solution which may not be as detailed and understood as vikingosegundo but it works and it can be extended.
Firstly from the check table I store the four numbers as a string to database. This allows me to store the formatting info with the "event". Each time i want to get the formatting i needed to get the string and change to array of numbers:
int option = 0;
unsigned int unitFlags = 0;
NSString *input = self.detailItem.prefs;
NSArray *myWords = [input componentsSeparatedByString:@","];
for (int fuu = 0; fuu < myWords.count; fuu++) {
option = [[myWords objectAtIndex:fuu]intValue];
switch (option)
{
case 0:
unitFlags = unitFlags | NSYearCalendarUnit;
break;
case 1:
unitFlags = unitFlags | NSMonthCalendarUnit;
break;
case 2:
unitFlags = unitFlags | NSWeekCalendarUnit;
break;
case 3:
unitFlags = unitFlags | NSDayCalendarUnit;
break;
case 4:
unitFlags = unitFlags | NSHourCalendarUnit;
break;
case 5:
unitFlags = unitFlags | NSMinuteCalendarUnit;
break;
}
}
As you can see I'm building my unit flag with units according to whats in the array.
Now some how i need to build the calendar, but how do i know whats are the components and the formatting?
Here is the answer:
NSString *prularity = @"";
if ([future compare:today] == NSOrderedAscending ) {
conversionInfo = [sysCalendar components:unitFlags fromDate:future toDate:today options:0];
untilOrSince = @"since";
} else {
conversionInfo = [sysCalendar components:unitFlags fromDate:today toDate:future options:0];
untilOrSince = @"until";
}
Here i build my calendar with the selected units, I also count from or to the date.
Finally I just need to do the format according to what was selected:
for (int fuu = 0; fuu < myWords.count; fuu++) {
option = [[myWords objectAtIndex:fuu]intValue];
switch (option)
{
case 0:
if ([conversionInfo year] >= 2 || [conversionInfo year] == 0) {
prularity = @"YEARS";
} else {
prularity = @"YEAR";
}
one = [one stringByAppendingString:[NSString stringWithFormat:@"%ld %@ ", (long)[conversionInfo year], prularity]];
break;
case 1:
if ([conversionInfo month] >= 2|| [conversionInfo month] == 0) {
prularity = @"MONTHS";
} else {
prularity = @"MONTH";
}
one = [one stringByAppendingString:[NSString stringWithFormat:@"%ld %@ ", (long)[conversionInfo month], prularity]];
break;
case 2:
if ([conversionInfo week] >= 2 || [conversionInfo week] == 0) {
prularity = @"WEEKS";
} else {
prularity = @"WEEK";
}
one = [one stringByAppendingString:[NSString stringWithFormat:@"%ld %@ ", (long)[conversionInfo week], prularity]];
break;
case 3:
if ([conversionInfo day] >= 2 || [conversionInfo day] == 0) {
prularity = @"DAYS";
} else {
prularity = @"DAY";
}
one = [one stringByAppendingString:[NSString stringWithFormat:@"%ld %@ ", (long)[conversionInfo day], prularity]];
break;
case 4:
if ([conversionInfo hour] >= 2 || [conversionInfo hour] == 0) {
prularity = @"HOURS";
} else {
prularity = @"HOUR";
}
one = [one stringByAppendingString:[NSString stringWithFormat:@"%ld %@ ", (long)[conversionInfo hour], prularity]];
break;
case 5:
if ([conversionInfo minute] >= 2 || [conversionInfo minute] == 0) {
prularity = @"MINUTES";
} else {
prularity = @"MINUTE";
}
one = [one stringByAppendingString:[NSString stringWithFormat:@"%ld %@ ", (long)[conversionInfo minute], prularity]];
break;
}
}
And print my counter :
one = [one stringByAppendingString:[NSString stringWithFormat:@" %@",untilOrSince]];
self.eventTimer.text = one;
Hope this will help someone with similar issue. I know this is not the best solution but so far it worked.