So, there is absolutely no reason why we should be having this problem in this day and age, but we do. Our database has datetime columns, and when the dates are pulled out from the database, they are retrieved as CDT (this time of year, CST in the winter). That time is then passed as CDT to the UI through JSON. This could not be more wrong.
The time stored in the database is the time relative to the location specified in the data. So, if we have a trip going from Los Angeles at 6AM PDT to New York at 11PM EDT, then the start time will be retrieved from the database as 6AM CDT and the end time will be retrieved as 11PM CDT.
Requirements:
- The UI needs to display in the time local to the data, 6AM and 11PM in the previous example.
- The UI needs to indicate items in the near future, such as arrivals within the next 3 hours.
- When the data is edited, it needs to be input in the same manner, the user enters 6AM in Los Angeles, and 11PM in New York.
- The user also needs to be able to enter times relative to the current time, such as "H+5" for 5 hours from now.
- The UI needs to sort based on time. This is more of a nice to have as the application they are used to using doesn't do this right either.
Our current solution is just burying our head in the sand and displaying it (untested for browsers in other timezones, such as those in our California office), which is actually surprisingly effective, even though it is not at all semantically correct.
- 11PM CDT is read from the database
- It gets displayed as 11PM, and is understood by the user to be EDT
- The user edits it, puts in 10PM
- It is parsed back as 10PM CDT and saved that way, exactly as we want it.
Where the current solution fails miserably is in times relative to the current time.
- 11PM CDT is read from the database
- It gets displayed as 11PM, and is understood by the user to be EDT
- The user edits it, puts in "NOW" (assume the user's local clock is 9PM CDT)
- It is parsed back as 9PM CDT and saved that way, but it should have been 10PM because it is 10PM in New York!
I'm looking for a way to handle these five cases that isn't totally hideous. I am open to a solution in any layer (architecture detailed above), but there are constraints because we share the database with another application. If there are additional tools/frameworks that would be useful and fit with what we already have, I am open to using them.
- Database: SQL Server 2008
- API: Rails with JSON responses
- Frontend: JS + Moment + other stuff unrelated to dates
- Any attempt to correct the data and/or schema is totally out of the question as we would break the other application.
- The addition of new views/table columns/tables/stored procedures is usually possible.
- The addition of indexes is NOT allowed. The status of any more exotic features is unknown.
- There are many tables/endpoints that are affected by this problem, so any brute-force solution is going to be incredibly tedious.
- Any solution only needs to work in the Continental US.
- Note that this is not a simple timezone conversion as the timezone we get back from the database is straight up wrong, so conversions of the timezone will also be wrong.