NSDate fixing timezones - ios

I know that NSDate doesn't have timezone information.
However, I'm trying to understand how to manipulate them properly.
At the moment I'm passing a date into an object. That date is the user selected date at time 00:00:00.
i.e. if the user hits October 21st then the NSDate passed in should be. 21/10/2013 00:00:00.
However, it isn't it's 20/10/2013 23:00:00. (One hour before).
Now, this is nothing about formatting them or displaying them. I'm just using the NSDates.
I'm creating the date using NSDateComponents and NSCalendar.
I guess my question is...
How can I tell what date an NSDate is actually referring to in my local time zone?
I need to send a UNIX time stamp for 00:00:00 and 23:59:59 for a given date. However, at the moment when I set the hour, minute and second to 0, 0 and 0 then I'm not getting midnight in the current time zone I'm getting midnight in GMT.
This isn't what I want.
Fixed?
OK, I've fixed it... I think. At least, it's doing what I want it to do.
The trick is...
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateComponents setSecond:timeZone.secondsFromGMT];

I've been confused by this many times. When you NSLog an NSDate, you'll always get the output in GMT. So the 20/10/2013 23:00:00 (GMT) you're seeing is the same as your expected 21/10/2013 00:00:00 (BST). The UNIX timestamp for both of these dates would be the same because it doesn't take into account timezone - it's always UTC.
If you want to output in a user-readable format, an NSDateFormatter will format the date using your current timezone and locale.

Related

NSTimeZone isDaylightSavingTime giving wrong value

Currently GMT-0700(US/pacific) is already in day-light-saving
But I am getting "NO" from NSTimeZone
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:secondsFromGMT]; //Getting timezone as GMT-0700
BOOL isDaylightSavingTime = [timeZone isDaylightSavingTime]; //getting boolean value as NO
How to fix this issue?
REQUIREMENT :I want to know ,my receiver is using dayLightSavingTime or not.i will get only receiver offset value.I have to support different timezones()..What is the best approach to do this
timeZoneForSecondsFromGMT is not specific enough.
The most accurate way is to create the time zone with the (full) region name:
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:#"America/Los_Angeles"];
This is not a wrong value. You get timezone GMT-0700 but this is not a Pacific timezone. To create pacific timezone you need:
timeZone = [NSTimeZone timeZoneWithName:#"PST"];
This is short description from apple documentation:
+ (instancetype)timeZoneForSecondsFromGMT:(NSInteger)seconds;
Description Returns a time zone object offset from Greenwich Mean
Time by a given number of seconds. The name of the new time zone is
GMT +/– the offset, in hours and minutes. Time zones created with this
method never have daylight savings, and the offset is constant no
matter the date.
Other answers mentioning timeZoneWithName are correct but there's one more detail I don't think has been mentioned. The reason that timeZoneForSecondsFromGMT doesn't work is that GMT does not have daylight savings time (or summer time, as it's more sensibly called in some other countries). GMT doesn't jump forward or back; it always moves ahead by one second per second. Since you ask for a fixed number of seconds from GMT, the result also does not have GMT. If it gave you a time zone that observed daylight saving time, the number of seconds from GMT would have to change twice a year. But since you asked for a fixed number of seconds, you get a result that doesn't do that, and never reports daylight saving time in effect.

Getting next date when trying to convert NSDate to String

I have this date in actuall
2016-09-03 19:00:00 +0000
Now I am trying to convert it to String using a specific format like below
But what I am getting in return is not as desired. the formatter is adding on day to the given date like below
Is this standard behaviour ?
This is not standard behaviour. This happen because of the time zone difference. Set time zone proper
Set the timezone.
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation: #"GMT"];
When you hover over the date, you can see that it is showing UTC, whereas the formatter is automatically converting this to a local date. If your timezone is 5 hours ahead of UTC, then it will be the next day locally from that time.

Ignoring time-zone changes after saving NSDate

My apps are time-zone agnostic: I want the dates and times saved and displayed in a manner that ignores changes in the user's time zone. Once I've recorded the local date/time of some user action in my app (typically by saving timeIntervalSince1970 in an sqlite DB), I want to show the same date/time string regardless if the user's time zone has changed or not, and regardless of the user's various time zone settings in his device.
For example, if a user's action occurs at 1:15 pm local time in San Francisco, and then he opens my app a few days later in New York, I want that prior action to still appear as 1:15 pm (not 4:15 pm), and even if he's manually set some other time zone in his device (e.g., Chicago). A second action in New York at 9:00 pm there, should forevermore display as 9:00 pm, even when back in California.
I could achieve my goal by saving every date/time as a string (based on the user's local time zone), and then never use the date methods again -- but then I couldn't do date/time arithmetic (e.g., sorting).
How can I be time-zone agnostic when saving and retrieving dates/times?
(Apple's Calendar app seems to behave this way when Time Zone Override is on, but only if I manually set the event time.)
Here's the working solution (#Hot Licks deserves the credit, but he didn't post an answer):
Subclass NSDate and add a gmtDate method that does the conversion from local TZ to GMT. gmtDate uses #"yyyy-MM-dd HH:mm" as the format string, which also drops seconds from the value. Like this:
+(NSDate *)gmtDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm"; // drops the seconds
dateFormatter.timeZone = [NSTimeZone systemTimeZone]; // the local TZ
NSString *localTimeStamp = [dateFormatter stringFromDate:[NSDate date]];
// localTimeStamp is the current clock time in the current TZ
// adjust date so it'll be the same clock time in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDate *gmtDate = [dateFormatter dateFromString:localTimeStamp];
return gmtDate;
}
In didFinishLaunchingWithOptions: add [NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]] so all date formatters make date strings in GMT by default (but never include the timezone in the format string).
When the date/time of a user action needs to be saved, get the date via [NSDate gmtDate] and save that value in your database.
Record the time zone along with the timestamp. Use that time zone when formatting the timestamp later. You'll need to add another column to your database table.
Either save the time zone, as Rob suggested, or save the time as both an NSDate and a string. Use the string for the UI and the NSDate for calculations.
Or hey, do all three, if you need them.

iOS Converting milliseconds to date returns wrong time

I am trying to convert milliseconds into date. Below shown is the code i am using.
double startDateDb=1380275880000;
NSDate *date=[NSDate dateWithTimeIntervalSince1970:(startDateDb/1000.0)];
NSLog(#"date---%#",date);
My log gives date as 2013-09-27 09:58:00 +0000
When i use online tool to convert i am getting "9/27/13 5:58 AM" which is correct.
Please help me to fix the issue.
NSLog used your timezone when displaying dates. TO get the date in UTC use the NSDate methods and specify the tie zone. All NSDates are UTC timezone based.
Use NSDateFormatter to display the date/time in another timezone.
Your NSLog put the timezone in GMT, the timezone you're looking for is GMT-4.

convert NSDate to GMT timezone

I am working on NSDate and i am new for it.I have start date and end date,getting from user.And it is 2013-01-01 and 2013-02-19.When i try to display in console it is showing me 2012-12-31 18:30:00 +0000.So you can say 5:30 is gap.So i am adding time interval
startDate = [aStartdDate dateByAddingTimeInterval:19800];
endDate = [aEndDate dateByAddingTimeInterval:19800];
My question is when i am trying to get current week using 2012-12-31 18:30:00 +0000 date, it is showing me correctly.But when i use 2013-02-01 00:00:00 +0000 date,it is showing me total number of weeks in month.Please help me.Thanking you.
Timezones shouldn't be part of your model - They are a presentation problem.
Your model should always use a common default timezone. If you present the date to your users, apply a NSDateFormatter that uses a specific timezone.
In your case this means, that you shouldn't try to fix your dates by applying arbitrary intervals but use a date formatter in the final step (output) instead.

Resources