2

strptime を使用"%Y-%m-%dT%H:%M:%SZ"して正しい UNIX タイムスタンプにフォーマットするにはどうすればよいですか?

testDateWithString(下記)というメソッドを書いてみました。

ただし、次のエラー メッセージで失敗します。

'2011-12-27 08:35:46 +0000' should be equal to '2011-12-27 04:35:46 +0000'

どうすればこれを修正できますか?

#import <SenTestingKit/SenTestingKit.h>

@implementation NSDate (DateWithString)

+ (id)dateWithString:(NSString *)string {
    struct tm time; strptime([string UTF8String], "%Y-%m-%dT%H:%M:%SZ", &time);
    return [NSDate dateWithTimeIntervalSince1970:mktime(&time)];
}

@end

@interface NSDateTests : SenTestCase
@end

@implementation NSDateTests

- (void)testDateWithString {
    NSDate *parsedDate = [NSDate dateWithString:@"2011-12-27T04:35:46Z"];
    NSDate *actualDate = [NSDate dateWithTimeIntervalSince1970:1324960546];
    STAssertEqualObjects(parsedDate, actualDate, nil);
}

@end
4

1 に答える 1

1

mktimeGMT ではなく現地時間を表示しています。

詳細については、この回答をご覧ください: Converting Between Local Times and GMT/UTC in C/C++

于 2012-01-15T22:21:36.373 に答える