NSTimezone how to set CST to nsdateformater - iphone

all
I'm new to ios programming.
I want to set CST in the method
[dateFormatter setTimeZone:]
I have created a sample project to get all known timezones
Code is here:
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
for (NSTimeZone *timezon in timeZoneNames) {
NSLog(#"%#",timezon);
}
It gives the following result:
[Session started at 2011-09-28 09:50:28 +0530.]
2011-09-28 09:50:29.535 Timezonetest[10352:207] Africa/Abidjan
2011-09-28 09:50:29.537 Timezonetest[10352:207] Africa/Accra
2011-09-28 09:50:29.537 Timezonetest[10352:207] Africa/Addis_Ababa
2011-09-28 09:50:29.538 Timezonetest[10352:207] Africa/Algiers
........................
........................
........................
........................
2011-09-28 09:50:29.765 Timezonetest[10352:207] Pacific/Pago_Pago
2011-09-28 09:50:29.766 Timezonetest[10352:207] Pacific/Palau
2011-09-28 09:50:29.766 Timezonetest[10352:207] Pacific/Pitcairn
2011-09-28 09:50:29.767 Timezonetest[10352:207] Pacific/Pohnpei
2011-09-28 09:50:29.768 Timezonetest[10352:207] Pacific/Ponape
2011-09-28 09:50:29.769 Timezonetest[10352:207] Pacific/Port_Moresby
2011-09-28 09:50:29.773 Timezonetest[10352:207] Pacific/Rarotonga
2011-09-28 09:50:29.775 Timezonetest[10352:207] Pacific/Saipan
2011-09-28 09:50:29.775 Timezonetest[10352:207] Pacific/Tahiti
2011-09-28 09:50:29.776 Timezonetest[10352:207] Pacific/Tarawa
2011-09-28 09:50:29.776 Timezonetest[10352:207] Pacific/Tongatapu
2011-09-28 09:50:29.777 Timezonetest[10352:207] Pacific/Truk
2011-09-28 09:50:29.778 Timezonetest[10352:207] Pacific/Wake
2011-09-28 09:50:29.778 Timezonetest[10352:207] Pacific/Wallis
Anyone can say which is the name for CST(central Standard timezone) in the above list
Thanks in Advance

try this by first method u got all time zone after that wat u want u set by key value.
NSLog(#"%#",[NSTimeZone abbreviationDictionary]);
[dateFormater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CST"]];

Just use the following code,
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CST"]];

Try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss";
NSTimeZone *cst = [NSTimeZone timeZoneWithAbbreviation:#"GMT-06:00"];
[dateFormatter setTimeZone:cst];

Related

Unexpected error while comparing NSdate

I am comparing one date (say start_date) with current date (current_date_from_db). But the comparison is failing with either of the two errors:
if ([current_date_from_db compare:start_date]== NSOrderedAscending)
{
NSLog(#"Yes");
}
else {
NSLog(#"No");
};
And
while([ current_date_from_db earlierDate:start_date] && [current_date_from_db laterDate:end_date])
The code is failing with
SIGBART: Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSCFString isEqualToDate:]
current_date_from_db is a string as you can see in your error message: [NSCFString isEqualToDate:]. You'll want to parse your date and then compare:
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:current_date_from_db];
You need to do conversion prior to comparison. Either make an NSDate object with the current_date_from_db or convert the start_date to a string and then compare.

Event Reminder time set, Off by 6 hours

I have searched for help on this issue but I am falling short of an answer. I am setting an event reminder with code. Using break points and stepping thru. I see the event time is correct. Below is how I set my reminder
EKEventStore *eventDB = [[[EKEventStore alloc] init]autorelease];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSString * eventTitle = [NSString stringWithFormat:#"%# - %#",app.dealerBusinessName,serviceOrComments.text];
myEvent.title = eventTitle;
// "destinationDate" is the date I want to set the reminder for it is correct in debugger
//its format is 2011-06-03 15:45:58 +0000 which means (i would think) that the reminder
//should be set for 6/3/2011 3:45PM but its always 6 hours earlier (in this case at 9:45am
NSLog(#"value: %#",destinationDate);
myEvent.startDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:destinationDate ]autorelease];
myEvent.endDate = [[[NSDate alloc] initWithTimeInterval:3600 sinceDate:myEvent.startDate]autorelease];
myEvent.allDay = NO;
myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-3600]; // 1 Hour
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day
[myAlarmsArray addObject:alarm1];
[myAlarmsArray addObject:alarm2];
myEvent.alarms = myAlarmsArray;
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
//no error, but do not show alert because we do that below.
}
Now im pretty sure I can just add 21,600 to the time but is that the correct way to do it? I am not understanding how NSDate works and its driving me nuts. Can someone explain to me what the correct way to do this is? Thanks!
I had a similar problem lately, it was really driving me nuts. You have to take your time zone into account (or the timezone your simulator/device are set to). NSDate represents always GMT time, NSCalendar the time within the time zone. So the time differences you are experiencing will exactly be the time difference between your time zone and GMT (your alarm gets triggered at 15:45 GMT).
Since I want my dates to be absolute, regardless of the time zone, I am setting all my NSCalendars to GMT - it works in my case, of course YMMV.

iPhone - UILocalNotification fireDate problem

I'm trying to get the fireDate from an already set notification
Heres my code:
NSArray *notificationArray = [[NSArray alloc] initWithObjects:[[UIApplication sharedApplication] scheduledLocalNotifications], nil];
if ([notificationArray count] > 0) {
NSDate *now = [NSDate date];
UILocalNotification *locNotification = [[UILocalNotification alloc] init];
locNotification = [notificationArray objectAtIndex:0];
NSDate *otherDate = locNotification.fireDate;
}
The locNotification has the values but the last line when I try to instantiate otherDate I'm getting
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM fireDate]: unrecognized selector sent to instance 0x1e4e20'
* Call stack at first throw:
I really don't know what I'm doing wrong.
Thanks in advance
You're using the wrong init method. Instead of NSArray's -initWithObjects:, you want:
NSArray *notificationArray = [[ NSArray alloc ] initWithArray:
[[ UIApplication sharedApplication ]
scheduledLocalNotifications ]];
You get the exception because notificationArray only contains one object, the array returned by -scheduledLocalNotifications.

Exception when sorting NSMutableArray which contains all NSDate objects

//list has type of NSMutableArray
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES] autorelease];
[list sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
I got this exception:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDate 0x5d57980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key date.'
Any idea? How to fix it?
An NSDate does not have the -date method, so your NSSortDescriptor won't work.
Why not just use
[list sortUsingSelector:#selector(compare:)];
?

iPhone app delegates does not work properly

I am new to iPhone development. I am converting the date to the desired format and set it to the delegate and get its value in the another view. The session restarts when I tried to get the value from delegate. If I set the original date and not the formatted date in the set delegate, then I able to get the value in the another view. If I also give any static string value, then also I am able to the static string value back.
Only the formatted date which is string is set then the session restarts. If I print and check the value of the formatted date it prints the correct formatted date only. Here is my code for date conversion
NSString *dateval=[[stories objectAtIndex: storyIndex] objectForKey:#"date"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"EEE, MMM dd, yyyy"];
NSDate *inputDate = [inputFormatter dateFromString:dateval];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMMM dd"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
[delegate setCurrentDates:outputDate];
Edit
This is displayed in console
inside view did load
[Session started at 2010-04-21 19:12:53 +0530.]
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 4216.
(gdb)
In another view
- (void)viewDidLoad {
NSLog(#"inside view did load");
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
NSString *titleValue=[delegate getCurrentDates];
self.navigationItem.title =titleValue ;
}
The get does not work properly. It works fine if I give any static string or the "dateval".
outputDate does not seems to be retained, so the value is lost at the end of the event loop (because of the NSAutoreleasePool).
You should retain the outputDate to avoid its release with something like that in the delegate:
- (void)setCurrentDates:(NSString *)value {
[value retain]; // <- Retain new value
[date release]; // <- Release old value;
date = value;
}
The best solution would be to have a declared property in the delegate with the retain attribute.