Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.

Commit c261b1b

Browse files
committed
fix for UTC/local time DST issue
1 parent bcf7875 commit c261b1b

2 files changed

Lines changed: 106 additions & 36 deletions

File tree

static/scripts/testcase.js

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ $("#ttpform").submit(function(e) {
193193
}
194194
});
195195

196+
// Convert relevant dates to UTC
197+
convertLocalToUtc("#time-start", formData, 'starttime');
198+
convertLocalToUtc("#time-end", formData, 'endtime');
199+
convertLocalToUtc("#time-alerttime", formData, 'alerttime');
200+
convertLocalToUtc("#time-preventtime", formData, 'preventtime');
201+
convertLocalToUtc("#time-incidenttime", formData, 'incidenttime');
202+
196203
fetch(e.target.action, {
197204
method: 'POST',
198205
body: formData
@@ -222,41 +229,20 @@ $("#ttpform").submit(function(e) {
222229
});
223230
});
224231

225-
// Convert UTC DB time to local time
226-
$( document ).ready(function() {
227-
offset = new Date().getTimezoneOffset()
228-
$("#timezone").val(offset)
229-
230-
if ($("#time-start").val()) {
231-
startTime = new Date($("#time-start").val());
232-
startTime.setMinutes(startTime.getMinutes() - offset * 2);
233-
$("#time-start").val(startTime.toISOString().slice(0,16))
234-
}
235-
236-
if ($("#time-end").val()) {
237-
endTime = new Date($("#time-end").val());
238-
endTime.setMinutes(endTime.getMinutes() - offset * 2);
239-
$("#time-end").val(endTime.toISOString().slice(0,16))
240-
}
241-
242-
if ($("#time-alerttime").val()) {
243-
endTime = new Date($("#time-alerttime").val());
244-
endTime.setMinutes(endTime.getMinutes() - offset * 2);
245-
$("#time-alerttime").val(endTime.toISOString().slice(0,16))
246-
}
247-
248-
if ($("#time-preventtime").val()) {
249-
endTime = new Date($("#time-preventtime").val());
250-
endTime.setMinutes(endTime.getMinutes() - offset * 2);
251-
$("#time-preventtime").val(endTime.toISOString().slice(0,16))
252-
}
253232

254-
if ($("#time-incidenttime").val()) {
255-
endTime = new Date($("#time-incidenttime").val());
256-
endTime.setMinutes(endTime.getMinutes() - offset * 2);
257-
$("#time-incidenttime").val(endTime.toISOString().slice(0,16))
258-
}
233+
// functions and calls to change DB UTC to local time, respecting DST
234+
$(document).ready(function() {
235+
const fieldsToConvert = [
236+
"#time-start",
237+
"#time-end",
238+
"#time-alerttime",
239+
"#time-preventtime",
240+
"#time-incidenttime"
241+
];
259242

243+
fieldsToConvert.forEach(selector => {
244+
displayUtcAsLocal(selector);
245+
});
260246
});
261247

262248
// Alter timestamps, button labels and state when hitting run button
@@ -414,4 +400,88 @@ function toggleVariablesinCodeBlocks() {
414400
});
415401
}
416402
//execute the function. Not sure where to put it else.
417-
toggleVariablesinCodeBlocks()
403+
toggleVariablesinCodeBlocks()
404+
405+
// Function to convert local time to UTC respecting DST for past and future dates
406+
function convertLocalToUtc(inputId, formData, fieldName) {
407+
const elementId = inputId.startsWith('#') ? inputId.slice(1) : inputId;
408+
const localTimeInput = document.getElementById(elementId);
409+
410+
// Validate that the element exists
411+
if (!localTimeInput) {
412+
return false; // Indicate failure: element not found
413+
}
414+
415+
const localValue = localTimeInput.value;
416+
417+
// Handle empty input value
418+
if (!localValue) {
419+
return true;
420+
}
421+
422+
// Create Date object from the input value (JS interprets this as local time)
423+
const localDate = new Date(localValue);
424+
425+
// Validate the created Date object
426+
// isNaN(date.getTime()) is the standard way to check for an "Invalid Date"
427+
if (isNaN(localDate.getTime())) {
428+
// console.error(`[convertLocalToUtc] Invalid date value "${localValue}" in input ${inputId}. Cannot convert.`);
429+
return false; // Indicate failure: invalid date
430+
}
431+
432+
// Convert directly to UTC ISO string
433+
// .toISOString() correctly handles the conversion from the local time
434+
// represented by 'localDate' to its UTC equivalent string.
435+
// Format: "YYYY-MM-DDTHH:mm:ss.sssZ"
436+
const utcIsoString = localDate.toISOString();
437+
438+
// Slice to get the desired 'YYYY-MM-DDTHH:mm' format
439+
const utcFormattedString = utcIsoString.slice(0, 16);
440+
formData.set(fieldName, utcFormattedString);
441+
442+
// console.log(`[convertLocalToUtc] Converted ${inputId} (value: "${localValue}") to UTC: ${utcFormattedString} for field "${fieldName}"`);
443+
444+
return true;
445+
}
446+
447+
// Pads a number with a leading zero if it's less than 10.
448+
function pad(num) {
449+
return num < 10 ? '0' + num : num.toString();
450+
}
451+
452+
//Reads a UTC date/time string from an input field, converts it to the local time
453+
function displayUtcAsLocal(inputSelector) {
454+
const inputElement = $(inputSelector);
455+
if (!inputElement.length) {
456+
console.warn(`[displayUtcAsLocal] Element not found for selector: ${inputSelector}`);
457+
return; // Exit if element doesn't exist
458+
}
459+
460+
const utcValue = inputElement.val();
461+
462+
// Only proceed if there is a value in the input
463+
if (utcValue) {
464+
// Ensure the Date constructor treats the string as UTC.
465+
// Append 'Z' if it's not already there. Handles ISO formats.
466+
const utcDateString = utcValue.endsWith('Z') ? utcValue : utcValue + 'Z';
467+
const dateObj = new Date(utcDateString);
468+
469+
// Check if the date parsed correctly
470+
if (!isNaN(dateObj.getTime())) {
471+
// Get components in LOCAL time directly from the Date object
472+
const year = dateObj.getFullYear();
473+
const month = pad(dateObj.getMonth() + 1); // getMonth() is 0-indexed
474+
const day = pad(dateObj.getDate());
475+
const hours = pad(dateObj.getHours());
476+
const minutes = pad(dateObj.getMinutes());
477+
478+
// Format for datetime-local input (YYYY-MM-DDTHH:mm)
479+
const localDateTimeString = `${year}-${month}-${day}T${hours}:${minutes}`;
480+
481+
// Update the input field value with the local time string
482+
inputElement.val(localDateTimeString);
483+
// console.log(`[displayUtcAsLocal] Updated ${inputSelector}: UTC "${utcValue}" -> Local "${localDateTimeString}"`);
484+
485+
}
486+
}
487+
}

utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def applyFormTimeData (obj, form, fields):
2525
for field in fields:
2626
if field in form: # and form[field]:
2727
if form[field] and form[field] != "None":
28-
localTime = datetime.strptime(form[field], "%Y-%m-%dT%H:%M")
29-
utcTime = localTime + timedelta(minutes=int(form["timezone"]))
28+
utcTime = datetime.strptime(form[field], "%Y-%m-%dT%H:%M")
29+
# utcTime = localTime + timedelta(minutes=int(form["timezone"]))
3030
obj[field] = utcTime
3131
else:
3232
obj[field] = None

0 commit comments

Comments
 (0)