2014-03-07 74 views
1

假设我从server @“2014-03-08T16:59 + 0000”有这样的字符串。如何从字符串中获取NSTimeZone?

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDate *date = [self dateFromJSONString:@"2014-03-08T16:59+0000"]; 
NSDateComponents *dateComponents = [calendar components:(NSTimeZoneCalendarUnit) fromDate:date]; 

NSLog(@"TimeZone is %@", [[dateComponents timeZone] abbreviation]); 

但是TimeZone并不基于字符串,它基于设备的currentTimeZone。

是否可以从字符串中提取timeZone?

+0

这些方法假定您指定您所在时区的日期。那个时间可能发生在任何地方,系统如何知道它应该属于哪一个? – SevenBits

回答

2

您必须自己解析来自字符串的GMT偏移量。

这样的事情,但你必须调整你的格式稍有不同:

/** 
This is assuming format yyyy-MM-dd'T'HH:mm:ssZZZZZ . ie the last 5 chars are timezone offset from gtm in the form (+|-)##:## 
*/ 
-(NSTimeZone*)timezoneFromDateString:(NSString*)dateString { 
    NSTimeZone *timezone = nil; 
    NSString *timezoneComponent = [dateString substringFromIndex:19]; 
    if(timezoneComponent.length == 6) { 
     NSArray *components = [[timezoneComponent substringFromIndex:1] componentsSeparatedByString:@":"]; 
     NSInteger offset = [[timezoneComponent substringToIndex:1] isEqualToString:@"-"] ? -1 : 1; 
     if(components.count == 2) { 
      offset *= [components[0] integerValue] * 60*60 + [components[1] integerValue] *60; 
      timezone = [NSTimeZone timeZoneForSecondsFromGMT:offset]; 
     } 
    } 
    return timezone; 
} 
1

如果你确信你需要解析字符串格式总是以这种方式,那么正则表达式提供简单的方法来做到这一点:

NSString *str = @"2014-03-08T16:59+0000"; 

NSString *pattern = @"^.*T\\d{2}:\\d{2}"; 

NSString *timezone = [str stringByReplacingOccurrencesOfString: pattern 
                withString: @"" 
                 options: NSRegularExpressionSearch 
                 range: NSMakeRange(0, str.length)]; 
1
-(NSArray*)convertToLocalDate:(NSString*)dateStr{ 
    NSArray *convert; 

    NSString *[email protected]""; 

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
    [dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]; 
    NSDate *date = [dateFormatter1 dateFromString:dateStr]; 
    //NSLog(@"date : %@",date); 

    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; 
    NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; 

    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date]; 
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date]; 
    NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset; 

    NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] ; 

    NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init]; 


    [dateFormatters setDateFormat:@"dd.MM.yyyy"]; 

    [dateFormatters setTimeZone:[NSTimeZone systemTimeZone]]; 
    dateStr = [dateFormatters stringFromDate: destinationDate]; 

    NSDateFormatter *dateFormatters1 = [[NSDateFormatter alloc] init]; 
    [dateFormatters1 setDateFormat:@"hh:mm a"]; 


    [dateFormatters1 setTimeZone:[NSTimeZone systemTimeZone]]; 
    time = [dateFormatters1 stringFromDate: destinationDate]; 


    convert = [[NSArray alloc ]initWithObjects:dateStr,time,nil]; 
    return convert; 
} 
相关问题