...
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 timestampsdates (and times):
- 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 do not represent an instant in time only after appropriate timezone is added - - timezone does not apply. In this document, these will be referred to as business dates.
- An example here would be the start date of a processing period. This is a date that represents when a processing period was started. It only becomes an instant in time when an appropriate timezone is added (in this case, an implementation's default timezone)As different facilities (in different timezones) enter into this start date, the processing period has started for them.
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.
...
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 date data type for business dates.
...
Java 8's ZonedDateTime should be used when dealing with instant dates (and generally be in UTC), and LocalDateTime LocalDate should be used when dealing with business dates.
...
- 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 simply 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):
...
When it is necessary to compare the business date to an instant in time (now for example), first convert the instant into a LocalDate, making a "best guess" about the timezone to use, and then compare. Rough steps for what timezone to use:
- If the
...
- code 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).
API Interface
During serialization (returning dates through the API):
- Dates should be serialized into a ISO-8601 formatted string. Create a new ZonedDateTime with the business date and the "best guess" timezone, and serialize that in the API responseThis 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 date string (with no time or timezone).
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 time or timezone information, and would deserialize into a LocalDateTime LocalDate object.
Frontend Client (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 date 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 dates with no time or timezone information.
When it is necessary to compare the business date to an instant in time (now for example), first convert the instant into a JavaScript date, making a "best guess" about the timezone to use, and then compare. Rough steps for what timezone to use:
- If the code 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).
Example 1: Instant Date
The example used here is the submitted date of a requisition.
...
- Persistent Storage - in the processing_periods table, there would be a startdate column of type text date.
- Java Backend Code - in the ProcessingPeriod object, there would be a startDate field of class LocalDateTime LocalDate. When persisting, startDate would have UTC added and be converted into a string (to match the text type)be stored as date. When retrieving, the a ZonedDateTime LocalDate object would be created based on the string from the database, then converted into LocalDateTimedate, which is then used for startDate.
- If the startDate needed to be compared to now, to see if the processing period had already started, LocalDate.now(timezone) would be used to get a LocalDate version of now. The timezone specified would be the implementation default configuration setting (or facility timezone setting if there was one). Then the now LocalDate would be compared to startDate to determine if the startDate was before (or at) the now date.
- API Interface
- Serialization: the "best guess" timezone would be determined first. In this case, it would be the implementation default time zone. (If the facility had a timezone profile, that would be used instead.) A new ZonedDateTime object would be created using startDate and the implementation default timezone. This would be converted into a String of ISO-8601 format, then returned in the response.
- Deserialization: convert the String into a LocalDateTime LocalDate object. This is what is assigned to startDate.
- Frontend Client
- When displaying to the user, use the timestamp date as-is.
- When calling the APIs, make sure timestamp the date does not have time or timezone information.
- To compare the start date with now, the frontend client would do something similar as described in the Java Backend Code section, except using JavaScript date objects and methods.
Appendix: Survey of Usage
...