유튜브 API는 날짜 문자열을 RFC3339 형식으로 반환합니다. 나는 매뉴얼에서 그것을 분석하는 방법을 발견했다. 어쨌든, 이것은 너무 길다.iOS에서 RFC3339 날짜 문자열을 구문 분석하는 가장 간단한 방법은 무엇입니까?
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
// Returns a user-visible date time string that corresponds to the
// specified RFC 3339 date time string. Note that this does not handle
// all possible RFC 3339 date time strings, just one of the most common
// styles.
{
NSString * userVisibleDateTimeString;
NSDateFormatter * rfc3339DateFormatter;
NSLocale * enUSPOSIXLocale;
NSDate * date;
NSDateFormatter * userVisibleDateFormatter;
userVisibleDateTimeString = nil;
// Convert the RFC 3339 date time string to an NSDate.
rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
if (date != nil) {
// Convert the NSDate to a user-visible date string.
userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
assert(userVisibleDateFormatter != nil);
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
[userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;
}
나는 기능이 포함되어 있는지 확인 할 수 있지만, 내가 알고 싶은이 작업을 수행하려면 코코아 재단 또는 표준 C 또는 POSIX 라이브러리에이 사전 정의 된 방법입니다. 그리고 있다면 사용하고 싶습니다. 더 간단한 방법이 있다는 것을 알려주시겠습니까? 또는 이것이 가장 간단한 방법이라고 확인하면 매우 감사하겠습니다 :)
불행히도,이 코드는 내가 한번 테스트했을 때 초의 파벌 부분을 처리 할 수 없었습니다. – Eonil
@Eonil : 형식 문자열을 수정해야합니다. 관련 사양에는 소수 자릿수 초로 정의 된 형식 문자가 있습니다. http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns –
오 이런 모양이 좋습니다. 나는 이것을 시도 할 것이다 :) – Eonil