Compare NSDate to date downloaded from internet - iphone

My iPhone app downloads an exchange rate from a website. It also downloads the time that the rate was last updated in EST. In my app, I want to compare that time to the [NSDate date].
Basically, I want to compare the downloaded time to the current EST using NSTimeInterval to calculate the difference.
I have looked on this site and read through the docs and am uncertain on how to achieve this. As far as I can understand, NSDate is without time zone. It is GMT. So I want to convert that to EST or convert my date to GMT and compare the two.
This is how I convert my downloaded data to a date:
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:hour];
[dateComps setMinute:minute];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
self.lastUpdatedExchangeRateDate = [calendar dateFromComponents:dateComps];
I appreciate your input on the best way to go about this.

You should set the NSDateComponents' timeZone too:
dateComps.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
It's important to create the NSDate with the correct timezone, because it represents a second in time, and have no awareness of timezones.

Related

Get next minute time (UTC) without using Internet

I wish to know if there is any way to find next minute UTC time without using internet connection. I have to perform an action (play song at a time on multiple devices without lag) at a given point of time.
Since NSDate is always in UTC, you could:
NSDateComponents *minuteComponent = [[NSDateComponents alloc] init];
minuteComponent.minute = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextMinuteDate = [theCalendar dateByAddingComponents:minuteComponent toDate:[NSDate date] options:0];
NSLog(#"%#",nextMinuteDate);

sethours in NSDateComponents

I'm trying to sethours in NSDateComponents, I wrote the following code
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:intDay];
[dateComps setMonth:intMonth];
[dateComps setYear:intYear];
[dateComps setHour:intHoures];
[dateComps setMinute:intMinutes];
NSDate *itemDate;
itemDate = [calendar dateFromComponents:dateComps];
NSLog(#"reminder date: %#", itemDate);
but when I set the hours as 13, it sets it as 11. I want hours in 24 style. Anyone can help me in this issue?
Thanks in Advance.
Time difference in the hours indicate that you might have problem with time zone, you could try to set the timezone for the date, I know NSDateFormatter has timeZone Property,I doubt if NSDateComponents have timeZone property. However you could set the timeZone for calendar
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

How to set time range in a day using UIDatePickerView

I am using UIDatePickerView, just like we can set the date range using "maximumDate" and "minimumDate", is there any way to set time range in a day like from 9.00 am to 6.00 pm ?
Not really. minimumDate and maximumDate can include a time of day (NSDate is more like a timestamp than a calendar date), but this only has an effect if they are both on the same day because otherwise all (clock) times are possible within the date range.
Actually you should set the time using these two as well. NSDate objects have both date and time components. You should do something like this:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
//set the date using setDate: setMonth: setYear:
[dateComponents setHour:9]; //setHour:18 for maximumDate
NSDate *targetDate = [gregorian dateFromComponents::dateComponents];
[dateComponents release];
[gregorian release];
datepick.minimumDate = targetDate;
If that doesnt help, have a look at these Qs:
UI Datepicker range. iPhone
How to set time on NSDate?

How to set variable with specified date and time?

I searched it a lot but coudn't find any instance of showing how to store the specified time. For example, i need to save time of 15:48 in code in a proper variable (i guess that should be NSDate object). That is needed because i want to hold the exact time to replace the method below with not the time interval since now but my exact specified time to fire notification. Thanks for the help.
NSDate *notificationDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.fireDate = notificationDate;
Use NSDateComponents:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:15]; // 15:48 from your example
[comps setMinute:48]; // 15:48 from your example
/// also set year, month and day
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
You can also use NSDateCompnents to read the components from the current date and time - so if you want to set an NSDate to 15:48 today, you would first create an NSDate for now and then extract the day, month and year from it but overwrite the hour and minutes.
Use NSDateComponents to generate an NSDate object. I guess the Apple document is what you want http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html
Take a look at NSDateFormatter and especially - (NSDate *)dateFromString:(NSString *)string

Determine if current local time is between two times (ignoring the date portion)

Considering that there is no NSTime in Cocoa-Touch (Objective-C on iPhone), and given two times as NSStrings and a timezone as an NSString, how can you calculate whether or not the current LOCAL time is between these two times. Keep in mind that the date in the time strings do NOT matter, and are filled with dummy dates.
For example:
TimeZone: Pacific Time (US & Canada)
Start Time: 2000-01-01T10:00:00Z
End Time: 2000-01-01T17:00:00Z
Local Time: now
How do you confirm whether or not local time is between the time range specified (ensuring to convert the start/end times to the proper timezone first)?
The biggest problem with this seems to come from the fact that the times may span two days (originally they might not, but when you do the timezone conversion, after that they might). So if we're to ignore the given date information completely, some assumptions have to be made with how to handle these date spans. Your question isn't exact on how to deal with this (i.e. I don't know exactly what you'd like to achieve) so here's just one way to go about it, which might not be exactly what you're after, but hopefully it will guide you in the right direction:
Parse the given strings to NSDate objects, ignoring the date information (result: times are handled such that they're assumed to be for the same day) and performing the time zone conversion
Get the time interval from the earlier NSDate to the later NSDate
Create NSDate objects for "today at the earlier given time" and "yesterday at the earlier given time"
Compare the time intervals from these two NSDates till the current date/time to the time interval between the two given date/times
Also note that time zone strings in the format you gave ("Pacific Time (US & Canada)") will not be understood by NSTimeZone so you'll need to do some conversion there.
Here's a code example (I wrote this on OS X since I don't have the iPhone SDK so hopefully all the used APIs will be available on iPhone as well):
- (BOOL)checkTimes
{
// won't work:
//NSString *tzs = #"Pacific Time (US & Canada)";
//
// will work (need to translate given timezone information
// to abbreviations accepted by NSTimeZone -- won't cover
// that here):
NSString *tzs = #"PST";
NSString *ds1 = #"2000-01-01T10:00:00Z";
NSString *ds2 = #"2000-01-01T17:00:00Z";
// remove dates from given strings (requirement was to ignore
// the dates completely)
ds1 = [ds1 substringFromIndex:11];
ds2 = [ds2 substringFromIndex:11];
// remove the UTC time zone designator from the end (don't know
// what it's doing there since the time zone is given as a
// separate field but I'll assume for the sake of this example
// that the time zone designator for the given dates will
// always be 'Z' and we'll always ignore it)
ds1 = [ds1 substringToIndex:8];
ds2 = [ds2 substringToIndex:8];
// parse given dates into NSDate objects
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"HH:mm:ss"];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:tzs]];
NSDate *date1 = [df dateFromString:ds1];
NSDate *date2 = [df dateFromString:ds2];
// get time interval from earlier to later given date
NSDate *earlierDate = date1;
NSTimeInterval ti = [date2 timeIntervalSinceDate:date1];
if (ti < 0)
{
earlierDate = date2;
ti = [date1 timeIntervalSinceDate:date2];
}
// get current date/time
NSDate *now = [NSDate date];
// create an NSDate for today at the earlier given time
NSDateComponents *todayDateComps = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:now];
NSDateComponents *earlierTimeComps = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
fromDate:earlierDate];
NSDateComponents *todayEarlierTimeComps = [[[NSDateComponents alloc] init] autorelease];
[todayEarlierTimeComps setYear:[todayDateComps year]];
[todayEarlierTimeComps setMonth:[todayDateComps month]];
[todayEarlierTimeComps setDay:[todayDateComps day]];
[todayEarlierTimeComps setHour:[earlierTimeComps hour]];
[todayEarlierTimeComps setMinute:[earlierTimeComps minute]];
[todayEarlierTimeComps setSecond:[earlierTimeComps second]];
NSDate *todayEarlierTime = [[NSCalendar currentCalendar]
dateFromComponents:todayEarlierTimeComps];
// create an NSDate for yesterday at the earlier given time
NSDateComponents *minusOneDayComps = [[[NSDateComponents alloc] init] autorelease];
[minusOneDayComps setDay:-1];
NSDate *yesterday = [[NSCalendar currentCalendar]
dateByAddingComponents:minusOneDayComps
toDate:now
options:0];
NSDateComponents *yesterdayDateComps = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:yesterday];
NSDateComponents *yesterdayEarlierTimeComps = [[[NSDateComponents alloc] init] autorelease];
[yesterdayEarlierTimeComps setYear:[yesterdayDateComps year]];
[yesterdayEarlierTimeComps setMonth:[yesterdayDateComps month]];
[yesterdayEarlierTimeComps setDay:[yesterdayDateComps day]];
[yesterdayEarlierTimeComps setHour:[earlierTimeComps hour]];
[yesterdayEarlierTimeComps setMinute:[earlierTimeComps minute]];
[yesterdayEarlierTimeComps setSecond:[earlierTimeComps second]];
NSDate *yesterdayEarlierTime = [[NSCalendar currentCalendar]
dateFromComponents:yesterdayEarlierTimeComps];
// check time interval from [today at the earlier given time] to [now]
NSTimeInterval ti_todayEarlierTimeTillNow = [now timeIntervalSinceDate:todayEarlierTime];
if (0 <= ti_todayEarlierTimeTillNow && ti_todayEarlierTimeTillNow <= ti)
return YES;
// check time interval from [yesterday at the earlier given time] to [now]
NSTimeInterval ti_yesterdayEarlierTimeTillNow = [now timeIntervalSinceDate:yesterdayEarlierTime];
if (0 <= ti_yesterdayEarlierTimeTillNow && ti_yesterdayEarlierTimeTillNow <= ti)
return YES;
return NO;
}
You should be able to do what you are wanting to do with a combination of NSDate, and NSDateFormatter.
NSDateFormatter has a dateFromString: method which will let you convert your strings into NSDate objects, then you can use the compare: method of NSDate to compare the two against [NSDate date] which will have the current time.
(NSDate encompasses both date and time)
You should probably look at NSCalendar and NSDateComponents. I'm using them to do some date math for an iPhone app. I don't see a builtin method to do what you want directly, but you can figure it out by breaking it into pieces I expect?