解剖SQLSERVER 第四篇 OrcaMDF里對dates類型數(shù)據(jù)的解析(譯) http://improve.dk/parsing-dates-in-orcamdf/ 在SQLSERVER里面有幾種不同的date相關(guān)類型,當(dāng)前OrcaMDF 支持三種最常用的date類型:date,datetime,smalldatetime SqlDate實現(xiàn) date 類型在三種
http://improve.dk/parsing-dates-in-orcamdf/
在SQLSERVER里面有幾種不同的date相關(guān)類型,當(dāng)前OrcaMDF 支持三種最常用的date類型:date,datetime,smalldatetime
SqlDate實現(xiàn)
date 類型在三種類型之中是最簡單的,他是一個3個字節(jié)的定長類型,存儲了日期值它支持的日期范圍從0001-01-01到9999-12-31
默認(rèn)值是1900-01-01
比較坑爹的是.NET里面還沒有任何標(biāo)準(zhǔn)實現(xiàn)能夠支持3個字節(jié)的整數(shù)類型,只有short類型和int類型,但是,他們要不太大要不太小
另外,要正確讀取日期值,對于.NET的4字節(jié)整型我們必須執(zhí)行一些轉(zhuǎn)變?nèi)カ@取正確的數(shù)字
一旦我們獲取到date的值,我們可以創(chuàng)建一個默認(rèn)的datetime類型并且添加天數(shù)進去
public class SqlDate : ISqlType { public bool IsVariableLength { get { return false; } } public short? FixedLength { get { return 3; } } public object GetValue(byte[] value) { if (value.Length != 3) throw new ArgumentException("Invalid value length: " + value.Length); // Magic needed to read a 3 byte integer into .NET's 4 byte representation. // Reading backwards due to assumed little endianness. int date = (value[2] << 16) + (value[1] << 8) + value[0]; return new DateTime(1, 1, 1).AddDays(date); } }
相關(guān)測試
using System; using NUnit.Framework; using OrcaMDF.Core.Engine.SqlTypes; namespace OrcaMDF.Core.Tests.Engine.SqlTypes { [TestFixture] public class SqlDateTests { [Test] public void GetValue() { var type = new SqlDate(); var input = new byte[] { 0xf6, 0x4c, 0x0b }; Assert.AreEqual(new DateTime(2028, 09, 09), Convert.ToDateTime(type.GetValue(input))); input = new byte[] { 0x71, 0x5c, 0x0b }; Assert.AreEqual(new DateTime(2039, 07, 17), Convert.ToDateTime(type.GetValue(input))); } [Test] public void Length() { var type = new SqlDate(); Assert.Throws(() => type.GetValue(new byte[2])); Assert.Throws(() => type.GetValue(new byte[4])); } } }
SqlDateTime實現(xiàn)
date類型只能存儲日期,而datetime類型不但能存儲date也能存儲time
datetime存儲8字節(jié)定長數(shù)據(jù)值,第一部分是time(4字節(jié)),而第二部分是date(4字節(jié))
計算date部分跟上面介紹date類型基本上一樣,不過這一次date部分是一個四字節(jié)整數(shù),比上面的例子容易處理多了,上面的date類型是3個字節(jié)
time部分存儲為自午夜時的ticks數(shù),一個tick就是1/300th 秒,為了顯示tick值,我們首先定義一個常量,常量值是10d/3d
time的各個部分實際同樣存儲在同一個整型值里面(比如時間,分鐘,秒,毫秒),所以我們要獨立訪問這些單獨的部分,我們必須
要執(zhí)行一些轉(zhuǎn)換 (包括取模和相除)
部分 計算 小時 X / 300 / 60 / 60 分鐘 X / 300 / 60 % 60 秒 X / 300 % 60 毫秒 X % 300 * 10d / 3d
public class SqlDateTime : ISqlType { private const double CLOCK_TICK_MS = 10d/3d; public bool IsVariableLength { get { return false; } } public short? FixedLength { get { return 8; } } public object GetValue(byte[] value) { if (value.Length != 8) throw new ArgumentException("Invalid value length: " + value.Length); int time = BitConverter.ToInt32(value, 0); int date = BitConverter.ToInt32(value, 4); return new DateTime(1900, 1, 1, time/300/60/60, time/300/60%60, time/300%60, (int)Math.Round(time%300*CLOCK_TICK_MS)).AddDays(date); } }
相關(guān)測試
using System; using NUnit.Framework; using OrcaMDF.Core.Engine.SqlTypes; namespace OrcaMDF.Core.Tests.Engine.SqlTypes { [TestFixture] public class SqlDateTimeTests { [Test] public void GetValue() { var type = new SqlDateTime(); byte[] input; input = new byte[] { 0x5e, 0x3b, 0x5d, 0x00, 0x25, 0x91, 0x00, 0x00 }; Assert.AreEqual(new DateTime(2001, 09, 25, 05, 39, 26, 820), (DateTime)type.GetValue(input)); input = new byte[] { 0xb6, 0x87, 0xf0, 0x00, 0xd1, 0x8b, 0x00, 0x00 }; Assert.AreEqual(new DateTime(1997, 12, 31, 14, 35, 44, 607), (DateTime)type.GetValue(input)); input = new byte[] { 0x2d, 0xfd, 0x1c, 0x01, 0x4a, 0x75, 0x00, 0x00 }; Assert.AreEqual(new DateTime(1982, 03, 18, 17, 17, 36, 790), (DateTime)type.GetValue(input)); input = new byte[] { 0xff, 0x81, 0x8b, 0x01, 0x7f, 0x24, 0x2d, 0x00 }; Assert.AreEqual(new DateTime(9999, 12, 31, 23, 59, 59, 997), (DateTime)type.GetValue(input)); } [Test] public void Length() { var type = new SqlDateTime(); Assert.Throws(() => type.GetValue(new byte[9])); Assert.Throws(() => type.GetValue(new byte[7])); } } }
SqlSmallDateTime實現(xiàn)
Smalldatetime 是一個不錯的數(shù)據(jù)類型當(dāng)你需要存儲范圍值內(nèi)的日期值(1900~2079)并且他能精確到秒
大多數(shù)場景下,精確到秒已經(jīng)足夠了,在一個范圍的時間間隔內(nèi)和精確值不需要太精確的情況下會節(jié)省很多空間
smalldatetime 數(shù)據(jù)類型會只占用4個字節(jié),前2個字節(jié)存儲自午夜的分鐘數(shù),后2個字節(jié)存儲日期,默認(rèn)值是1900-1-1
處理的方法跟datetime差不多,只不過使用更小的范圍
部分 計算 小時 X / 60 分鐘 X % 60
public class SqlSmallDateTime : ISqlType { public bool IsVariableLength { get { return false; } } public short? FixedLength { get { return 4; } } public object GetValue(byte[] value) { if (value.Length != 4) throw new ArgumentException("Invalid value length: " + value.Length); ushort time = BitConverter.ToUInt16(value, 0); ushort date = BitConverter.ToUInt16(value, 2); return new DateTime(1900, 1, 1, time / 60, time % 60, 0).AddDays(date); } }
相關(guān)測試
using System; using NUnit.Framework; using OrcaMDF.Core.Engine.SqlTypes; namespace OrcaMDF.Core.Tests.Engine.SqlTypes { [TestFixture] public class SqlSmallDateTimeTests { [Test] public void GetValue() { var type = new SqlSmallDateTime(); var input = new byte[] { 0xab, 0x02, 0x5d, 0x26 }; Assert.AreEqual(new DateTime(1926, 11, 22, 11, 23, 0), Convert.ToDateTime(type.GetValue(input))); input = new byte[] { 0x49, 0x03, 0x99, 0x09 }; Assert.AreEqual(new DateTime(1906, 9, 24, 14, 1, 0), Convert.ToDateTime(type.GetValue(input))); } [Test] public void Length() { var type = new SqlSmallDateTime(); Assert.Throws(() => type.GetValue(new byte[3])); Assert.Throws(() => type.GetValue(new byte[5])); } } }
第四篇完
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com