Skip to content

Instantly share code, notes, and snippets.

@rpgmaker
Created February 21, 2012 03:25
Show Gist options
  • Save rpgmaker/1873410 to your computer and use it in GitHub Desktop.
Save rpgmaker/1873410 to your computer and use it in GitHub Desktop.
BytesToTimeSpan
public static TimeSpan BytesToTimeSpan(byte[] buffer) {
if (buffer.Length == 1) {
if (buffer[0] == 0) return TimeSpan.MinValue;
else return TimeSpan.MaxValue;
} else if (buffer.Length == 2) {
return TimeSpan.FromTicks(TimeSpanTicks[buffer[0]] * buffer[1]);
}
var buffer2 = new byte[buffer.Length - 1];
Buffer.BlockCopy(buffer, 1, buffer2, 0, buffer2.Length);
var ticks = BytesToInt64(buffer2);
return TimeSpan.FromTicks(buffer[0] == TimeSpanTicksLength ? ticks : ticks * TimeSpanTicks[buffer[0]]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment