Repository navigation
Assume that the Unspecified datetime is user's local time zone.
#24049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
171f797
Redo `AbpDateTimeModelBinder` to align with the current TZ
gdlcf88 9491c46
Merge branch 'rel-10.1' into pr/24049
maliming 275b181
Enhance DateTime handling with timezone support in model binding and …
maliming d7ae2b9
Merge branch 'dev' into pr/24049
maliming 84cf25f
Refactor DateTime handling to use GetUtcOffset for accurate timezone …
maliming 028362c
Add logging for DateTime conversion failures
maliming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 14 additions & 55 deletions
69
...p.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,84 +1,43 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.Options; | ||
| using Volo.Abp.DependencyInjection; | ||
| using Volo.Abp.Timing; | ||
|
|
||
| namespace Volo.Abp.Json.SystemTextJson.JsonConverters; | ||
|
|
||
| public class AbpDateTimeConverter : JsonConverter<DateTime>, ITransientDependency | ||
| public class AbpDateTimeConverter : AbpDateTimeConverterBase<DateTime>, ITransientDependency | ||
| { | ||
| private readonly IClock _clock; | ||
| private readonly AbpJsonOptions _options; | ||
| private bool _skipDateTimeNormalization; | ||
|
|
||
| public AbpDateTimeConverter(IClock clock, IOptions<AbpJsonOptions> abpJsonOptions) | ||
| public AbpDateTimeConverter( | ||
| IClock clock, | ||
| IOptions<AbpJsonOptions> abpJsonOptions, | ||
| ICurrentTimezoneProvider currentTimezoneProvider, | ||
| ITimezoneProvider timezoneProvider) | ||
| : base(clock, abpJsonOptions, currentTimezoneProvider, timezoneProvider) | ||
| { | ||
| _clock = clock; | ||
| _options = abpJsonOptions.Value; | ||
| } | ||
|
|
||
| public virtual AbpDateTimeConverter SkipDateTimeNormalization() | ||
| { | ||
| _skipDateTimeNormalization = true; | ||
| IsSkipDateTimeNormalization = true; | ||
| return this; | ||
| } | ||
|
|
||
| public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (_options.InputDateTimeFormats.Any()) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.String) | ||
| { | ||
| foreach (var format in _options.InputDateTimeFormats) | ||
| { | ||
| var s = reader.GetString(); | ||
| if (DateTime.TryParseExact(s, format, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1)) | ||
| { | ||
| return Normalize(d1); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| throw new JsonException("Reader's TokenType is not String!"); | ||
| } | ||
| } | ||
|
|
||
| if (reader.TryGetDateTime(out var d3)) | ||
| { | ||
| return Normalize(d3); | ||
| } | ||
|
|
||
| var dateText = reader.GetString(); | ||
| if (!dateText.IsNullOrWhiteSpace()) | ||
| if (Options.InputDateTimeFormats.Any() && reader.TokenType != JsonTokenType.String) | ||
| { | ||
| if (DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d4)) | ||
| { | ||
| return Normalize(d4); | ||
| } | ||
| throw new JsonException("Reader's TokenType is not String!"); | ||
| } | ||
|
|
||
| throw new JsonException("Can't get datetime from the reader!"); | ||
| return TryReadDateTime(ref reader, out var result) | ||
| ? result | ||
| : throw new JsonException("Can't get datetime from the reader!"); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
| { | ||
| if (_options.OutputDateTimeFormat.IsNullOrWhiteSpace()) | ||
| { | ||
| writer.WriteStringValue(Normalize(value)); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteStringValue(Normalize(value).ToString(_options.OutputDateTimeFormat, CultureInfo.CurrentUICulture)); | ||
| } | ||
| } | ||
|
|
||
| protected virtual DateTime Normalize(DateTime dateTime) | ||
| { | ||
| return _skipDateTimeNormalization ? dateTime : _clock.Normalize(dateTime); | ||
| WriteDateTime(writer, value); | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
...on.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Microsoft.Extensions.Options; | ||
| using Volo.Abp.Timing; | ||
|
|
||
| namespace Volo.Abp.Json.SystemTextJson.JsonConverters; | ||
|
|
||
| public abstract class AbpDateTimeConverterBase<T> : JsonConverter<T> | ||
| { | ||
| public ILogger<AbpDateTimeConverterBase<T>> Logger { get; set; } | ||
|
|
||
| protected IClock Clock { get; } | ||
| protected AbpJsonOptions Options { get; } | ||
| protected ICurrentTimezoneProvider CurrentTimezoneProvider { get; } | ||
| protected ITimezoneProvider TimezoneProvider { get; } | ||
| protected bool IsSkipDateTimeNormalization { get; set; } | ||
|
|
||
| protected AbpDateTimeConverterBase( | ||
| IClock clock, | ||
| IOptions<AbpJsonOptions> abpJsonOptions, | ||
| ICurrentTimezoneProvider currentTimezoneProvider, | ||
| ITimezoneProvider timezoneProvider) | ||
| { | ||
| Logger = NullLogger<AbpDateTimeConverterBase<T>>.Instance; | ||
|
|
||
| Clock = clock; | ||
| CurrentTimezoneProvider = currentTimezoneProvider; | ||
| TimezoneProvider = timezoneProvider; | ||
| Options = abpJsonOptions.Value; | ||
| } | ||
|
|
||
| protected bool TryReadDateTime(ref Utf8JsonReader reader, out DateTime value) | ||
| { | ||
| value = default; | ||
|
|
||
| if (Options.InputDateTimeFormats.Any()) | ||
| { | ||
| if (reader.TokenType != JsonTokenType.String) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var s = reader.GetString(); | ||
| foreach (var format in Options.InputDateTimeFormats) | ||
| { | ||
| if (!DateTime.TryParseExact(s, format, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| value = Normalize(d1); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| if (reader.TryGetDateTime(out var d2)) | ||
| { | ||
| value = Normalize(d2); | ||
| return true; | ||
| } | ||
|
|
||
| var dateText = reader.GetString(); | ||
| if (dateText.IsNullOrWhiteSpace()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d3)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| value = Normalize(d3); | ||
| return true; | ||
|
|
||
| } | ||
|
|
||
| protected void WriteDateTime(Utf8JsonWriter writer, DateTime value) | ||
| { | ||
| if (Options.OutputDateTimeFormat.IsNullOrWhiteSpace()) | ||
| { | ||
| writer.WriteStringValue(Normalize(value)); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteStringValue(Normalize(value).ToString(Options.OutputDateTimeFormat, CultureInfo.CurrentUICulture)); | ||
| } | ||
| } | ||
|
|
||
| protected virtual DateTime Normalize(DateTime dateTime) | ||
| { | ||
| if (dateTime.Kind != DateTimeKind.Unspecified || | ||
| !Clock.SupportsMultipleTimezone || | ||
| CurrentTimezoneProvider.TimeZone.IsNullOrWhiteSpace()) | ||
| { | ||
| return IsSkipDateTimeNormalization ? dateTime : Clock.Normalize(dateTime); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var timezoneInfo = TimezoneProvider.GetTimeZoneInfo(CurrentTimezoneProvider.TimeZone); | ||
| dateTime = new DateTimeOffset(dateTime, timezoneInfo.GetUtcOffset(dateTime)).UtcDateTime; | ||
| } | ||
| catch | ||
| { | ||
| Logger.LogWarning("Could not convert DateTime with unspecified Kind using timezone '{TimeZone}'.", CurrentTimezoneProvider.TimeZone); | ||
| } | ||
|
maliming marked this conversation as resolved.
Resolved
|
||
|
|
||
| return IsSkipDateTimeNormalization ? dateTime : Clock.Normalize(dateTime); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.