|
1 |
| -/* |
2 |
| - * The contents of this file are subject to the Initial |
3 |
| - * Developer's Public License Version 1.0 (the "License"); |
4 |
| - * you may not use this file except in compliance with the |
5 |
| - * License. You may obtain a copy of the License at |
6 |
| - * https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt. |
7 |
| - * |
8 |
| - * Software distributed under the License is distributed on |
9 |
| - * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either |
10 |
| - * express or implied. See the License for the specific |
11 |
| - * language governing rights and limitations under the License. |
12 |
| - * |
13 |
| - * All Rights Reserved. |
14 |
| - */ |
15 |
| - |
16 |
| -//$Authors = Jiri Cincura ([email protected]) |
17 |
| - |
18 |
| -using System; |
19 |
| -using System.Runtime.InteropServices; |
20 |
| - |
21 |
| -namespace FirebirdSql.Data.Types; |
22 |
| - |
23 |
| -[StructLayout(LayoutKind.Auto)] |
24 |
| -public readonly struct FbZonedDateTime : IEquatable<FbZonedDateTime> |
25 |
| -{ |
26 |
| - public DateTime DateTime { get; } |
27 |
| - public string TimeZone { get; } |
28 |
| - public TimeSpan? Offset { get; } |
29 |
| - |
30 |
| - internal FbZonedDateTime(DateTime dateTime, string timeZone, TimeSpan? offset) |
31 |
| - { |
32 |
| - if (dateTime.Kind != DateTimeKind.Utc) |
33 |
| - throw new ArgumentException("Value must be in UTC.", nameof(dateTime)); |
34 |
| - if (timeZone == null) |
35 |
| - throw new ArgumentNullException(nameof(timeZone)); |
36 |
| - if (string.IsNullOrWhiteSpace(timeZone)) |
37 |
| - throw new ArgumentException(nameof(timeZone)); |
38 |
| - |
39 |
| - DateTime = dateTime; |
40 |
| - TimeZone = timeZone; |
41 |
| - Offset = offset; |
42 |
| - } |
43 |
| - |
44 |
| - public FbZonedDateTime(DateTime dateTime, string timeZone) |
45 |
| - : this(dateTime, timeZone, null) |
46 |
| - { } |
47 |
| - |
48 |
| - public override string ToString() |
49 |
| - { |
50 |
| - if (Offset != null) |
51 |
| - { |
52 |
| - return $"{DateTime} {TimeZone} ({Offset})"; |
53 |
| - } |
54 |
| - return $"{DateTime} {TimeZone}"; |
55 |
| - } |
56 |
| - |
57 |
| - public override bool Equals(object obj) |
58 |
| - { |
59 |
| - return obj is FbZonedDateTime fbZonedDateTime && Equals(fbZonedDateTime); |
60 |
| - } |
61 |
| - |
62 |
| - public override int GetHashCode() |
63 |
| - { |
64 |
| - unchecked |
65 |
| - { |
66 |
| - var hash = (int)2166136261; |
67 |
| - hash = (hash * 16777619) ^ DateTime.GetHashCode(); |
68 |
| - hash = (hash * 16777619) ^ TimeZone.GetHashCode(); |
69 |
| - if (Offset != null) |
70 |
| - hash = (hash * 16777619) ^ Offset.GetHashCode(); |
71 |
| - return hash; |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - public bool Equals(FbZonedDateTime other) => DateTime.Equals(other.DateTime) && TimeZone.Equals(other.TimeZone, StringComparison.OrdinalIgnoreCase); |
76 |
| - |
77 |
| - public static bool operator ==(FbZonedDateTime lhs, FbZonedDateTime rhs) => lhs.Equals(rhs); |
78 |
| - |
79 |
| - public static bool operator !=(FbZonedDateTime lhs, FbZonedDateTime rhs) => lhs.Equals(rhs); |
80 |
| -} |
| 1 | +/* |
| 2 | + * The contents of this file are subject to the Initial |
| 3 | + * Developer's Public License Version 1.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the |
| 5 | + * License. You may obtain a copy of the License at |
| 6 | + * https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt. |
| 7 | + * |
| 8 | + * Software distributed under the License is distributed on |
| 9 | + * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either |
| 10 | + * express or implied. See the License for the specific |
| 11 | + * language governing rights and limitations under the License. |
| 12 | + * |
| 13 | + * All Rights Reserved. |
| 14 | + */ |
| 15 | + |
| 16 | +//$Authors = Jiri Cincura ([email protected]) |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Runtime.InteropServices; |
| 20 | + |
| 21 | +namespace FirebirdSql.Data.Types; |
| 22 | + |
| 23 | +[StructLayout(LayoutKind.Auto)] |
| 24 | +public readonly struct FbZonedDateTime : IEquatable<FbZonedDateTime>, IConvertible |
| 25 | +{ |
| 26 | + public DateTime DateTime { get; } |
| 27 | + public string TimeZone { get; } |
| 28 | + public TimeSpan? Offset { get; } |
| 29 | + |
| 30 | + internal FbZonedDateTime(DateTime dateTime, string timeZone, TimeSpan? offset) |
| 31 | + { |
| 32 | + if (dateTime.Kind != DateTimeKind.Utc) |
| 33 | + throw new ArgumentException("Value must be in UTC.", nameof(dateTime)); |
| 34 | + if (timeZone == null) |
| 35 | + throw new ArgumentNullException(nameof(timeZone)); |
| 36 | + if (string.IsNullOrWhiteSpace(timeZone)) |
| 37 | + throw new ArgumentException(nameof(timeZone)); |
| 38 | + |
| 39 | + DateTime = dateTime; |
| 40 | + TimeZone = timeZone; |
| 41 | + Offset = offset; |
| 42 | + } |
| 43 | + |
| 44 | + public FbZonedDateTime(DateTime dateTime, string timeZone) |
| 45 | + : this(dateTime, timeZone, null) |
| 46 | + { } |
| 47 | + |
| 48 | + public override string ToString() |
| 49 | + { |
| 50 | + if (Offset != null) |
| 51 | + { |
| 52 | + return $"{DateTime} {TimeZone} ({Offset})"; |
| 53 | + } |
| 54 | + return $"{DateTime} {TimeZone}"; |
| 55 | + } |
| 56 | + |
| 57 | + public override bool Equals(object obj) |
| 58 | + { |
| 59 | + return obj is FbZonedDateTime fbZonedDateTime && Equals(fbZonedDateTime); |
| 60 | + } |
| 61 | + |
| 62 | + public override int GetHashCode() |
| 63 | + { |
| 64 | + unchecked |
| 65 | + { |
| 66 | + var hash = (int)2166136261; |
| 67 | + hash = (hash * 16777619) ^ DateTime.GetHashCode(); |
| 68 | + hash = (hash * 16777619) ^ TimeZone.GetHashCode(); |
| 69 | + if (Offset != null) |
| 70 | + hash = (hash * 16777619) ^ Offset.GetHashCode(); |
| 71 | + return hash; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public bool Equals(FbZonedDateTime other) => DateTime.Equals(other.DateTime) && TimeZone.Equals(other.TimeZone, StringComparison.OrdinalIgnoreCase); |
| 76 | + |
| 77 | + TypeCode IConvertible.GetTypeCode() => TypeCode.Object; |
| 78 | + |
| 79 | + DateTime IConvertible.ToDateTime(IFormatProvider provider) => DateTime; |
| 80 | + |
| 81 | + string IConvertible.ToString(IFormatProvider provider) => ToString(); |
| 82 | + |
| 83 | + object IConvertible.ToType(Type conversionType, IFormatProvider provider) |
| 84 | + => ReferenceEquals(conversionType, typeof(FbZonedDateTime)) |
| 85 | + ? this |
| 86 | + : throw new InvalidCastException(conversionType?.FullName); |
| 87 | + |
| 88 | + bool IConvertible.ToBoolean(IFormatProvider provider) => throw new InvalidCastException(nameof(Boolean)); |
| 89 | + byte IConvertible.ToByte(IFormatProvider provider) => throw new InvalidCastException(nameof(Byte)); |
| 90 | + char IConvertible.ToChar(IFormatProvider provider) => throw new InvalidCastException(nameof(Char)); |
| 91 | + decimal IConvertible.ToDecimal(IFormatProvider provider) => throw new InvalidCastException(nameof(Decimal)); |
| 92 | + double IConvertible.ToDouble(IFormatProvider provider) => throw new InvalidCastException(nameof(Double)); |
| 93 | + short IConvertible.ToInt16(IFormatProvider provider) => throw new InvalidCastException(nameof(Int16)); |
| 94 | + int IConvertible.ToInt32(IFormatProvider provider) => throw new InvalidCastException(nameof(Int32)); |
| 95 | + long IConvertible.ToInt64(IFormatProvider provider) => throw new InvalidCastException(nameof(Int64)); |
| 96 | + sbyte IConvertible.ToSByte(IFormatProvider provider) => throw new InvalidCastException(nameof(SByte)); |
| 97 | + float IConvertible.ToSingle(IFormatProvider provider) => throw new InvalidCastException(nameof(Single)); |
| 98 | + ushort IConvertible.ToUInt16(IFormatProvider provider) => throw new InvalidCastException(nameof(UInt16)); |
| 99 | + uint IConvertible.ToUInt32(IFormatProvider provider) => throw new InvalidCastException(nameof(UInt32)); |
| 100 | + ulong IConvertible.ToUInt64(IFormatProvider provider) => throw new InvalidCastException(nameof(UInt64)); |
| 101 | + |
| 102 | + public static bool operator ==(FbZonedDateTime lhs, FbZonedDateTime rhs) => lhs.Equals(rhs); |
| 103 | + |
| 104 | + public static bool operator !=(FbZonedDateTime lhs, FbZonedDateTime rhs) => lhs.Equals(rhs); |
| 105 | +} |
0 commit comments