Is there a more direct way to do this?
Definitely. Currently it’s doing all this:
calling datetime() within SQLite to convert a stored Julian value to a string;
using one DateFormatter to parse that string into a Date value;
using a second DateFormatter to convert that Date value back to a string.
You can eliminate 1 and 2. Just project the raw Julian value in your query (fetching it via sqlite3_column_double) and then construct your Date value directly using init(timeIntervalSinceReferenceDate:). It just requires simple math to convert from Julian (days since 4713 BC) to Apple reference date (seconds since 2001).
Also this will help your ORDER BY clause a little, since now it will be sorting by a number rather than a string.
And (basic “code review” comment) you can construct mediumDateFormatter just once, before the loop. No need to do it inside the loop.
Topic:
Programming Languages
SubTopic:
Swift
Tags: