Problem Description
OpenLMIS v3 has certain data that stores dates and times (timestamps). There is a question about how that data should be stored in the database, used in the backend, exposed through the API and used by clients (UI). There are two main types of data that would be stored using timestamps:
- Dates that represent an instant in time - timezone applies. In this document, these will be referred to as instant dates.
- An example here would be the submitted date field of a requisition. This is the specific point in time where the requisition was submitted for authorization/approval.
- Dates that represent an instant in time only after appropriate timezone is added - In this document, these will be referred to as business dates.
- An example here would be the start date of a program. This is a date that represents when a program was started. It only becomes an instant in time when an appropriate timezone is added (in this case, an implementation's default timezone).
In v2, most dates in the system were not timezone-aware. As a result, assumptions about timezone would be made, usually some system default that would be different in different contexts (database, Java, browser). There could also be confusion about exactly "when" something occurred, without timezone data.
As v3 development is underway, this pattern continues; many fields in the system are not timezone aware, or system defaults are used, which could potentially create issues (database might have one timezone default, while a microservice Java default has another, which could be different from a client's timezone, etc.).
Dates with Timezone Proposal
Persistent Storage
Since we are currently using Postgres as our backend database, Postgres' timestamp with time zone data type should be used for instant dates, and text data type for business dates.
Java backend code
Java 8's ZonedDateTime should be used when dealing with instant dates (and generally be in UTC), and LocalDate should be used when dealing with business dates.
Custom attribute converters should be defined for both to convert between Java and the database. If necessary, these converters should "translate" dates into UTC timezone before persisting.
Translation details:
- For an instant date, a UTC version of it would be created, which would be the one persisted to the database.
- For a business date, a UTC ZonedDateTime object would be created based on it, then it would be converted into a string, which would be persisted to the database.
Note: a ZonedDateTimeAttributeConverter has already been implemented in the Reference Data Service for use. This converts between ZonedDateTime and java.sql.Timestamp. However, the instant date fields that are of type ZonedDateTime will also need a JPA annotation to explicitly define the column as "timestamp with time zone". See https://github.com/OpenLMIS/openlmis-referencedata/blob/master/src/main/java/org/openlmis/referencedata/domain/SupportedProgram.java's startDate field for an example.
API Interface
During serialization (returning dates through the API):
- Dates should be serialized into a ISO-8601 formatted string. This is because when Jackson serializes date classes, it turns them into an array of values, which is not as useful or readable.
- Instant dates should return a timestamp string with timezone UTC.
- Business dates should return a timestamp string with an appropriate timezone. Rough steps to add the appropriate timezone (this is to avoid using a default timezone from LocalDate):
- Make a "best guess" about the timezone to use
- If the API call is respective to a user, use the timezone in the user's profile
- If respective to a facility, use the facility's timezone (Note: this would be a new feature, as facility timezone profiles were not in v2)
- If neither, use an implementation default timezone (some configuration setting in the system)
- Create a new ZonedDateTime with the business date and the "best guess" timezone, and serialize that in the API response
- Make a "best guess" about the timezone to use
During deserialization (when dates are provided by a client to an API call):
- Instant dates should be in timezone UTC, which would deserialize into a ZonedDateTime object. If it is not in UTC, it would be "translated" into UTC.
- Business dates should not have timezone information, and would deserialize into a LocalDate object.
Frontend Client Usage (i.e. AngularJS UI)
Clients using API calls to display timestamps to user
- For instant dates, the client would know the user (or browser's) timezone and would "translate" the UTC timestamp into a local time for the user (or browser).
- For business dates, since the timestamp has already been set to the appropriate timezone, the client would simply display the timestamp to the user.
Clients making API calls
- For instant dates, the client should send timestamps in UTC timezone.
- For business dates, the client should send timestamps with no timezone information.
Appendix: Survey of Usage
Places where dates and times are being used currently (as of 2016-12-19 / 2016-12-20):
- Reference Data
- Facility
- goLiveDate - Date
- goDownDate - Date
- RefDataErrorHandling - uses Date
- ProcessingPeriod
- startDate - LocalDate
- endDate - LocalDate
- controller (search), validator, service, repository, custom, impl classes use these
- LocalDatePersistenceConverter, LocalDateTimePersistenceConverter - uses LocalDate and java.sql.Date
- SupportedProgram
- startDate - ZonedDateTime
- SupportedProgramDto
- This is an unusual situation because is being serialized to a string, with no time or timezone information and therefore when deserialized, it is essentially a LocalDateTime, which needs to be converted to a ZonedDateTime. We might need to serialize it to a timezone-based string.
- ProcessingSchedule
- modifiedDate - LocalDateTime
- Facility
- Auth
- CustomTokenServices - uses Date to get now + validitySeconds to determine when token expires
- PasswordResetToken
- expiryDate - LocalDateTime
- Requisition
- FacilityDto, ProcessingPeriodDto, SupportedProgramDto, CommentDto, ProcessingScheduleDto, RequisitionDto
- LocalDatePersistenceConverter, LocalDateTimePersistenceConverter - uses LocalDate and java.sql.Date
- BaseTimestampedEntity (Comment, Requisition, RequisitionTemplate)
- createdDate - LocalDateTime
- Requisition controller (search), service, repository, custom, impl classes
- FacilitySupportsProgramHelper - uses ZonedDateTime
- Fulfillment
- FacilityDto, ProcessingPeriodDto, ProcessingScheduleDto
- LocalDatePersistenceConverter, LocalDateTimePersistenceConverter - uses LocalDate and java.sql.Date
- ProofOfDelivery
- receivedDate - LocalDate
- OrderCsvHelper - uses LocalDate and LocalDateTime for order file columns
- Order
- createdDate - LocalDateTime
- OrderService - uses LocalDateTime from order
- Notification
- N/A