Please note that update 3 is probably most relevant
Im setting a NSTimeInterval
property of a managed object with an nsdate object using setValue:forKey:
When i attempt to get the value I get weird stuff, at runtime this
NSLog(@"[managedObject valueForKey:@\"startTime\"] : %@, [NSDate dateWithTimeIntervalSince1970:[managedObject startTime]]: %@",
[managedObject valueForKey:@"startTime"],[NSDate dateWithTimeIntervalSince1970:[managedObject startTime]]);
Returns
[managedObject valueForKey:@"startTime"] : 2012-07-14 08:13:05 +0000,
[NSDate dateWithTimeIntervalSince1970:[managedObject startTime]]: 1981-07-14 08:13:05 +0000
Update 1
The value returned by [managedObject valueForKey:@"startTime"]
is correct. However I would prefer to use [NSDate dateWithTimeIntervalSince1970:[managedObject startTime]]
or something similar so that it is more strongly typed.
I believe [managedObject startTime]
returns an incorrect value => 363954111.000000
.
However i set it with something like this:
managedObject setValue:1342261311 forKey:@"startTime"
It is worth noting that I am unsure whether this is incorrect because [managedObject valueForKey:@"startTime"]
returns a correct NSDate object.
Update 2
I've logged the double values returned by KVC and .
syntax.
managedObject.startTime = 363954111.000000
valueForKey timeIntervalSince1970 = 1342261311.000000
Update 3
Okay, I've set up a test, start time is set like this entity.startTime = [[NSDate dateWithTimeIntervalSince1970:1342261311] timeIntervalSince1970];
and end time is set like this [entity setValue:[NSDate dateWithTimeIntervalSince1970:1342261311] forKey:@"endTime"];
When i write them to log i get this start = 1342261311.000000, end = 363954111.000000
It seems that the NSDate object is being unwrapped incorrectly, has anyone seen this before?
It was a problem with the difference in epochs. NSDate uses Jan 1 2001 as an epoch. So when I was getting the value I was using the unix epoch (1970). That gave me a difference in values.
When KVC unwraps and wraps NSTimeInterval with a NSDate object it uses the NSDate 2001 epoch.
So instead of using dateWithTimeIntervalSince1970
I used dateWithTimeIntervalSinceReferenceDate
when getting the value.