Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

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:

  1. Dates that represent an instant in time - timezone applies. In this document, these will be referred to as instant dates.
    1. 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.
  2. Dates that represent an instant in time only after appropriate timezone is added - In this document, these will be referred to as business dates.
    1. 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).

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 LocalDateTime 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 LocalDateTime):
    1. Make a "best guess" about the timezone to use.
      1. If the API call is respective to a user, use the timezone in the user's profile.
      2. 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).
      3. If neither, use an implementation default timezone (some configuration setting in the system).
    2. Create a new ZonedDateTime with the business date and the "best guess" timezone, and serialize that in the API response.

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 LocalDateTime 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 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.

Example 1: Instant Date

The example used here is the submitted date of a requisition.

  • Persistent Storage - in the requisitions table, there would be a submitteddate column of type timestamp with time zone.
  • Java Backend Code - in the Requisition object, there would be a submittedDate field of class ZonedDateTime. This field would store a timestamp with UTC timezone. If somehow it was not in UTC timezone, it would be saved in UTC to the database (using the ZonedDateTimeAttributeConverter).
  • API Interface
    • Serialization: since it is already in UTC timezone, it would simply be converted into a String of ISO-8601 format, then returned in the response. (This conversion would probably be done in the DTO).
    • Deserialization: convert the String into a ZonedDateTime object. If it is not in UTC timezone, create a UTC version of it. This is what is assigned to submittedDate.
  • Frontend Client
    • When displaying to the user, change the UTC timestamp into "local" time.
    • When calling the APIs, make sure timestamp is in UTC timezone.

Example 2: Business Date

The example used here is the start date of a processing period.

  • Persistent Storage - in the processing_periods table, there would be a startdate column of type text.
  • Java Backend Code - in the ProcessingPeriod object, there would be a startDate field of class LocalDateTime. When persisting, startDate would have UTC added and be converted into a string (to match the text type). When retrieving, the a ZonedDateTime object would be created based on the string from the database, then converted into LocalDateTime, which is then used for startDate.
  • 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 object. This is what is assigned to startDate.
  • Frontend Client
    • When displaying to the user, use the timestamp as-is.
    • When calling the APIs, make sure timestamp does not have 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
  • 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
  • No labels