This repository has been archived by the owner on Dec 18, 2023. It is now read-only.
forked from tiaringhio/Surreal.NET
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Du-z/ConversionTests
- Loading branch information
Showing
22 changed files
with
854 additions
and
331 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
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,54 @@ | ||
namespace SurrealDB.Json; | ||
|
||
public static class TimeZoneHelper { | ||
private static Dictionary<TimeSpan, TimeZoneInfo>? s_cacheTz; | ||
private static readonly object s_cacheTzLock = new(); | ||
|
||
private static IReadOnlyDictionary<TimeSpan, TimeZoneInfo> GetTimeZones() { | ||
Dictionary<TimeSpan, TimeZoneInfo> cache; | ||
lock (s_cacheTzLock) { | ||
cache = EnsureTzCacheUnchecked(); | ||
} | ||
|
||
return cache; | ||
} | ||
|
||
private static Dictionary<TimeSpan, TimeZoneInfo> EnsureTzCacheUnchecked() { | ||
Dictionary<TimeSpan, TimeZoneInfo>? cache = s_cacheTz; | ||
if (cache is null || cache.Count <= 0) { | ||
cache = TimeZoneInfo.GetSystemTimeZones().ToDictionary(static tz => tz.BaseUtcOffset); | ||
s_cacheTz = cache; | ||
} | ||
|
||
return cache; | ||
} | ||
|
||
private static TimeZoneInfo AddTimeZone(string name, in TimeSpan off) { | ||
lock (s_cacheTzLock) { | ||
Dictionary<TimeSpan, TimeZoneInfo> cache = EnsureTzCacheUnchecked(); | ||
if (cache.TryGetValue(off, out TimeZoneInfo? tz)) { | ||
return tz; | ||
} | ||
tz = TimeZoneInfo.CreateCustomTimeZone(name, off, null, null, null, null, false); | ||
cache[off] = tz; | ||
return tz; | ||
} | ||
} | ||
|
||
public static TimeZoneInfo FromOffset(in TimeSpan offset) { | ||
return GetTimeZones().TryGetValue(offset, out TimeZoneInfo? tz) ? tz : AddTimeZone(offset.ToString(), in offset); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the <see cref="DateTime"/> to a <see cref="DateTimeOffset"/> by adding a offset. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method ignores the <see cref="DateTimeKind"/> property and assumes UTC time by setting <see cref="DateTimeKind.Unspecified"/>! | ||
/// </remarks> | ||
public static DateTimeOffset WithOffset(in this DateTime dt, in TimeSpan offset) { | ||
if (dt.Kind != DateTimeKind.Unspecified) { | ||
return new(new DateTime(dt.Ticks), offset); | ||
} | ||
return new(dt, offset); | ||
} | ||
} |
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
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
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
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
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,92 @@ | ||
using System.Globalization; | ||
|
||
namespace SurrealDB.Json.Numbers; | ||
|
||
internal static class SpecialNumbers { | ||
public static string NaN { get; } = NumberFormatInfo.InvariantInfo.NaNSymbol; | ||
public static string PosinfInv { get; } = NumberFormatInfo.InvariantInfo.PositiveInfinitySymbol; | ||
public static string NeginfInv { get; } = NumberFormatInfo.InvariantInfo.NegativeInfinitySymbol; | ||
public static string PosinfCur { get; } = NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol; | ||
public static string NeginfCur { get; } = NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol; | ||
public static string Posinf => "∞"; // ∞ | ||
public static string Neginf => "−∞"; // −∞ | ||
|
||
private static bool IsNegInf(string special) { | ||
return special.Equals(Neginf, StringComparison.OrdinalIgnoreCase) || special.Equals(NeginfCur, StringComparison.OrdinalIgnoreCase) || special.Equals(NeginfInv, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
private static bool IsPosInf(string special) { | ||
return special.Equals(Posinf, StringComparison.OrdinalIgnoreCase) || special.Equals(PosinfCur, StringComparison.OrdinalIgnoreCase) || special.Equals(PosinfInv, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public static float ToSingle(string special) { | ||
if (special.Equals(NaN, StringComparison.OrdinalIgnoreCase)) { | ||
return Single.NaN; | ||
} | ||
|
||
if (IsPosInf(special)) { | ||
return Single.PositiveInfinity; | ||
} | ||
|
||
if (IsNegInf(special)) { | ||
return Single.NegativeInfinity; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public static string? ToSpecial(in float value) { | ||
if (Single.IsNaN(value)) { | ||
return NaN; | ||
} | ||
|
||
if (Single.IsPositiveInfinity(value)) { | ||
return Posinf; | ||
} | ||
|
||
if (Single.IsNegativeInfinity(value)) { | ||
return Neginf; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static double ToDouble(string special) { | ||
if (special.Equals(NaN, StringComparison.OrdinalIgnoreCase)) { | ||
return Double.NaN; | ||
} | ||
|
||
if (IsPosInf(special)) { | ||
return Double.PositiveInfinity; | ||
} | ||
|
||
if (IsNegInf(special)) { | ||
return Double.NegativeInfinity; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public static string? ToSpecial(in double value) { | ||
if (Double.IsNaN(value)) { | ||
return NaN; | ||
} | ||
|
||
if (Double.IsPositiveInfinity(value)) { | ||
return Posinf; | ||
} | ||
|
||
if (Double.IsNegativeInfinity(value)) { | ||
return Neginf; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static decimal ToDecimal(string str) { | ||
return default; | ||
} | ||
public static string? ToSpecial(in decimal value) { | ||
return null; | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.