From 9336f4508a2cade9d2b15cfd76860849fe2d2335 Mon Sep 17 00:00:00 2001 From: kjac Date: Tue, 22 Mar 2016 19:35:40 +0100 Subject: [PATCH] Handle publishing in UTC --- .../Models/ArchetypeFieldsetModel.cs | 4 +-- .../ArcheTypePropertyEditor.cs | 19 ++++++++++++ app/controllers/controller.js | 30 +++++++++++++++---- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/ArchetypeFieldsetModel.cs b/app/Umbraco/Umbraco.Archetype/Models/ArchetypeFieldsetModel.cs index fba352f..d835b85 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/ArchetypeFieldsetModel.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/ArchetypeFieldsetModel.cs @@ -142,8 +142,8 @@ namespace Archetype.Models internal bool IsDisabled() { return Disabled - || (ReleaseDate.HasValue && ReleaseDate > DateTime.Now) - || (ExpireDate.HasValue && DateTime.Now > ExpireDate); + || (ReleaseDate.HasValue && ReleaseDate > DateTime.UtcNow) + || (ExpireDate.HasValue && DateTime.UtcNow > ExpireDate); } #endregion diff --git a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs index 7ae82e6..0067d54 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using Archetype.Extensions; +using Archetype.Models; using ClientDependency.Core; using Newtonsoft.Json; using Umbraco.Core; @@ -175,6 +176,10 @@ namespace Archetype.PropertyEditors var uploadedFiles = editorValue.AdditionalData.ContainsKey("files") ? editorValue.AdditionalData["files"] as IEnumerable : null; foreach (var fieldset in archetype.Fieldsets) { + // make sure the publishing dates are in UTC + fieldset.ReleaseDate = EnsureUtcDate(fieldset.ReleaseDate); + fieldset.ExpireDate = EnsureUtcDate(fieldset.ExpireDate); + // assign an id to the fieldset if it has none (e.g. newly created fieldset) fieldset.Id = fieldset.Id == Guid.Empty ? Guid.NewGuid() : fieldset.Id; // find the corresponding fieldset in the current Archetype value (if any) @@ -237,6 +242,20 @@ namespace Archetype.PropertyEditors ? new ArchetypePropertyEditor() : (PropertyEditor) new TextboxPropertyEditor(); } + + /// + /// Ensures that a datetime is in UTC + /// + /// The datetime + /// The datetime in UTC (or null if it's not set) + private DateTime? EnsureUtcDate(DateTime? dateTime) + { + if(dateTime.HasValue == false || dateTime.Value.Kind == DateTimeKind.Utc) + { + return dateTime; + } + return new DateTime(dateTime.Value.Ticks, DateTimeKind.Utc); + } } #endregion diff --git a/app/controllers/controller.js b/app/controllers/controller.js index 71e22c0..84824f9 100644 --- a/app/controllers/controller.js +++ b/app/controllers/controller.js @@ -281,6 +281,8 @@ angular.module("umbraco").controller("Imulus.ArchetypeController", function ($sc if ($scope.canPublish() === false) { return false; } + // NOTE: all comparison is done in local datetime + // - that's fine because the selected local datetimes will be converted to UTC datetimes when submitted if (fieldset.expireDateModel && fieldset.expireDateModel.value) { // an expired release affects the fieldset return moment() > moment(fieldset.expireDateModel.value); @@ -321,16 +323,18 @@ angular.module("umbraco").controller("Imulus.ArchetypeController", function ($sc } function addCustomPropertiesToFieldset(fieldset) { - // create models for publish configuration + + // create models for publish configuration (utilizing the built-in datepicker data type) + // NOTE: all datetimes must be converted from UTC to local fieldset.releaseDateModel = { alias: _.uniqueId("archetypeReleaseDate_"), view: "datepicker", - value: fieldset.releaseDate + value: fromUtc(fieldset.releaseDate) }; fieldset.expireDateModel = { alias: _.uniqueId("archetypeExpireDate_"), view: "datepicker", - value: fieldset.expireDate + value: fromUtc(fieldset.expireDate) }; } @@ -555,10 +559,24 @@ angular.module("umbraco").controller("Imulus.ArchetypeController", function ($sc } $scope.submitWatcherOnSubmit = function () { _.each($scope.model.value.fieldsets, function(fieldset) { - // extract the publish configuration from the fieldsets - fieldset.releaseDate = fieldset.releaseDateModel.value; - fieldset.expireDate = fieldset.expireDateModel.value; + // extract the publish configuration from the fieldsets (and convert local datetimes to UTC) + fieldset.releaseDate = toUtc(fieldset.releaseDateModel.value); + fieldset.expireDate = toUtc(fieldset.expireDateModel.value); }); $scope.$broadcast("archetypeFormSubmitting"); } + + function toUtc(date) { + if (!date) { + return null; + } + return moment(date, "YYYY-MM-DD HH:mm:ss").utc().toDate(); + } + + function fromUtc(date) { + if (!date) { + return null; + } + return moment(moment.utc(date).toDate()).format("YYYY-MM-DD HH:mm:ss") + } });