Dealing with legacy webservices that inconsistently localize dates can be challenging. Sometimes the dates include timezone information, but other times they do not. In this case, I need to accommodate both scenarios.
The requirement is for the dates to always use Italy's locale (UTC+1 for standard time and UTC+2 for daylight saving time). However, there are instances where the timezone is omitted from the ISO date strings returned by the services.
For example, when expecting the Italian New Year as 2018-01-01T00:00:00+0100
, the service may only provide 2018-01-01T00:00:00
.
This inconsistency causes issues in Javascript, especially when interacting with clients in different timezones or setting deadlines.
To address this issue, I aim to write a code snippet that can parse an ISO-formatted date string, assuming Italian localization if no timezone is specified.
While my current code works well for most cases (although it doesn't handle milliseconds), it fails when executed in browsers located in time zones without daylight saving time. What steps should I take next? Am I overlooking something critical?
Any help or guidance on this matter would be greatly appreciated.
/**
* Get the local timezone using standard time (no daylight saving time).
*/
Date.prototype.stdTimezoneOffset = function() {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
/**
* Check whether current time is daylight saving time.
*/
Date.prototype.isDst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
/**
* Check whether daylight saving time is observed in the current timezone.
*/
Date.prototype.isDstObserved = function() {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return jan.getTimezoneOffset() != jul.getTimezoneOffset();
}
/**
* Cross-browser parse of a date using CET as the default timezone.
*/
Date.parseFromCET = function(str) {
// Code implementation here
}