Reputation: 1447
I have a function which produces the current date depending on the users location. this provides the correct date and time even after daylight saving changes here in the UK.
However when i try to create a string with the date it seems to add an extra hour on for me...
E.g date stored in Coredata is 1/3/2013 12:00 however when converted to a string it appears as 1/3/2013 13:00. What ever i change with locale and timezones don't seem to make a difference.
Any ideas how i can fix this? It might be worth noting that the "Local Date" function i have never creates the date time with the correct timezone.
Code:
+(NSDate*)localDate{
NSDate* date = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
return [NSDate dateWithTimeInterval:interval sinceDate:date];
}
Formatted Code:
-(NSString*)dateWithTime{
NSDateFormatter*formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd"];
NSInteger day = [[formatter stringFromDate:self]integerValue];
NSInteger fullday = day;
NSString* strDay = @"";
if(day>29){
day = day - 30;
}
else if(day>19){
day = day - 20;
}
else if(day>9){
day = day - 10;
}
if(fullday ==11){
strDay = @"th";
}
else if(fullday ==12){
strDay = @"th";
}
else if(fullday ==13){
strDay = @"th";
}
else if(day == 1){
strDay = @"st";
}
else if(day ==2){
strDay = @"nd";
}
else if(day ==3){
strDay = @"rd";
}
else{
strDay = @"th";
}
[formatter setDateFormat:@"MMMM"];
NSString* month = [formatter stringFromDate:self];
[formatter setDateFormat:@"HH:mm"];
NSString* time = [formatter stringFromDate:self];
return [NSString stringWithFormat:@"%u%@ %@ %@ - %@",fullday,strDay,month,[self yearString],time];
}
EDIT: Even NSDateComponants returns the incorrect time..
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self];
Upvotes: 0
Views: 2392
Reputation: 3360
Please note that printing NSDate's in the console will always print the date in UTC.
Simply store the [NSDate date]
in your database. When the user never changed it's time zone, reading the date from your database and format it will return the correct result.
NSDate objects in Core Data are stored as UNIX timestamp with time zone UTC. When reading and formatting the date object the current time zone of the user is applied.
When storing dates with Core Data and having dependences to different time zones the only way to format dates correctly is to store the appropriate time zone (name of time zone as string) as well. When reading and formatting the date object you have to apply the stored time zone to the dateFormatter
. Then you'll get the correct results.
Upvotes: 3
Reputation: 1801
Depending on your needs after the initial date/time is set, you may want to be notified if the time zone (including DST) has changed. If so, applicationSignificantTimeChange: would be your friend.
Upvotes: 0
Reputation: 1807
Set the locale of dateformatter;
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
Upvotes: 0
Reputation: 1731
NSDate doesn't know about timezones it's only when converted that the timezone counts, so your localDate method is probably fudging things in a way you don't intend. What you probably need to do is set the timezone for the formatter or calendar to the local zone, so:
[formatter setTimeZone:localTimeZone];
or
[gregorian setTimeZone:localTimeZone];
and leave the value returned by [NSDate date]
alone.
Upvotes: 2