自1970年以来,RKValueTransformer能够解析历元中的毫秒数,例如357861600000

时间:2014-04-17 13:03:29

标签: json nsdate restkit nsdateformatter

我很难为自1970年以来的Restoch时间定义Restkit的全局变换器(JSON:" birthDay":357861600000),以毫秒为单位。我试图使用这段代码

NSDateFormatter *localOffsetDateFormatter = [[NSDateFormatter alloc] init];
[localOffsetDateFormatter setLocale:[NSLocale currentLocale]];
[localOffsetDateFormatter setDateFormat:@"SSSSSSSS.SSS"]; //and similar versions
[localOffsetDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[[RKValueTransformer defaultValueTransformer] localOffsetDateFormatter atIndex:0];
// also tried to register RKDotNetDateFormatter

它不像我期望的那样工作。或者我应该创建一个类似于RKDotNetDateFormatter的新特定NSDateFormatter?此外,上面的代码不会触发自定义格式化程序中的实现,以防我使用例如RKDotNetDateFormatter。

谢谢您的提示。

更新1:自定义格式化程序

但是,Restkit映射引擎会忽略/不使用它。

TSNEpochSince1970DateFormatter* epochSince1970Formater = [TSNEpochSince1970DateFormatter epochSince1970DateFormatterWithTimeZone:[NSTimeZone localTimeZone]];
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:epochSince1970Formater atIndex:0];

TSNEpochSince1970DateFormatter.h:

@interface TSNEpochSince1970DateFormatter : NSDateFormatter

+ (instancetype)epochSince1970DateFormatterWithTimeZone:(NSTimeZone *)timeZone;
- (NSDate *)dateFromString:(NSString *)string;
- (NSString *)stringFromDate:(NSDate *)date;

@end

TSNEpochSince1970DateFormatter.m:

@implementation TSNEpochSince1970DateFormatter

+ (instancetype)epochSince1970DateFormatterWithTimeZone:(NSTimeZone *)newTimeZone
{
    TSNEpochSince1970DateFormatter *formatter = [self new];
    if (newTimeZone) formatter.timeZone = newTimeZone;
    return formatter;
}

- (NSDate *)dateFromString:(NSString *)string
{
    return [NSDate dateWithTimeIntervalSince1970:[string integerValue]];
}


- (NSString *)stringFromDate:(NSDate *)date
{
    if (!date) {
        TSNLogError(@"Attempted to represent an invalid date: %@", date);
        return nil;
    }
    return [self stringForObjectValue:date];
}

- (BOOL)getObjectValue:(id *)outValue forString:(NSString *)string errorDescription:(NSString **)error
{
    NSDate *date = [self dateFromString:string];
    if (outValue)
        *outValue = date;
    return (date != nil);
}

- (NSString *)stringForObjectValue:(id)value
{
    NSParameterAssert([value isKindOfClass:[NSDate class]]);
    NSTimeInterval milliseconds = [(NSDate *)value timeIntervalSince1970];
    return [NSString stringWithFormat:@"%1.0lf", milliseconds];
}

@end

更新2 从RKCompoundValueTransformer实现中我可以看到已经有一个名为" timeIntervalSince1970ToDateValueTransformer"的合适的变换器。 TSNEpochSince1970DateFormatter是我的。

(lldb) po self.valueTransformers
<__NSArrayM 0x10ef04b30>(
<TSNEpochSince1970DateFormatter: 0x109987a70>,
<RKBlockValueTransformer: 0x109d287a0, name: identityValueTransformer>,
<RKBlockValueTransformer: 0x109d296a0, name: stringToURLValueTransformer>,
<RKBlockValueTransformer: 0x109d1d640, name: decimalNumberToNumberValueTransformer>,
<RKBlockValueTransformer: 0x109d2a7c0, name: decimalNumberToStringValueTransformer>,
<RKBlockValueTransformer: 0x10ef03e90, name: numberToStringValueTransformer>,
<RKBlockValueTransformer: 0x10ef03610, name: arrayToOrderedSetValueTransformer>,
<RKBlockValueTransformer: 0x10ef02210, name: arrayToSetValueTransformer>,
<RKBlockValueTransformer: 0x10ef00a30, name: nullValueTransformer>,
<RKBlockValueTransformer: 0x10ef02700, name: keyedArchivingValueTransformer>,
<RKBlockValueTransformer: 0x10ef02830, name: stringValueTransformer>,
<RKBlockValueTransformer: 0x10ef03fd0, name: objectToCollectionValueTransformer>,
<RKBlockValueTransformer: 0x10ef02830, name: stringValueTransformer>,
<RKBlockValueTransformer: 0x10ef02b80, name: keyOfDictionaryValueTransformer>,
<RKBlockValueTransformer: 0x10ef04630, name: mutableValueTransformer>,
<RKBlockValueTransformer: 0x10ef04b00, name: iso8601TimestampToDateValueTransformer>,
<RKBlockValueTransformer: 0x10ef042d0, name: timeIntervalSince1970ToDateValueTransformer>,
<NSDateFormatter: 0x10ef04bd0>,
<NSDateFormatter: 0x10ef05fb0>
)

更新3

我已经发现NSDate本身在[RKValueTransformers timeIntervalSince1970ToDateValueTransformer]中返回了错误的日期:

[NSDate dateWithTimeIntervalSince1970:357861600000];

返回13310-03-08 16:00:00 +0000。

1 个答案:

答案 0 :(得分:1)

输入值应除以1000.RKValueTransformer中timeIntervalSince1970ToDateValueTransformer的代码可以在辅助类中重用,并在映射过程中插入

[[RKValueTransformer defaultValueTransformer] insertValueTransformer:[TSNTimeIntervalInMillisecondsSince1970ValueTransformer timeIntervalInMillisecondsSince1970ToDateValueTransformer]
                                                             atIndex:0];

<强>更新

开发分支中已有一个实现:

https://github.com/RestKit/RestKit/issues/1587