Absolute GMT date on iPhone - iphone

I'm trying to get a date that represents noon GMT for a recurring local notification.
I need this local notification to fire at the exact moment to anyone using the app.
So far, I'm trying this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:12];
[dateComps setMinute:0];
[dateComps setMonth:1];
[dateComps setDay:1];
[dateComps setYear:2010];
NSDate *localDate = [calendar dateFromComponents:dateComps];
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
localNotification.fireDate = gmtDate;
This isn't working, however. Any suggestions?
Edit::
Will this work if I don't specify a time zone on the notfication:
localNotification.fireDate = [NSDate dateWithTimeIntervalSince1970:1284483600];

Does setting the calendar's timezone to GMT work?
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:12];
[dateComps setMinute:0];
[dateComps setMonth:1];
[dateComps setDay:1];
[dateComps setYear:2010];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *gmtDate = [calendar dateFromComponents:dateComps];

Related

Fire UILocalNotification Weekly

I can't seem to figure out why this notification is not scheduling on Saturdays... I've tried both NSGregorianCalendar and autoupdatingCurrentCalendar. I'm setting [dateComps setWeekday:7];, but it keeps returning Sundays.
//NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *timeComponent = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:reminderTime];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear: 2012];;
[dateComps setHour: timeComponent.hour];
[dateComps setMinute: timeComponent.minute];
[dateComps setSecond:0];
// Set day of the week
[dateComps setWeekday:7];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification == nil)
return;
notification.fireDate = itemDate;
notification.repeatInterval = NSWeekCalendarUnit;
dateComps as you calculate it, does not contain a day or month so that setWeekday is just ignored and itemDate is a date on the first January 2012 (which was a Sunday).
To compute the next Saturday, you can proceed as described in Adding Components to a Date of the "Date and Time Programming Guide":
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Weekday of "reminderTime":
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:reminderTime];
// Number of weekdays until next Saturday:
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
[componentsToAdd setDay: 7 - [weekdayComponents weekday]];
NSDate *itemDate = [calendar dateByAddingComponents:componentsToAdd toDate:reminderTime options:0];

alarm and date comparison

I am trying to make a simple alarm.
in this code portion a take the date from the pickerdate (a ONLY time picker).
and if the alarm is the current date before i add a day.
// ALARM UNO
// Set Allarme Uno
-(void)viewDidAppear:(BOOL)animated{
NSDate *pickerDate = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
//Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
[alarmDatePickerUno setDate:itemDate animated:YES];
}
-(IBAction)setAlarmUno:(id)sender{
NSLog(#"picker in allarme");
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
AlarmDateUno = [alarmDatePickerUno date];
NSLog(#"allarme settata alle");
NSLog(#"%#", AlarmDateUno);
if ([alarmDatePickerUno.date compare:[NSDate date]] == NSOrderedAscending)
{
NSLog(#"Date is not valid");
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
NSLog(#"Aggiungo un giorno");
AlarmDateUno = [currentCalendar dateByAddingComponents:oneDay toDate:AlarmDateUno options:0];
NSLog(#"%#", AlarmDateUno);
//return;
}
After that i compare the current date with the alarm...
// CONTROLLO TEMPO UNO
- (void)runTimerUno {
myTickerUno = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: #selector(showActivityUno)
userInfo: nil
repeats: YES];
}
- (void)showActivityUno {
// NSDate *date = [NSDate date];
if(isAlarmUnoOn == 1 && AlarmUnoPlaying ==0){
NSLog(#"Confronto data e tempo");
if ([[[NSDate date] earlierDate:AlarmDateUno] isEqualToDate: AlarmDateUno])
{
AlarmUnoPlaying = 1;
[self playAlarmSoundUno];
}
}
}
That's the problem in the date in the picker view was before the current time and i add an hours i receive an EXC_BAD_ACCESS error at the line:
if ([[[NSDate date] earlierDate:AlarmDateUno] isEqualToDate: AlarmDateUno])
Zombie say me
[__NSDate timeIntervalSinceReferenceDate]: message sent to deallocated instance 0x914b710
i try every things and i don't understand why.... anyone can help me please?
It appears that AlarmDateUno is a property. Is that correct? If so, you need to retain it whenever you set it - otherwise, it may be deallocated before you try to access it again.

iphone how to get timezone?

i want to get timezone but cant.
i use NSLog is show to me is show.time = 2011-06-29 04:20:00 +0000
here is my code.
-(IBAction)Save:(id)sender{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
//[dateComps setSecond:[timeComponents second]];
timedata = [calendar dateFromComponents:dateComps];
[dateComps release];
DataClass *obj=[DataClass getInstance];
obj.showdate = timedata;
//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//[defaults setObject:timedata forKey:#"selectdate"];
//NSLog(#"save time = %#",obj.showdate);
[self dismissModalViewControllerAnimated:YES];
}
You're setting the time zone for your NSDateComponents object to GMT:
[dateComps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Thus your date will be interpreted as being in GMT. If you want the time zone that the device is in, use systemTimeZone:
[dateComps setTimeZone:[NSTimeZone systemTimeZone]];
In general, as noted in the docs for timeZoneWithAbbreviation:, the abbreviations can be ambiguous. If you want to get a particular time zone in a way that will always work no matter the location of the device, you can use timeZoneForSecondsFromGMT:.
You can also set a time zone for your app, if you want, rather than always referring to a specific time zone when creating date-related objects:
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:60*8]];
NSTimeZone * appTZ = [NSTimeZone defaultTimeZone];

Local notification ,time setting

HI All,
i am trying to use local notification
by using this
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay: [dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]]; // Notification will fire in one minute
[dateComps setMinute:0 ]; //[timeComponents minute]];
[dateComps setSecond:60]; //[timeComponents second]];
and i hv string something like current time : 2011-03-26 11:59:31 GMT
according to which i need to set sec,min, hours.
How can i set min, week,month, year etc, to pop up notification in above condition??
Guidenece needed.
Appreciate it
You can make a NSDateComponent from the above date as
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
If you want to set sec,min,hours etc. with this string "current time : 2011-03-26 11:59:31 GMT"
then first make a NSDate with this string using NSDateFormatter and get your values from this date.
NSString *dateStr = #"2011-03-26 11:59:31";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
dateStr = [dateFormat stringFromDate:date];

Set an NSDate object to custom date, combined with datepicker date?

I want to make an NSDate object which has the date set as the current date, but the time set as the time in the datepicker (because the datepicker's mode is the time one). How can i do this?
and if you really want to use a custom date use something like this.
- (IBAction)dpChange:(UIDatePicker *)sender {
// replace this with your custom date.
NSDate *myCustomDate = [NSDate dateWithTimeIntervalSinceNow:7*24*60*60];
NSInteger dateFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:dateFlags fromDate:myCustomDate];
NSDate *myCustomTime = [sender date];
NSInteger timeFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:timeFlags fromDate:myCustomTime];
NSDateComponents *newComponents = [[[NSDateComponents alloc] init] autorelease];
[newComponents setYear:[dateComponents year]];
[newComponents setMonth:[dateComponents month]];
[newComponents setDay:[dateComponents day]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
[newComponents setSecond:0];
NSDate *newDate = [[NSCalendar currentCalendar] dateFromComponents:newComponents];
NSLog(#" Date: %#", myCustomDate);
NSLog(#" Time: %#", myCustomTime);
NSLog(#"Combined: %#", newDate);
}