Stop clock from using local timezone

This commit is contained in:
Dale Davies
2022-02-18 09:25:25 +00:00
parent 3c4ed8ea69
commit 1b1c7d814e
2 changed files with 32 additions and 4 deletions

View File

@@ -1,18 +1,45 @@
/**
* Calculate the time, local to the requested location from
* the OpenWeather API, by passing in the number of seconds
* that location has shifted from UTC based on the timezones.
*
* @author Dale Davies <dale@daledavies.co.uk>
* @license MIT
*/
export default class Clock {
/**
* Calculate the time shifted from UTC.
*
* @param number utcshift Number of seconds to shift time from UTC.
*/
constructor(utcshift = 0) {
this.utcshift = utcshift*1000;
this.shiftedtimestamp = new Date().getTime()+this.utcshift;
this.shifteddate = new Date(this.shiftedtimestamp);
}
/**
* Return a string representing time in HH:MM format.
*
* @returns string The time string.
*/
get_formatted_time() {
const hour = String(this.shifteddate.getHours()).padStart(2, "0");
const minutes = String(this.shifteddate.getMinutes()).padStart(2, "0");
// We need to use getUTCHours and getUTC Minutes here to stop
// the Date() object adjusting the returned time relative to the
// browser's local timezone.
const hour = String(this.shifteddate.getUTCHours()).padStart(2, "0");
const minutes = String(this.shifteddate.getUTCMinutes()).padStart(2, "0");
return hour + ":" + minutes;
}
/**
* Returns just the hour.
*
* @returns number The hour.
*/
get_hour() {
return this.shifteddate.getHours();
return this.shifteddate.getUTCHours();
}
}