Skip to content

Commit 67f96a7

Browse files
committed
Replace DvDateTime, DvDateTimeZone, DvTimeSpan with .NET standard datatypes.
1 parent 44c6e90 commit 67f96a7

File tree

21 files changed

+165
-288
lines changed

21 files changed

+165
-288
lines changed

src/Microsoft.ML.Api/ApiUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static OpCode GetAssignmentOpCode(Type t)
2323
t == typeof(DvBool) || t == typeof(DvText) || t == typeof(string) || t.IsArray ||
2424
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(VBuffer<>)) ||
2525
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) ||
26-
t == typeof(DvDateTime) || t == typeof(DvDateTimeZone) || t == typeof(DvTimeSpan) || t == typeof(UInt128))
26+
t == typeof(DateTime) || t == typeof(DateTimeOffset) || t == typeof(TimeSpan) || t == typeof(UInt128))
2727
{
2828
return OpCodes.Stobj;
2929
}

src/Microsoft.ML.Core/Data/ColumnType.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,47 +120,38 @@ public bool IsBool
120120
}
121121

122122
/// <summary>
123-
/// Whether this type is the standard timespan type.
123+
/// Whether this type is the standard <see cref="TimeSpan"/> type.
124124
/// </summary>
125125
public bool IsTimeSpan
126126
{
127127
get
128128
{
129-
if (!(this is TimeSpanType))
130-
return false;
131-
// TimeSpanType is a singleton.
132-
Contracts.Assert(this == TimeSpanType.Instance);
133-
return true;
129+
Contracts.Assert((this == TimeSpanType.Instance) == (this is TimeSpanType));
130+
return this is TimeSpanType;
134131
}
135132
}
136133

137134
/// <summary>
138-
/// Whether this type is a DvDateTime.
135+
/// Whether this type is a <see cref="DateTime"/>.
139136
/// </summary>
140137
public bool IsDateTime
141138
{
142139
get
143140
{
144-
if (!(this is DateTimeType))
145-
return false;
146-
// DateTimeType is a singleton.
147-
Contracts.Assert(this == DateTimeType.Instance);
148-
return true;
141+
Contracts.Assert((this == DateTimeType.Instance) == (this is DateTimeType));
142+
return this is DateTimeType;
149143
}
150144
}
151145

152146
/// <summary>
153-
/// Whether this type is a DvDateTimeZone.
147+
/// Whether this type is a <see cref="DateTimeOffset"/>
154148
/// </summary>
155149
public bool IsDateTimeZone
156150
{
157151
get
158152
{
159-
if (!(this is DateTimeZoneType))
160-
return false;
161-
// DateTimeZoneType is a singleton.
162-
Contracts.Assert(this == DateTimeZoneType.Instance);
163-
return true;
153+
Contracts.Assert((this == DateTimeOffsetType.Instance) == (this is DateTimeOffsetType));
154+
return this is DateTimeOffsetType;
164155
}
165156
}
166157

@@ -319,7 +310,7 @@ public static PrimitiveType FromKind(DataKind kind)
319310
if (kind == DataKind.DT)
320311
return DateTimeType.Instance;
321312
if (kind == DataKind.DZ)
322-
return DateTimeZoneType.Instance;
313+
return DateTimeOffsetType.Instance;
323314
return NumberType.FromKind(kind);
324315
}
325316
}
@@ -605,7 +596,7 @@ public static DateTimeType Instance
605596
}
606597

607598
private DateTimeType()
608-
: base(typeof(DvDateTime), DataKind.DT)
599+
: base(typeof(DateTime), DataKind.DT)
609600
{
610601
}
611602

@@ -623,29 +614,29 @@ public override string ToString()
623614
}
624615
}
625616

626-
public sealed class DateTimeZoneType : PrimitiveType
617+
public sealed class DateTimeOffsetType : PrimitiveType
627618
{
628-
private static volatile DateTimeZoneType _instance;
629-
public static DateTimeZoneType Instance
619+
private static volatile DateTimeOffsetType _instance;
620+
public static DateTimeOffsetType Instance
630621
{
631622
get
632623
{
633624
if (_instance == null)
634-
Interlocked.CompareExchange(ref _instance, new DateTimeZoneType(), null);
625+
Interlocked.CompareExchange(ref _instance, new DateTimeOffsetType(), null);
635626
return _instance;
636627
}
637628
}
638629

639-
private DateTimeZoneType()
640-
: base(typeof(DvDateTimeZone), DataKind.DZ)
630+
private DateTimeOffsetType()
631+
: base(typeof(DateTimeOffset), DataKind.DZ)
641632
{
642633
}
643634

644635
public override bool Equals(ColumnType other)
645636
{
646637
if (other == this)
647638
return true;
648-
Contracts.Assert(!(other is DateTimeZoneType));
639+
Contracts.Assert(!(other is DateTimeOffsetType));
649640
return false;
650641
}
651642

@@ -672,7 +663,7 @@ public static TimeSpanType Instance
672663
}
673664

674665
private TimeSpanType()
675-
: base(typeof(DvTimeSpan), DataKind.TS)
666+
: base(typeof(TimeSpan), DataKind.TS)
676667
{
677668
}
678669

src/Microsoft.ML.Core/Data/DataKind.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ public static Type ToType(this DataKind kind)
165165
case DataKind.BL:
166166
return typeof(DvBool);
167167
case DataKind.TS:
168-
return typeof(DvTimeSpan);
168+
return typeof(TimeSpan);
169169
case DataKind.DT:
170-
return typeof(DvDateTime);
170+
return typeof(DateTime);
171171
case DataKind.DZ:
172-
return typeof(DvDateTimeZone);
172+
return typeof(DateTimeOffset);
173173
case DataKind.UG:
174174
return typeof(UInt128);
175175
}
@@ -209,11 +209,11 @@ public static bool TryGetDataKind(this Type type, out DataKind kind)
209209
kind = DataKind.TX;
210210
else if (type == typeof(DvBool) || type == typeof(bool) || type == typeof(bool?))
211211
kind = DataKind.BL;
212-
else if (type == typeof(DvTimeSpan))
212+
else if (type == typeof(TimeSpan))
213213
kind = DataKind.TS;
214-
else if (type == typeof(DvDateTime))
214+
else if (type == typeof(DateTime))
215215
kind = DataKind.DT;
216-
else if (type == typeof(DvDateTimeZone))
216+
else if (type == typeof(DateTimeOffset))
217217
kind = DataKind.DZ;
218218
else if (type == typeof(UInt128))
219219
kind = DataKind.UG;

src/Microsoft.ML.Data/Data/Conversion.cs

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
namespace Microsoft.ML.Runtime.Data.Conversion
1616
{
1717
using BL = DvBool;
18-
using DT = DvDateTime;
19-
using DZ = DvDateTimeZone;
18+
using DT = DateTime;
19+
using DZ = DateTimeOffset;
2020
using I1 = DvInt1;
2121
using I2 = DvInt2;
2222
using I4 = DvInt4;
@@ -28,7 +28,7 @@ namespace Microsoft.ML.Runtime.Data.Conversion
2828
using RawI4 = Int32;
2929
using RawI8 = Int64;
3030
using SB = StringBuilder;
31-
using TS = DvTimeSpan;
31+
using TS = TimeSpan;
3232
using TX = DvText;
3333
using U1 = Byte;
3434
using U2 = UInt16;
@@ -252,9 +252,6 @@ private Conversions()
252252
AddIsNA<R8>(IsNA);
253253
AddIsNA<BL>(IsNA);
254254
AddIsNA<TX>(IsNA);
255-
AddIsNA<TS>(IsNA);
256-
AddIsNA<DT>(IsNA);
257-
AddIsNA<DZ>(IsNA);
258255

259256
AddGetNA<I1>(GetNA);
260257
AddGetNA<I2>(GetNA);
@@ -264,9 +261,6 @@ private Conversions()
264261
AddGetNA<R8>(GetNA);
265262
AddGetNA<BL>(GetNA);
266263
AddGetNA<TX>(GetNA);
267-
AddGetNA<TS>(GetNA);
268-
AddGetNA<DT>(GetNA);
269-
AddGetNA<DZ>(GetNA);
270264

271265
AddHasNA<I1>(HasNA);
272266
AddHasNA<I2>(HasNA);
@@ -276,9 +270,6 @@ private Conversions()
276270
AddHasNA<R8>(HasNA);
277271
AddHasNA<BL>(HasNA);
278272
AddHasNA<TX>(HasNA);
279-
AddHasNA<TS>(HasNA);
280-
AddHasNA<DT>(HasNA);
281-
AddHasNA<DZ>(HasNA);
282273

283274
AddIsDef<I1>(IsDefault);
284275
AddIsDef<I2>(IsDefault);
@@ -853,9 +844,6 @@ public ValueGetter<T> GetNAOrDefaultGetter<T>(ColumnType type)
853844
private bool IsNA(ref R4 src) => src.IsNA();
854845
private bool IsNA(ref R8 src) => src.IsNA();
855846
private bool IsNA(ref BL src) => src.IsNA;
856-
private bool IsNA(ref TS src) => src.IsNA;
857-
private bool IsNA(ref DT src) => src.IsNA;
858-
private bool IsNA(ref DZ src) => src.IsNA;
859847
private bool IsNA(ref TX src) => src.IsNA;
860848
#endregion IsNA
861849

@@ -867,9 +855,6 @@ public ValueGetter<T> GetNAOrDefaultGetter<T>(ColumnType type)
867855
private bool HasNA(ref VBuffer<R4> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA()) return true; } return false; }
868856
private bool HasNA(ref VBuffer<R8> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA()) return true; } return false; }
869857
private bool HasNA(ref VBuffer<BL> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
870-
private bool HasNA(ref VBuffer<TS> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
871-
private bool HasNA(ref VBuffer<DT> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
872-
private bool HasNA(ref VBuffer<DZ> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
873858
private bool HasNA(ref VBuffer<TX> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
874859
#endregion HasNA
875860

@@ -907,9 +892,6 @@ public ValueGetter<T> GetNAOrDefaultGetter<T>(ColumnType type)
907892
private void GetNA(ref R4 value) => value = R4.NaN;
908893
private void GetNA(ref R8 value) => value = R8.NaN;
909894
private void GetNA(ref BL value) => value = BL.NA;
910-
private void GetNA(ref TS value) => value = TS.NA;
911-
private void GetNA(ref DT value) => value = DT.NA;
912-
private void GetNA(ref DZ value) => value = DZ.NA;
913895
private void GetNA(ref TX value) => value = TX.NA;
914896
#endregion GetNA
915897

@@ -1041,9 +1023,9 @@ public void Convert(ref BL src, ref SB dst)
10411023
else if (src.IsTrue)
10421024
dst.Append("1");
10431025
}
1044-
public void Convert(ref TS src, ref SB dst) { ClearDst(ref dst); if (!src.IsNA) dst.AppendFormat("{0:c}", (TimeSpan)src); }
1045-
public void Convert(ref DT src, ref SB dst) { ClearDst(ref dst); if (!src.IsNA) dst.AppendFormat("{0:o}", (DateTime)src); }
1046-
public void Convert(ref DZ src, ref SB dst) { ClearDst(ref dst); if (!src.IsNA) dst.AppendFormat("{0:o}", (DateTimeOffset)src); }
1026+
public void Convert(ref TS src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:c}", src); }
1027+
public void Convert(ref DT src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:o}", src); }
1028+
public void Convert(ref DZ src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:o}", src); }
10471029
#endregion ToStringBuilder
10481030

10491031
#region FromR4
@@ -1472,62 +1454,50 @@ public bool TryParse(ref TX src, out R8 dst)
14721454

14731455
public bool TryParse(ref TX src, out TS dst)
14741456
{
1457+
dst = default;
14751458
if (!src.HasChars)
14761459
{
1477-
if (src.IsNA)
1478-
dst = TS.NA;
1479-
else
1480-
dst = default(TS);
1460+
Contracts.Check(!src.IsNA, "Missing values cannot be converted to boolean value.");
14811461
return true;
14821462
}
1483-
TimeSpan res;
1484-
if (TimeSpan.TryParse(src.ToString(), CultureInfo.InvariantCulture, out res))
1485-
{
1486-
dst = new TS(res);
1463+
1464+
if (TimeSpan.TryParse(src.ToString(), CultureInfo.InvariantCulture, out dst))
14871465
return true;
1488-
}
1489-
dst = TS.NA;
1490-
return IsStdMissing(ref src);
1466+
1467+
Contracts.Check(!IsStdMissing(ref src), "Missing values cannot be converted to boolean value.");
1468+
return true;
14911469
}
14921470

14931471
public bool TryParse(ref TX src, out DT dst)
14941472
{
1473+
dst = default;
14951474
if (!src.HasChars)
14961475
{
1497-
if (src.IsNA)
1498-
dst = DvDateTime.NA;
1499-
else
1500-
dst = default(DvDateTime);
1476+
Contracts.Check(!src.IsNA, "Missing values cannot be converted to boolean value.");
15011477
return true;
15021478
}
1503-
DateTime res;
1504-
if (DateTime.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out res))
1505-
{
1506-
dst = new DT(res);
1479+
1480+
if (DateTime.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out dst))
15071481
return true;
1508-
}
1509-
dst = DvDateTime.NA;
1510-
return IsStdMissing(ref src);
1482+
1483+
Contracts.Check(!IsStdMissing(ref src), "Missing values cannot be converted to boolean value.");
1484+
return true;
15111485
}
15121486

15131487
public bool TryParse(ref TX src, out DZ dst)
15141488
{
1489+
dst = default;
15151490
if (!src.HasChars)
15161491
{
1517-
if (src.IsNA)
1518-
dst = DvDateTimeZone.NA;
1519-
else
1520-
dst = default(DvDateTimeZone);
1492+
Contracts.Check(!src.IsNA, "Missing values cannot be converted to boolean value.");
15211493
return true;
15221494
}
1523-
DateTimeOffset res;
1524-
if (DateTimeOffset.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out res))
1525-
{
1526-
dst = new DZ(res);
1495+
1496+
if (DateTimeOffset.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dst))
15271497
return true;
1528-
}
1529-
dst = DvDateTimeZone.NA;
1530-
return IsStdMissing(ref src);
1498+
1499+
Contracts.Check(!IsStdMissing(ref src), "Missing values cannot be converted to boolean value.");
1500+
return true;
15311501
}
15321502

15331503
// These map unparsable and overflow values to "NA", which is the value Ix.MinValue. Note that this NA
@@ -1804,21 +1774,10 @@ public void Convert(ref TX src, ref SB dst)
18041774
src.AddToStringBuilder(dst);
18051775
}
18061776

1807-
public void Convert(ref TX span, ref TS value)
1808-
{
1809-
if (!TryParse(ref span, out value))
1810-
Contracts.Assert(value.IsNA);
1811-
}
1812-
public void Convert(ref TX span, ref DT value)
1813-
{
1814-
if (!TryParse(ref span, out value))
1815-
Contracts.Assert(value.IsNA);
1816-
}
1817-
public void Convert(ref TX span, ref DZ value)
1818-
{
1819-
if (!TryParse(ref span, out value))
1820-
Contracts.Assert(value.IsNA);
1821-
}
1777+
public void Convert(ref TX span, ref TS value) => TryParse(ref span, out value);
1778+
public void Convert(ref TX span, ref DT value) => TryParse(ref span, out value);
1779+
public void Convert(ref TX span, ref DZ value) => TryParse(ref span, out value);
1780+
18221781
#endregion FromTX
18231782

18241783
#region FromBL

src/Microsoft.ML.Data/DataLoadSave/Binary/CodecFactory.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ public CodecFactory(IHostEnvironment env, MemoryStreamPool memPool = null)
5454
RegisterSimpleCodec(new UnsafeTypeCodec<ulong>(this));
5555
RegisterSimpleCodec(new UnsafeTypeCodec<Single>(this));
5656
RegisterSimpleCodec(new UnsafeTypeCodec<Double>(this));
57-
RegisterSimpleCodec(new UnsafeTypeCodec<DvTimeSpan>(this));
57+
RegisterSimpleCodec(new UnsafeTypeCodec<TimeSpan>(this));
5858
RegisterSimpleCodec(new DvTextCodec(this));
5959
RegisterSimpleCodec(new BoolCodec(this));
6060
RegisterSimpleCodec(new DateTimeCodec(this));
61-
RegisterSimpleCodec(new DateTimeZoneCodec(this));
61+
RegisterSimpleCodec(new DateTimeOffsetCodec(this));
6262
RegisterSimpleCodec(new UnsafeTypeCodec<UInt128>(this));
6363

6464
// Register the old boolean reading codec.
6565
var oldBool = new OldBoolCodec(this);
6666
RegisterOtherCodec(oldBool.LoadName, oldBool.GetCodec);
67-
6867
RegisterOtherCodec("VBuffer", GetVBufferCodec);
68+
RegisterOtherCodec("DvDateTimeZone", new DateTimeOffsetCodec(this).GetCodec);
69+
RegisterOtherCodec("DvDateTime", new DateTimeCodec(this).GetCodec);
70+
RegisterOtherCodec("DvTimeSpan", new UnsafeTypeCodec<TimeSpan>(this).GetCodec);
6971
RegisterOtherCodec("Key", GetKeyCodec);
7072
}
7173

0 commit comments

Comments
 (0)