From 0d4861fc69e3e6c0e5f1011e910ca85ea364b33b Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Sat, 21 Sep 2024 21:04:45 +0800 Subject: [PATCH] Use CultureInfo.InvariantCulture for date formatting Added System.Globalization namespace and updated date formatting to use CultureInfo.InvariantCulture in CreatePostCommand.cs, UpdatePostCommand.cs, and Seed.cs. This ensures consistent date string formatting across different cultures. --- src/Moonglade.Core/PostFeature/CreatePostCommand.cs | 5 +++-- src/Moonglade.Core/PostFeature/UpdatePostCommand.cs | 3 ++- src/Moonglade.Data/Seed.cs | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Moonglade.Core/PostFeature/CreatePostCommand.cs b/src/Moonglade.Core/PostFeature/CreatePostCommand.cs index c4bc7514a..18aa8afd7 100644 --- a/src/Moonglade.Core/PostFeature/CreatePostCommand.cs +++ b/src/Moonglade.Core/PostFeature/CreatePostCommand.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Configuration; +using System.Globalization; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Moonglade.Configuration; using Moonglade.Data; @@ -53,7 +54,7 @@ public async Task Handle(CreatePostCommand request, CancellationToke IsOutdated = request.Payload.IsOutdated, }; - post.RouteLink = $"{post.PubDateUtc.GetValueOrDefault():yyyy/M/d}/{request.Payload.Slug}"; + post.RouteLink = $"{post.PubDateUtc.GetValueOrDefault().ToString("yyyy/M/d", CultureInfo.InvariantCulture)}/{request.Payload.Slug}"; // check if exist same slug under the same day var todayUtc = DateTime.UtcNow.Date; diff --git a/src/Moonglade.Core/PostFeature/UpdatePostCommand.cs b/src/Moonglade.Core/PostFeature/UpdatePostCommand.cs index 1422ee220..1a388b48a 100644 --- a/src/Moonglade.Core/PostFeature/UpdatePostCommand.cs +++ b/src/Moonglade.Core/PostFeature/UpdatePostCommand.cs @@ -6,6 +6,7 @@ using Moonglade.Data; using Moonglade.Data.Specifications; using Moonglade.Utils; +using System.Globalization; namespace Moonglade.Core.PostFeature; @@ -92,7 +93,7 @@ public async Task Handle(UpdatePostCommand request, CancellationToke post.IsFeatured = postEditModel.Featured; post.HeroImageUrl = string.IsNullOrWhiteSpace(postEditModel.HeroImageUrl) ? null : Helper.SterilizeLink(postEditModel.HeroImageUrl); post.IsOutdated = postEditModel.IsOutdated; - post.RouteLink = $"{post.PubDateUtc.GetValueOrDefault():yyyy/M/d}/{postEditModel.Slug}"; + post.RouteLink = $"{post.PubDateUtc.GetValueOrDefault().ToString("yyyy/M/d", CultureInfo.InvariantCulture)}/{postEditModel.Slug}"; // 1. Add new tags to tag lib var tags = string.IsNullOrWhiteSpace(postEditModel.Tags) ? diff --git a/src/Moonglade.Data/Seed.cs b/src/Moonglade.Data/Seed.cs index 498cf68f5..e49d8e772 100644 --- a/src/Moonglade.Data/Seed.cs +++ b/src/Moonglade.Data/Seed.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using Moonglade.Data.Entities; +using System.Globalization; namespace Moonglade.Data; @@ -48,7 +49,7 @@ public static async Task SeedAsync(BlogDbContext dbContext, ILogger logger, int ContentLanguageCode = "en-us", Tags = dbContext.Tag.ToList(), PostCategory = dbContext.PostCategory.ToList(), - RouteLink = $"{DateTime.UtcNow:yyyy/M/d}/welcome-to-moonglade" + RouteLink = $"{DateTime.UtcNow.ToString("yyyy/M/d", CultureInfo.InvariantCulture)}/welcome-to-moonglade" }; await dbContext.Post.AddAsync(post);