Created
February 21, 2012 02:57
-
-
Save rpgmaker/1873260 to your computer and use it in GitHub Desktop.
WriteTimeSpanToBuffer
This file contains hidden or 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
public static void WriteTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan value, int tag) { | |
if (value == TimeSpan.Zero) return; | |
byte[] buffer = null; | |
if (value == TimeSpan.MaxValue) buffer = MaxTimeSpanBytes; | |
else if (value == TimeSpan.MinValue) buffer = MinTimeSpanBytes; | |
else { | |
int tickIndex = -1; | |
long ticksValue; | |
var ticks = value.Ticks; | |
for (var i = 0; i < TimeSpanTicksLength; i++) { | |
if (ticks % (ticksValue = TimeSpanTicks[i]) == 0) { | |
ticks /= ticksValue; | |
tickIndex = i; | |
break; | |
} | |
} | |
if (ticks <= 255 && tickIndex != -1) buffer = new byte[] { (byte)tickIndex, (byte)ticks }; | |
else { | |
var ticksBuffer = Int64ToBytes(ticks); | |
buffer = new byte[ticksBuffer.Length + 1]; | |
buffer[0] = (byte)(tickIndex > - 1 ? tickIndex : TimeSpanTicksLength); | |
Buffer.BlockCopy(ticksBuffer, 0, buffer, 1, ticksBuffer.Length); | |
} | |
} | |
WriteUnBufferedBytes(customBuffer, buffer, tag); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment