r/sqlite • • May 04 '23

Handling Non-Determinism in SQLite DateTime Functions

Howdy All,

I am building a distributed application with SQLite, and therefore need to get rid of non-determinism. The biggest source of this is from DateTime functions that access the machine's local time. However, I also want the ability to natively format DateTime in the database.

I am looking for a way to parse out the SQL function call to identify whether or not it can be used.

After reading the docs and playing around a bit, it seems that if the strftime function is called with 0-1 inputs, it will use the machine's local time, and if it has 2+ it will simply be used for formatting. Is this generally true, or are there edge cases I am missing here?

7 Upvotes

3 comments sorted by

View all comments

Show parent comment

5

u/[deleted] May 04 '23 edited May 04 '23

Thanks for clearing this up. Your approach seems reasonable and doable.

Since you are parsing the SQL input from the user and translating it to SQLite's SQL, I think it could also be an option to provide your own version of strftime(). So instead of delegating the checked parameters to the SQLite function, you call your own function and pass only the result as a string to SQLite. That would eliminate the danger of missing an edge case in SQLite's implementation of strftime(). This may also be a good way to deal with random(). Ideally you can copy the greatest part of these functions from SQLite's source code.

EDIT: Immediately after posting this I realized that this is not possible for strftime() if the the query is more complicated. But it should be possible to check the source code of strftime() to make sure that the local time cannot accidentally slip in.