Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/Controller.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ component extends="wheels.Controller" {
// Auto-approve and publish for admin users
params.statusId = 2;
params.status = "Approved";
params.publishedAt = now();
// Convert to UTC
params.publishedAt = toUTC(now());
} else {
params.statusId = 2; // Under Review
}
Expand Down Expand Up @@ -564,7 +565,8 @@ component extends="wheels.Controller" {
}

if (structKeyExists(params, "postCreatedDate") && len(trim(params.postCreatedDate))) {
blog.postCreatedDate = params.postCreatedDate;
// Convert the provided date to UTC before saving
blog.postCreatedDate = toUTC(params.postCreatedDate);
}

// Update tracking fields
Expand Down
16 changes: 10 additions & 6 deletions app/controllers/web/BlogController.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,12 @@ component extends="app.Controllers.Controller" {
blogData.statusId = 2;
blogData.status = "Approved";
if (structKeyExists(blogData, "postCreatedDate") && !isNull(blogData.postCreatedDate) && !isEmpty(blogData.postCreatedDate)) {
blogData.publishedAt = blogData.postCreatedDate;
// Convert the provided date to UTC before saving
blogData.publishedAt = toUTC(blogData.postCreatedDate);
} else {
blogData.publishedAt = now();
blogData.postCreatedDate = now();
// Use current UTC time
blogData.publishedAt = toUTC(now());
blogData.postCreatedDate = toUTC(now());
}
} else {
blogData.statusId = 2; // Under Review
Expand Down Expand Up @@ -988,10 +990,12 @@ component extends="app.Controllers.Controller" {
newBlog.status = blogData.status;
}
if (structKeyExists(blogData, "postCreatedDate") && !isNull(blogData.postCreatedDate) && !isEmpty(blogData.postCreatedDate)) {
newBlog.publishedAt = blogData.postCreatedDate;
newBlog.postCreatedDate = blogData.postCreatedDate;
// Convert the provided date to UTC before saving
newBlog.publishedAt = toUTC(blogData.postCreatedDate);
newBlog.postCreatedDate = toUTC(blogData.postCreatedDate);
} else {
newBlog.postCreatedDate = now();
// Use current UTC time
newBlog.postCreatedDate = toUTC(now());
}
newBlog.save();

Expand Down
11 changes: 11 additions & 0 deletions app/global/functions.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
public function GetSignedInUserId(){
return structKeyExists(session, "userID") ? session.userID : 0
}

/**
* Convert a local datetime to UTC using server's timezone offset
* @localTime The datetime to convert to UTC
* @return The datetime in UTC
*/
public datetime function toUTC(required datetime localTime) {
var tzInfo = GetTimeZoneInfo();
var offsetSeconds = tzInfo.utcTotalOffset * 60;
return dateAdd("s", -offsetSeconds, arguments.localTime);
}
public function GetUserRoleId(){
return 3;
}
Expand Down
15 changes: 15 additions & 0 deletions app/views/layout.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,21 @@
<script src="/js/swiper.js"></script>
<script src="/js/infinite-scroll.pkgd.min.js"></script>
<script src="/js/global.js"></script>
<script>
// Convert UTC dates to local timezone for elements with data-utc attribute
document.addEventListener("DOMContentLoaded", function() {
var utcElements = document.querySelectorAll('[data-utc]');
utcElements.forEach(function(el) {
var utcDate = el.getAttribute('data-utc');
if (utcDate) {
var date = new Date(utcDate);
if (!isNaN(date.getTime())) {
el.textContent = date.toLocaleString();
}
}
});
});
</script>
</body>
</html>
</cfif>
35 changes: 33 additions & 2 deletions app/views/web/BlogController/create.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<label class="form-label mb-1 fs-14 fw-medium">
Publish Date
</label>
<input class="form-control fs-14" type="datetime-local" name="postCreatedDate" id="postCreatedDate" value="<cfif isEdit>#dateFormat(blog.postCreatedDate, 'yyyy-mm-dd')#T#timeFormat(blog.postCreatedDate, 'HH:mm')#</cfif>">
<input class="form-control fs-14" type="datetime-local" name="postCreatedDate" id="postCreatedDate" value="<cfif isEdit>#dateFormat(blog.postCreatedDate, 'yyyy-mm-dd')#T#timeFormat(blog.postCreatedDate, 'HH:mm')#</cfif>" data-utc-value="<cfif isEdit>#dateFormat(blog.postCreatedDate, 'yyyy-mm-dd')#T#timeFormat(blog.postCreatedDate, 'HH:mm:ss')#</cfif>">
<small class="text-muted mt-1 d-block fs-13">Leave empty to use the current date and time</small>
</div>

Expand Down Expand Up @@ -108,4 +108,35 @@
</div>
</cfoutput>
<script src="/js/createBlog.js"></script>
</main>
<script>
// Convert UTC datetime from server to local time for display in datetime-local input
document.addEventListener("DOMContentLoaded", function() {
var postCreatedDateInput = document.getElementById('postCreatedDate');
if (postCreatedDateInput) {
var utcValue = postCreatedDateInput.getAttribute('data-utc-value');
if (utcValue) {
// Parse the UTC value from server and convert to local time
var utcDate = new Date(utcValue + 'Z');
if (!isNaN(utcDate.getTime())) {
// Convert to local datetime-local format (YYYY-MM-DDTHH:mm)
var localDate = new Date(utcDate.getTime() - (utcDate.getTimezoneOffset() * 60000));
postCreatedDateInput.value = localDate.toISOString().slice(0, 16);
}
}
}
});

// Convert local datetime to UTC before form submission via HTMX
document.addEventListener("htmx:configRequest", function(event) {
var postCreatedDateInput = document.getElementById('postCreatedDate');
if (postCreatedDateInput && postCreatedDateInput.value) {
// Parse the local datetime and convert to UTC
var localDate = new Date(postCreatedDateInput.value);
if (!isNaN(localDate.getTime())) {
// Convert to ISO string (which is in UTC) and update the form parameter
event.detail.parameters.postCreatedDate = localDate.toISOString();
}
}
});
</script>
</main>
2 changes: 1 addition & 1 deletion app/views/web/BlogController/partials/_blogList.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<p class="text--secondary fw-bold fs-14 m-0 author-name">
#htmlEditFormat(blogs.fullName)#
</p>
<time class="text--lightGray fs-12 fw-medium" datetime="#dateformat(blogs.postDate, 'yyyy-mm-dd')#">
<time class="text--lightGray fs-12 fw-medium" datetime="#dateformat(blogs.postDate, 'yyyy-mm-dd')#" data-utc="#dateFormat(blogs.postDate, 'yyyy-mm-dd HH:mm:ss')#">
#formattedDate#
</time>
</button>
Expand Down
2 changes: 1 addition & 1 deletion app/views/web/BlogController/show.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<cfif categories.recordCount GT 0 OR tags.recordCount GT 0>
<div class="d-flex flex-wrap justify-content-center flex-grow-1 align-items-end gap-lg-5 gap-2 mt-3">
<!-- Blog date -->
<p class="fw-medium fs-12 text--lightGray mb-0">
<p class="fw-medium fs-12 text--lightGray mb-0" data-utc="#dateFormat(blog.getDisplayDate(), 'yyyy-mm-dd HH:mm:ss')#">
#dateformat(blog.getDisplayDate(), 'MMMM DD, YYYY')#
</p>

Expand Down