OpenCerts / legacy-templates

Apache License 2.0
0 stars 25 forks source link

fix: compute year with reference to SGT timezone #131

Closed HJunyuan closed 2 years ago

HJunyuan commented 2 years ago

Context

Received MOP feedback that the year rendered in their SEAB O-Level certificate was incorrect (2016). However, checking the .opencert file revealed the correct year (2015).

This was caused by a timezone difference depending on which country the document was rendered in. Especially so when attainmentDate is the last day and last minute of the year 2015: 2015-12-31T23:59:00+08:00

An example to illustrate issue:

/* Device on Japanese timezone (GMT +09:00) */
const date = new Date('2015-12-31T23:59:00+08:00'); // Fri Jan 01 2016 00:59:00 GMT+0900 (日本標準時)
const year = date.getFullYear(); // 2016

Fix

Incorrect as .getFullYear() will obtain the year with respect to the device's timezone:

const year = date.getFullYear();

Correct as .toLocaleDateString() allows you to specify a timezone to use:

const year = date.toLocaleDateString("en-SG", {
  timeZone: "Asia/Singapore",
  year: "numeric",
});