Utils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (C) <2007-2016> <Kay Diefenthal>
  3. SatIp.RtspSample is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. SatIp.RtspSample is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with SatIp.RtspSample. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. using System;
  15. using System.Text;
  16. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp
  17. {
  18. public class Utils
  19. {
  20. public static int Convert2BytesToInt(byte[] buffer, int offset)
  21. {
  22. int temp = (int)buffer[offset];
  23. temp = (temp * 256) + buffer[offset + 1];
  24. return (temp);
  25. }
  26. public static int Convert3BytesToInt(byte[] buffer, int offset)
  27. {
  28. int temp = (int)buffer[offset];
  29. temp = (temp * 256) + buffer[offset + 1];
  30. temp = (temp * 256) + buffer[offset + 2];
  31. return (temp);
  32. }
  33. public static int Convert4BytesToInt(byte[] buffer, int offset)
  34. {
  35. int temp =(int)buffer[offset];
  36. temp = (temp * 256) + buffer[offset + 1];
  37. temp = (temp * 256) + buffer[offset + 2];
  38. temp = (temp * 256) + buffer[offset + 3];
  39. return (temp);
  40. }
  41. public static long Convert4BytesToLong(byte[] buffer, int offset)
  42. {
  43. long temp = 0;
  44. for (int index = 0; index < 4; index++)
  45. temp = (temp * 256) + buffer[offset + index];
  46. return (temp);
  47. }
  48. public static long Convert8BytesToLong(byte[] buffer, int offset)
  49. {
  50. long temp = 0;
  51. for (int index = 0; index < 8; index++)
  52. temp = (temp * 256) + buffer[offset + index];
  53. return (temp);
  54. }
  55. public static string ConvertBytesToString(byte[] buffer, int offset, int length)
  56. {
  57. StringBuilder reply = new StringBuilder(4);
  58. for (int index = 0; index < length; index++)
  59. reply.Append((char)buffer[offset + index]);
  60. return (reply.ToString());
  61. }
  62. public static DateTime NptTimestampToDateTime(long nptTimestamp) { return NptTimestampToDateTime((uint)((nptTimestamp >> 32) & 0xFFFFFFFF), (uint)(nptTimestamp & 0xFFFFFFFF),null); }
  63. public static DateTime NptTimestampToDateTime(uint seconds, uint fractions, DateTime? epoch )
  64. {
  65. ulong ticks =(ulong)((seconds * TimeSpan.TicksPerSecond) + ((fractions * TimeSpan.TicksPerSecond) / 0x100000000L));
  66. if (epoch.HasValue) return epoch.Value + TimeSpan.FromTicks((Int64)ticks);
  67. return (seconds & 0x80000000L) == 0 ? UtcEpoch2036 + TimeSpan.FromTicks((Int64)ticks) : UtcEpoch1900 + TimeSpan.FromTicks((Int64)ticks);
  68. }
  69. //When the First Epoch will wrap (The real Y2k)
  70. public static DateTime UtcEpoch2036 = new DateTime(2036, 2, 7, 6, 28, 16, DateTimeKind.Utc);
  71. public static DateTime UtcEpoch1900 = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  72. public static DateTime UtcEpoch1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  73. }
  74. }