NSDate - GMT on iPhone

I have the following code in a production application that calculates GMT date from user input date:

NSDate *localDate = pickedDate;
    NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; // You could also use the systemTimeZone method
    NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
    NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];

The code worked fine until a scary daylight saving time sucked in the UK last week.

How can I convert a date to GMT based on daylight saving time?

+3
source share
1 answer

NSDate . GMT GMT, . (, - ), NSDateFormatter, , .

, (, mysql) GMT ( ):

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // e.g., set for mysql date strings
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSString* mysqlGMTString = [formatter stringFromDate:[NSDate date]];

GMT, . , , , NSDate , GMT. , , , (, , ).

EDIT: ! .

+6

Source: https://habr.com/ru/post/1739918/


All Articles