Question

i'm trying to have a NSTimeInterval of a NSDate with time set to zero, so the start of the day, but give me always my timezone that is GMT+2 and not GMT, i'm doing this:

This is the category to set the start of a day in a NSDate:

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]

- (NSDate *) dateAtStartOfDay
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
components.hour = 0;
components.minute = 0;
components.second = 0;
return [CURRENT_CALENDAR dateFromComponents:components];
}

then i do this:

NSTimeInterval now = [[[NSDate date] dateAtStartOfDay] timeIntervalSince1970];

and give me this NSTimeInterval:

1375567200

that if i check on:

http://www.epochconverter.com

the gmt time is this:

GMT: Sat, 03 Aug 2013 22:00:00 GMT

Mine tiem zone: 04 agosto 2013 00:00:00 CEST GMT+2

how i can have always the GMT time interval? i'm looking for a solution that work at New York, Rome, Rio de janerio for example, have to give me the nstimeinterval to gmt where ever the user is, anyone can help me?

Was it helpful?

Solution

The calendar you get from currentCalendar uses the current locale and thus the time zone of whatever system this is running on. You should create your own calendar for this calculation, setting its time zone to GMT.

NSCalendar * GMTCal = [[NSCalendar alloc] initWithCalendarIdentifier:[[NSCalendar currentCalendar] calendarIdentifier];
[GMTCal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top