New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NSDate being truncated to the second #875
Comments
Nice find! Indeed, Realm discards sub-second information from If you need to store more precise time information in Realm, you can create We'll update our docs to clarify this point. |
I just reopened the ticket because we'll be looking into alternatives to store NSDate's in a lossless way. |
I think that's a smart choice. It would be better to not support NSDate than to lose precision. Even if you documented the discrepancy, it would be an unending source of confusion and bugs because the expectation is that when you put a value in, you get the same value out. |
1+ for this. I ran into this my first day of testing out Realm. In my case, I was just using the iOS Master-Detail Xcode template and quickly hitting + while recording the date. Using date to sort in that case led to weirdness as I added a few within the same second. Obviously can work around it, but for clarity if nothing else, seems like it would be a good default behavior. 👍 |
Just a small update on this, we're still investigating changes to Realm's date support, our main concern being maintaining accuracy and interoperability with common date formats across platforms. In the meantime, if you need sub-second accurate dates, please use |
Another find: persisting |
how to convert a string date from json to |
I have to agree with @dginsburg that having Realm supports the following property types: |
@pizthewiz thanks for the suggestion, @yoshyosh just made the change on the website: http://realm.io/docs/cocoa/0.91.0/#property-types |
Is it correct that the Realm database is storing the seconds but your Realm Browser app does not display them? Is there a way to see the seconds in the Realm Browser? |
Yes, seconds are being persisted, but the browser doesn't display seconds. To see seconds in the browser, you could build it from source and change this line to use |
What's the status on NSDate property type being lossless? @jpsim |
@marcoscurvello it's something that's still being discussed, since it would require changes at the core database level as to how we store dates. JP's earlier suggestion as to how to workaround the issue still stands:
|
@segiddins Okay, thanks. That's what I am currently doing. 😏 |
A question about NSTimeInterval: when I declare a property of type NSTimeInterval I can't use the |
@WolfTrinity are you sure you're not declaring that property as a If you need this property to be optional, you can mark it as |
@jpsim Oh sorry, my fault: I've just substituted Thanks for the hint! |
You can't specify a
Source: https://realm.io/docs/swift/latest/#property-attributes But they're still KVO-compliant. |
Has this been assigned to a milestone / sprint? Seems JSON parsing is blowing up due to this |
This is being actively worked on. It's hard to see how it would break JSON parsing though. Feel free to file a new issue with more details if you'd like some help understanding whatever bad behavior you're seeing. |
That's great to hear it. Can't wait. Replacing the model with yet more DB shims to store NSTimeInterval but expose NSDates in the code is making our models look crazier than they already are. Yes it's a place you would least expect it. But unfort when the truncation happens NSDate serialization returns without the decimal and the JSON parsing fails since it expects "eee dd-MMM-yyyy GG HH:mm:ss.SSS ZZZ" but gets "d MMM yyyy HH:mm:ss ZZZ". Maybe we're just using a crappy library (SwiftDate) but the downstream results happen none the less. |
That sounds like a bug that should be fixed in your JSON handling either way. It's completely possible for a date to legitimately have no fractional seconds. |
NSDates are being truncated to a whole second when being stored and then retrieved from the realm. Looking at the date setter/getter, the NSTimeInterval is being cast to a std::time_t. NSTimeInterval is a double with sub second precision. My C++ is rusty but I believe time_t is a integer value of whole seconds. Could the underlying storage of NSDate be changed to a double?
The text was updated successfully, but these errors were encountered: