Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. [DebuggerDisplay("{ToString(),nq}")]
  2. public struct Timestamp : IComparable, IEquatable<Timestamp>, IComparable<Timestamp>
  3. {
  4. public static readonly Timestamp Zero = default(Timestamp);
  5.  
  6. private readonly ulong value;
  7.  
  8. private Timestamp(ulong value)
  9. {
  10. this.value = value;
  11. }
  12.  
  13. public static implicit operator Timestamp(ulong value)
  14. {
  15. return new Timestamp(value);
  16. }
  17. public static implicit operator Timestamp(long value)
  18. {
  19. return new Timestamp(unchecked((ulong)value));
  20. }
  21. public static explicit operator Timestamp(byte[] value)
  22. {
  23. return new Timestamp(((ulong)value[0] << 56) | ((ulong)value[1] << 48) | ((ulong)value[2] << 40) | ((ulong)value[3] << 32) | ((ulong)value[4] << 24) | ((ulong)value[5] << 16) | ((ulong)value[6] << 8) | value[7]);
  24. }
  25. public static explicit operator Timestamp?(byte[] value)
  26. {
  27. if (value == null) return null;
  28. return new Timestamp(((ulong)value[0] << 56) | ((ulong)value[1] << 48) | ((ulong)value[2] << 40) | ((ulong)value[3] << 32) | ((ulong)value[4] << 24) | ((ulong)value[5] << 16) | ((ulong)value[6] << 8) | value[7]);
  29. }
  30. public static implicit operator byte[](Timestamp timestamp)
  31. {
  32. var r = new byte[8];
  33. r[0] = (byte)(timestamp.value >> 56);
  34. r[1] = (byte)(timestamp.value >> 48);
  35. r[2] = (byte)(timestamp.value >> 40);
  36. r[3] = (byte)(timestamp.value >> 32);
  37. r[4] = (byte)(timestamp.value >> 24);
  38. r[5] = (byte)(timestamp.value >> 16);
  39. r[6] = (byte)(timestamp.value >> 8);
  40. r[7] = (byte)timestamp.value;
  41. return r;
  42. }
  43.  
  44. public override bool Equals(object obj)
  45. {
  46. return obj is Timestamp && Equals((Timestamp)obj);
  47. }
  48.  
  49. public override int GetHashCode()
  50. {
  51. return value.GetHashCode();
  52. }
  53.  
  54. public bool Equals(Timestamp other)
  55. {
  56. return other.value == value;
  57. }
  58.  
  59. int IComparable.CompareTo(object obj)
  60. {
  61. return obj == null ? 1 : CompareTo((Timestamp)obj);
  62. }
  63.  
  64. public int CompareTo(Timestamp other)
  65. {
  66. return value == other.value ? 0 : value < other.value ? -1 : 1;
  67. }
  68.  
  69. public static bool operator ==(Timestamp comparand1, Timestamp comparand2)
  70. {
  71. return comparand1.Equals(comparand2);
  72. }
  73. public static bool operator !=(Timestamp comparand1, Timestamp comparand2)
  74. {
  75. return !comparand1.Equals(comparand2);
  76. }
  77. public static bool operator >(Timestamp comparand1, Timestamp comparand2)
  78. {
  79. return comparand1.CompareTo(comparand2) > 0;
  80. }
  81. public static bool operator >=(Timestamp comparand1, Timestamp comparand2)
  82. {
  83. return comparand1.CompareTo(comparand2) >= 0;
  84. }
  85. public static bool operator <(Timestamp comparand1, Timestamp comparand2)
  86. {
  87. return comparand1.CompareTo(comparand2) < 0;
  88. }
  89. public static bool operator <=(Timestamp comparand1, Timestamp comparand2)
  90. {
  91. return comparand1.CompareTo(comparand2) <= 0;
  92. }
  93.  
  94. public override string ToString()
  95. {
  96. return value.ToString("x16");
  97. }
  98.  
  99. public static Timestamp Max(Timestamp comparand1, Timestamp comparand2)
  100. {
  101. return comparand1.value < comparand2.value ? comparand2 : comparand1;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement