Skip to content

Commit

Permalink
fix to save all DateTime items on post and comment as scientific utc …
Browse files Browse the repository at this point in the history
…string (madskristensen#85)
  • Loading branch information
braegelno5 authored and madskristensen committed Aug 30, 2018
1 parent 15e0d35 commit 2eb20a6
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/Services/FileBlogService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -49,7 +49,6 @@ where p.Categories.Contains(category, StringComparer.OrdinalIgnoreCase)
select p;

return Task.FromResult(posts);

}

public virtual Task<Post> GetPostBySlug(string slug)
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -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")),
};

Expand Down Expand Up @@ -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));
Expand All @@ -280,6 +289,5 @@ protected bool IsAdmin()
{
return _contextAccessor.HttpContext?.User?.Identity.IsAuthenticated == true;
}

}
}
}

0 comments on commit 2eb20a6

Please sign in to comment.