-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apollo3zehn
committed
Mar 14, 2024
1 parent
cfb0f6b
commit f6d4fe7
Showing
4 changed files
with
204 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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,94 @@ | ||
using System.Globalization; | ||
|
||
internal record struct CustomDateTimeOffset | ||
{ | ||
public CustomDateTimeOffset(DateTime dateTime, TimeSpan offset) | ||
{ | ||
if (dateTime.Kind != DateTimeKind.Unspecified) | ||
throw new Exception("Only Kind = DateTimeKind.Unspecified is allowed."); | ||
|
||
DateTime = dateTime; | ||
Offset = offset; | ||
|
||
UtcDateTime = DateTime == default && Offset > TimeSpan.Zero | ||
? new DateTime(0, DateTimeKind.Utc) | ||
: new DateTimeOffset(DateTime, Offset).UtcDateTime; | ||
} | ||
|
||
public DateTime DateTime { get; } | ||
|
||
public DateTime UtcDateTime { get; } | ||
|
||
public TimeSpan Offset { get; } | ||
|
||
public static bool TryParseExact( | ||
string input, | ||
string format, | ||
TimeSpan utcOffset, | ||
out CustomDateTimeOffset dateTimeOffset) | ||
{ | ||
dateTimeOffset = default; | ||
|
||
var result1 = DateTime.TryParseExact( | ||
input, | ||
format, | ||
default, | ||
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal | DateTimeStyles.NoCurrentDateDefault, | ||
out var tmpDateTime | ||
); | ||
|
||
var result2 = DateTimeOffset.TryParseExact( | ||
input, | ||
format, | ||
default, | ||
DateTimeStyles.AssumeUniversal, | ||
out var tmpDateTimeOffset | ||
); | ||
|
||
if (!result1 || !result2) | ||
return false; | ||
|
||
/* This happens when there is no date in the input string | ||
* (see also 'NoCurrentDateDefault', which does not work | ||
* for DateTimeOffset) | ||
*/ | ||
if (tmpDateTime.Date != tmpDateTimeOffset.UtcDateTime.Date) | ||
{ | ||
dateTimeOffset = new CustomDateTimeOffset( | ||
new DateTime(01, 01, 0001) + tmpDateTimeOffset.TimeOfDay, | ||
tmpDateTimeOffset.Offset | ||
); | ||
} | ||
|
||
else | ||
{ | ||
dateTimeOffset = FromDateTimeOffset(tmpDateTimeOffset); | ||
} | ||
|
||
/* timezone information in file path */ | ||
if ( | ||
#warning improve | ||
input.Contains("Z") || | ||
input.Contains("+")) | ||
{ | ||
// do nothing | ||
} | ||
|
||
/* no timezone information in file path */ | ||
else | ||
{ | ||
dateTimeOffset = new CustomDateTimeOffset | ||
( | ||
dateTimeOffset.DateTime, | ||
utcOffset | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static CustomDateTimeOffset FromDateTimeOffset(DateTimeOffset value) | ||
{ | ||
return new CustomDateTimeOffset(value.DateTime, value.Offset); | ||
} | ||
} |
Oops, something went wrong.