Skip to content

Commit

Permalink
add support for Windows timezones,
Browse files Browse the repository at this point in the history
  • Loading branch information
codepoet80 committed Jan 1, 2023
1 parent e9e585b commit 6a4d105
Show file tree
Hide file tree
Showing 6 changed files with 2,462 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -7,7 +7,7 @@ distribute.cmd

tests/
tests-open/

_scratch/

node_modules
sync-framework-patches
Expand Down
Empty file modified build.sh 100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion package/packageinfo.json
Expand Up @@ -2,7 +2,7 @@
"id": "org.webosports.cdav",
"package_format_version": 2,
"loc_name": "C+DAV synergy connector",
"version": "0.3.34",
"version": "0.3.35",
"vendor": "WebOS Ports - Stefan Schmidt",
"vendorurl": "www.webos-ports.org",
"app": "org.webosports.cdav.app",
Expand Down
43 changes: 37 additions & 6 deletions service/javascript/utils/iCalTimeHandling.js
Expand Up @@ -2,6 +2,7 @@
/*global Calendar, Log, Future, checkResult */

//Only for time and timezone handling. Ahrg.
var timezoneMapper = require(libPath + "timezoneMapper.js");

var Time = (function () {
"use strict";
Expand All @@ -16,9 +17,8 @@ var Time = (function () {
TZManager = Calendar.TimezoneManager(),
TZManagerInitialized = false,
shiftAllDay = true;

/**
* Converts iCal time string of format YYYYMMDDTHHMM(Z) into javascript timestamp (from local timezone or UTC of Z is present).
* Converts iCal time string of format YYYYMMDDTHHMM(Z) into javascript timestamp (from local timezone or UTC if Z is present).
*/
function iCalTimeToWebOsTime(time) {
var t = 0, result, date, utc = time.charAt(time.length - 1) === "Z",
Expand Down Expand Up @@ -251,13 +251,43 @@ var Time = (function () {
* Normalize events to local timezone or
* event in local timezone into the timezone specified by tzId.
*/

/* Add support for Windows timezones */
function getOffsetWithMapping(year, tzString, timestampNoMillis) {
var offSet = TZManager.getOffset(year, tzString, timestampNoMillis);
if (offSet == 0) {
Log.log_icalDebug("** Offset of ", tzString, " is 0, trying mapper with ", timezoneMapper.tzMap.length, " possible matches");
var mappedTZ = timezoneMapper.mapWindowsToIANA(tzString);
if (mappedTZ) {
return TZManager.getOffset(year, mappedTZ, timestampNoMillis);
}
}
return offSet;
}
function convertTime (timestamp, srcTz, destTz) {
var defaultTz = TZManager.timezone;
var source = srcTz || defaultTz;
var dest = destTz || defaultTz;
if (source == dest)
return timestamp;

var year = (new Date(timestamp)).getFullYear();
var timestampNoMillis = timestamp / 1E3;
var destOffset = getOffsetWithMapping(year, dest, timestampNoMillis);
var sourceOffset = getOffsetWithMapping(year, source, timestampNoMillis);
Log.log_icalDebug("** getOffsetWithMapping thinks offset of ", source, " is ", sourceOffset);
Log.log_icalDebug("** getOffsetWithMapping thinks offset of ", dest, " is ", destOffset);
var convertedTime = (timestampNoMillis + destOffset - sourceOffset) * 1E3;
return convertedTime
}
/* End support for Windows timezones */

function normalizeToTimezone(events, direction) {
var future = fetchTimezones(events),
tsFields = ["dtstamp", "created", "lastModified"];

future.then(function (future) {
future.getResult();

Log.log_icalDebug("Processing ", events.length, " events.", events);
events.forEach(function (event) {
Log.log_icalDebug("normalizeTo", direction, "Timezone(): ");
Expand All @@ -270,8 +300,9 @@ var Time = (function () {
if (event.dtstart) {
Log.log_icalDebug("----CONVERTING TZ from ", source, " to ", target);
oldVal = event.dtstart;
event.dtstart = TZManager.convertTime(event.dtstart, source, target);
Log.log_icalDebug(" ", oldVal, " -> ", event.dtstart);
event.dtstart = convertTime(event.dtstart, source, target);
//Log.log_icalDebug(" ", oldVal, " -> ", event.dtstart);
Log.log_icalDebug(" ", (new Date(oldVal).toDateString() + " " + new Date(oldVal).toLocaleTimeString()), " -> ", (new Date(event.dtstart).toDateString() + " " + new Date(event.dtstart).toLocaleTimeString()));
}

if (event.dtend) {
Expand All @@ -289,7 +320,7 @@ var Time = (function () {
dt.setHours(23);
dt.setMinutes(59);
dt.setSeconds(59);
newDtend = TZManager.convertTime(dt.getTime(), source, target);
newDtend = convertTime(dt.getTime(), source, target);
Log.log_icalDebug("----DTEND DID NOT EXIST ", dt.getTime(), " -> ", newDtend);
event.dtend = newDtend;
}
Expand Down

0 comments on commit 6a4d105

Please sign in to comment.