-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Postgres-nio date handling with Swift does not appear to work as one would expect #514
Comments
I think this fixes the issue - I assume for some reason when the date if fetched from Postgres the Swift date has some slight rounding difference so a subsequent comparison fails. Strange given we are talking Date not Timestamp.
|
Hmm - I was mistaken - it seems I am getting different behaviour depending on where the server is hosted. Testing on my local laptop seems to behave differently to a Postgres server running in AWS. Any pointers as to have to ensure that date comparisons are always done using the same timezone - I am assuming that is possible the source of the problem. Still seems odd that fetching a Date from the server and then comparing that same value with the same record set does not match the record. |
It seems the only way to reliably compare dates when using a Swift Date is to do the following: WHERE somedate = to_date(swiftdatestring, 'yyyy-mm-dd') Would love to know if there is a cleaner way to use native swift dates with Postgres-nio. |
Not a "you" issue, there should definitely be better docs etc... about these, but in the meantime you can take a look at the tests: I personally was pleasantly surprised that PostgresNIO "just works" when decoding our |
Not sure this is the same issue as fetching a Date value from a table and storing into a Swift Date variable and then doing a query and passing in the Swift variable as part of the WHERE clause. So it would require a slightly different test and also would need to be tested in difference scenarios - i.e with server in a different timezone to the client application. In my case using Swift Date variables resulted in different behaviour when using a local database (i.e. same computer as the client application) vs using an AWS based Postgres server. What is the easiest way to build and run the tests - I will see if I can replicate the issue via a test - this doesn't seem to be set up as an Xcode project. ? |
Here is the test I used
Results below:
Fetching all launchperiods from AWS server
The table data was loaded from the same csv file using the /copy command "681B6C81-9E98-4005-8E58-05B7794E2CAC","2024-01-15 13:37:57.834",69,"August 2024","2024-07-30 00:00:00.000","2024-08-26 00:00:00.000",1 |
Describe the issue
When fetching a date value from a database table and then using that same value to find the record containing that value the query fails to fid a matching record
Vapor version
N/A
Operating system and version
macOS 14
Swift version
Swift 5.10.0-dev
Steps to reproduce
Create a Postgres table with records having a date field e.g. "startDate"
Query the database using PostgresClient and save the value into a Swift Date variable.
Now run a query to fetch the records with "statDate" field equals the date just retrieved.
This appears to fail - perhaps because the Swift internal format is different to the Postgres format.
Outcome
If I cast the input value using to_date(value, 'yyyy-mm-dd') e.g.
SELECT * FROM DATETABLE
WHERE startDate=to_date(value, 'yyyy-mm-dd')
then it seems to work.
I would have expected that Postgres-nio would correctly handle the necessary conversion when constructing the SQL statements.
Additional notes
No response
The text was updated successfully, but these errors were encountered: