/* Client-side conversion of a HTML time element’s human-
   readable representation into the client’s local time zone.

   Example from UTC to users visiting from CET and CST:

   UTC: <time datetime="2020-02-20T01:00Z">2020-02-20 01:00</time>
   CET: <time datetime="2020-02-20T01:00Z">2020-02-20 02:00</time>
   CST: <time datetime="2020-02-20T01:00Z">2020-02-19 21:00</time>

   (Human-readable format is of course adjustable.)

    Note that the datetime attribute (interchange format)
    doesn’t ever need changing. It should preferably
    always be in UTC (indicated by the literal "Z").
*/


// Adds a leading zero to single-digit integers to get
// nice standard zero-padded two-digit time strings.
function zero_pad(i) {
  var i = String(i);
  if (i.length < 2)
    return '0' + i;
  else
    return i;
}

function updateDisplayDate(elm, fmt_date) {
  // Check that it’s a usable time element.
  if (!elm || !elm.dateTime)
    return false;

  // Parse the ISO 8601 datetime attribute from the
  // time element, will be converted to UTC in the process.
  var date = new Date(Date.parse(elm.dateTime));

  // Turn the UTC date object into a formatted string
  // in the client’s local time zone. Adjust formatting
  // as needed (shown here formatted as YYYY-MM-DD).
  // Note that getMonth is zero-based so add one.
  var loc_date = date.getFullYear() + '-'
      + zero_pad(date.getMonth() + 1) + '-'
      + zero_pad(date.getDate());

  // Replace the time element’s text node with local
  // time representation.
  elm.innerHTML = loc_date;

  // Add tooltip to the time element to
  // indicate date conversion.
  var tooltip = '(in local time)';
  if (elm.title)
    tooltip = elm.title + ' ' + tooltip;
  elm.title = tooltip;
}

// Find the time element on page (shown here selected by
// the class name “.dt-published”). Repeat or loop over
// other time elements as needed.
var pubelm = document.querySelector('.dt-published');
updateDisplayDate(pubelm);