From 2eb20a64841cf84f0801b79a1b38de4f969cc8d3 Mon Sep 17 00:00:00 2001 From: Torsten Schuster Date: Thu, 30 Aug 2018 22:56:57 +0200 Subject: [PATCH] fix to save all DateTime items on post and comment as scientific utc string (#85) --- src/Services/FileBlogService.cs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Services/FileBlogService.cs b/src/Services/FileBlogService.cs index e94e535e..ada0fa99 100644 --- a/src/Services/FileBlogService.cs +++ b/src/Services/FileBlogService.cs @@ -1,4 +1,7 @@ -using System; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Miniblog.Core.Models; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -7,9 +10,6 @@ using System.Threading.Tasks; using System.Xml.Linq; using System.Xml.XPath; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Miniblog.Core.Models; namespace Miniblog.Core.Services { @@ -49,7 +49,6 @@ where p.Categories.Contains(category, StringComparer.OrdinalIgnoreCase) select p; return Task.FromResult(posts); - } public virtual Task GetPostBySlug(string slug) @@ -100,8 +99,8 @@ public async Task SavePost(Post post) new XElement("post", new XElement("title", post.Title), new XElement("slug", post.Slug), - new XElement("pubDate", post.PubDate.ToString("yyyy-MM-dd HH:mm:ss")), - new XElement("lastModified", post.LastModified.ToString("yyyy-MM-dd HH:mm:ss")), + new XElement("pubDate", FormatDateTime(post.PubDate)), + new XElement("lastModified", FormatDateTime(post.LastModified)), new XElement("excerpt", post.Excerpt), new XElement("content", post.Content), new XElement("ispublished", post.IsPublished), @@ -122,7 +121,7 @@ public async Task SavePost(Post post) new XElement("comment", new XElement("author", comment.Author), new XElement("email", comment.Email), - new XElement("date", comment.PubDate.ToString("yyyy-MM-dd HH:m:ss")), + new XElement("date", FormatDateTime(comment.PubDate)), new XElement("content", comment.Content), new XAttribute("isAdmin", comment.IsAdmin), new XAttribute("id", comment.ID) @@ -207,7 +206,7 @@ private void LoadPosts() Content = ReadValue(doc, "content"), Slug = ReadValue(doc, "slug").ToLowerInvariant(), PubDate = DateTime.Parse(ReadValue(doc, "pubDate")), - LastModified = DateTime.Parse(ReadValue(doc, "lastModified", DateTime.Now.ToString(CultureInfo.InvariantCulture))), + LastModified = DateTime.Parse(ReadValue(doc, "lastModified", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture))), IsPublished = bool.Parse(ReadValue(doc, "ispublished", "true")), }; @@ -271,6 +270,16 @@ private static string ReadAttribute(XElement element, XName name, string default return defaultValue; } + + private static string FormatDateTime(DateTime dateTime) + { + const string UTC = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"; + + return dateTime.Kind == DateTimeKind.Utc + ? dateTime.ToString(UTC) + : dateTime.ToUniversalTime().ToString(UTC); + } + protected void SortCache() { _cache.Sort((p1, p2) => p2.PubDate.CompareTo(p1.PubDate)); @@ -280,6 +289,5 @@ protected bool IsAdmin() { return _contextAccessor.HttpContext?.User?.Identity.IsAuthenticated == true; } - } -} +} \ No newline at end of file