I have an array of NSDates which I build from strings using [NSDate dateFromString]
In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate each time I need it?
I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.
Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.
NSdate works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.
I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/
Thanks for any help given.
You should take one standard timezone like UTC/GMT format for all calculation.
According to the NSDate reference, dateWithString: takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).
Looking at the NSTimeZone reference, you can use the abbreviationForDate: and the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.
I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)
+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset
{
NSDate *now = [NSDate date];
NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
now = [now addTimeInterval:(localTimeZoneOffset * -1)];
date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];
return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.
Once you have an NSDate you can make normal calculations and ignore the timezones.
You only need to take care of timezones when reading strings into NSDates and when printing them out.
Related
I want to display the following string on my time axis:
"GMT/BST"
Here's the code:
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"zzz"];
timeZoneString = [NSMutableString stringWithFormat:#"%# / %#",[dateformatter stringFromDate:startDate],[dateformatter stringFromDate:endDate]];
But this gives "GMT/GMT+01:00"
What is the NSDateFormatter code to turn "GMT+01:00" into "BST" ? I can't get the right formatters to do this, having tried z|zzz|Z|ZZZ|v|V see... http://waracle.net/iphone-nsdateformatter-date-formatting-table/
Turns out there is a built in array of 48 time zone abbreviations (e.g. 'BST') in iOS.
NSDictionary *tzDict = [NSTimeZone abbreviationDictionary];
There is an array of 419 time zone names in this array (e.g. 'Europe/London'):
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
tzDict contains the abbreviations for daylight saving time for a subset of time zone names. So the algorithm would be to check if we are in DST, then see if tzDict has an entry, and subsitute that or if not, use
[NSTimeZone abbreviation];
Here are a few other topics on time zones in general.
Daylight saving time and time zone best practices
How can I map tz database names to city and country names?
C# british summer time (BST) timezone abbreviation
http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
GMT timezone conversion in objective c
I spent a day on this and wanted to make sure so others don't get hung up on it.
For a UK region/locale, the 'z' field pattern in DateFormatter returns the proper string for the "America/London" timezone (e.g. "GMT" or "BST"). However, if you use the US region/locale, 'z' will give you "GMT+1".
The gotcha happens when an abbreviation for the given target timezone with a given locale doesn't exist. I was using the 'v' field pattern, and with the US region, "Europe/London" was falling back to "United Kingdom Time", which blew up my text label. 'z' is better, it falls back to "GMT+1" while in DST, but still not the desired "BST".
The abbreviationDictionary doesn't consider daylight savings time. It's meant to map an abbreviation to a timezone. You'll find "BST":"Americal/London", but this only applies while observing daylight savings time as mentioned by #Nick T. During standard time, Brits are used to seeing "GMT".
Unfortunately, without recreating timezone functionality yourself, you're left with compromises. I think iOS can do better here.
Reference
Date Field Symbol Table as part of the Unicode Technical Standard
I have to convert a Double value into a Date value in Objective-C
I tried many ways but im not getting it.
date=var1/24;
Here var1 is a double value which should be divided by 24 and stored as a Date into the variable date. How can I do this using Objective-C?
I created the date variable like this:
NSDate *date=[[NSDate alloc]init];
date=(nsdate)var1/24;
How can I do this?
Its a Double Variable.. which will be containing values upto 24 it will be representing HOURS from TODAY..
OK, so you have a relative value that you want to add to an absolute value. Additional work is required.
First, you must decide "What is today?". Is it 12:01am? If it is, in which time zone? GMT? Something else? You have to know this, because 12:01am GMT is not the same thing as 12:01am EDT. So: what is today?
Once you've decided where you're going to be measuring time from, you have to construct an NSDate representing that point in time.
For example:
NSDate *rightNow = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:rightNow];
This will give you an NSDateComponents object, which is an object that represents a point in time relative to a calendar. In this case, we're using the "current calendar" (probably the Gregorian calendar, but not necessarily), and the default time zone is your current time zone. If you need to have it be relative to a different time zone, you can create a new NSTimeZone object and use -[NSCalendar setTimeZone:] to set the calendar's time zone (before asking for the date components).
Now that you've got the date components, we can "reset" things to the appropriate time:
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
Then we can turn it back into an NSDate to make it an absolute point in time:
NSDate *startingPoint = [calendar dateFromComponents:components];
NOW we have our starting point, and can deal with the "hours from today". First, we'll create a date components object to represent however many hours the difference is:
NSInteger hourDelta = var1 / 24;
NSDateComponents *delta = [[NSDateComponents alloc] init];
[delta setHour:hourDelta];
Now, we'll add this relative difference to our absolute starting date:
NSDate *finalDate = [calendar dateByAddingComponents:delta toDate:startingPoint options:0];
And because we're good citizens, we'll clean up our memory (unless you're using Garbage Collection or compiling with ARC):
[delta release];
Some important notes:
All of the manipulations are done via the NSCalendar object, because the NSCalendar is what defines what a "day" means and what an "hour" is. You think "there are 24 hours in a day and 60 minutes in an hour...", but that's not necessarily true. I can create my own calendar that has 10 hours in a day, and 10 minutes in an hour. If I want, I can define a "day" as something other than "one full rotation of the Earth". The NSCalendar define all these rules. Thus, all relative manipulations of dates (adding "hours" and "days" or whatever) must be done via the calendar.
There are methods on NSDate like -dateByAddingTimeInterval:, but this is for when you're dealing with absolute intervals and not relative amounts.
It depends of what you want to do exactly, but a direct cast as (nsdate)var1/24 will definitely NOT work!!!
Anway, what does your variable var1 represent exactly? Minutes ? Seconds ? Hours ? From which reference date? Today? The UNIX Epoch? Is it a UNIX timestamp (seconds since 01/01/1970)? Just asking to "convert a number to a date" means nothing on its own ;-)
Depending on the answer to those questions, you may use either NSDateComponents to produce a date giving the different date components (month, day, hours, minutes, seconds, …), or create a NSDate from a UNIX timestamp using dateWithTimeIntervalSince1970... or use any other method from NSDate or NSDateComponents depending on your needs.
Whatever your problem is, do read* the Date and TIme Programming Guide!! Everything is explained here about date manipulation; it will contain everything you need to answer your question.
When i call [NSDate date] it returns 2 hours less than it should. I've checked my computer clock settings and they are all ok. I've checked settings in iPhone and time zone and clock are all OK. Even simulator shows correct time on top toolbar. But when I try to log current date it shows 2 hours less than it should. I ran out of ideas where to look.
All dates returned by [NSDate date] are in the GMT time zone.
When you use any NSDateFormatter, just set the time zone and it will print out the correct time.
Look carefully at the output of the NSLog() statement. You will see that the output always contains the timezone using standard UTC. Therefore, the date is actually correct, taking into account the timezone.
I found my solution here: How to convert time to the time zone of the iPhone device?
So if you ever want to change [NSDate date] to point to local time just use the code provided in the link above. And dont forget to change timeZoneWithAbbreviation on sourceTimeZone from EST to GMT (because NSDate is always in GMT)
Once again thanks everyone for helping out..
I am creating a calendar based app.
I have two dates say present date, and future date(due date), now i need to show the difference between these two dates in terms of days, hours, minutes & seconds and i want to show a timer which will continues to decrement second by second as the time increases and ultimately reaches to zero when due date comes.Basically it will be showing that how much days, hrs and seconds are left for the event.
How can I do that, please help me.
It would be a great help if i can get a similar kind of sample code.
Many Thanks in advance.
iPhone Developer
Maybe It can be helpful to you - http://blog.webscale.co.in/?p=244
Basically you want to use NSTimeInterval and NSDate to calculate the difference in time. For example:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *now = [NSDate date];
NSDate *futureDate = [dateFormat dateFromString:#"07/15/2015"];
// Now we can calculate the time interval, which really is an integer
// This is the number of seconds between the 2 dates, so with math you can calculate
// what you need. Remember, when you do dateFromString, the time is midnight 00:00:00
// however, *now is set to the current time. You may want to work on that a bit
NSTimeInterval timeDifference = [futureDate timeIntervalSinceDate:now];
// Also note, if you swap the order and do now timeIntervalSinceDate:futureDate, you
// get a negative number.
I'm getting the current date/time using [NSDate date]. The value returned is an hour in the future. I've check my phones location & time settings and they are correct.
I can display the correct date and time as a string using the code below.
[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle]
But I need it to return the correct date/time as a date object as I use it to calculate the estimated time of arrival using -
[date dateByAddingTimeInterval:interval]
I realise my question is similar to this one already asked but none of the answers suit my needs. Thanks in advance!
init] returning date an hour in the past?
Maybe you are confusing the point in time (ie the NSDate object) and the point in time at your location (ie your local time).
If you print a NSDate (like NSLog(#"%#", [NSDate date]); which invokes [date description]) the date representation that is printed is in UTC timezone (+0000) (at least it is on my computer).
So as long as you don't live in an area that uses UTC the date printed by [date description]; is always "wrong". But wrong only means that its representation is not the same representation as the clock in your office. The date (as in point in time) is still correct.
When you use localizedStringFromDate:dateStyle:timeStyle: you are printing the date in your local timezone.
NSDate *date = [NSDate date];
NSLog(#"%#", date);
NSLog(#"%#", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);
at my computer this results in:
2011-02-12 08:32:10.658 x[75647:207] Date: 2011-02-12 07:32:10 +0000
2011-02-12 08:32:10.661 x[75647:207] Date: Saturday, February 12, 2011 8:32:10 AM Central European Time
the printed strings are different, but the NSDate object is still the same. That's why you have to use NSDateFormatters when you show a date to the user. Because the same point in time looks different on different places of the world.
But there are only three places where an UTC formatted date would be one hour in the future, so if you don't live in greenland, cape verde or on the azores I might be totally wrong and there is something wrong with your NSDate objects.
Edit: Out of curiosity I read the documentation about [date description] again. And it says
A string representation of the
receiver in the international format
YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM
represents the time zone offset in
hours and minutes from GMT (for
example, “2001-03-24 10:45:32 +0600”).
So I don't know why the date at my computer is printed in GMT timezone. It might be in another timezone at your computer.
But still, it's only the representation, the date is still the same.