NSDate dateFromString, how to parse UTC, GMT, and user locales?

I am parsing some values โ€‹โ€‹from an XML file. There is a line @"25-12-2010'T'23:40:00"with time and date, and there is a line with GMT offset, similar to this @ "+ 0200". Thus, the time has come December 25 23:40:00 time Zone +0200 GMT. (or 21:40 UTC) I have a lot of these dates with different GMT offsets. I have to show these dates as they are, i.e. they should not be changed to suit the user's language. Therefore, if the time is 1: 22:45 +0500, then this is what I should show to the user, even if the user is in a different time zone.

I have all the problems with displaying, calculating and analyzing these lines.

If I use dateFormatter and dateFromString, then the resulting NSDateuser information will be GMT. This means that the above will be saved as 23:40:00 +0100 GMT, because these are the settings of my phones and, possibly, 23: 40: 00 -0400 for a user from a new New York phone.

When I subsequently do subtraction, addition and matching between these dates, I have to maintain the GMT offset, and it gets worse if the phone switches the locale settings, starting from the date when the date was parsed, when the date is displayed ...

Is there a way to extract this date from a string in UTC format and then save it as an interval instead of the actual (time zone dependent) date. I know that deadlines are always kept domestically. But I canโ€™t understand how to do this with a separate GMT line and taking into account the locale of users.

Greetings

+3
source share
2 answers

Use NSDateFormatter setTimeZone:. You will also need to keep the time zone offsets separate from NSDate, in order to later display the time in these time zones again.

+4
source

, . , , , NSDate NSDateformatter "" " 1 2001 " .

" ", . NSDate, , , , , UTC.

- (void) testGMTDateParser {

    NSMutableArray *arrayDates = [NSMutableArray arrayWithCapacity:5];
    [self setParsedDates:arrayDates];

    NSMutableArray *arrayGMTOffsets = [NSMutableArray arrayWithCapacity:5];
    [self setParsedGMTOffsets:arrayGMTOffsets]; 

    NSString *date00    = @"2010-03-30T12:00:00";   
    NSString *GMT00     = @"-2";

    NSString *date01    = @"2010-03-30T12:00:00";   
    NSString *GMT01     = @"-1";

    NSString *date02    = @"2010-03-30T12:00:00";   
    NSString *GMT02     = @"+0";

    NSString *date03    = @"2010-03-30T12:00:00";   
    NSString *GMT03     = @"+1";

    NSString *date04    = @"2010-03-30T12:00:00";   
    NSString *GMT04     = @"+2";    

    NSArray *dateArray  = [NSArray arrayWithObjects:date00, date01, date02, date03, date04,nil];
    NSArray *GMTArray   = [NSArray arrayWithObjects:GMT00, GMT01, GMT02, GMT03, GMT04, nil];

    for (int i = 0; i < [dateArray count]; i++) {

        [self parseDateString:[dateArray objectAtIndex:i] withGMTString:[GMTArray objectAtIndex:i]];
    }   

}

GMT . UTC .

-(void) parseDateString:(NSString*) dateString withGMTString:(NSString*) GMTString {

    NSInteger hoursFromGMT      = [GMTString intValue]; 
    NSInteger secondsFromGMT    = (hoursFromGMT * 60 * 60);

    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:secondsFromGMT];

    NSDateFormatter *dateFormatterGMTAware = [[NSDateFormatter alloc] init];
    [dateFormatterGMTAware setTimeZone:timeZone];
    [dateFormatterGMTAware setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    NSDate *date = [dateFormatterGMTAware dateFromString:dateString];
    [dateFormatterGMTAware release];

    [self.parsedDates addObject:date];
    [self.parsedGMTOffsets addObject:[NSNumber numberWithInt:secondsFromGMT]];
}

NSDateformatter, GMT. UTC GMT.

-(void) printOutDates {

    for (int i = 0; i < [self.parsedDates count]; i++) {

        NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[[parsedGMTOffsets objectAtIndex:i] intValue]];

        NSDateFormatter *dateFormatterGMTAware = [[NSDateFormatter alloc] init];
        [dateFormatterGMTAware setTimeZone:timeZone];
        [dateFormatterGMTAware setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

        NSLog(@"%@ in Original GMT", [dateFormatterGMTAware stringFromDate:[parsedDates objectAtIndex:i]]);
        NSLog(@"%@ in Local GMT\n\n", [parsedDates objectAtIndex:i]);
    }
}

2010-03-30 18: 50: 31.284 TimeZonePOC [39830: 207] 2010-03-30 12:00:00 -0200 GMT 2010-03-30 18: 50: 31.285 TimeZonePOC [39830: 207] 2010-03-30 16:00:00 +0200 GMT

2010-03-30 18: 50: 31.287 TimeZonePOC [39830: 207] 2010-03-30 12:00:00 -0100 GMT 2010-03-30 18: 50: 31.287 TimeZonePOC [39830: 207] 2010-03-30 15:00:00 +0200 GMT

2010-03-30 18: 50: 31.289 TimeZonePOC [39830: 207] 2010-03-30 12:00:00 +0000 GMT 2010-03-30 18: 50: 31.289 TimeZonePOC [39830: 207] 2010-03-30 14:00:00 +0200 GMT

2010-03-30 18: 50: 31.290 TimeZonePOC [39830: 207] 2010-03-30 12:00:00 +0100 GMT 2010-03-30 18: 50: 31.292 TimeZonePOC [39830: 207] 2010-03-30 13:00:00 +0200 GMT

2010-03-30 18: 50: 31.292 TimeZonePOC [39830: 207] 2010-03-30 12:00:00 +0200 GMT 2010-03-30 18: 50: 31.294 TimeZonePOC [39830: 207] 2010-03-30 12:00:00 +0200 GMT

+4

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


All Articles