Skip to content

Instantly share code, notes, and snippets.

@rpgmaker
Created February 21, 2012 02:57
Show Gist options
  • Save rpgmaker/1873260 to your computer and use it in GitHub Desktop.
Save rpgmaker/1873260 to your computer and use it in GitHub Desktop.
WriteTimeSpanToBuffer
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